]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki.pm
web commit by neal: Note how wiki links related to file names.
[ikiwiki.git] / IkiWiki.pm
index c0b26e43fb33e77da03b36081effc1090de854bf..560647e067f224bd62d2c9cd49b9c17359933b9f 100644 (file)
@@ -4,17 +4,19 @@ package IkiWiki;
 use warnings;
 use strict;
 use Encode;
+use HTML::Entities;
 use open qw{:utf8 :std};
 
 # Optimisation.
 use Memoize;
 memoize("abs2rel");
+memoize("pagespec_translate");
 
-use vars qw{%config %links %oldlinks %oldpagemtime %pagectime
-            %renderedfiles %pagesources %depends %hooks};
+use vars qw{%config %links %oldlinks %oldpagemtime %pagectime %pagecase
+            %renderedfiles %pagesources %depends %hooks %forcerebuild};
 
 sub defaultconfig () { #{{{
-       wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$|\.rss$)},
+       wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.x?html?$|\.rss$)},
        wiki_link_regexp => qr/\[\[(?:([^\]\|]+)\|)?([^\s\]]+)\]\]/,
        wiki_processor_regexp => qr/\[\[(\w+)\s+([^\]]*)\]\]/,
        wiki_file_regexp => qr/(^[-[:alnum:]_.:\/+]+$)/,
@@ -104,6 +106,12 @@ sub loadplugins () { #{{{
                        error("Failed to load plugin $mod: $@");
                }
        }
