use template for page rendering

master
joey 2006-03-12 03:11:30 +00:00
parent 1311d67f0d
commit 94eab28a86
2 changed files with 88 additions and 38 deletions

88
ikiwiki
View File

@ -5,6 +5,7 @@ use strict;
use File::Find; use File::Find;
use Memoize; use Memoize;
use File::Spec; use File::Spec;
use HTML::Template;
BEGIN { BEGIN {
$blosxom::version="is a proper perl module too much to ask?"; $blosxom::version="is a proper perl module too much to ask?";
@ -12,8 +13,8 @@ BEGIN {
} }
$ENV{PATH}="/usr/local/bin:/usr/bin:/bin"; $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
my ($srcdir, $destdir, %links, %oldlinks, %oldpagemtime, %renderedfiles, my ($srcdir, $templatedir, $destdir, %links, %oldlinks, %oldpagemtime,
%pagesources); %renderedfiles, %pagesources);
my $wiki_link_regexp=qr/\[\[([^\s]+)\]\]/; my $wiki_link_regexp=qr/\[\[([^\s]+)\]\]/;
my $wiki_file_regexp=qr/(^[-A-Za-z0-9_.:\/+]+$)/; my $wiki_file_regexp=qr/(^[-A-Za-z0-9_.:\/+]+$)/;
my $wiki_file_prune_regexp=qr!((^|/).svn/|\.\.|^\.|\/\.|\.html?$)!; my $wiki_file_prune_regexp=qr!((^|/).svn/|\.\.|^\.|\/\.|\.html?$)!;
@ -27,7 +28,7 @@ my $historyurl="";
my $svn=1; my $svn=1;
sub usage { #{{{ sub usage { #{{{
die "usage: ikiwiki [options] source dest\n"; die "usage: ikiwiki [options] source templates dest\n";
} #}}} } #}}}
sub error ($) { #{{{ sub error ($) { #{{{
@ -216,8 +217,7 @@ sub htmlize ($$) { #{{{
} }
} #}}} } #}}}
sub linkbacks ($$) { #{{{ sub backlinks ($) { #{{{
my $content=shift;
my $page=shift; my $page=shift;
my @links; my @links;
@ -235,13 +235,31 @@ sub linkbacks ($$) { #{{{
$p_trimmed=~s/^\Q$dir\E// && $p_trimmed=~s/^\Q$dir\E// &&
$page_trimmed=~s/^\Q$dir\E//; $page_trimmed=~s/^\Q$dir\E//;
push @links, "<a href=\"$href\">$p_trimmed</a>"; push @links, { url => $href, page => $p_trimmed };
} }
} }
$content.="<hr><p>Links: ".join(" ", sort @links)."</p>\n" if @links; return @links;
return $content;
} #}}} } #}}}
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 () { #{{{ sub indexlink () { #{{{
return "<a href=\"$url\">$wikiname</a>/ "; return "<a href=\"$url\">$wikiname</a>/ ";
@ -254,38 +272,30 @@ sub finalize ($$) { #{{{
my $title=basename($page); my $title=basename($page);
$title=~s/_/ /g; $title=~s/_/ /g;
my $pagelink=""; my $template=HTML::Template->new(blind_cache => 1,
my $path=""; filename => "$templatedir/page.tmpl");
foreach my $dir (reverse split("/", $page)) {
if (length($pagelink)) {
$pagelink="<a href=\"$path$dir.html\">$dir</a>/ $pagelink";
}
else {
$pagelink=$dir;
}
$path.="../";
}
$path=~s/\.\.\/$/index.html/;
$pagelink=indexlink()." $pagelink";
my @actions;
if (length $cgiurl) { if (length $cgiurl) {
push @actions, "<a href=\"$cgiurl?do=edit&page=$page\">Edit</a>"; $template->param(editurl => "$cgiurl?do=edit&page=$page");
push @actions, "<a href=\"$cgiurl?do=recentchanges\">RecentChanges</a>"; $template->param(recentchangesurl => "$cgiurl?do=recentchanges");
} }
if (length $historyurl) { if (length $historyurl) {
my $url=$historyurl; my $u=$historyurl;
$url=~s/\[\[\]\]/$pagesources{$page}/g; $u=~s/\[\[\]\]/$pagesources{$page}/g;
push @actions, "<a href=\"$url\">History</a>"; $template->param(historyurl => $u);
} }
$content="<html>\n<head><title>$title</title></head>\n<body>\n". $template->param(
"<h1>$pagelink</h1>\n". title => $title,
"@actions\n<hr>\n". indexlink => $url,
$content. wikiname => $wikiname,
"</body>\n</html>\n"; parentlinks => [parentlinks($page)],
content => $content,
backlinks => [backlinks($page)],
);
return $content; return $template->output;
} #}}} } #}}}
sub render ($) { #{{{ sub render ($) { #{{{
@ -300,7 +310,6 @@ sub render ($) { #{{{
$content=linkify($content, $file); $content=linkify($content, $file);
$content=htmlize($type, $content); $content=htmlize($type, $content);
$content=linkbacks($content, $page);
$content=finalize($content, $page); $content=finalize($content, $page);
writefile("$destdir/".htmlpage($page), $content); 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 # pages it links to
# TODO: inefficient; pages may get rendered above and again here; # 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 # above
if (%rendered) { if (%rendered) {
my %linkchanged; my %linkchanged;
@ -559,7 +568,7 @@ FILE: foreach my $file (@files) {
foreach my $link (keys %linkchanged) { foreach my $link (keys %linkchanged) {
my $linkfile=$pagesources{$link}; my $linkfile=$pagesources{$link};
if (defined $linkfile) { if (defined $linkfile) {
debug("rendering $linkfile, to update its linkbacks"); debug("rendering $linkfile, to update its backlinks");
render($linkfile); render($linkfile);
} }
} }
@ -649,6 +658,8 @@ EOF
sub cgi_recentchanges ($) { #{{{ sub cgi_recentchanges ($) { #{{{
my $q=shift; my $q=shift;
my $list="<ul>\n"; my $list="<ul>\n";
foreach my $change (rcs_recentchanges(100)) { foreach my $change (rcs_recentchanges(100)) {
$list.="<li>"; $list.="<li>";
@ -931,8 +942,9 @@ if (grep /^-/, @ARGV) {
"historyurl=s" => \$historyurl, "historyurl=s" => \$historyurl,
) || usage(); ) || usage();
} }
usage() unless @ARGV == 2; usage() unless @ARGV == 3;
($srcdir) = possibly_foolish_untaint(shift); ($srcdir) = possibly_foolish_untaint(shift);
($templatedir) = possibly_foolish_untaint(shift);
($destdir) = possibly_foolish_untaint(shift); ($destdir) = possibly_foolish_untaint(shift);
if ($cgi && ! length $url) { if ($cgi && ! length $url) {

View File

@ -0,0 +1,38 @@
<html>
<head><title><TMPL_VAR TITLE></title></head>
<body>
<h1>
<a href="<TMPL_VAR INDEXLINK>"><TMPL_VAR WIKINAME></a>/
<TMPL_LOOP NAME="PARENTLINKS">
<a href="<TMPL_VAR NAME=URL>"><TMPL_VAR NAME=PAGE></a> /
</TMPL_LOOP>
<TMPL_VAR TITLE>
</h1>
<TMPL_IF NAME="EDITURL">
<a href="<TMPL_VAR EDITURL>">Edit</a>
</TMPL_IF>
<TMPL_IF NAME="RECENTCHANGESURL">
<a href="<TMPL_VAR RECENTCHANGESURL>">RecentChanges</a>
</TMPL_IF>
<TMPL_IF NAME="HISTORYURL">
<a href="<TMPL_VAR HISTORYURL>">History</a>
</TMPL_IF>
<hr>
<TMPL_VAR CONTENT>
<hr>
<TMPL_IF NAME="BACKLINKS">
<p>Links:
<TMPL_LOOP NAME="BACKLINKS">
<a href="<TMPL_VAR NAME=URL>"><TMPL_VAR NAME=PAGE></a>
</TMPL_LOOP>
</p>
</TMPL_IF>
</body>
</html>