]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/pagespec_aliases.mdwn
(no commit message)
[ikiwiki.git] / doc / todo / pagespec_aliases.mdwn
1 [[!tag patch wishlist]]I quite often find myself repeating a boiler-plate
2 [[ikiwiki/pagespec]] chunk, e.g.
3
4     and !*.png and !*.jpg...
5
6 it would be quite nice if I could conveniently bundle them together into a
7 pagespec "alias", and instead write
8
9     and !image()...
10
11 I wrote the following plugin to achieve this:
12
13     commit f3a9dd113338fe5d2b717de1dc69679ff74e2f8d
14     Author: Jon Dowland <jmtd@debian.org>
15     Date:   Tue May 3 17:40:16 2011 +0100
16     
17         new plugin: alias.pm - pagespec aliases
18     
19     diff --git a/IkiWiki/Plugin/alias.pm b/IkiWiki/Plugin/alias.pm
20     new file mode 100644
21     index 0000000..b8d4574
22     --- /dev/null
23     +++ b/IkiWiki/Plugin/alias.pm
24     @@ -0,0 +1,47 @@
25     +package IkiWiki::Plugin::alias;
26     +
27     +use warnings;
28     +use strict;
29     +use IkiWiki '3.00';
30     +
31     +sub import {
32     +  hook(type => "getsetup", id=> "alias", call => \&getsetup);
33     +  hook(type => "checkconfig", id=> "alias", call => \&checkconfig);
34     +}
35     +
36     +sub getsetup () {
37     +    return
38     +        plugin => {
39     +            description => "allows the definition of pagespec aliases",
40     +            safe => 1,
41     +            rebuild => 1,
42     +            section => "misc",
43     +        },
44     +        pagespec_aliases => {
45     +            type => "string",
46     +            example => {"image" => "*jpg or *jpeg or *png or *gif or *ico" },
47     +            description => "a set of mappings from alias name to pagespec",
48     +            safe => 1,
49     +            rebuild => 0,
50     +        },
51     +}
52     +
53     +sub checkconfig () {
54     +    no strict 'refs';
55     +    no warnings 'redefine';
56     +
57     +    if ($config{pagespec_aliases}) {
58     +        foreach my $key (keys %{$config{pagespec_aliases}}) {
59     +            my $value = ${$config{pagespec_aliases}}{$key};
60     +            # XXX: validate key?
61     +            my $subname = "IkiWiki::PageSpec::match_$key";
62     +            *{ $subname } = sub {
63     +              my $path = shift;
64     +              return IkiWiki::pagespec_match($path, $value);
65     +            }
66     +        }
67     +    }
68     +}
69     +
70     +1;
71
72 I need to reflect on this a bit more before I send a pull request.  In
73 particular I imagine the strict/warnings stuff will make you puke.  Also, I'm
74 not sure whether I should name-grab 'alias' since [[todo/alias_directive]] is
75 an existing wishlist item.
76
77 Here's an example setup chunk:
78
79      pagespec_aliases:
80        image: "*.png or *.jpg or *.jpeg or *.gif or *.ico"
81        helper: "*.css or *.js"
82        boring: "image() or helper()"
83
84 The above demonstrates self-referential dynamic pagespec aliases.  It doesn't work,
85 however, to add ' or internal()' to `boring`, for some reason.
86
87 -- [[Jon]]
88
89 > another useful pagespec alias for large maps:
90
91        basewiki: "sandbox or templates or templates/* or ikiwiki or ikiwiki/* or shortcuts or recentchanges or wikiicons/*"
92
93 > -- [[Jon]]