]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/shortcut.pm
meta plugin: also collect [[!meta uuid="..."]]
[ikiwiki.git] / IkiWiki / Plugin / shortcut.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::shortcut;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 sub import { #{{{
9         hook(type => "checkconfig", id => "shortcut", call => \&checkconfig);
10         hook(type => "preprocess", id => "shortcut", call => \&preprocess_shortcut);
11 } #}}}
12
13 sub checkconfig () { #{{{
14         # Preprocess the shortcuts page to get all the available shortcuts
15         # defined before other pages are rendered.
16         my $srcfile=srcfile("shortcuts.mdwn", 1);
17         if (! defined $srcfile) {
18                 error(gettext("shortcut plugin will not work without a shortcuts.mdwn"));
19         }
20         IkiWiki::preprocess("shortcuts", "shortcuts", readfile($srcfile));
21 } # }}}
22
23 sub preprocess_shortcut (@) { #{{{
24         my %params=@_;
25
26         if (! defined $params{name} || ! defined $params{url}) {
27                 return "[[shortcut ".gettext("missing name or url parameter")."]]";
28         }
29
30         hook(type => "preprocess", no_override => 1, id => $params{name},
31                 call => sub { shortcut_expand($params{url}, $params{desc}, @_) });
32
33         #translators: This is used to display what shortcuts are defined.
34         #translators: First parameter is the name of the shortcut, the second
35         #translators: is an URL.
36         return sprintf(gettext("shortcut %s points to <i>%s</i>"), $params{name}, $params{url});
37 } # }}}
38
39 sub shortcut_expand ($$@) { #{{{
40         my $url=shift;
41         my $desc=shift;
42         my %params=@_;
43
44         # Get params in original order.
45         my @params;
46         while (@_) {
47                 my $key=shift;
48                 my $value=shift;
49                 push @params, $key if ! length $value;
50         }
51
52         # If the shortcuts page changes, all pages that use shortcuts will
53         # need to be updated.
54         add_depends($params{destpage}, "shortcuts");
55
56         my $text=join(" ", @params);
57         my $encoded_text=$text;
58         $encoded_text=~s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
59         
60         $url=~s{\%([sS])}{
61                 $1 eq 's' ? $encoded_text : $text
62         }eg;
63
64         $text=~s/_/ /g;
65         if (defined $params{desc}) {
66                 $desc=$params{desc};
67         }
68         if (defined $desc) {
69                 $desc=~s/\%s/$text/g;
70         }
71         else {
72                 $desc=$text;
73         }
74
75         return "<a href=\"$url\">$desc</a>";
76 } #}}}
77
78 1