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