summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/mdwn.pm
blob: 920d90e5e5eb8f81abf8560f402cd8d849e4c72b (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 => "getsetup", id => "mdwn", call => \&getsetup);
  9. hook(type => "htmlize", id => "mdwn", call => \&htmlize);
  10. } # }}}
  11. sub getsetup () { #{{{
  12. return
  13. multimarkdown => {
  14. type => "boolean",
  15. default => 0,
  16. description => "enable use of multimarkdown features",
  17. safe => 1,
  18. rebuild => 1,
  19. },
  20. } #}}}
  21. my $markdown_sub;
  22. sub htmlize (@) { #{{{
  23. my %params=@_;
  24. my $content = $params{content};
  25. if (! defined $markdown_sub) {
  26. # Markdown is forked and splintered upstream and can be
  27. # available in a variety of forms. Support them all.
  28. no warnings 'once';
  29. $blosxom::version="is a proper perl module too much to ask?";
  30. use warnings 'all';
  31. if (exists $config{multimarkdown} && $config{multimarkdown}) {
  32. eval q{use Text::MultiMarkdown};
  33. if ($@) {
  34. debug(gettext("multimarkdown is enabled, but Text::MultiMarkdown is not installed"));
  35. }
  36. $markdown_sub=sub {
  37. Text::MultiMarkdown::markdown(shift, {use_metadata => 0});
  38. }
  39. }
  40. if (! defined $markdown_sub) {
  41. eval q{use Text::Markdown};
  42. if (! $@) {
  43. if (Text::Markdown->can('markdown')) {
  44. $markdown_sub=\&Text::Markdown::markdown;
  45. }
  46. else {
  47. $markdown_sub=\&Text::Markdown::Markdown;
  48. }
  49. }
  50. else {
  51. eval q{use Markdown};
  52. if (! $@) {
  53. $markdown_sub=\&Markdown::Markdown;
  54. }
  55. else {
  56. do "/usr/bin/markdown" ||
  57. error(sprintf(gettext("failed to load Markdown.pm perl module (%s) or /usr/bin/markdown (%s)"), $@, $!));
  58. $markdown_sub=\&Markdown::Markdown;
  59. }
  60. }
  61. }
  62. require Encode;
  63. }
  64. # Workaround for perl bug (#376329)
  65. $content=Encode::encode_utf8($content);
  66. eval {$content=&$markdown_sub($content)};
  67. if ($@) {
  68. eval {$content=&$markdown_sub($content)};
  69. print STDERR $@ if $@;
  70. }
  71. $content=Encode::decode_utf8($content);
  72. return $content;
  73. } # }}}
  74. 1