summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/mdwn.pm
blob: 3de59ef3d036d56f7fa4876f8a1020ad59d994aa (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. else {
  41. $markdown_sub=sub {
  42. Text::MultiMarkdown::markdown(shift, {use_metadata => 0});
  43. }
  44. }
  45. }
  46. if (! defined $markdown_sub) {
  47. eval q{use Text::Markdown};
  48. if (! $@) {
  49. if (Text::Markdown->can('markdown')) {
  50. $markdown_sub=\&Text::Markdown::markdown;
  51. }
  52. else {
  53. $markdown_sub=\&Text::Markdown::Markdown;
  54. }
  55. }
  56. else {
  57. eval q{use Markdown};
  58. if (! $@) {
  59. $markdown_sub=\&Markdown::Markdown;
  60. }
  61. else {
  62. do "/usr/bin/markdown" ||
  63. error(sprintf(gettext("failed to load Markdown.pm perl module (%s) or /usr/bin/markdown (%s)"), $@, $!));
  64. $markdown_sub=\&Markdown::Markdown;
  65. }
  66. }
  67. }
  68. require Encode;
  69. }
  70. # Workaround for perl bug (#376329)
  71. $content=Encode::encode_utf8($content);
  72. eval {$content=&$markdown_sub($content)};
  73. if ($@) {
  74. eval {$content=&$markdown_sub($content)};
  75. print STDERR $@ if $@;
  76. }
  77. $content=Encode::decode_utf8($content);
  78. return $content;
  79. }
  80. 1