summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/mdwn.pm
blob: 2ffe7bc3e6b8cc1656fcba3998cde0e81258fc55 (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. if (Text::Markdown->can('markdown')) {
  29. $markdown_sub=\&Text::Markdown::markdown;
  30. }
  31. else {
  32. $markdown_sub=\&Text::Markdown::Markdown;
  33. }
  34. }
  35. else {
  36. do "/usr/bin/markdown" ||
  37. error(sprintf(gettext("failed to load Markdown.pm perl module (%s) or /usr/bin/markdown (%s)"), $@, $!));
  38. $markdown_sub=\&Markdown::Markdown;
  39. }
  40. }
  41. require Encode;
  42. }
  43. # Workaround for perl bug (#376329)
  44. $content=Encode::encode_utf8($content);
  45. eval {$content=&$markdown_sub($content)};
  46. if ($@) {
  47. eval {$content=&$markdown_sub($content)};
  48. print STDERR $@ if $@;
  49. }
  50. $content=Encode::decode_utf8($content);
  51. return $content;
  52. } # }}}
  53. 1