From cb8b2f80b2f8c91eba3f3a6a5b9913ab80326df8 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Mon, 5 Apr 2010 22:50:51 +0100 Subject: [PATCH] Use $a and $b for SortSpec cmp callbacks --- IkiWiki.pm | 27 ++++++++++++++++++--------- IkiWiki/Plugin/meta.pm | 14 ++++++-------- IkiWiki/Plugin/sortnaturally.pm | 4 ++-- doc/plugins/write.mdwn | 20 ++++++++++---------- t/pagespec_match_list.t | 2 +- 5 files changed, 37 insertions(+), 30 deletions(-) diff --git a/IkiWiki.pm b/IkiWiki.pm index d716e8b39..da36494fb 100644 --- a/IkiWiki.pm +++ b/IkiWiki.pm @@ -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 diff --git a/IkiWiki/Plugin/meta.pm b/IkiWiki/Plugin/meta.pm index 4992617d0..553f93455 100644 --- a/IkiWiki/Plugin/meta.pm +++ b/IkiWiki/Plugin/meta.pm @@ -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(@_); } diff --git a/IkiWiki/Plugin/sortnaturally.pm b/IkiWiki/Plugin/sortnaturally.pm index f498820a5..92453749d 100644 --- a/IkiWiki/Plugin/sortnaturally.pm +++ b/IkiWiki/Plugin/sortnaturally.pm @@ -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; diff --git a/doc/plugins/write.mdwn b/doc/plugins/write.mdwn index b67142230..f42cc86ae 100644 --- a/doc/plugins/write.mdwn +++ b/doc/plugins/write.mdwn @@ -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 diff --git a/t/pagespec_match_list.t b/t/pagespec_match_list.t index 68112f5c0..2ad7a9105 100755 --- a/t/pagespec_match_list.t +++ b/t/pagespec_match_list.t @@ -12,7 +12,7 @@ IkiWiki::checkconfig(); { package IkiWiki::SortSpec; - sub cmp_path { $_[0] cmp $_[1] } + sub cmp_path { $a cmp $b } } %pagesources=( -- 2.32.0.93.g670b81a890