X-Git-Url: http://git.vanrenterghem.biz/git.ikiwiki.info.git/blobdiff_plain/929b9de9c2b0f3ed1a0c9e006d084519bd5e0479..b8a0364859b3a02120c303a47a87149666f53b57:/IkiWiki.pm?ds=sidebyside diff --git a/IkiWiki.pm b/IkiWiki.pm index 7be1c7f58..7ef9007ff 100644 --- a/IkiWiki.pm +++ b/IkiWiki.pm @@ -3,11 +3,16 @@ package IkiWiki; use warnings; use strict; -use encoding "utf8"; # force use of utf8 for io layer use Encode; +use HTML::Entities; +use open qw{:utf8 :std}; + +# Optimisation. +use Memoize; +memoize("abs2rel"); use vars qw{%config %links %oldlinks %oldpagemtime %pagectime - %renderedfiles %pagesources %depends %hooks}; + %renderedfiles %pagesources %depends %hooks %forcerebuild}; sub defaultconfig () { #{{{ wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$|\.rss$)}, @@ -16,7 +21,7 @@ sub defaultconfig () { #{{{ wiki_file_regexp => qr/(^[-[:alnum:]_.:\/+]+$)/, verbose => 0, wikiname => "wiki", - default_pageext => ".mdwn", + default_pageext => "mdwn", cgi => 0, rcs => 'svn', notify => 0, @@ -30,6 +35,7 @@ sub defaultconfig () { #{{{ rebuild => 0, refresh => 0, getctime => 0, + w3mmode => 0, wrapper => undef, wrappermode => undef, svnrepo => undef, @@ -42,11 +48,32 @@ sub defaultconfig () { #{{{ setup => undef, adminuser => undef, adminemail => undef, - plugin => [qw{inline htmlscrubber}], + plugin => [qw{mdwn inline htmlscrubber}], timeformat => '%c', + locale => undef, } #}}} - + sub checkconfig () { #{{{ + # locale stuff; avoid LC_ALL since it overrides everything + if (defined $ENV{LC_ALL}) { + $ENV{LANG} = $ENV{LC_ALL}; + delete $ENV{LC_ALL}; + } + if (defined $config{locale}) { + eval q{use POSIX}; + $ENV{LANG} = $config{locale} + if POSIX::setlocale(&POSIX::LC_TIME, $config{locale}); + } + + if ($config{w3mmode}) { + eval q{use Cwd q{abs_path}}; + $config{srcdir}=possibly_foolish_untaint(abs_path($config{srcdir})); + $config{destdir}=possibly_foolish_untaint(abs_path($config{destdir})); + $config{cgiurl}="file:///\$LIB/ikiwiki-w3m.cgi/".$config{cgiurl} + unless $config{cgiurl} =~ m!file:///!; + $config{url}="file://".$config{destdir}; + } + if ($config{cgi} && ! length $config{url}) { error("Must specify url to wiki with --url when using --cgi\n"); } @@ -67,6 +94,10 @@ sub checkconfig () { #{{{ require IkiWiki::Rcs::Stub; } + run_hooks(checkconfig => sub { shift->() }); +} #}}} + +sub loadplugins () { #{{{ foreach my $plugin (@{$config{plugin}}) { my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin); eval qq{use $mod}; @@ -74,12 +105,12 @@ sub checkconfig () { #{{{ error("Failed to load plugin $mod: $@"); } } - - if (exists $hooks{checkconfig}) { - foreach my $id (keys %{$hooks{checkconfig}}) { - $hooks{checkconfig}{$id}{call}->(); - } - } + run_hooks(getopt => sub { shift->() }); + if (grep /^-/, @ARGV) { + print STDERR "Unknown option: $_\n" + foreach grep /^-/, @ARGV; + usage(); + } } #}}} sub error ($) { #{{{ @@ -123,12 +154,10 @@ sub dirname ($) { #{{{ sub pagetype ($) { #{{{ my $page=shift; - if ($page =~ /\.mdwn$/) { - return ".mdwn"; - } - else { - return "unknown"; + if ($page =~ /\.([^.]+)$/) { + return $1 if exists $hooks{htmlize}{$1}; } + return undef; } #}}} sub pagename ($) { #{{{ @@ -136,7 +165,7 @@ sub pagename ($) { #{{{ my $type=pagetype($file); my $page=$file; - $page=~s/\Q$type\E*$// unless $type eq 'unknown'; + $page=~s/\Q.$type\E*$// if defined $type; return $page; } #}}} @@ -164,12 +193,7 @@ sub readfile ($;$) { #{{{ local $/=undef; open (IN, $file) || error("failed to read $file: $!"); - if (! $binary) { - binmode(IN, ":utf8"); - } - else { - binmode(IN); - } + binmode(IN) if ($binary); my $ret=; close IN; return $ret; @@ -201,12 +225,7 @@ sub writefile ($$$;$) { #{{{ } open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!"); - if (! $binary) { - binmode(OUT, ":utf8"); - } - else { - binmode(OUT); - } + binmode(OUT) if ($binary); print OUT $content; close OUT; } #}}} @@ -272,6 +291,19 @@ sub styleurl (;$) { #{{{ return $page."style.css"; } #}}} +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! + my $path="/".shift; + my $base="/".shift; + + require File::Spec; + my $ret=File::Spec->abs2rel($path, $base); + $ret=~s/^// if defined $ret; + return $ret; +} #}}} + sub htmllink ($$$;$$$) { #{{{ my $lpage=shift; # the page doing the linking my $page=shift; # the page that will contain the link (different for inline) @@ -304,8 +336,7 @@ sub htmllink ($$$;$$$) { #{{{ "\">?$linktext" } - require File::Spec; - $bestlink=File::Spec->abs2rel($bestlink, dirname($page)); + $bestlink=abs2rel($bestlink, dirname($page)); if (! $noimageinline && isinlinableimage($bestlink)) { return "\"$linktext\""; @@ -342,7 +373,7 @@ sub unlockwiki () { #{{{ } #}}} sub loadindex () { #{{{ - open (IN, "<:utf8", "$config{wikistatedir}/index") || return; + open (IN, "$config{wikistatedir}/index") || return; while () { $_=possibly_foolish_untaint($_); chomp; @@ -350,7 +381,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 @@ -361,8 +392,7 @@ 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]; } $pagectime{$page}=$items{ctime}[0]; @@ -371,10 +401,12 @@ sub loadindex () { #{{{ } #}}} sub saveindex () { #{{{ + run_hooks(savestate => sub { shift->() }); + if (! -d $config{wikistatedir}) { mkdir($config{wikistatedir}); } - open (OUT, ">:utf8", "$config{wikistatedir}/index") || + open (OUT, ">$config{wikistatedir}/index") || error("cannot write to $config{wikistatedir}/index: $!"); foreach my $page (keys %oldpagemtime) { next unless $oldpagemtime{$page}; @@ -384,7 +416,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"; } @@ -422,57 +454,159 @@ sub misctemplate ($$) { #{{{ return $template->output; }#}}} -sub glob_match ($$) { #{{{ - my $page=shift; - my $glob=shift; +sub hook (@) { # {{{ + my %param=@_; + + if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) { + error "hook requires type, call, and id parameters"; + } + + $hooks{$param{type}}{$param{id}}=\%param; +} # }}} - if ($glob =~ /^link\((.+)\)$/) { - my $rev = $links{$page} or return undef; - foreach my $p (@$rev) { - return 1 if lc $p eq $1; +sub run_hooks ($$) { # {{{ + # Calls the given sub for each hook of the given type, + # passing it the hook function to call. + my $type=shift; + my $sub=shift; + + if (exists $hooks{$type}) { + foreach my $id (keys %{$hooks{$type}}) { + $sub->($hooks{$type}{$id}{call}); + } + } +} #}}} + +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)"; } - return 0; - } elsif ($glob =~ /^backlink\((.+)\)$/) { - my $rev = $links{$1} or return undef; - foreach my $p (@$rev) { - return 1 if lc $p eq $page; + else { + $spec=$skip; } - return 0; - } else { - # turn glob into safe regexp - $glob=quotemeta($glob); - $glob=~s/\\\*/.*/g; - $glob=~s/\\\?/./g; - $glob=~s!\\/!/!g; - - return $page=~/^$glob$/i; } + 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 globlist_match ($$) { #{{{ +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_match ($$) { #{{{ my $page=shift; - my @globlist=split(" ", shift); + my $spec=shift; - # check any negated globs first - foreach my $glob (@globlist) { - return 0 if $glob=~/^!(.*)/ && glob_match($page, $1); + # Support for old-style GlobLists. + if (is_globlist($spec)) { + $spec=globlist_to_pagespec($spec); } - foreach my $glob (@globlist) { - return 1 if glob_match($page, $glob); + # 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|creation_month|creation_year|creation_day)\((.+)\)$/) { + $code.=" match_$1(\$page, ".safequote($2).")"; + } + else { + $code.=" match_glob(\$page, ".safequote($word).")"; + } + } + + return eval $code; +} #}}} + +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=shift; + + my $links = $links{$page} or return undef; + foreach my $p (@$links) { + return 1 if lc $p eq $link; } - return 0; } #}}} -sub hook (@) { # {{{ - my %param=@_; - - if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) { - error "hook requires type, call, and id parameters"; +sub match_backlink ($$) { #{{{ + my $page=shift; + my $linkto=shift; + + my $links = $links{$linkto} or return undef; + foreach my $p (@$links) { + return 1 if lc $p eq $page; } - - $hooks{$param{type}}{$param{id}}=\%param; -} # }}} + return 0; +} #}}} + +sub match_creation_day ($$) { #{{{ + return if (gmtime($pagectime{shift()}))[3] == shift; +} #}}} + +sub match_creation_month ($$) { #{{{ + return if (gmtime($pagectime{shift()}))[4] + 1 == shift; +} #}}} + +sub match_creation_year ($$) { #{{{ + return if (gmtime($pagectime{shift()}))[5] + 1900 == shift; +} #}}} + 1