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