2 package IkiWiki::Plugin::smiley;
12 add_underlay("smiley");
13 hook(type => "filter", id => "smiley", call => \&filter);
16 sub build_regexp () { #{{{
17 my $list=readfile(srcfile("smileys.mdwn"));
18 while ($list =~ m/^\s*\*\s+\\([^\s]+)\s+\[\[([^]]+)\]\]/mg) {
23 debug(gettext("failed to parse any smileys"));
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);
38 build_regexp() unless defined $smiley_regexp;
41 return $_ unless length $smiley_regexp;
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
50 foreach my $tag ("pre", "code") {
51 if (m/.*?<(\/)?\s*$tag\s*>/isg) {
53 # Inside tag, so do nothing.
54 # (Smiley hunting will continue after
59 # Reset pos back to where it was before
68 substr($_, $-[1], 1)="";
71 # Replace the smiley with its expanded value.
72 substr($_, $-[2], length($2))=
73 htmllink($params{page}, $params{destpage}, $smileys{$2}, linktext => $2);