Simplify mdwn.pm Markdown::Markdown error message.
[ikiwiki.git] / 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 3.00;
8
9 sub import {
10         hook(type => "getsetup", id => "mdwn", call => \&getsetup);
11         hook(type => "htmlize", id => "mdwn", call => \&htmlize, longname => "Markdown");
12 }
13
14 sub getsetup () {
15         return
16                 plugin => {
17                         safe => 1,
18                         rebuild => 1, # format plugin
19                         section => "format",
20                 },
21                 multimarkdown => {
22                         type => "boolean",
23                         example => 0,
24                         description => "enable multimarkdown features?",
25                         safe => 1,
26                         rebuild => 1,
27                 },
28                 markdown_path => {
29                         type => "string",
30                         example => 0, # /usr/bin/markdown
31                         description => "path to an external markdown binary",
32                         safe => 0,
33                         rebuild => undef,
34                 },
35 }
36
37 my $markdown_sub;
38 my $tempdir;
39 sub htmlize (@) {
40         my %params=@_;
41         my $content = $params{content};
42
43         if (! defined $markdown_sub) {
44                 # Markdown is forked and splintered upstream and can be
45                 # available in a variety of forms. Support them all.
46                 no warnings 'once';
47                 $blosxom::version="is a proper perl module too much to ask?";
48                 use warnings 'all';
49         
50                 if ($config{markdown_path}) {
51                         eval q{use File::Temp};
52                         if ($@) {
53                                 debug(gettext("markdown_path is set, but File::Temp is not installed"));
54                         }
55                         else {
56                                 debug("Markdown: $config{markdown_path}");
57                                 $tempdir=File::Temp::tempdir( CLEANUP => 1 );
58                                 $markdown_sub=sub {
59                                         my $content=shift;
60                                         my $fh;
61                                         my $filename;
62                                         ($fh, $filename) = File::Temp::tempfile( DIR => $tempdir );
63                                         print $fh "$content\n";
64                                         close($fh);
65                                         $content = `$config{markdown_path} $filename`;
66                                         return $content;
67                                 }
68                         }
69                 } elsif (exists $config{multimarkdown} && $config{multimarkdown}) {
70                         eval q{use Text::MultiMarkdown};
71                         if ($@) {
72                                 debug(gettext("multimarkdown is enabled, but Text::MultiMarkdown is not installed"));
73                         }
74                         else {
75                                 debug("Markdown: Text::MultiMarkdown::markdown()");
76                                 $markdown_sub=sub {
77                                         Text::MultiMarkdown::markdown(shift, {use_metadata => 0});
78                                 }
79                         }
80                 }
81                 if (! defined $markdown_sub) {
82                         eval q{use Text::Markdown};
83                         if (! $@) {
84                                 if (Text::Markdown->can('markdown')) {
85                                         debug("Markdown: Text::Markdown::markdown()");
86                                         $markdown_sub=\&Text::Markdown::markdown;
87                                 }
88                                 else {
89                                         debug("Markdown: Text::Markdown::Markdown()");
90                                         $markdown_sub=\&Text::Markdown::Markdown;
91                                 }
92                         }
93                         else {
94                                 eval q{use Markdown};
95                                 if (! $@) {
96                                         debug("Markdown: Markdown::Markdown()");
97                                         $markdown_sub=\&Markdown::Markdown;
98                                 }
99                                 else {
100                                         error("failed to load Markdown.pm perl module or $config{markdown_path}");
101                                 }
102                         }
103                 }
104                 
105                 require Encode;
106         }
107         
108         # Workaround for perl bug (#376329)
109         $content=Encode::encode_utf8($content);
110         eval {$content=&$markdown_sub($content)};
111         if ($@) {
112                 eval {$content=&$markdown_sub($content)};
113                 print STDERR $@ if $@;
114         }
115         $content=Encode::decode_utf8($content);
116
117         return $content;
118 }
119
120 1