-sub linkbacks ($$) {
- my $content=shift;
- my $page=shift;
-
- my @links;
- foreach my $p (keys %links) {
- next if bestlink($page, $p) eq $page;
- if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
- my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
-
- # Trim common dir prefixes from both pages.
- my $p_trimmed=$p;
- my $page_trimmed=$page;
- my $dir;
- 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
- defined $dir &&
- $p_trimmed=~s/^\Q$dir\E// &&
- $page_trimmed=~s/^\Q$dir\E//;
-
- push @links, "<a href=\"$href\">$p_trimmed</a>";
- }
- }
-
- $content.="<hr><p>Links: ".join(" ", sort @links)."</p>\n" if @links;
- return $content;
-}
-
-sub finalize ($$) {
- my $content=shift;
- my $page=shift;
-
- 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="<a href=\"$path\">$wikiname</a>/ $pagelink";
-
- $content="<html>\n<head><title>$title</title></head>\n<body>\n".
- "<h1>$pagelink</h1>\n".
- $content.
- "</body>\n</html>\n";
-
- return $content;
-}
-
-sub render ($) {
- my $file=shift;
-
- my $type=pagetype($file);
- my $content=readpage($file);
- if ($type ne 'unknown') {
- my $page=pagename($file);
-
- $links{$page}=[findlinks($content)];
-
- $content=linkify($content, $file);
- $content=htmlize($type, $content);
- $content=linkbacks($content, $page);
- $content=finalize($content, $page);
-
- writepage(htmlpage($page), $content);
- $oldpagemtime{$page}=time;
- $renderedfiles{$page}=htmlpage($page);
- }
- else {
- $links{$file}=[];
- writepage($file, $content);
- $oldpagemtime{$file}=time;
- $renderedfiles{$file}=$file;
- }
-}
-
-sub loadindex () {
- open (IN, "$srcdir/.index") || return;
- while (<IN>) {
- $_=possibly_foolish_untaint($_);
- chomp;
- my ($mtime, $file, $rendered, @links)=split(' ', $_);
- my $page=pagename($file);
- $pagesources{$page}=$file;
- $oldpagemtime{$page}=$mtime;
- $oldlinks{$page}=[@links];
- $links{$page}=[@links];
- $renderedfiles{$page}=$rendered;
- }
- close IN;
-}
-
-sub saveindex () {
- open (OUT, ">$srcdir/.index") || error("cannot write to .index: $!");
- foreach my $page (keys %oldpagemtime) {
- print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
- join(" ", @{$links{$page}})."\n"
- if $oldpagemtime{$page};
- }
- close OUT;
-}
-
-sub prune ($) {
- my $file=shift;
-
- unlink($file);
- my $dir=dirname($file);
- while (rmdir($dir)) {
- $dir=dirname($dir);
- }
-}
-
-sub refresh () {
- # Find existing pages.
- my %exists;
- my @files;
- find({
- no_chdir => 1,
- wanted => sub {
- if (/\/\.svn\//) {
- $File::Find::prune=1;
- }
- elsif (! -d $_ && ! /\.html$/ && ! /\/\./) {
- my ($f)=/(^[-A-Za-z0-9_.:\/+]+$)/; # untaint
- if (! defined $f) {
- warn("skipping bad filename $_\n");
- }
- else {
- $f=~s/^\Q$srcdir\E\/?//;
- push @files, $f;
- $exists{pagename($f)}=1;