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