]> sipb.mit.edu Git - ikiwiki.git/commitdiff
pagespec_match_list added and used in most appropriate places
authorJoey Hess <joey@gnu.kitenet.net>
Thu, 23 Apr 2009 19:45:30 +0000 (15:45 -0400)
committerJoey Hess <joey@gnu.kitenet.net>
Thu, 23 Apr 2009 19:45:30 +0000 (15:45 -0400)
* pagespec_match_list: New API function, matches pages in a list
  and throws an error if the pagespec is bad.
* inline, brokenlinks, calendar, linkmap, map, orphans, pagecount,
  pagestate, postsparkline: Display a handy error message if the pagespec
  is erronious.

13 files changed:
IkiWiki.pm
IkiWiki/Plugin/brokenlinks.pm
IkiWiki/Plugin/calendar.pm
IkiWiki/Plugin/external.pm
IkiWiki/Plugin/inline.pm
IkiWiki/Plugin/linkmap.pm
IkiWiki/Plugin/map.pm
IkiWiki/Plugin/orphans.pm
IkiWiki/Plugin/pagecount.pm
IkiWiki/Plugin/pagestats.pm
IkiWiki/Plugin/postsparkline.pm
debian/changelog
doc/plugins/write.mdwn

index fca8da874f4eac31e66ad6e3f4fbde0ba286a0e2..e260fffea7aad7802e673541533056ad6960a118 100644 (file)
@@ -18,10 +18,10 @@ use vars qw{%config %links %oldlinks %pagemtime %pagectime %pagecase
 
 use Exporter q{import};
 our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
-                 bestlink htmllink readfile writefile pagetype srcfile pagename
-                 displaytime will_render gettext urlto targetpage
-                add_underlay pagetitle titlepage linkpage newpagefile
-                inject
+                 pagespec_match_list bestlink htmllink readfile writefile
+                pagetype srcfile pagename displaytime will_render gettext urlto
+                targetpage add_underlay pagetitle titlepage linkpage
+                newpagefile inject
                  %config %links %pagestate %wikistate %renderedfiles
                  %pagesources %destsources);
 our $VERSION = 3.00; # plugin interface version, next is ikiwiki version
@@ -1832,6 +1832,30 @@ sub pagespec_match ($$;@) {
        return $sub->($page, @params);
 }
 
