summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/smiley.pm
blob: 5f05e3a4c2234aa053f6162f2c7eaf1028ede88b (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. IkiWiki::hook(type => "checkconfig", id => "smiley", call => \&setup);
  10. } # }}}
  11. sub setup () { #{{{
  12. my $list=IkiWiki::readfile(IkiWiki::srcfile("smileys.mdwn"));
  13. while ($list =~ m/^\s*\*\s+\\([^\s]+)\s+\[\[([^]]+)\]\]/mg) {
  14. $smileys{$1}=$2;
  15. }
  16. if (! %smileys) {
  17. IkiWiki::debug("failed to parse any smileys, disabling plugin");
  18. return;
  19. }
  20. IkiWiki::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. #IkiWiki::debug($smiley_regexp);
  26. } #}}}
  27. sub filter (@) { #{{{
  28. my %params=@_;
  29. $params{content} =~ s{(?<=\s)(\\?)$smiley_regexp(?=\s)}{
  30. $1 ? $2 : IkiWiki::htmllink($params{page}, $smileys{$2}, 0, 0, $2)
  31. }egs;
  32. return $params{content};
  33. } # }}}
  34. 1