]> sipb.mit.edu Git - ikiwiki.git/blob - doc/plugins/contrib/field/discussion.mdwn
Merge branch 'master' of ssh://git.ikiwiki.info/srv/git/ikiwiki.info
[ikiwiki.git] / doc / plugins / contrib / field / discussion.mdwn
1 Having tried out `field`, some comments (from [[smcv]]):
2
3 The general concept looks great.
4
5 The `pagetemplate` hook seems quite namespace-polluting: on a site containing
6 a list of books, I'd like to have an `author` field, but that would collide
7 with IkiWiki's use of `<TMPL_VAR AUTHOR>` for the author of the *page*
8 (i.e. me). Perhaps it'd be better if the pagetemplate hook was only active for
9 `<TMPL_VAR FIELD_AUTHOR>` or something? (For those who want the current
10 behaviour, an auxiliary plugin would be easy.)
11
12 From a coding style point of view, the `$CamelCase` variable names aren't
13 IkiWiki style, and the `match_foo` functions look as though they could benefit
14 from being thin wrappers around a common `&IkiWiki::Plugin::field::match`
15 function (see `meta` for a similar approach).
16
17 I think the documentation would probably be clearer in a less manpage-like
18 and more ikiwiki-like style?
19
20 If one of my branches from [[todo/allow_plugins_to_add_sorting_methods]] is
21 accepted, a `field()` cmp type would mean that [[plugins/contrib/report]] can
22 stop reimplementing sorting. Here's the implementation I'm using, with
23 your "sortspec" concept (a sort-hook would be very similar): if merged,
24 I think it should just be part of `field` rather than a separate plugin.
25
26         # Copyright © 2010 Simon McVittie, released under GNU LGPL >= 2.1
27         package IkiWiki::Plugin::fieldsort;
28         use warnings;
29         use strict;
30         use IkiWiki 3.00;
31         use IkiWiki::Plugin::field;
32
33         sub import {
34                 hook(type => "getsetup", id => "fieldsort",  call => \&getsetup);
35         }
36
37         sub getsetup () {
38                 return
39                         plugin => {
40                                 safe => 1,
41                                 rebuild => undef,
42                         },
43         }
44
45         package IkiWiki::PageSpec;
46
47         sub check_cmp_field {
48                 if (!length $_[0]) {
49                         error("sort=field requires a parameter");
50                 }
51         }
52
53         sub cmp_field {
54                 my $left = IkiWiki::Plugin::field::field_get_value($_[2], $_[0]);
55                 my $right = IkiWiki::Plugin::field::field_get_value($_[2], $_[1]);
56
57                 $left = "" unless defined $left;
58                 $right = "" unless defined $right;
59                 return $left cmp $right;
60         }
61
62         1;