]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki.pm
glob2re: return a precompiled, anchored case-insensitive regex
[ikiwiki.git] / IkiWiki.pm
index 941ee13c4d7cb6d7a76ea9643b133977f092e5e1..bb7d46ccfb63f8de96a4246b49de1dea67d9b8ea 100644 (file)
@@ -2388,7 +2388,7 @@ sub glob2re ($) {
        my $re=quotemeta(shift);
        $re=~s/\\\*/.*/g;
        $re=~s/\\\?/./g;
-       return $re;
+       return qr/^$re$/i;
 }
 
 package IkiWiki::FailReason;
@@ -2482,6 +2482,8 @@ sub derel ($$) {
        return $path;
 }
 
+my %glob_cache;
+
 sub match_glob ($$;@) {
        my $page=shift;
        my $glob=shift;
@@ -2489,8 +2491,13 @@ sub match_glob ($$;@) {
        
        $glob=derel($glob, $params{location});
 
-       my $regexp=IkiWiki::glob2re($glob);
-       if ($page=~/^$regexp$/i) {
+       # Instead of converting the glob to a regex every time,
+       # cache the compiled regex to save time.
+       if (!defined $glob_cache{$glob}) {
+               my $re = IkiWiki::glob2re($glob);
+               $glob_cache{$glob} = qr/^$re$/i;
+       }
+       if ($page=~ $glob_cache{$glob}) {
                if (! IkiWiki::isinternal($page) || $params{internal}) {
                        return IkiWiki::SuccessReason->new("$glob matches $page");
                }
@@ -2608,11 +2615,12 @@ sub match_created_after ($$;@) {
 }
 
 sub match_creation_day ($$;@) {
+       my $page=shift;
        my $d=shift;
        if ($d !~ /^\d+$/) {
-               return IkiWiki::FailReason->new('invalid day');
+               return IkiWiki::ErrorReason->new("invalid day $d");
        }
-       if ((localtime($IkiWiki::pagectime{shift()}))[3] == shift) {
+       if ((localtime($IkiWiki::pagectime{$page}))[3] == $d) {
                return IkiWiki::SuccessReason->new('creation_day matched');
        }
        else {
@@ -2621,11 +2629,12 @@ sub match_creation_day ($$;@) {
 }
 
 sub match_creation_month ($$;@) {
+       my $page=shift;
        my $m=shift;
        if ($m !~ /^\d+$/) {
-               return IkiWiki::FailReason->new('invalid month');
+               return IkiWiki::ErrorReason->new("invalid month $m");
        }
-       if ((localtime($IkiWiki::pagectime{shift()}))[4] + 1 == shift) {
+       if ((localtime($IkiWiki::pagectime{$page}))[4] + 1 == $m) {
                return IkiWiki::SuccessReason->new('creation_month matched');
        }
        else {
@@ -2634,11 +2643,12 @@ sub match_creation_month ($$;@) {
 }
 
 sub match_creation_year ($$;@) {
+       my $page=shift;
        my $y=shift;
        if ($y !~ /^\d+$/) {
-               return IkiWiki::FailReason->new('invalid year');
+               return IkiWiki::ErrorReason->new("invalid year $y");
        }
-       if ((localtime($IkiWiki::pagectime{shift()}))[5] + 1900 == $y) {
+       if ((localtime($IkiWiki::pagectime{$page}))[5] + 1900 == $y) {
                return IkiWiki::SuccessReason->new('creation_year matched');
        }
        else {