optional template
[ikiwiki.git] / ikiwiki
diff --git a/ikiwiki b/ikiwiki
index 7e47110593c30b9a2e1b5bd06f4f50b3b73ab8ca..fa214971bb96bc11c8684844f319b31ec8830e26 100755 (executable)
--- 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?$)!;
@@ -25,9 +26,10 @@ my $url="";
 my $cgiurl="";
 my $historyurl="";
 my $svn=1;
+my $anonok=0;
 
 sub usage { #{{{
-       die "usage: ikiwiki [options] source dest\n";
+       die "usage: ikiwiki [options] source templates dest\n";
 } #}}}
 
 sub error ($) { #{{{
@@ -216,8 +218,7 @@ sub htmlize ($$) { #{{{
        }
 } #}}}
 
-sub linkbacks ($$) { #{{{
-       my $content=shift;
+sub backlinks ($) { #{{{
        my $page=shift;
 
        my @links;
@@ -235,12 +236,30 @@ sub linkbacks ($$) { #{{{
                                $p_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 $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 () { #{{{
@@ -254,38 +273,30 @@ sub finalize ($$) { #{{{
        my $title=basename($page);
        $title=~s/_/ /g;
        
-       my $pagelink="";
-       my $path="";
-       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 $template=HTML::Template->new(blind_cache => 1,
+               filename => "$templatedir/page.tmpl");
        
-       my @actions;
        if (length $cgiurl) {
-               push @actions, "<a href=\"$cgiurl?do=edit&page=$page\">Edit</a>";
-               push @actions, "<a href=\"$cgiurl?do=recentchanges\">RecentChanges</a>";
+               $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, "<a href=\"$url\">History</a>";
-       }
-       
-       $content="<html>\n<head><title>$title</title></head>\n<body>\n".
-                 "<h1>$pagelink</h1>\n".
-                 "@actions\n<hr>\n".
-                 $content.
-                 "</body>\n</html>\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 +311,6 @@ sub render ($) { #{{{
                
                $content=linkify($content, $file);
                $content=htmlize($type, $content);
-               $content=linkbacks($content, $page);
                $content=finalize($content, $page);
                
                writefile("$destdir/".htmlpage($page), $content);
@@ -397,7 +407,7 @@ sub rcs_recentchanges ($) { #{{{
                my $div=qr/^--------------------+$/;
                my $infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
                my $state='start';
-               my ($rev, $user, $when, @pages, $message);
+               my ($rev, $user, $when, @pages, @message);
                foreach (`LANG=C svn log -v '$svn_url'`) {
                        chomp;
                        if ($state eq 'start' && /$div/) {
@@ -409,23 +419,24 @@ sub rcs_recentchanges ($) { #{{{
                                $when=concise(ago(time - str2time($3)));
                        }
                        elsif ($state eq 'header' && /^\s+[A-Z]\s+\Q$svn_base\E\/(.+)$/) {
-                               push @pages, pagename($1) if length $1;
+                               push @pages, { link => htmllink("", pagename($1), 1) }
+                                       if length $1;
                        }
                        elsif ($state eq 'header' && /^$/) {
                                $state='body';
                        }
                        elsif ($state eq 'body' && /$div/) {
                                push @ret, { rev => $rev, user => $user,
-                                       when => $when, message => $message,
+                                       when => $when, message => [@message],
                                        pages => [@pages] } if @pages;
                                return @ret if @ret >= $num;
                                
                                $state='header';
-                               $message=$rev=$user=$when=undef;
-                               @pages=();
+                               $rev=$user=$when=undef;
+                               @pages=@message=();
                        }
                        elsif ($state eq 'body') {
-                               $message.="$_<br>\n";
+                               push @message, {line => $_},
                        }
                }
        }
@@ -526,10 +537,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 +570,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);
                        }
                }
@@ -579,7 +590,7 @@ sub gen_wrapper ($$) { #{{{
                error("$this doesn't seem to be executable");
        }
 
-       my @params=($srcdir, $destdir, "--wikiname=$wikiname");
+       my @params=($srcdir, $templatedir, $destdir, "--wikiname=$wikiname");
        push @params, "--verbose" if $verbose;
        push @params, "--rebuild" if $rebuild;
        push @params, "--nosvn" if !$svn;
@@ -587,6 +598,7 @@ sub gen_wrapper ($$) { #{{{
        push @params, "--url=$url" if $url;
        push @params, "--cgiurl=$cgiurl" if $cgiurl;
        push @params, "--historyurl=$historyurl" if $historyurl;
+       push @params, "--anonok" if $anonok;
        my $params=join(" ", @params);
        my $call='';
        foreach my $p ($this, $this, @params) {
@@ -649,24 +661,15 @@ EOF
 sub cgi_recentchanges ($) { #{{{
        my $q=shift;
        
-       my $list="<ul>\n";
-       foreach my $change (rcs_recentchanges(100)) {
-               $list.="<li>";
-               $list.=join(", ", map { htmllink("", $_, 1) } @{$change->{pages}});
-               $list.="<br>\n";
-               $list.="changed ".$change->{when}." by ".
-                      htmllink("", $change->{user}, 1).
-                      ": <i>".$change->{message}."</i>\n";
-               $list.="</li>\n";
-       }
-       $list.="</ul>\n";
-               
-       print $q->header,
-             $q->start_html("RecentChanges"),
-             $q->h1(indexlink()." RecentChanges"),
-             $list,
-             $q->end_form,
-             $q->end_html;
+       my $template=HTML::Template->new(
+               filename => "$templatedir/recentchanges.tmpl");
+       $template->param(
+               title => "RecentChanges",
+               indexlink => $url,
+               wikiname => $wikiname,
+               changelog => [rcs_recentchanges(100)],
+       );
+       print $q->header, $template->output;
 } #}}}
 
 sub cgi_signin ($$) { #{{{
@@ -690,6 +693,7 @@ sub cgi_signin ($$) { #{{{
                javascript => 0,
                params => $q,
                action => $q->request_uri,
+               template => (-e "$templatedir/signin.tmpl" ? "$templatedir/signin.tmpl" : "")
        );
        
        $form->sessionid($session->id);
@@ -774,24 +778,27 @@ sub cgi () { #{{{
        eval q{use CGI::Session};
        
        my $q=CGI->new;
-       # session id has to be _sessionid for CGI::FormBuilder to work.
-       # TODO: stop having the formbuilder emit cookies and change session
-       # id to something else.
-       CGI::Session->name("_sessionid");
-       my $session = CGI::Session->new(undef, $q,
-               { Directory=> "$srcdir/.ikiwiki/sessions" });
        
        my $do=$q->param('do');
        if (! defined $do || ! length $do) {
                error("\"do\" parameter missing");
        }
        
+       # This does not need a session.
        if ($do eq 'recentchanges') {
                cgi_recentchanges($q);
                return;
        }
        
-       if (! defined $session->param("name") || $do eq 'signin') {
+       # session id has to be _sessionid for CGI::FormBuilder to work.
+       # TODO: stop having the formbuilder emit cookies and change session
+       # id to something else.
+       CGI::Session->name("_sessionid");
+       my $session = CGI::Session->new(undef, $q,
+               { Directory=> "$srcdir/.ikiwiki/sessions" });
+       
+       # Everything below this point needs the user to be signed in.
+       if ((! $anonok && ! defined $session->param("name")) || $do eq 'signin') {
                cgi_signin($q, $session);
                return;
        }
@@ -925,14 +932,16 @@ if (grep /^-/, @ARGV) {
                "rebuild" => \$rebuild,
                "wrapper" => \$wrapper,
                "svn!" => \$svn,
+               "anonok!" => \$anonok,
                "cgi" => \$cgi,
                "url=s" => \$url,
                "cgiurl=s" => \$cgiurl,
                "historyurl=s" => \$historyurl,
        ) || usage();
 }
-usage() unless @ARGV == 2;
+usage() unless @ARGV == 3;
 ($srcdir) = possibly_foolish_untaint(shift);
+($templatedir) = possibly_foolish_untaint(shift);
 ($destdir) = possibly_foolish_untaint(shift);
 
 if ($cgi && ! length $url) {