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