]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/smiley.pm
* Fix dates in rss feeds if running localised, so they're still rfc 822.
[ikiwiki.git] / IkiWiki / Plugin / smiley.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::smiley;
3
4 use warnings;
5 use strict;
6 use IkiWiki;
7
8 my %smileys;
9 my $smiley_regexp;
10
11 sub import { #{{{
12         IkiWiki::hook(type => "checkconfig", id => "smiley", call => \&setup);
13 } # }}}
14
15 sub setup () { #{{{
16         my $list=IkiWiki::readfile(IkiWiki::srcfile("smileys.mdwn"));
17         while ($list =~ m/^\s*\*\s+\\([^\s]+)\s+\[\[([^]]+)\]\]/mg) {
18                 $smileys{$1}=$2;
19         }
20         
21         if (! %smileys) {
22                 IkiWiki::debug("failed to parse any smileys, disabling plugin");
23                 return;
24         }
25         
26         IkiWiki::hook(type => "filter", id => "smiley", call => \&filter);
27         # sort and reverse so that substrings come after longer strings
28         # that contain them, in most cases.
29         $smiley_regexp='('.join('|', map { quotemeta }
30                 reverse sort keys %smileys).')';
31         #IkiWiki::debug($smiley_regexp);
32 } #}}}
33
34 sub filter (@) { #{{{
35         my %params=@_;
36         
37         $params{content} =~ s{(?<=\s)(\\?)$smiley_regexp(?=\s)}{
38                 $1 ? $2 : IkiWiki::htmllink($params{page}, $params{page}, $smileys{$2}, 0, 0, $2)
39         }egs;
40         
41         return $params{content};
42 } # }}}
43
44 1