]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/shortcut.pm
reorg all the pages about rcs backends. Fix all links
[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         IkiWiki::preprocess("shortcuts", "shortcuts",
17                 readfile(srcfile("shortcuts.mdwn")));
18 } # }}}
19
20 sub preprocess_shortcut (@) { #{{{
21         my %params=@_;
22
23         if (! defined $params{name} || ! defined $params{url}) {
24                 return "[[shortcut ".gettext("missing name or url parameter")."]]";
25         }
26
27         hook(type => "preprocess", no_override => 1, id => $params{name},
28                 call => sub { shortcut_expand($params{url}, $params{desc}, @_) });
29
30         #translators: This is used to display what shortcuts are defined.
31         #translators: First parameter is the name of the shortcut, the second
32         #translators: is an URL.
33         return sprintf(gettext("shortcut %s points to <i>%s</i>"), $params{name}, $params{url});
34 } # }}}
35
36 sub shortcut_expand ($$@) { #{{{
37         my $url=shift;
38         my $desc=shift;
39         my %params=@_;
40
41         # Get params in original order.
42         my @params;
43         while (@_) {
44                 my $key=shift;
45                 my $value=shift;
46                 push @params, $key if ! length $value;
47         }
48
49         # If the shortcuts page changes, all pages that use shortcuts will
50         # need to be updated.
51         add_depends($params{destpage}, "shortcuts");
52
53         my $text=join(" ", @params);
54         my $encoded_text=$text;
55         $encoded_text=~s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
56
57         $text=~s/_/ /g;
58         if (defined $desc) {
59                 $desc=~s/\%s/$text/g;
60         }
61         else {
62                 $desc=$text;
63         }
64
65         $url=~s/\%s/$encoded_text/g;
66         return "<a href=\"$url\">$desc</a>";
67 } #}}}
68
69 1