]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/shortcut.pm
* Avoid creating edit links when not in cgi mode.
[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;
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 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         return "shortcut $params{name} points to $params{url}";
31 } # }}}
32
33 sub shortcut_expand ($$@) { #{{{
34         my $url=shift;
35         my $desc=shift;
36         my %params=@_;
37
38         # Get params in original order.
39         my @params;
40         while (@_) {
41                 my $key=shift;
42                 my $value=shift;
43                 push @params, $key if ! length $value;
44         }
45
46         # If the shortcuts page changes, all pages that use shortcuts will
47         # need to be updated.
48         add_depends($params{destpage}, "shortcuts");
49
50         my $text=join(" ", @params);
51         my $encoded_text=$text;
52         $encoded_text=~s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
53         
54         if (defined $desc) {
55                 $desc=~s/\%s/$text/g;
56         }
57         else {
58                 $desc=$text;
59         }
60
61         $url=~s/\%s/$encoded_text/g;
62         return "<a href=\"$url\">$desc</a>";
63 } #}}}
64
65 1