+sub pagespec_match_list ($$;@) {
+       my $pages=shift;
+       my $spec=shift;
+       my @params=@_;
+
+       my $sub=pagespec_translate($spec);
+       error "syntax error in pagespec \"$spec\""
+               if $@ || ! defined $sub;
+       
+       my @ret;
+       my $r;
+       foreach my $page (@$pages) {
+               $r=$sub->($page, @params);
+               push @ret, $page if $r;
+       }
+
+       if (! @ret && defined $r && $r->isa("IkiWiki::ErrorReason")) {
+               error(sprintf(gettext("cannot match pages: %s"), $r));
+       }
+       else {
+               return @ret;
+       }
+}
+
 sub pagespec_valid ($) {
        my $spec=shift;
 
index bf0d7560dbc8a86a28b1ad694d9de326d0153f1f..da97dbc2855bc59f17efe5792bacb6c36fe8e7f7 100644 (file)
@@ -28,18 +28,17 @@ sub preprocess (@) {
        add_depends($params{page}, $params{pages});
        
        my %broken;
-       foreach my $page (keys %links) {
-               if (pagespec_match($page, $params{pages}, location => $params{page})) {
-                       my $discussion=gettext("discussion");
-                       my %seen;
-                       foreach my $link (@{$links{$page}}) {
-                               next if $seen{$link};
-                               $seen{$link}=1;
-                               next if $link =~ /.*\/\Q$discussion\E/i && $config{discussion};
-                               my $bestlink=bestlink($page, $link);
-                               next if length $bestlink;
-                               push @{$broken{$link}}, $page;
-                       }
+       foreach my $page (pagespec_match_list([keys %links],
+                       $params{pages}, location => $params{page})) {
+               my $discussion=gettext("discussion");
+               my %seen;
+               foreach my $link (@{$links{$page}}) {
+                       next if $seen{$link};
+                       $seen{$link}=1;
+                       next if $link =~ /.*\/\Q$discussion\E/i && $config{discussion};
+                       my $bestlink=bestlink($page, $link);
+                       next if length $bestlink;
+                       push @{$broken{$link}}, $page;
                }
        }
 
index d473c8348234145b5dc7526584b9b7825b027e4a..fe4b16072131eea5571a86d75c65a7a117bb3efc 100644 (file)
@@ -369,8 +369,7 @@ sub preprocess (@) {
        my $page =$params{page};
 
        if (! defined $cache{$pagespec}) {
-               foreach my $p (keys %pagesources) {
-                       next unless pagespec_match($p, $pagespec);
+               foreach my $p (pagespec_match_list([keys %pagesources], $pagespec)) {
                        my $mtime = $IkiWiki::pagectime{$p};
                        my $src   = $pagesources{$p};
                        my @date  = localtime($mtime);
index 066f15cf1b2f12ef4a027e5c9842d1b859358f23..aeee15deafd58b789f1c51ea7aa5c349582db465 100644 (file)
@@ -230,10 +230,17 @@ sub hook ($@) {
 }
 
 sub pagespec_match ($@) {
-       # convert pagespec_match's return object into a XML RPC boolean
+       # convert return object into a XML RPC boolean
        my $plugin=shift;
 
        return RPC::XML::boolean->new(0 + IkiWiki::pagespec_march(@_));
 }
 
+sub pagespec_match_list ($@) {
+       # convert return object into a XML RPC boolean
+       my $plugin=shift;
+
+       return RPC::XML::boolean->new(0 + IkiWiki::pagespec_march_list(@_));
+}
+
 1
index 551c38a65157c64aef2fd6d8629eb9c18eeb2ee6..366357095ee24cee2ac61691538b92196f870e9d 100644 (file)
@@ -183,20 +183,9 @@ sub preprocess_inline (@) {
                $params{template} = $archive ? "archivepage" : "inlinepage";
        }
 
-       my @list;
-       my $lastmatch;
-       foreach my $page (keys %pagesources) {
-               next if $page eq $params{page};
-               $lastmatch=pagespec_match($page, $params{pages}, location => $params{page});
-               if ($lastmatch) {
-                       push @list, $page;
-               }
-       }
-
-       if (! @list && defined $lastmatch &&
-           $lastmatch->isa("IkiWiki::ErrorReason")) {
-               error(sprintf(gettext("cannot match pages: %s"), $lastmatch));
-       }
+       my @list=pagespec_match_list(
+               [ grep { $_ ne $params{page}} keys %pagesources ],
+               $params{pages}, location => $params{page});
 
        if (exists $params{sort} && $params{sort} eq 'title') {
                @list=sort { pagetitle(basename($a)) cmp pagetitle(basename($b)) } @list;
index 941ed5f3672145bb6111c5a6650fb14431641297..0137476acfd60b5aa07693960c59743bab4214df 100644 (file)
@@ -56,10 +56,9 @@ sub genmap ($) {
 
        # Get all the items to map.
        my %mapitems = ();
-       foreach my $item (keys %links) {
-               if (pagespec_match($item, $params{pages}, location => $params{page})) {
-                       $mapitems{$item}=urlto($item, $params{destpage});
-               }
+       foreach my $item (pagespec_match_list([keys %links],
+                               $params{pages}, location => $params{page})) {
+               $mapitems{$item}=urlto($item, $params{destpage});
        }
 
        my $dest=$params{page}."/linkmap.png";
index 328493116e42e29125aca0353062886588e38e5f..120451b5dbde60a26ae165e391cf56232cc30111 100644 (file)
@@ -32,32 +32,31 @@ sub preprocess (@) {
 
        # Get all the items to map.
        my %mapitems;
-       foreach my $page (keys %pagesources) {
-               if (pagespec_match($page, $params{pages}, location => $params{page})) {
-                       if (exists $params{show} && 
-                           exists $pagestate{$page} &&
-                           exists $pagestate{$page}{meta}{$params{show}}) {
-                               $mapitems{$page}=$pagestate{$page}{meta}{$params{show}};
-                       }
-                       else {
-                               $mapitems{$page}='';
-                       }
-                       # Check for a common prefix.
-                       if (! defined $common_prefix) {
-                               $common_prefix=$page;
-                       }
-                       elsif (length $common_prefix &&
-                              $page !~ /^\Q$common_prefix\E(\/|$)/) {
-                               my @a=split(/\//, $page);
-                               my @b=split(/\//, $common_prefix);
-                               $common_prefix="";
-                               while (@a && @b && $a[0] eq $b[0]) {
-                                       if (length $common_prefix) {
-                                               $common_prefix.="/";
-                                       }
-                                       $common_prefix.=shift(@a);
-                                       shift @b;
+       foreach my $page (pagespec_match_list([keys %pagesources],
+                               $params{pages}, location => $params{page})) {
+               if (exists $params{show} && 
+                   exists $pagestate{$page} &&
+                   exists $pagestate{$page}{meta}{$params{show}}) {
+                       $mapitems{$page}=$pagestate{$page}{meta}{$params{show}};
+               }
+               else {
+                       $mapitems{$page}='';
+               }
+               # Check for a common prefix.
+               if (! defined $common_prefix) {
+                       $common_prefix=$page;
+               }
+               elsif (length $common_prefix &&
+                      $page !~ /^\Q$common_prefix\E(\/|$)/) {
+                       my @a=split(/\//, $page);
+                       my @b=split(/\//, $common_prefix);
+                       $common_prefix="";
+                       while (@a && @b && $a[0] eq $b[0]) {
+                               if (length $common_prefix) {
+                                       $common_prefix.="/";
                                }
+                               $common_prefix.=shift(@a);
+                               shift @b;
                        }
                }
        }
index 605e6e43ada79398afe07d1c7c07393612eeca6d..cf74c9b794bb4977d0ed8eb17fbc6e36ed045ba0 100644 (file)
@@ -35,9 +35,10 @@ sub preprocess (@) {
        
        my @orphans;
        my $discussion=gettext("discussion");
-       foreach my $page (keys %pagesources) {
-               next if $linkedto{$page} || $page eq 'index';
-               next unless pagespec_match($page, $params{pages}, location => $params{page});
+       foreach my $page (pagespec_match_list(
+                       [ grep { ! $linkedto{$_} && $_ ne 'index' }
+                               keys %pagesources ],
+                       $params{pages}, location => $params{page})) {
                # If the page has a link to some other page, it's
                # indirectly linked to a page via that page's backlinks.
                next if grep { 
index a143f24d089d3ff0d405fd0c6f1544558375600c..f8881a04bdacab3f3f086cefd29323f8463c82c5 100644 (file)
@@ -27,12 +27,9 @@ sub preprocess (@) {
        add_depends($params{page}, $params{pages});
        
        my @pages=keys %pagesources;
-       return $#pages+1 if $params{pages} eq "*"; # optimisation
-       my $count=0;
-       foreach my $page (@pages) {
-               $count++ if pagespec_match($page, $params{pages}, location => $params{page});
-       }
-       return $count;
+       @pages=pagespec_match_list(\@pages, $params{pages}, location => $params{page})
+               if $params{pages} ne "*"; # optimisation;
+       return $#pages+1;
 }
 
 1
index dbe69539d72311d8b43c752c9ae33ebe8017a763..8ab5d36662ce71716a1733e1a4b5de223c92602b 100644 (file)
@@ -41,12 +41,11 @@ sub preprocess (@) {
        
        my %counts;
        my $max = 0;
-       foreach my $page (keys %links) {
-               if (pagespec_match($page, $params{pages}, location => $params{page})) {
-                       use IkiWiki::Render;
-                       $counts{$page} = scalar(IkiWiki::backlinks($page));
-                       $max = $counts{$page} if $counts{$page} > $max;
-               }
+       foreach my $page (pagespec_match_list([keys %links],
+                       $params{pages}, location => $params{page})) {
+               use IkiWiki::Render;
+               $counts{$page} = scalar(IkiWiki::backlinks($page));
+               $max = $counts{$page} if $counts{$page} > $max;
        }
 
        if ($style eq 'table') {
index ba43561fbc0d5f291f3eb80163dc88c9b506effe..c2ebbc5eb1da5f9e6daf6fcb704f48ebd2fe6dbf 100644 (file)
@@ -50,13 +50,9 @@ sub preprocess (@) {
 
        add_depends($params{page}, $params{pages});
 
-       my @list;
-       foreach my $page (keys %pagesources) {
-               next if $page eq $params{page};
-               if (pagespec_match($page, $params{pages}, location => $params{page})) {
-                       push @list, $page;
-               }
-       }
+       my @list=pagespec_match_list(
+               [ grep { $_ ne $params{page} } keys %pagesources],
+               $params{pages}, location => $params{page});
        
        @list = sort { $params{timehash}->{$b} <=> $params{timehash}->{$a} } @list;
 
index de4ac4a8c33c977540dc724942997f7a2542a951..37b69ada0fdc41af36b7c17326e4391019ac7e33 100644 (file)
@@ -11,8 +11,11 @@ ikiwiki (3.11) UNRELEASED; urgency=low
   * Add IkiWiki::ErrorReason objects, and modify pagespecs to return
     them in cases where they fail to match due to a configuration or syntax
     error.
-  * inline: Display a handy error message if the inline cannot display any
-    pages due to such an error.
+  * pagespec_match_list: New API function, matches pages in a list
+    and throws an error if the pagespec is bad.
+  * inline, brokenlinks, calendar, linkmap, map, orphans, pagecount,
+    pagestate, postsparkline: Display a handy error message if the pagespec
+    is erronious.
   * comments: Add link to comment post form to allow user to sign in
     if they wish to, if the configuration makes signin optional
     for commenting.
index 23df01ca7a73546e9e9354868464a3ba5664a7be..0b358b215a4193efa292d70212eeebf6a14805f4 100644 (file)
@@ -572,6 +572,19 @@ The most often used is "location", which specifies the location the
 PageSpec should match against. If not passed, relative PageSpecs will match
 relative to the top of the wiki.
 
+#### `pagespec_match_list($$;@)`
+
+Passed a reference to a list of page names, and [[ikiwiki/PageSpec]],
+returns the set of pages that match the [[ikiwiki/PageSpec]].
+
+Additional named parameters can be passed, to further limit the match.
+The most often used is "location", which specifies the location the
+PageSpec should match against. If not passed, relative PageSpecs will match
+relative to the top of the wiki.
+
+Unlike pagespec_match, this may throw an error if there is an error in
+the pagespec.
+
 #### `bestlink($$)`
 
 Given a page and the text of a link on the page, determine which