]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/mdwn.pm
doc update, add --exclude ikiwiki.cgi to examples
[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                         $markdown_sub=sub {
47                                 Text::MultiMarkdown::markdown(shift, {use_metadata => 0});
48                         }
49                 }
50                 if (! defined $markdown_sub) {
51                         eval q{use Text::Markdown};
52                         if (! $@) {
53                                 if (Text::Markdown->can('markdown')) {
54                                         $markdown_sub=\&Text::Markdown::markdown;
55                                 }
56                                 else {
57                                         $markdown_sub=\&Text::Markdown::Markdown;
58                                 }
59                         }
60                         else {
61                                 eval q{use Markdown};
62                                 if (! $@) {
63                                         $markdown_sub=\&Markdown::Markdown;
64                                 }
65                                 else {
66                                         do "/usr/bin/markdown" ||
67                                                 error(sprintf(gettext("failed to load Markdown.pm perl module (%s) or /usr/bin/markdown (%s)"), $@, $!));
68                                         $markdown_sub=\&Markdown::Markdown;
69                                 }
70                         }
71                 }
72                 
73                 require Encode;
74         }
75         
76         # Workaround for perl bug (#376329)
77         $content=Encode::encode_utf8($content);
78         eval {$content=&$markdown_sub($content)};
79         if ($@) {
80                 eval {$content=&$markdown_sub($content)};
81                 print STDERR $@ if $@;
82         }
83         $content=Encode::decode_utf8($content);
84
85         return $content;
86 }
87
88 1