]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/shortcut.pm
Corrected error: month pages were created even without calendar_autocreate config...
[git.ikiwiki.info.git] / IkiWiki / Plugin / shortcut.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::shortcut;
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
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 }
14 sub getsetup () {
15         return
16                 plugin => {
17                         safe => 1,
18                         rebuild => undef,
19                         section => "widget",
20                 },
21 }
23 sub checkconfig () {
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);
30                 }
31                 if (! defined $srcfile) {
32                         print STDERR sprintf(gettext("shortcut plugin will not work without %s"),
33                                 "shortcuts.".$config{default_pageext})."\n";
34                 }
35                 else {
36                         IkiWiki::preprocess("shortcuts", "shortcuts", readfile($srcfile));
37                 }
38         }
39 }
41 sub preprocess_shortcut (@) {
42         my %params=@_;
44         if (! defined $params{name} || ! defined $params{url}) {
45                 error gettext("missing name or url parameter");
46         }
48         hook(type => "preprocess", no_override => 1, id => $params{name},
49                 shortcut => 1,
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});
56 }
58 sub shortcut_expand ($$@) {
59         my $url=shift;
60         my $desc=shift;
61         my %params=@_;
63         # Get params in original order.
64         my @params;
65         while (@_) {
66                 my $key=shift;
67                 my $value=shift;
68                 push @params, $key if ! length $value;
69         }
71         # If the shortcuts page changes, all pages that use shortcuts will
72         # need to be updated.
73         add_depends($params{destpage}, "shortcuts");
75         my $text=join(" ", @params);
76         
77         $url=~s{\%([sSW])}{
78                 if ($1 eq 's') {
79                         my $t=$text;
80                         $t=~s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
81                         $t;
82                 }
83                 elsif ($1 eq 'S') {
84                         $text;
85                 }
86                 elsif ($1 eq 'W') {
87                         my $t=Encode::encode_utf8($text);
88                         $t=~s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
89                         $t;
90                 }
91         }eg;
93         $text=~s/_/ /g;
94         if (defined $params{desc}) {
95                 $desc=$params{desc};
96         }
97         if (defined $desc) {
98                 $desc=~s/\%s/$text/g;
99         }
100         else {
101                 $desc=$text;
102         }
104         return "<a href=\"$url\">$desc</a>";