2 package IkiWiki::Plugin::smiley;
12 add_underlay("smiley");
13 hook(type => "sanitize", id => "smiley", call => \&sanitize);
16 sub build_regexp () { #{{{
17 my $list=readfile(srcfile("smileys.mdwn"));
18 while ($list =~ m/^\s*\*\s+\\\\([^\s]+)\s+\[\[([^]]+)\]\]/mg) {
22 $smileys{$smiley}=$file;
24 # Add a version with < and > escaped, since they probably
25 # will be (by markdown) by the time the sanitize hook runs.
28 $smileys{$smiley}=$file;
32 debug(gettext("failed to parse any smileys"));
37 # sort and reverse so that substrings come after longer strings
38 # that contain them, in most cases.
39 $smiley_regexp='('.join('|', map { quotemeta }
40 reverse sort keys %smileys).')';
41 #debug($smiley_regexp);
44 sub sanitize (@) { #{{{
47 build_regexp() unless defined $smiley_regexp;
50 return $_ unless length $smiley_regexp;
52 MATCH: while (m{(?:^|(?<=\s|>))(\\?)$smiley_regexp(?:(?=\s|<)|$)}g) {
58 # Smilies are not allowed inside <pre> or <code>.
59 # For each tag in turn, match forward to find the next <tag>
60 # or </tag> after the smiley.
62 foreach my $tag ("pre", "code") {
63 if (m/<(\/)?\s*$tag\s*>/isg && defined $1) {
64 # </tag> found first, so the smiley is
65 # inside the tag, so do not expand it.
68 # Reset pos back to where it was before this test.
74 substr($_, $epos, 1)="";
78 # Replace the smiley with its expanded value.
79 substr($_, $spos, length($smiley))=
80 htmllink($params{page}, $params{destpage},
81 $smileys{$smiley}, linktext => $smiley);
85 # Breaks out at end, otherwise it will scan through again,
86 # replacing de-escaped ones.
87 #last unless defined pos;