From 893610f575065bc12e808db6d265f1e7067b11ba Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sun, 5 Oct 2008 21:37:05 +0200 Subject: [PATCH] Added an example and Temple::Links from MJ's home page. --- UCW/Temple/Links.pm | 83 +++ example/Makefile | 15 + example/bin/.htaccess | 2 + example/bin/dtd/HTML4.cat | 8 + example/bin/dtd/HTML4.decl | 81 +++ example/bin/dtd/HTMLlat1.ent | 194 ++++++ example/bin/dtd/HTMLspecial.ent | 77 +++ example/bin/dtd/HTMLsymbol.ent | 241 +++++++ example/bin/dtd/frameset.dtd | 37 ++ example/bin/dtd/loose.dtd | 1093 +++++++++++++++++++++++++++++++ example/bin/dtd/strict.dtd | 870 ++++++++++++++++++++++++ example/bin/htmlcheck | 12 + example/bin/newer | 21 + example/bin/temple | 28 + example/lib/.htaccess | 2 + example/lib/UCW | 1 + example/template/.htaccess | 2 + example/template/head.t | 6 + example/template/prolog.t | 15 + example/template/tail.t | 11 + example/test.html | 11 + example/test.thtml | 5 + 22 files changed, 2815 insertions(+) create mode 100644 UCW/Temple/Links.pm create mode 100644 example/Makefile create mode 100644 example/bin/.htaccess create mode 100644 example/bin/dtd/HTML4.cat create mode 100644 example/bin/dtd/HTML4.decl create mode 100644 example/bin/dtd/HTMLlat1.ent create mode 100644 example/bin/dtd/HTMLspecial.ent create mode 100644 example/bin/dtd/HTMLsymbol.ent create mode 100644 example/bin/dtd/frameset.dtd create mode 100644 example/bin/dtd/loose.dtd create mode 100644 example/bin/dtd/strict.dtd create mode 100755 example/bin/htmlcheck create mode 100755 example/bin/newer create mode 100755 example/bin/temple create mode 100644 example/lib/.htaccess create mode 120000 example/lib/UCW create mode 100644 example/template/.htaccess create mode 100644 example/template/head.t create mode 100644 example/template/prolog.t create mode 100644 example/template/tail.t create mode 100644 example/test.html create mode 100644 example/test.thtml diff --git a/UCW/Temple/Links.pm b/UCW/Temple/Links.pm new file mode 100644 index 0000000..f60d67c --- /dev/null +++ b/UCW/Temple/Links.pm @@ -0,0 +1,83 @@ +#!/usr/bin/perl +# A really simple template engine: Link helpers +# (c) 2008 Martin Mares + +package UCW::Temple::Links; + +use strict; +use warnings; + +require Exporter; +our $VERSION = 1.0; +our @ISA = qw(Exporter); +our @EXPORT = qw(url href normalize_url); +our @EXPORT_OK = qw(); + +our $relpath; # relative path from the root of the tree to the current file +our $rootpath; # relative path from the current URL back to the root of the tree + +sub init($$) +{ + my ($destpath, $destdir) = @_; + + $relpath = $destpath; + $relpath =~ s{^$destdir/}{}; + $relpath =~ s{^\./}{}; + my @tmp = split(m{/}, "$relpath."); + my $dircnt = @tmp; + $rootpath = ""; + while ($dircnt-- > 1) { + $rootpath .= "../"; + } + #print STDERR "DESTPATH: $destpath, DESTDIR: $destdir -> RELPATH: $relpath, ROOTPATH: $rootpath\n"; + + return; +} + +# Compose a relative path with its absolute base +sub compose_path($$) { + my ($base, $rel) = @_; + return $rel if $rel =~ m{^/}; + $base =~ s{/[^/]*$}{/}; + while ($rel =~ s{^\.(\.)?/}{}) { + $base =~ s{(^|/)[^/]+/$}{$1} if defined($1); + } + return $base . $rel; +} + +# Change $url to be relative to $base +sub relativize_path($$) { + my ($base, $url) = @_; + my @b = split m{/}, $base; + pop @b unless $base =~ m{/$}; + my @u = split m{/}, $url; + push @u, "" if $url =~ m{/$}; + push @u, "" if $url eq "/"; + # print STDERR "B: <", join(":", @b), "> ", scalar @b, "\n"; + # print STDERR "U: <", join(":", @u), "> ", scalar @u, "\n"; + while (@b && @u && $b[0] eq $u[0]) { + shift @b; + shift @u; + } + return join("/", (map { ".." } @b), @u); +} + +sub normalize_url($) { + my $url = shift @_; + $url =~ s{(^|/)index\.\w+$}{$1}; + $url ne "" or $url = "./"; + return $url; +} + +sub url($) { + my $url = shift @_; + $url = compose_path("/$relpath", $url); + $url = relativize_path("/$relpath", $url); + return normalize_url($url); +} + +sub href($) { + return '"' . url($_[0]) . '"'; +} + +42; diff --git a/example/Makefile b/example/Makefile new file mode 100644 index 0000000..7579d0f --- /dev/null +++ b/example/Makefile @@ -0,0 +1,15 @@ +.PHONY: all clean + +DEST=. +PAGES := $(addprefix $(DEST)/,$(patsubst %.thtml,%.html,$(shell find . -name "*.thtml" | sed "s@^./@@"))) +DEPS := $(wildcard template/*.t) + +all: $(PAGES) + +clean: + +$(DEST)/%.html: %.thtml $(DEPS) + @echo $< -\> $@ + @bin/temple -o $@.new -e "@[\$$destdir='$(DEST)'; \$$destpath='$@'; \$$srcpath='$<']" template/prolog.t $< + @bin/htmlcheck $@.new + @bin/newer $@ diff --git a/example/bin/.htaccess b/example/bin/.htaccess new file mode 100644 index 0000000..281d5c3 --- /dev/null +++ b/example/bin/.htaccess @@ -0,0 +1,2 @@ +order allow,deny +deny from all diff --git a/example/bin/dtd/HTML4.cat b/example/bin/dtd/HTML4.cat new file mode 100644 index 0000000..9dfe1e1 --- /dev/null +++ b/example/bin/dtd/HTML4.cat @@ -0,0 +1,8 @@ +OVERRIDE YES + +PUBLIC "-//W3C//DTD HTML 4.01//EN" strict.dtd +PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" loose.dtd +PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" frameset.dtd +PUBLIC "-//W3C//ENTITIES Latin1//EN//HTML" HTMLlat1.ent +PUBLIC "-//W3C//ENTITIES Special//EN//HTML" HTMLspecial.ent +PUBLIC "-//W3C//ENTITIES Symbols//EN//HTML" HTMLsymbol.ent diff --git a/example/bin/dtd/HTML4.decl b/example/bin/dtd/HTML4.decl new file mode 100644 index 0000000..9b78f18 --- /dev/null +++ b/example/bin/dtd/HTML4.decl @@ -0,0 +1,81 @@ + \ No newline at end of file diff --git a/example/bin/dtd/HTMLlat1.ent b/example/bin/dtd/HTMLlat1.ent new file mode 100644 index 0000000..c644486 --- /dev/null +++ b/example/bin/dtd/HTMLlat1.ent @@ -0,0 +1,194 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/example/bin/dtd/HTMLspecial.ent b/example/bin/dtd/HTMLspecial.ent new file mode 100644 index 0000000..5ce5c6a --- /dev/null +++ b/example/bin/dtd/HTMLspecial.ent @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/example/bin/dtd/HTMLsymbol.ent b/example/bin/dtd/HTMLsymbol.ent new file mode 100644 index 0000000..524bfe1 --- /dev/null +++ b/example/bin/dtd/HTMLsymbol.ent @@ -0,0 +1,241 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/example/bin/dtd/frameset.dtd b/example/bin/dtd/frameset.dtd new file mode 100644 index 0000000..2ef45a7 --- /dev/null +++ b/example/bin/dtd/frameset.dtd @@ -0,0 +1,37 @@ + + + + + ... + + + ... + + +--> + + + +%HTML4.dtd; \ No newline at end of file diff --git a/example/bin/dtd/loose.dtd b/example/bin/dtd/loose.dtd new file mode 100644 index 0000000..ba233cb --- /dev/null +++ b/example/bin/dtd/loose.dtd @@ -0,0 +1,1093 @@ + + + + + ... + + + ... + + + + The URI used as a system identifier with the public identifier allows + the user agent to download the DTD and entity sets as needed. + + The FPI for the Strict HTML 4.01 DTD is: + + "-//W3C//DTD HTML 4.01//EN" + + This version of the strict DTD is: + + http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd + + Authors should use the Strict DTD unless they need the + presentation control for user agents that don't (adequately) + support style sheets. + + If you are writing a document that includes frames, use + the following FPI: + + "-//W3C//DTD HTML 4.01 Frameset//EN" + + This version of the frameset DTD is: + + http://www.w3.org/TR/1999/REC-html401-19991224/frameset.dtd + + Use the following (relative) URIs to refer to + the DTDs and entity definitions of this specification: + + "strict.dtd" + "loose.dtd" + "frameset.dtd" + "HTMLlat1.ent" + "HTMLsymbol.ent" + "HTMLspecial.ent" + +--> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +%HTMLlat1; + + +%HTMLsymbol; + + +%HTMLspecial; + + + + + + + + + + + + + +]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +]]> + + + + +]]> + + + + + +]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +]]> + + + + + diff --git a/example/bin/dtd/strict.dtd b/example/bin/dtd/strict.dtd new file mode 100644 index 0000000..a834333 --- /dev/null +++ b/example/bin/dtd/strict.dtd @@ -0,0 +1,870 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +%HTMLlat1; + + +%HTMLsymbol; + + +%HTMLspecial; + + + + + + + + + + + + + +]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/example/bin/htmlcheck b/example/bin/htmlcheck new file mode 100755 index 0000000..a0501a1 --- /dev/null +++ b/example/bin/htmlcheck @@ -0,0 +1,12 @@ +#!/bin/sh + +onsgmls -Dbin/dtd -cHTML4.cat -s $1 2>&1 | { + any_error=0 + while read line + do + any_error=1 + echo $line + done + exit $any_error +} +exit $? diff --git a/example/bin/newer b/example/bin/newer new file mode 100755 index 0000000..9912485 --- /dev/null +++ b/example/bin/newer @@ -0,0 +1,21 @@ +#!/bin/sh +set -e + +#do the files exists? +if [ ! -f "$1.new" ]; then exit 0; fi + +#do the original file exists? +if [ ! -f "$1" ] +then + mv $1.new $1 + exit 0 +fi + +#they do, compare them, but ignore the line with +if diff -I "Last modified:.*" $1.new $1 > /dev/null +then #they are the same - choose the old one, but set actual timestamp + rm -f $1.new + touch $1 +else #they differ + mv -f $1.new $1 +fi diff --git a/example/bin/temple b/example/bin/temple new file mode 100755 index 0000000..8e185b4 --- /dev/null +++ b/example/bin/temple @@ -0,0 +1,28 @@ +#!/usr/bin/perl +# A really simple template engine +# (c) 2004 Martin Mares + +use lib 'lib'; + +use strict; +use warnings; + +use Getopt::Long; +use UCW::Temple; + +my $out; +my @execs = (); + +GetOptions('out|o=s' => \$out, 'exec|e=s' => \@execs) or die "Usage: temple [-o ] [-e ] ..."; +if (defined $out) { + close STDOUT; + open STDOUT, ">$out" or die "Cannot open $out: $!"; +} +UCW::Temple::start(); +foreach (@execs) { + UCW::Temple::parse_string($_); +} +foreach (@ARGV) { + UCW::Temple::parse_file($_); +} +UCW::Temple::finish(); diff --git a/example/lib/.htaccess b/example/lib/.htaccess new file mode 100644 index 0000000..281d5c3 --- /dev/null +++ b/example/lib/.htaccess @@ -0,0 +1,2 @@ +order allow,deny +deny from all diff --git a/example/lib/UCW b/example/lib/UCW new file mode 120000 index 0000000..79c2661 --- /dev/null +++ b/example/lib/UCW @@ -0,0 +1 @@ +../../UCW \ No newline at end of file diff --git a/example/template/.htaccess b/example/template/.htaccess new file mode 100644 index 0000000..281d5c3 --- /dev/null +++ b/example/template/.htaccess @@ -0,0 +1,2 @@ +order allow,deny +deny from all diff --git a/example/template/head.t b/example/template/head.t new file mode 100644 index 0000000..67cb7a3 --- /dev/null +++ b/example/template/head.t @@ -0,0 +1,6 @@ + + + +@$TITLE +@$HEAD_EXTRAS + diff --git a/example/template/prolog.t b/example/template/prolog.t new file mode 100644 index 0000000..461662a --- /dev/null +++ b/example/template/prolog.t @@ -0,0 +1,15 @@ +@[ +use POSIX; +use UCW::Temple::Links; + +# The Makefile gives us: +# $destdir Destination directory +# $destpath Destination file (including $destdir) +# $srcpath Source file + +UCW::Temple::Links::init($destpath, $destdir); + +# Variables controlling the behavior of template/head.t +$HEAD_EXTRAS = ""; + +]@ diff --git a/example/template/tail.t b/example/template/tail.t new file mode 100644 index 0000000..22eb642 --- /dev/null +++ b/example/template/tail.t @@ -0,0 +1,11 @@ +@[ +$lm = "???"; +if (my @st = stat $srcpath) { + my $mtime = $st[9]; + $lm = POSIX::strftime("%Y-%m-%d", localtime $mtime); +} +]@ + +@{"

Last modification: $lm by Martin Mares"}@ + + diff --git a/example/test.html b/example/test.html new file mode 100644 index 0000000..4881d63 --- /dev/null +++ b/example/test.html @@ -0,0 +1,11 @@ + + + +A test page + + + + +

A Test Page

+ +

Last modification: 2008-10-05 by Martin MareĀ¹ diff --git a/example/test.thtml b/example/test.thtml new file mode 100644 index 0000000..141f692 --- /dev/null +++ b/example/test.thtml @@ -0,0 +1,5 @@ +@include("template/head.t", "TITLE" => "A test page") + +

A Test Page

+ +@include "template/tail.t" -- 2.39.5