summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/mdwn.pm
blob: 11f3f0137627643fe285e0464b59b2e0086b3f88 (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 forms. Support them all.
  17. no warnings 'once';
  18. $blosxom::version="is a proper perl module too much to ask?";
  19. use warnings 'all';
  20. if (exists $config{multimarkdown} && $config{multimarkdown}) {
  21. eval q{use Text::MultiMarkdown};
  22. if ($@) {
  23. error(gettext("multimarkdown is enabled, but Text::MultiMarkdown is not installed"));
  24. }
  25. $markdown_sub=sub {
  26. Text::MultiMarkdown::markdown(shift, {use_metadata => 0});
  27. }
  28. }
  29. else {
  30. eval q{use Text::Markdown};
  31. if (! $@) {
  32. if (Text::Markdown->can('markdown')) {
  33. $markdown_sub=\&Text::Markdown::markdown;
  34. }
  35. else {
  36. $markdown_sub=\&Text::Markdown::Markdown;
  37. }
  38. }
  39. else {
  40. eval q{use Markdown};
  41. if (! $@) {
  42. $markdown_sub=\&Markdown::Markdown;
  43. }
  44. else {
  45. do "/usr/bin/markdown" ||
  46. error(sprintf(gettext("failed to load Markdown.pm perl module (%s) or /usr/bin/markdown (%s)"), $@, $!));
  47. $markdown_sub=\&Markdown::Markdown;
  48. }
  49. }
  50. }
  51. require Encode;
  52. }
  53. # Workaround for perl bug (#376329)
  54. $content=Encode::encode_utf8($content);
  55. eval {$content=&$markdown_sub($content)};
  56. if ($@) {
  57. eval {$content=&$markdown_sub($content)};
  58. print STDERR $@ if $@;
  59. }
  60. $content=Encode::decode_utf8($content);
  61. return $content;
  62. } # }}}
  63. 1