2 package IkiWiki::Plugin::shortcut;
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);
24 if (defined $config{srcdir} && length $config{srcdir}) {
25 # Preprocess the shortcuts page to get all the available shortcuts
26 # defined before other pages are rendered.
27 my $srcfile=srcfile("shortcuts.".$config{default_pageext}, 1);
28 if (! defined $srcfile) {
29 $srcfile=srcfile("shortcuts.mdwn", 1);
31 if (! defined $srcfile) {
32 print STDERR sprintf(gettext("shortcut plugin will not work without %s"),
33 "shortcuts.".$config{default_pageext})."\n";
36 IkiWiki::preprocess("shortcuts", "shortcuts", readfile($srcfile));
41 sub preprocess_shortcut (@) {
44 if (! defined $params{name} || ! defined $params{url}) {
45 error gettext("missing name or url parameter");
48 hook(type => "preprocess", no_override => 1, id => $params{name},
50 call => sub { shortcut_expand($params{url}, $params{desc}, @_) });
52 #translators: This is used to display what shortcuts are defined.
53 #translators: First parameter is the name of the shortcut, the second
54 #translators: is an URL.
55 return sprintf(gettext("shortcut %s points to <i>%s</i>"), $params{name}, $params{url});
58 sub shortcut_expand ($$@) {
63 # Get params in original order.
68 push @params, $key if ! length $value;
71 # If the shortcuts page changes, all pages that use shortcuts will
73 add_depends($params{destpage}, "shortcuts");
75 my $text=join(" ", @params);
76 my $encoded_text=$text;
77 $encoded_text=~s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
80 $1 eq 's' ? $encoded_text : $text
84 if (defined $params{desc}) {
94 return "<a href=\"$url\">$desc</a>";