]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/allow_plugins_to_add_sorting_methods.mdwn
21800f4ded3f2d4098e25b099f325aa16be3f3c1
[ikiwiki.git] / doc / todo / allow_plugins_to_add_sorting_methods.mdwn
1 [[!template id=gitbranch branch=smcv/sort-hooks author="[[Simon_McVittie|smcv]]"]]
2 [[!tag patch]]
3
4 The available [[ikiwiki/pagespec/sorting]] methods are currently hard-coded in
5 IkiWiki.pm, making it difficult to add any extra sorting mechanisms. I've
6 prepared a branch which adds 'sort' as a hook type and uses it to implement a
7 new `meta_title` sort type.
8
9 Someone could use this hook to make `\[[!inline sort=title]]` prefer the meta
10 title over the page name, but for compatibility, I'm not going to (I do wonder
11 whether it would be worth making sort=name an alias for the current sort=title,
12 and changing the meaning of sort=title in 4.0, though).
13
14 Gitweb:
15 <http://git.pseudorandom.co.uk/smcv/ikiwiki.git?a=shortlog;h=refs/heads/sort-hooks>
16
17 I briefly tried to turn *all* the current sort types into hook functions, and
18 have some of them pre-registered, but decided that probably wasn't a good idea.
19 That earlier version of the branch is also available for comparison:
20
21 <http://git.pseudorandom.co.uk/smcv/ikiwiki.git?a=shortlog;h=refs/heads/sort-hooks-excessive>
22
23 (The older version is untested, and probably doesn't really work as-is - I
24 misunderstood the details of how the built-in function `sort` works when using
25 `$a` and `$b`. The newer version has been tested, and has a regression test for
26 its core functionality.)
27
28 This hook *isn't* (yet) sufficient to implement [[plugins/contrib/report]]'s
29 NIH'd sorting mechanisms:
30
31 * `report` can sort by any [[plugins/contrib/field]], whereas this one has a
32   finite number of hooks: if the `field` plugin's functionality is desirable,
33   perhaps parameterized sort mechanisms similar to pagespec match functions
34   would be useful? Then the `field` plugin could register
35   `hook(type => "sort", id => "field")` and you could have
36   `\[[!inline ... sort="field(Mood)"]]` or something?
37
38 * `report` can sort by multiple criteria, with independent direction-changing:
39   if this is desirable, perhaps `pagespec_match_list` could be enhanced to
40   interpret `sort="x -y z(w)"` as sorting by (pseudocode)
41   `{ $cmp_x->($a, $b) || (-$cmp_y->($a, $b)) || $cmp_z->($a, $b, "w") }`?
42
43 ## Documentation extracted from the branch
44
45 ### sort hook (added to [[plugins/write]])
46
47        hook(type => "sort", id => "foo", call => \&sort_by_foo);
48
49 This hook adds an additional [[ikiwiki/pagespec/sorting]] order or overrides
50 an existing one. The callback is given two page names as arguments, and
51 returns negative, zero or positive if the first page should come before,
52 close to (i.e. undefined order), or after the second page.
53
54 For instance, the built-in `title` sort order could be reimplemented as
55
56        sub sort_by_title {
57                pagetitle(basename($_[0])) cmp pagetitle(basename($_[1]));
58        }
59
60 ### meta_title sort order (conditionally added to [[ikiwiki/pagespec/sorting]])
61
62 * `meta_title` - Order according to the `\[[!meta title="foo" sort="bar"]]`
63   or `\[[!meta title="foo"]]` [[ikiwiki/directive]], or the page name if no
64   full title was set.
65
66 ### meta title sort parameter (added to [[ikiwiki/directive/meta]])
67
68 An optional `sort` parameter will be used preferentially when
69 [[ikiwiki/pagespec/sorting]] by `meta_title`:
70
71        \[[!meta title="The Beatles" sort="Beatles, The"]]
72
73        \[[!meta title="David Bowie" sort="Bowie, David"]]