summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/smiley.pm
blob: 85a719337518fc3443b501318ce54ec3847af63b (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::smiley;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. my %smileys;
  7. my $smiley_regexp;
  8. sub import { #{{{
  9. hook(type => "checkconfig", id => "smiley", call => \&setup);
  10. } # }}}
  11. sub setup () { #{{{
  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, disabling plugin"));
  18. return;
  19. }
  20. hook(type => "filter", id => "smiley", call => \&filter);
  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. $params{content} =~ s{(?<=\s)(\\?)$smiley_regexp(?=\s)}{
  30. $1 ? $2 : htmllink($params{page}, $params{page}, $smileys{$2}, 0, 0, $2)
  31. }egs;
  32. return $params{content};
  33. } # }}}
  34. 1