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