]> sipb.mit.edu Git - ikiwiki.git/commitdiff
Use $a and $b for SortSpec cmp callbacks
authorSimon McVittie <smcv@debian.org>
Mon, 5 Apr 2010 21:50:51 +0000 (22:50 +0100)
committerSimon McVittie <smcv@debian.org>
Mon, 5 Apr 2010 21:50:51 +0000 (22:50 +0100)
IkiWiki.pm
IkiWiki/Plugin/meta.pm
IkiWiki/Plugin/sortnaturally.pm
doc/plugins/write.mdwn
t/pagespec_match_list.t

index d716e8b394b9c88b61470d91d7a20ea612bbe3e3..da36494fb4e03f3f1808d7ea68f9a329b71ccab8 100644 (file)
@@ -1975,10 +1975,10 @@ sub sortspec_translate ($) {
                if (exists $IkiWiki::SortSpec::{"cmp_$word"}) {
                        if (defined $params) {
                                push @data, $params;
-                               $code .= "IkiWiki::SortSpec::cmp_$word(\@_, \$data[$#data])";
+                               $code .= "IkiWiki::SortSpec::cmp_$word(\$data[$#data])";
                        }
                        else {
-                               $code .= "IkiWiki::SortSpec::cmp_$word(\@_, undef)";
+                               $code .= "IkiWiki::SortSpec::cmp_$word(undef)";
                        }
                }
                else {
@@ -2095,9 +2095,8 @@ sub pagespec_match_list ($$;@) {
        }
 
        if (defined $params{sort}) {
-               my $f = sortspec_translate($params{sort});
-
-               @candidates = sort { $f->($a, $b) } @candidates;
+               @candidates = IkiWiki::SortSpec::sort_pages($params{sort},
+                       @candidates);
        }
 
        @candidates=reverse(@candidates) if $params{reverse};
@@ -2412,13 +2411,23 @@ sub match_ip ($$;@) {
 
 package IkiWiki::SortSpec;
 
+# This is in the SortSpec namespace so that the $a and $b that sort() uses
+# $IkiWiki::SortSpec::a and $IkiWiki::SortSpec::b, so that plugins' cmp
+# functions can access them easily.
+sub sort_pages
+{
+       my $f = IkiWiki::sortspec_translate(shift);
+
+       return sort $f @_;
+}
+
 sub cmp_title {
-       IkiWiki::pagetitle(IkiWiki::basename($_[0]))
+       IkiWiki::pagetitle(IkiWiki::basename($a))
        cmp
-       IkiWiki::pagetitle(IkiWiki::basename($_[1]))
+       IkiWiki::pagetitle(IkiWiki::basename($b))
 }
 
-sub cmp_mtime { $IkiWiki::pagemtime{$_[1]} <=> $IkiWiki::pagemtime{$_[0]} }
-sub cmp_age { $IkiWiki::pagectime{$_[1]} <=> $IkiWiki::pagectime{$_[0]} }
+sub cmp_mtime { $IkiWiki::pagemtime{$b} <=> $IkiWiki::pagemtime{$a} }
+sub cmp_age { $IkiWiki::pagectime{$b} <=> $IkiWiki::pagectime{$a} }
 
 1
index 4992617d02376c15f822d586550c04c397e4795f..553f9345587889cc779c27679cc5e14ddccf65e2 100644 (file)
@@ -374,25 +374,23 @@ sub match_copyright ($$;@) {
 package IkiWiki::SortSpec;
 
 sub cmp_meta {
-       my $left = $_[0];
-       my $right = $_[1];
-       my $meta = $_[2];
+       my $meta = $_[0];
        error(gettext("sort=meta requires a parameter")) unless defined $meta;
 
        if ($meta eq 'updated' || $meta eq 'date') {
-               return IkiWiki::Plugin::meta::get_sort_key($left, $meta)
+               return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
                        <=>
-                       IkiWiki::Plugin::meta::get_sort_key($right, $meta);
+                       IkiWiki::Plugin::meta::get_sort_key($b, $meta);
        }
 
-       return IkiWiki::Plugin::meta::get_sort_key($left, $meta)
+       return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
                cmp
-               IkiWiki::Plugin::meta::get_sort_key($right, $meta);
+               IkiWiki::Plugin::meta::get_sort_key($b, $meta);
 }
 
 # A prototype of how sort=title could behave in 4.0 or something
 sub cmp_meta_title {
-       $_[2] = 'title';
+       $_[0] = 'title';
        return cmp_meta(@_);
 }
 
index f498820a5bc7fa6107d4f182ef498641776262d7..92453749d9048f3acdea42bfc4058360e67d1e63 100644 (file)
@@ -25,8 +25,8 @@ sub checkconfig () {
 package IkiWiki::SortSpec;
 
 sub cmp_title_natural {
-       Sort::Naturally::ncmp(IkiWiki::pagetitle(IkiWiki::basename($_[0])),
-               IkiWiki::pagetitle(IkiWiki::basename($_[1])))
+       Sort::Naturally::ncmp(IkiWiki::pagetitle(IkiWiki::basename($a)),
+               IkiWiki::pagetitle(IkiWiki::basename($b)))
 }
 
 1;
index b671422306b16915300f5afeb19e0778593407a5..f42cc86ae29bc00f26ed655d57bf2f83adbb3869 100644 (file)
@@ -1117,16 +1117,16 @@ Similarly, it's possible to write plugins that add new functions as
 the IkiWiki::SortSpec package named `cmp_foo`, which will be used when sorting
 by `foo` or `foo(...)` is requested.
 
-The function will be passed three or more parameters. The first two are
-page names, and the third is `undef` if invoked as `foo`, or the parameter
-`"bar"` if invoked as `foo(bar)`. It may also be passed additional, named
-parameters.
-
-It should return the same thing as Perl's `cmp` and `<=>` operators: negative
-if the first argument is less than the second, positive if the first argument
-is greater, or zero if they are considered equal. It may also raise an
-error using `error`, for instance if it needs a parameter but one isn't
-provided.
+The names of pages to be compared are in the global variables `$a` and `$b`
+in the IkiWiki::SortSpec package. The function should return the same thing
+as Perl's `cmp` and `<=>` operators: negative if `$a` is less than `$b`,
+positive if `$a` is greater, or zero if they are considered equal. It may
+also raise an error using `error`, for instance if it needs a parameter but
+one isn't provided.
+
+The function will also be passed one or more parameters. The first is
+`undef` if invoked as `foo`, or the parameter `"bar"` if invoked as `foo(bar)`;
+it may also be passed additional, named parameters.
 
 ### Setup plugins
 
index 68112f5c0f77245a771fd7e73cda3d7586b5ae86..2ad7a910506b1ef3d98a207477eba8ebd9787340 100755 (executable)
@@ -12,7 +12,7 @@ IkiWiki::checkconfig();
 {
        package IkiWiki::SortSpec;
 
-       sub cmp_path { $_[0] cmp $_[1] }
+       sub cmp_path { $a cmp $b }
 }
 
 %pagesources=(