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