From: joey Date: Sun, 12 Mar 2006 03:11:30 +0000 (+0000) Subject: use template for page rendering X-Git-Tag: 1.0~447 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=94eab28a86d518c39d8a71accee7b25818e38e63;p=ikiwiki.git use template for page rendering --- diff --git a/ikiwiki b/ikiwiki index 7e4711059..2815a8e1d 100755 --- a/ikiwiki +++ b/ikiwiki @@ -5,6 +5,7 @@ use strict; use File::Find; use Memoize; use File::Spec; +use HTML::Template; BEGIN { $blosxom::version="is a proper perl module too much to ask?"; @@ -12,8 +13,8 @@ BEGIN { } $ENV{PATH}="/usr/local/bin:/usr/bin:/bin"; -my ($srcdir, $destdir, %links, %oldlinks, %oldpagemtime, %renderedfiles, - %pagesources); +my ($srcdir, $templatedir, $destdir, %links, %oldlinks, %oldpagemtime, + %renderedfiles, %pagesources); my $wiki_link_regexp=qr/\[\[([^\s]+)\]\]/; my $wiki_file_regexp=qr/(^[-A-Za-z0-9_.:\/+]+$)/; my $wiki_file_prune_regexp=qr!((^|/).svn/|\.\.|^\.|\/\.|\.html?$)!; @@ -27,7 +28,7 @@ my $historyurl=""; my $svn=1; sub usage { #{{{ - die "usage: ikiwiki [options] source dest\n"; + die "usage: ikiwiki [options] source templates dest\n"; } #}}} sub error ($) { #{{{ @@ -216,8 +217,7 @@ sub htmlize ($$) { #{{{ } } #}}} -sub linkbacks ($$) { #{{{ - my $content=shift; +sub backlinks ($) { #{{{ my $page=shift; my @links; @@ -235,13 +235,31 @@ sub linkbacks ($$) { #{{{ $p_trimmed=~s/^\Q$dir\E// && $page_trimmed=~s/^\Q$dir\E//; - push @links, "$p_trimmed"; + push @links, { url => $href, page => $p_trimmed }; } } - $content.="

Links: ".join(" ", sort @links)."

\n" if @links; - return $content; + return @links; } #}}} + +sub parentlinks ($) { + my $page=shift; + + my @ret; + my $pagelink=""; + my $path=""; + my $skip=1; + foreach my $dir (reverse split("/", $page)) { + if (! $skip) { + unshift @ret, { url => "$path$dir.html", page => $dir }; + } + else { + $skip=0; + } + $path.="../"; + } + return @ret; +} sub indexlink () { #{{{ return "$wikiname/ "; @@ -254,38 +272,30 @@ sub finalize ($$) { #{{{ my $title=basename($page); $title=~s/_/ /g; - my $pagelink=""; - my $path=""; - foreach my $dir (reverse split("/", $page)) { - if (length($pagelink)) { - $pagelink="$dir/ $pagelink"; - } - else { - $pagelink=$dir; - } - $path.="../"; - } - $path=~s/\.\.\/$/index.html/; - $pagelink=indexlink()." $pagelink"; + my $template=HTML::Template->new(blind_cache => 1, + filename => "$templatedir/page.tmpl"); - my @actions; if (length $cgiurl) { - push @actions, "Edit"; - push @actions, "RecentChanges"; + $template->param(editurl => "$cgiurl?do=edit&page=$page"); + $template->param(recentchangesurl => "$cgiurl?do=recentchanges"); } + if (length $historyurl) { - my $url=$historyurl; - $url=~s/\[\[\]\]/$pagesources{$page}/g; - push @actions, "History"; - } - - $content="\n$title\n\n". - "

$pagelink

\n". - "@actions\n
\n". - $content. - "\n\n"; + my $u=$historyurl; + $u=~s/\[\[\]\]/$pagesources{$page}/g; + $template->param(historyurl => $u); + } + + $template->param( + title => $title, + indexlink => $url, + wikiname => $wikiname, + parentlinks => [parentlinks($page)], + content => $content, + backlinks => [backlinks($page)], + ); - return $content; + return $template->output; } #}}} sub render ($) { #{{{ @@ -300,7 +310,6 @@ sub render ($) { #{{{ $content=linkify($content, $file); $content=htmlize($type, $content); - $content=linkbacks($content, $page); $content=finalize($content, $page); writefile("$destdir/".htmlpage($page), $content); @@ -526,10 +535,10 @@ FILE: foreach my $file (@files) { } } - # handle linkbacks; if a page has added/removed links, update the + # handle backlinks; if a page has added/removed links, update the # pages it links to # TODO: inefficient; pages may get rendered above and again here; - # problem is the linkbacks could be wrong in the first pass render + # problem is the backlinks could be wrong in the first pass render # above if (%rendered) { my %linkchanged; @@ -559,7 +568,7 @@ FILE: foreach my $file (@files) { foreach my $link (keys %linkchanged) { my $linkfile=$pagesources{$link}; if (defined $linkfile) { - debug("rendering $linkfile, to update its linkbacks"); + debug("rendering $linkfile, to update its backlinks"); render($linkfile); } } @@ -649,6 +658,8 @@ EOF sub cgi_recentchanges ($) { #{{{ my $q=shift; + + my $list="