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