summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/mdwn.pm
blob: 254ab51d03b078afbbeed1727fdb56b18a6c1a1c (plain)
  1. #!/usr/bin/perl
  2. # Markdown markup language
  3. package IkiWiki::Plugin::mdwn;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 2.00;
  7. sub import { #{{{
  8. hook(type => "htmlize", id => "mdwn", call => \&htmlize);
  9. } # }}}
  10. my $markdown_sub;
  11. sub htmlize (@) { #{{{
  12. my %params=@_;
  13. my $content = $params{content};
  14. if (! defined $markdown_sub) {
  15. # Markdown is forked and splintered upstream and can be
  16. # available in a variety of incompatible forms. Support
  17. # them all.
  18. no warnings 'once';
  19. $blosxom::version="is a proper perl module too much to ask?";
  20. use warnings 'all';
  21. eval q{use Markdown};
  22. if (! $@) {
  23. $markdown_sub=\&Markdown::Markdown;
  24. }
  25. else {
  26. eval q{use Text::Markdown};
  27. if (! $@) {
  28. $markdown_sub=\&Text::Markdown::Markdown;
  29. }
  30. else {
  31. do "/usr/bin/markdown" ||
  32. error(sprintf(gettext("failed to load Markdown.pm perl module (%s) or /usr/bin/markdown (%s)"), $@, $!));
  33. $markdown_sub=\&Markdown::Markdown;
  34. }
  35. }
  36. require Encode;
  37. }
  38. # Workaround for perl bug (#376329)
  39. $content=Encode::encode_utf8($content);
  40. eval {$content=&$markdown_sub($content)};
  41. if ($@) {
  42. eval {$content=&$markdown_sub($content)};
  43. print STDERR $@ if $@;
  44. }
  45. $content=Encode::decode_utf8($content);
  46. return $content;
  47. } # }}}
  48. 1