summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/mdwn.pm
blob: 1520b3eecc66d96eb7b0da44a441b01bc3fc5ad1 (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. my $oneline = $content !~ /\n/;
  39. # Workaround for perl bug (#376329)
  40. $content=Encode::encode_utf8($content);
  41. eval {$content=&$markdown_sub($content)};
  42. if ($@) {
  43. eval {$content=&$markdown_sub($content)};
  44. print STDERR $@ if $@;
  45. }
  46. $content=Encode::decode_utf8($content);
  47. if ($oneline) {
  48. # hack to get rid of enclosing junk added by markdown
  49. $content=~s!^<p>!!;
  50. $content=~s!</p>$!!;
  51. chomp $content;
  52. }
  53. return $content;
  54. } # }}}
  55. 1