Use $a and $b for SortSpec cmp callbacks

master
Simon McVittie 2010-04-05 22:50:51 +01:00
parent 490f95616a
commit cb8b2f80b2
5 changed files with 38 additions and 31 deletions

View 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;
sub cmp_title {
IkiWiki::pagetitle(IkiWiki::basename($_[0]))
cmp
IkiWiki::pagetitle(IkiWiki::basename($_[1]))
# 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_mtime { $IkiWiki::pagemtime{$_[1]} <=> $IkiWiki::pagemtime{$_[0]} }
sub cmp_age { $IkiWiki::pagectime{$_[1]} <=> $IkiWiki::pagectime{$_[0]} }
sub cmp_title {
IkiWiki::pagetitle(IkiWiki::basename($a))
cmp
IkiWiki::pagetitle(IkiWiki::basename($b))
}
sub cmp_mtime { $IkiWiki::pagemtime{$b} <=> $IkiWiki::pagemtime{$a} }
sub cmp_age { $IkiWiki::pagectime{$b} <=> $IkiWiki::pagectime{$a} }
1

View 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(@_);
}

View 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;

View 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.
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.
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 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

View File

@ -12,7 +12,7 @@ IkiWiki::checkconfig();
{
package IkiWiki::SortSpec;
sub cmp_path { $_[0] cmp $_[1] }
sub cmp_path { $a cmp $b }
}
%pagesources=(