summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/smiley.pm
blob: 1a9833e6eed899ae2ed3a77d337f2e06ea789c29 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::smiley;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  6. my %smileys;
  7. my $smiley_regexp;
  8. sub import { #{{{
  9. hook(type => "filter", id => "smiley", call => \&filter);
  10. } # }}}
  11. sub build_regexp () { #{{{
  12. my $list=readfile(srcfile("smileys.mdwn"));
  13. while ($list =~ m/^\s*\*\s+\\([^\s]+)\s+\[\[([^]]+)\]\]/mg) {
  14. $smileys{$1}=$2;
  15. }
  16. if (! %smileys) {
  17. debug(gettext("failed to parse any smileys"));
  18. $smiley_regexp='';
  19. return;
  20. }
  21. # sort and reverse so that substrings come after longer strings
  22. # that contain them, in most cases.
  23. $smiley_regexp='('.join('|', map { quotemeta }
  24. reverse sort keys %smileys).')';
  25. #debug($smiley_regexp);
  26. } #}}}
  27. sub filter (@) { #{{{
  28. my %params=@_;
  29. build_regexp() unless defined $smiley_regexp;
  30. $params{content} =~ s{(?:^|(?<=\s))(\\?)$smiley_regexp(?:(?=\s)|$)}{
  31. $1 ? $2 : htmllink($params{page}, $params{page}, $smileys{$2}, linktext => $2)
  32. }egs if length $smiley_regexp;
  33. return $params{content};
  34. } # }}}
  35. 1