smiley: Detect smileys inside pre and tags, and do not expand.
[ikiwiki] / IkiWiki / Plugin / smiley.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::smiley;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 my %smileys;
9 my $smiley_regexp;
10
11 sub import { #{{{
12         add_underlay("smiley");
13         hook(type => "filter", id => "smiley", call => \&filter);
14 } # }}}
15
16 sub build_regexp () { #{{{
17         my $list=readfile(srcfile("smileys.mdwn"));
18         while ($list =~ m/^\s*\*\s+\\([^\s]+)\s+\[\[([^]]+)\]\]/mg) {
19                 $smileys{$1}=$2;
20         }
21         
22         if (! %smileys) {
23                 debug(gettext("failed to parse any smileys"));
24                 $smiley_regexp='';
25                 return;
26         }
27         
28         # sort and reverse so that substrings come after longer strings
29         # that contain them, in most cases.
30         $smiley_regexp='('.join('|', map { quotemeta }
31                 reverse sort keys %smileys).')';
32         #debug($smiley_regexp);
33 } #}}}
34
35 sub filter (@) { #{{{
36         my %params=@_;
37
38         build_regexp() unless defined $smiley_regexp;
39         
40         $_=$params{content};
41         return $_ unless length $smiley_regexp;
42         
43 MATCH:  while (m{(?:^|(?<=\s))(\\?)$smiley_regexp(?:(?=\s)|$)}g) {
44                 # Smilies are not allowed inside <pre> or <code>.
45                 # For each tag in turn, match forward to find <tag> or
46                 # </tag>. If it's </tag>, then the smiley is inside the
47                 # tag, and is not expanded. If it's <tag>, the smiley is
48                 # outside the block.
49                 my $pos=pos;
50                 foreach my $tag ("pre", "code") {
51                         if (m/.*?<(\/)?\s*$tag\s*>/isg) {
52                                 if (defined $1) {
53                                         # Inside tag, so do nothing.
54                                         # (Smiley hunting will continue after
55                                         # the tag.)
56                                         next MATCH;
57                                 }
58                                 else {
59                                         # Reset pos back to where it was before
60                                         # this test.
61                                         pos=$pos;
62                                 }
63                         }
64                 }
65
66                 if ($1) {
67                         # Remove escape.
68                         substr($_, $-[1], 1)="";
69                 }
70                 else {
71                         # Replace the smiley with its expanded value.
72                         substr($_, $-[2], length($2))=
73                                 htmllink($params{page}, $params{destpage}, $smileys{$2}, linktext => $2);
74                 }
75         }
76
77         return $_;
78 } # }}}
79
80 1