add advanced and basic modes
[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 => "getsetup", id => "mdwn", call => \&getsetup);
11         hook(type => "htmlize", id => "mdwn", call => \&htmlize);
12 } # }}}
13
14 sub getsetup () { #{{{
15         return
16                 multimarkdown => {
17                         type => "boolean",
18                         example => 0,
19                         description => "enable multimarkdown features?",
20                         safe => 1,
21                         rebuild => 1,
22                 },
23 } #}}}
24
25 my $markdown_sub;
26 sub htmlize (@) { #{{{
27         my %params=@_;
28         my $content = $params{content};
29
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
37                 if (exists $config{multimarkdown} && $config{multimarkdown}) {
38                         eval q{use Text::MultiMarkdown};
39                         if ($@) {
40                                 debug(gettext("multimarkdown is enabled, but Text::MultiMarkdown is not installed"));
41                         }
42                         $markdown_sub=sub {
43                                 Text::MultiMarkdown::markdown(shift, {use_metadata => 0});
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                 
69                 require Encode;
70         }
71         
72         # Workaround for perl bug (#376329)
73         $content=Encode::encode_utf8($content);
74         eval {$content=&$markdown_sub($content)};
75         if ($@) {
76                 eval {$content=&$markdown_sub($content)};
77                 print STDERR $@ if $@;
78         }
79         $content=Encode::decode_utf8($content);
80
81         return $content;
82 } # }}}
83
84 1