* Finally apply the index.html patch, with thanks to everyone who worked
[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;
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 incompatible forms. Support
21                 # them all.
22                 no warnings 'once';
23                 $blosxom::version="is a proper perl module too much to ask?";
24                 use warnings 'all';
25
26                 eval q{use Markdown};
27                 if (! $@) {
28                         $markdown_sub=\&Markdown::Markdown;
29                 }
30                 else {
31                         eval q{use Text::Markdown};
32                         if (! $@) {
33                                 $markdown_sub=\&Text::Markdown::Markdown;
34                         }
35                         else {
36                                 do "/usr/bin/markdown" ||
37                                         error(sprintf(gettext("failed to load Markdown.pm perl module (%s) or /usr/bin/markdown (%s)"), $@, $!));
38                                 $markdown_sub=\&Markdown::Markdown;
39                         }
40                 }
41                 require Encode;
42         }
43         
44         # Workaround for perl bug (#376329)
45         $content=Encode::encode_utf8($content);
46         $content=Encode::encode_utf8($content);
47         $content=&$markdown_sub($content);
48         $content=Encode::decode_utf8($content);
49         $content=Encode::decode_utf8($content);
50
51         return $content;
52 } # }}}
53
54 1