+       run_hooks(getopt => sub { shift->() });
+       if (grep /^-/, @ARGV) {
+               print STDERR "Unknown option: $_\n"
+                       foreach grep /^-/, @ARGV;
+               usage();
+       }
 } #}}}
 
 sub error ($) { #{{{
@@ -230,7 +238,7 @@ sub bestlink ($$) { #{{{
        # goes down the directory tree to the base looking for matching
        # pages.
        my $page=shift;
-       my $link=lc(shift);
+       my $link=shift;
        
        my $cwd=$page;
        do {
@@ -239,9 +247,11 @@ sub bestlink ($$) { #{{{
                $l.=$link;
 
                if (exists $links{$l}) {
-                       #debug("for $page, \"$link\", use $l");
                        return $l;
                }
+               elsif (exists $pagecase{lc $l}) {
+                       return $pagecase{lc $l};
+               }
        } while $cwd=~s!/?[^/]+$!!;
 
        #print STDERR "warning: page $page, broken link: $link\n";
@@ -284,7 +294,7 @@ sub styleurl (;$) { #{{{
        return $page."style.css";
 } #}}}
 
-sub abs2rel ($$) {
+sub abs2rel ($$) { #{{{
        # Work around very innefficient behavior in File::Spec if abs2rel
        # is passed two relative paths. It's much faster if paths are
        # absolute!
@@ -295,7 +305,7 @@ sub abs2rel ($$) {
        my $ret=File::Spec->abs2rel($path, $base);
        $ret=~s/^// if defined $ret;
        return $ret;
-}
+} #}}}
 
 sub htmllink ($$$;$$$) { #{{{
        my $lpage=shift; # the page doing the linking
@@ -325,7 +335,7 @@ sub htmllink ($$$;$$$) { #{{{
        }
        if (! grep { $_ eq $bestlink } values %renderedfiles) {
                return "<span><a href=\"".
-                       cgiurl(do => "create", page => $link, from => $page).
+                       cgiurl(do => "create", page => lc($link), from => $page).
                        "\">?</a>$linktext</span>"
        }
        
@@ -374,7 +384,7 @@ sub loadindex () { #{{{
                $items{link}=[];
                foreach my $i (split(/ /, $_)) {
                        my ($item, $val)=split(/=/, $i, 2);
-                       push @{$items{$item}}, $val;
+                       push @{$items{$item}}, decode_entities($val);
                }
 
                next unless exists $items{src}; # skip bad lines for now
@@ -385,9 +395,9 @@ sub loadindex () { #{{{
                        $oldpagemtime{$page}=$items{mtime}[0];
                        $oldlinks{$page}=[@{$items{link}}];
                        $links{$page}=[@{$items{link}}];
-                       $depends{$page}=join(" ", @{$items{depends}})
-                               if exists $items{depends};
+                       $depends{$page}=$items{depends}[0] if exists $items{depends};
                        $renderedfiles{$page}=$items{dest}[0];
+                       $pagecase{lc $page}=$page;
                }
                $pagectime{$page}=$items{ctime}[0];
        }
@@ -410,7 +420,7 @@ sub saveindex () { #{{{
                        "dest=$renderedfiles{$page}";
                $line.=" link=$_" foreach @{$links{$page}};
                if (exists $depends{$page}) {
-                       $line.=" depends=$_" foreach split " ", $depends{$page};
+                       $line.=" depends=".encode_entities($depends{$page}, " \t\n");
                }
                print OUT $line."\n";
        }
@@ -425,7 +435,10 @@ sub template_params (@) { #{{{
                        my $text_ref = shift;
                        $$text_ref=&Encode::decode_utf8($$text_ref);
                },
-               filename => "$config{templatedir}/$filename", @_;
+               filename => "$config{templatedir}/$filename",
+               loop_context_vars => 1,
+               die_on_bad_params => 0,
+               @_;
 } #}}}
 
 sub template ($;@) { #{{{
@@ -448,49 +461,6 @@ sub misctemplate ($$) { #{{{
        return $template->output;
 }#}}}
 
-sub glob_match ($$) { #{{{
-       my $page=shift;
-       my $glob=shift;
-
-       if ($glob =~ /^link\((.+)\)$/) {
-               my $rev = $links{$page} or return undef;
-               foreach my $p (@$rev) {
-                       return 1 if lc $p eq $1;
-               }
-               return 0;
-       } elsif ($glob =~ /^backlink\((.+)\)$/) {
-               my $rev = $links{$1} or return undef;
-               foreach my $p (@$rev) {
-                       return 1 if lc $p eq $page;
-               }
-               return 0;
-       } else {
-               # turn glob into safe regexp
-               $glob=quotemeta($glob);
-               $glob=~s/\\\*/.*/g;
-               $glob=~s/\\\?/./g;
-               $glob=~s!\\/!/!g;
-               
-               return $page=~/^$glob$/i;
-       }
-} #}}}
-
-sub globlist_match ($$) { #{{{
-       my $page=shift;
-       my @globlist=split(" ", shift);
-
-       # check any negated globs first
-       foreach my $glob (@globlist) {
-               return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
-       }
-
-       foreach my $glob (@globlist) {
-               return 1 if glob_match($page, $glob);
-       }
-       
-       return 0;
-} #}}}
-
 sub hook (@) { # {{{
        my %param=@_;
        
@@ -514,4 +484,160 @@ sub run_hooks ($$) { # {{{
        }
 } #}}}
 
+sub globlist_to_pagespec ($) { #{{{
+       my @globlist=split(' ', shift);
+
+       my (@spec, @skip);
+       foreach my $glob (@globlist) {
+               if ($glob=~/^!(.*)/) {
+                       push @skip, $glob;
+               }
+               else {
+                       push @spec, $glob;
+               }
+       }
+
+       my $spec=join(" or ", @spec);
+       if (@skip) {
+               my $skip=join(" and ", @skip);
+               if (length $spec) {
+                       $spec="$skip and ($spec)";
+               }
+               else {
+                       $spec=$skip;
+               }
+       }
+       return $spec;
+} #}}}
+
+sub is_globlist ($) { #{{{
+       my $s=shift;
+       $s=~/[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or";
+} #}}}
+
+sub safequote ($) { #{{{
+       my $s=shift;
+       $s=~s/[{}]//g;
+       return "q{$s}";
+} #}}}
+
+sub pagespec_merge ($$) { #{{{
+       my $a=shift;
+       my $b=shift;
+
+        # Support for old-style GlobLists.
+        if (is_globlist($a)) {
+                $a=globlist_to_pagespec($a);
+        }
+        if (is_globlist($b)) {
+                $b=globlist_to_pagespec($b);
+        }
+
+       return "($a) or ($b)";
+} #}}}
+
+sub pagespec_translate ($) { #{{{
+       # This assumes that $page is in scope in the function
+       # that evalulates the translated pagespec code.
+       my $spec=shift;
+
+       # Support for old-style GlobLists.
+       if (is_globlist($spec)) {
+               $spec=globlist_to_pagespec($spec);
+       }
+
+       # Convert spec to perl code.
+       my $code="";
+       while ($spec=~m/\s*(\!|\(|\)|\w+\([^\)]+\)|[^\s()]+)\s*/ig) {
+               my $word=$1;
+               if (lc $word eq "and") {
+                       $code.=" &&";
+               }
+               elsif (lc $word eq "or") {
+                       $code.=" ||";
+               }
+               elsif ($word eq "(" || $word eq ")" || $word eq "!") {
+                       $code.=" ".$word;
+               }
+               elsif ($word =~ /^(link|backlink|created_before|created_after|creation_month|creation_year|creation_day)\((.+)\)$/) {
+                       $code.=" match_$1(\$page, ".safequote($2).")";
+               }
+               else {
+                       $code.=" match_glob(\$page, ".safequote($word).")";
+               }
+       }
+
+       return $code;
+} #}}}
+
+sub pagespec_match ($$) { #{{{
+       my $page=shift;
+       my $spec=shift;
+
+       return eval pagespec_translate($spec);
+} #}}}
+
+sub match_glob ($$) { #{{{
+       my $page=shift;
+       my $glob=shift;
+
+       # turn glob into safe regexp
+       $glob=quotemeta($glob);
+       $glob=~s/\\\*/.*/g;
+       $glob=~s/\\\?/./g;
+
+       return $page=~/^$glob$/i;
+} #}}}
+
+sub match_link ($$) { #{{{
+       my $page=shift;
+       my $link=lc(shift);
+
+       my $links = $links{$page} or return undef;
+       foreach my $p (@$links) {
+               return 1 if lc $p eq $link;
+       }
+       return 0;
+} #}}}
+
+sub match_backlink ($$) { #{{{
+       match_link(pop, pop);
+} #}}}
+
+sub match_created_before ($$) { #{{{
+       my $page=shift;
+       my $testpage=shift;
+
+       if (exists $pagectime{$testpage}) {
+               return $pagectime{$page} < $pagectime{$testpage};
+       }
+       else {
+               return 0;
+       }
+} #}}}
+
+sub match_created_after ($$) { #{{{
+       my $page=shift;
+       my $testpage=shift;
+
+       if (exists $pagectime{$testpage}) {
+               return $pagectime{$page} > $pagectime{$testpage};
+       }
+       else {
+               return 0;
+       }
+} #}}}
+
+sub match_creation_day ($$) { #{{{
+       return ((gmtime($pagectime{shift()}))[3] == shift);
+} #}}}
+
+sub match_creation_month ($$) { #{{{
+       return ((gmtime($pagectime{shift()}))[4] + 1 == shift);
+} #}}}
+
+sub match_creation_year ($$) { #{{{
+       return ((gmtime($pagectime{shift()}))[5] + 1900 == shift);
+} #}}}
+
 1