if (defined $params{sort}) {
my $f;
- if ($params{sort} eq 'title') {
+
+ if (exists $hooks{sort}{$params{sort}}{call}) {
+ $f = sub { $hooks{sort}{$params{sort}}{call}($a, $b) };
+ }
+ elsif ($params{sort} eq 'title') {
$f=sub { pagetitle(basename($a)) cmp pagetitle(basename($b)) };
}
elsif ($params{sort} eq 'title_natural') {
installed. Orders by title, but numbers in the title are treated
as such, ("1 2 9 10 20" instead of "1 10 2 20 9")
+Plugins can add additional sort orders.
+
[[!meta robots="noindex, follow"]]
This hook is used to inject C code (which it returns) into the `main`
function of the ikiwiki wrapper when it is being generated.
+### sort
+
+ hook(type => "sort", id => "foo", call => \&sort_by_foo);
+
+This hook adds an additional [[ikiwiki/pagespec/sorting]] order or overrides
+an existing one. The callback is given two page names as arguments, and
+returns negative, zero or positive if the first page should come before,
+close to (i.e. undefined order), or after the second page.
+
+For instance, the built-in `title` sort order could be reimplemented as
+
+ sub sort_by_title {
+ pagetitle(basename($_[0])) cmp pagetitle(basename($_[1]));
+ }
+
## Exported variables
Several variables are exported to your plugin when you `use IkiWiki;`
#!/usr/bin/perl
use warnings;
use strict;
-use Test::More tests => 88;
+use Test::More tests => 89;
BEGIN { use_ok("IkiWiki"); }
$config{srcdir}=$config{destdir}="/dev/null";
IkiWiki::checkconfig();
+hook(type => "sort", id => "path", call => sub { $_[0] cmp $_[1] });
+
%pagesources=(
foo => "foo.mdwn",
foo2 => "foo2.mdwn",
is_deeply([pagespec_match_list("foo", "post/*", sort => "title",
filter => sub { $_[0] =~ /3/}) ],
["post/1", "post/2"]);
+is_deeply([pagespec_match_list("foo", "*", sort => "path", num => 2)],
+ ["bar", "foo"]);
my $r=eval { pagespec_match_list("foo", "beep") };
ok(eval { pagespec_match_list("foo", "beep") } == 0);
ok(! $@, "does not fail with error when unable to match anything");