Allow hooks to add sorting functions to pagespec_match_list
parent
e74a85c671
commit
e67a9382f6
|
@ -2035,7 +2035,11 @@ sub pagespec_match_list ($$;@) {
|
||||||
|
|
||||||
if (defined $params{sort}) {
|
if (defined $params{sort}) {
|
||||||
my $f;
|
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)) };
|
$f=sub { pagetitle(basename($a)) cmp pagetitle(basename($b)) };
|
||||||
}
|
}
|
||||||
elsif ($params{sort} eq 'title_natural') {
|
elsif ($params{sort} eq 'title_natural') {
|
||||||
|
|
|
@ -10,4 +10,6 @@ orders can be specified.
|
||||||
installed. Orders by title, but numbers in the title are treated
|
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")
|
as such, ("1 2 9 10 20" instead of "1 10 2 20 9")
|
||||||
|
|
||||||
|
Plugins can add additional sort orders.
|
||||||
|
|
||||||
[[!meta robots="noindex, follow"]]
|
[[!meta robots="noindex, follow"]]
|
||||||
|
|
|
@ -588,6 +588,21 @@ describes the plugin as a whole. For example:
|
||||||
This hook is used to inject C code (which it returns) into the `main`
|
This hook is used to inject C code (which it returns) into the `main`
|
||||||
function of the ikiwiki wrapper when it is being generated.
|
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
|
## Exported variables
|
||||||
|
|
||||||
Several variables are exported to your plugin when you `use IkiWiki;`
|
Several variables are exported to your plugin when you `use IkiWiki;`
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#!/usr/bin/perl
|
#!/usr/bin/perl
|
||||||
use warnings;
|
use warnings;
|
||||||
use strict;
|
use strict;
|
||||||
use Test::More tests => 88;
|
use Test::More tests => 89;
|
||||||
|
|
||||||
BEGIN { use_ok("IkiWiki"); }
|
BEGIN { use_ok("IkiWiki"); }
|
||||||
|
|
||||||
|
@ -9,6 +9,8 @@ BEGIN { use_ok("IkiWiki"); }
|
||||||
$config{srcdir}=$config{destdir}="/dev/null";
|
$config{srcdir}=$config{destdir}="/dev/null";
|
||||||
IkiWiki::checkconfig();
|
IkiWiki::checkconfig();
|
||||||
|
|
||||||
|
hook(type => "sort", id => "path", call => sub { $_[0] cmp $_[1] });
|
||||||
|
|
||||||
%pagesources=(
|
%pagesources=(
|
||||||
foo => "foo.mdwn",
|
foo => "foo.mdwn",
|
||||||
foo2 => "foo2.mdwn",
|
foo2 => "foo2.mdwn",
|
||||||
|
@ -34,6 +36,8 @@ is_deeply([pagespec_match_list("foo", "post/*", sort => "title", num => 50)],
|
||||||
is_deeply([pagespec_match_list("foo", "post/*", sort => "title",
|
is_deeply([pagespec_match_list("foo", "post/*", sort => "title",
|
||||||
filter => sub { $_[0] =~ /3/}) ],
|
filter => sub { $_[0] =~ /3/}) ],
|
||||||
["post/1", "post/2"]);
|
["post/1", "post/2"]);
|
||||||
|
is_deeply([pagespec_match_list("foo", "*", sort => "path", num => 2)],
|
||||||
|
["bar", "foo"]);
|
||||||
my $r=eval { pagespec_match_list("foo", "beep") };
|
my $r=eval { pagespec_match_list("foo", "beep") };
|
||||||
ok(eval { pagespec_match_list("foo", "beep") } == 0);
|
ok(eval { pagespec_match_list("foo", "beep") } == 0);
|
||||||
ok(! $@, "does not fail with error when unable to match anything");
|
ok(! $@, "does not fail with error when unable to match anything");
|
||||||
|
|
Loading…
Reference in New Issue