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 {
}
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};
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
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(@_);
}
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;
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
{
package IkiWiki::SortSpec;
- sub cmp_path { $_[0] cmp $_[1] }
+ sub cmp_path { $a cmp $b }
}
%pagesources=(