estseek patch (slightly altered) and other replies
[ikiwiki] / 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         hook(type => "checkconfig", id => "smiley", call => \&setup);
13 } # }}}
14
15 sub setup () { #{{{
16         my $list=readfile(srcfile("smileys.mdwn"));
17         while ($list =~ m/^\s*\*\s+\\([^\s]+)\s+\[\[([^]]+)\]\]/mg) {
18                 $smileys{$1}=$2;
19         }
20         
21         if (! %smileys) {
22                 debug("failed to parse any smileys, disabling plugin");
23                 return;
24         }
25         
26         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         #debug($smiley_regexp);
32 } #}}}
33
34 sub filter (@) { #{{{
35         my %params=@_;
36         
37         $params{content} =~ s{(?<=\s)(\\?)$smiley_regexp(?=\s)}{
38                 $1 ? $2 : htmllink($params{page}, $params{page}, $smileys{$2}, 0, 0, $2)
39         }egs;
40         
41         return $params{content};
42 } # }}}
43
44 1