]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/mdwn.pm
clarify error
[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                 },
20                 multimarkdown => {
21                         type => "boolean",
22                         example => 0,
23                         description => "enable multimarkdown features?",
24                         safe => 1,
25                         rebuild => 1,
26                 },
27 }
28
29 my $markdown_sub;
30 sub htmlize (@) {
31         my %params=@_;
32         my $content = $params{content};
33
34         if (! defined $markdown_sub) {
35                 # Markdown is forked and splintered upstream and can be
36                 # available in a variety of forms. Support them all.
37                 no warnings 'once';
38                 $blosxom::version="is a proper perl module too much to ask?";
39                 use warnings 'all';
40
41                 if (exists $config{multimarkdown} && $config{multimarkdown}) {
42                         eval q{use Text::MultiMarkdown};
43                         if ($@) {
44                                 debug(gettext("multimarkdown is enabled, but Text::MultiMarkdown is not installed"));
45                         }
46                         else {
47                                 $markdown_sub=sub {
48                                         Text::MultiMarkdown::markdown(shift, {use_metadata => 0});
49                                 }
50                         }
51                 }
52                 if (! defined $markdown_sub) {
53                         eval q{use Text::Markdown};
54                         if (! $@) {
55                                 if (Text::Markdown->can('markdown')) {
56                                         $markdown_sub=\&Text::Markdown::markdown;
57                                 }
58                                 else {
59                                         $markdown_sub=\&Text::Markdown::Markdown;
60                                 }
61                         }
62                         else {
63                                 eval q{use Markdown};
64                                 if (! $@) {
65                                         $markdown_sub=\&Markdown::Markdown;
66                                 }
67                                 else {
68                                         do "/usr/bin/markdown" ||
69                                                 error(sprintf(gettext("failed to load Markdown.pm perl module (%s) or /usr/bin/markdown (%s)"), $@, $!));
70                                         $markdown_sub=\&Markdown::Markdown;
71                                 }
72                         }
73                 }
74                 
75                 require Encode;
76         }
77         
78         # Workaround for perl bug (#376329)
79         $content=Encode::encode_utf8($content);
80         eval {$content=&$markdown_sub($content)};
81         if ($@) {
82                 eval {$content=&$markdown_sub($content)};
83                 print STDERR $@ if $@;
84         }
85         $content=Encode::decode_utf8($content);
86
87         return $content;
88 }
89
90 1