+
+ return 1;
+} #}}}
+
+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;
+ return ( $s =~ /[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or" );
+} #}}}
+
+sub safequote ($) { #{{{
+ my $s=shift;
+ $s=~s/[{}]//g;
+ return "q{$s}";
+} #}}}
+
+sub add_depends ($$) { #{{{
+ my $page=shift;
+ my $pagespec=shift;
+
+ if (! exists $depends{$page}) {
+ $depends{$page}=$pagespec;
+ }
+ else {
+ $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
+ }
+
+ return 1;
+} # }}}
+
+sub file_pruned ($$) { #{{{
+ require File::Spec;
+ my $file=File::Spec->canonpath(shift);
+ my $base=File::Spec->canonpath(shift);
+ $file =~ s#^\Q$base\E/*##;
+
+ my $regexp='('.join('|', @{$config{wiki_file_prune_regexps}}).')';
+ return $file =~ m/$regexp/;
+} #}}}
+
+sub gettext { #{{{
+ # Only use gettext in the rare cases it's needed.
+ if ((exists $ENV{LANG} && length $ENV{LANG}) ||
+ (exists $ENV{LC_ALL} && length $ENV{LC_ALL}) ||
+ (exists $ENV{LC_MESSAGES} && length $ENV{LC_MESSAGES})) {
+ if (! $gettext_obj) {
+ $gettext_obj=eval q{
+ use Locale::gettext q{textdomain};
+ Locale::gettext->domain('ikiwiki')
+ };
+ if ($@) {
+ print STDERR "$@";
+ $gettext_obj=undef;
+ return shift;
+ }
+ }
+ return $gettext_obj->get(shift);
+ }
+ else {
+ return shift;
+ }
+} #}}}
+
+sub pagespec_merge ($$) { #{{{
+ my $a=shift;
+ my $b=shift;
+
+ return $a if $a eq $b;
+
+ # 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* # ignore whitespace
+ ( # 1: match a single word
+ \! # !
+ |
+ \( # (
+ |
+ \) # )
+ |
+ \w+\([^\)]*\) # command(params)
+ |
+ [^\s()]+ # any other text
+ )
+ \s* # ignore whitespace
+ }igx) {
+ 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 =~ /^(\w+)\((.*)\)$/) {
+ if (exists $IkiWiki::PageSpec::{"match_$1"}) {
+ $code.="IkiWiki::PageSpec::match_$1(\$page, ".safequote($2).", \@params)";
+ }
+ else {
+ $code.=' 0';
+ }
+ }
+ else {
+ $code.=" IkiWiki::PageSpec::match_glob(\$page, ".safequote($word).", \@params)";
+ }
+ }
+
+ return $code;
+} #}}}
+
+sub pagespec_match ($$;@) { #{{{
+ my $page=shift;
+ my $spec=shift;
+ my @params=@_;
+
+ # Backwards compatability with old calling convention.
+ if (@params == 1) {
+ unshift @params, 'location';
+ }
+
+ my $ret=eval pagespec_translate($spec);
+ return IkiWiki::FailReason->new('syntax error') if $@;
+ return $ret;
+} #}}}
+
+package IkiWiki::FailReason;
+
+use overload ( #{{{
+ '""' => sub { ${$_[0]} },
+ '0+' => sub { 0 },
+ '!' => sub { bless $_[0], 'IkiWiki::SuccessReason'},
+ fallback => 1,
+); #}}}
+
+sub new { #{{{
+ return bless \$_[1], $_[0];
+} #}}}
+
+package IkiWiki::SuccessReason;
+
+use overload ( #{{{
+ '""' => sub { ${$_[0]} },
+ '0+' => sub { 1 },
+ '!' => sub { bless $_[0], 'IkiWiki::FailReason'},
+ fallback => 1,
+); #}}}
+
+sub new { #{{{
+ return bless \$_[1], $_[0];
+}; #}}}
+
+package IkiWiki::PageSpec;
+
+sub match_glob ($$;@) { #{{{
+ my $page=shift;
+ my $glob=shift;
+ my %params=@_;
+
+ my $from=exists $params{location} ? $params{location} : '';
+
+ # relative matching
+ if ($glob =~ m!^\./!) {
+ $from=~s#/?[^/]+$##;
+ $glob=~s#^\./##;
+ $glob="$from/$glob" if length $from;
+ }
+
+ # turn glob into safe regexp
+ $glob=quotemeta($glob);
+ $glob=~s/\\\*/.*/g;
+ $glob=~s/\\\?/./g;
+
+ if ($page=~/^$glob$/i) {
+ return IkiWiki::SuccessReason->new("$glob matches $page");
+ }
+ else {
+ return IkiWiki::FailReason->new("$glob does not match $page");
+ }
+} #}}}
+
+sub match_link ($$;@) { #{{{
+ my $page=shift;
+ my $link=lc(shift);
+ my %params=@_;
+
+ my $from=exists $params{location} ? $params{location} : '';
+
+ # relative matching
+ if ($link =~ m!^\.! && defined $from) {
+ $from=~s#/?[^/]+$##;
+ $link=~s#^\./##;
+ $link="$from/$link" if length $from;
+ }
+
+ my $links = $IkiWiki::links{$page};
+ return IkiWiki::FailReason->new("$page has no links") unless $links && @{$links};
+ my $bestlink = IkiWiki::bestlink($from, $link);
+ foreach my $p (@{$links}) {
+ if (length $bestlink) {
+ return IkiWiki::SuccessReason->new("$page links to $link")
+ if $bestlink eq IkiWiki::bestlink($page, $p);
+ }
+ else {
+ return IkiWiki::SuccessReason->new("$page links to page $p matching $link")
+ if match_glob($p, $link, %params);
+ }
+ }
+ return IkiWiki::FailReason->new("$page does not link to $link");
+} #}}}
+
+sub match_backlink ($$;@) { #{{{
+ return match_link($_[1], $_[0], @_);
+} #}}}
+
+sub match_created_before ($$;@) { #{{{
+ my $page=shift;
+ my $testpage=shift;
+
+ if (exists $IkiWiki::pagectime{$testpage}) {
+ if ($IkiWiki::pagectime{$page} < $IkiWiki::pagectime{$testpage}) {
+ return IkiWiki::SuccessReason->new("$page created before $testpage");
+ }
+ else {
+ return IkiWiki::FailReason->new("$page not created before $testpage");
+ }
+ }
+ else {
+ return IkiWiki::FailReason->new("$testpage has no ctime");
+ }
+} #}}}
+
+sub match_created_after ($$;@) { #{{{
+ my $page=shift;
+ my $testpage=shift;
+
+ if (exists $IkiWiki::pagectime{$testpage}) {
+ if ($IkiWiki::pagectime{$page} > $IkiWiki::pagectime{$testpage}) {
+ return IkiWiki::SuccessReason->new("$page created after $testpage");
+ }
+ else {
+ return IkiWiki::FailReason->new("$page not created after $testpage");
+ }
+ }
+ else {
+ return IkiWiki::FailReason->new("$testpage has no ctime");
+ }
+} #}}}
+
+sub match_creation_day ($$;@) { #{{{
+ if ((gmtime($IkiWiki::pagectime{shift()}))[3] == shift) {
+ return IkiWiki::SuccessReason->new('creation_day matched');
+ }
+ else {
+ return IkiWiki::FailReason->new('creation_day did not match');
+ }
+} #}}}
+
+sub match_creation_month ($$;@) { #{{{
+ if ((gmtime($IkiWiki::pagectime{shift()}))[4] + 1 == shift) {
+ return IkiWiki::SuccessReason->new('creation_month matched');
+ }
+ else {
+ return IkiWiki::FailReason->new('creation_month did not match');
+ }
+} #}}}
+
+sub match_creation_year ($$;@) { #{{{
+ if ((gmtime($IkiWiki::pagectime{shift()}))[5] + 1900 == shift) {
+ return IkiWiki::SuccessReason->new('creation_year matched');
+ }
+ else {
+ return IkiWiki::FailReason->new('creation_year did not match');
+ }
+} #}}}
+
+sub match_user ($$;@) { #{{{
+ shift;
+ my $user=shift;
+ my %params=@_;
+
+ return IkiWiki::FailReason->new('cannot match user')
+ unless exists $params{user};
+ if ($user eq $params{user}) {
+ return IkiWiki::SuccessReason->new("user is $user")
+ }
+ else {
+ return IkiWiki::FailReason->new("user is not $user");
+ }