dc5d8f01a93efbb2b9693272c3145fefff391c29
[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,
31                         description => "path to an external markdown binary",
32                         safe => 0,
33                         rebuild => undef,
34                 },
35 }
36
37 sub checkconfig () {
38         if (! defined $config{markdown_path}) {
39                 $config{markdown_path}="/usr/bin/markdown";
40         }
41 }
42
43 my $markdown_sub;
44 my $tempdir;
45 sub htmlize (@) {
46         my %params=@_;
47         my $content = $params{content};
48
49         if (! defined $markdown_sub) {
50                 # Markdown is forked and splintered upstream and can be
51                 # available in a variety of forms. Support them all.
52                 no warnings 'once';
53                 $blosxom::version="is a proper perl module too much to ask?";
54                 use warnings 'all';
55         
56                 if ($config{markdown_path}) {
57                         eval q{use File::Temp};
58                         if ($@) {
59                                 debug(gettext("markdown_path is set, but File::Temp is not installed"));
60                         }
61                         else {
62                                 debug("Markdown: $config{markdown_path}");
63                                 $tempdir=File::Temp::tempdir( CLEANUP => 1 );
64                                 $markdown_sub=sub {
65                                         my $content=shift;
66                                         my $fh;
67                                         my $filename;
68                                         ($fh, $filename) = File::Temp::tempfile( DIR => $tempdir );
69                                         print $fh "$content\n";
70                                         close($fh);
71                                         $content = `$config{markdown_path} $filename`;
72                                         return $content;
73                                 }
74                         }
75                 } elsif (exists $config{multimarkdown} && $config{multimarkdown}) {
76                         eval q{use Text::MultiMarkdown};
77                         if ($@) {
78                                 debug(gettext("multimarkdown is enabled, but Text::MultiMarkdown is not installed"));
79                         }
80                         else {
81                                 debug("Markdown: Text::MultiMarkdown::markdown()");
82                                 $markdown_sub=sub {
83                                         Text::MultiMarkdown::markdown(shift, {use_metadata => 0});
84                                 }
85                         }
86                 }
87                 if (! defined $markdown_sub) {
88                         eval q{use Text::Markdown};
89                         if (! $@) {
90                                 if (Text::Markdown->can('markdown')) {
91                                         debug("Markdown: Text::Markdown::markdown()");
92                                         $markdown_sub=\&Text::Markdown::markdown;
93                                 }
94                                 else {
95                                         debug("Markdown: Text::Markdown::Markdown()");
96                                         $markdown_sub=\&Text::Markdown::Markdown;
97                                 }
98                         }
99                         else {
100                                 eval q{use Markdown};
101                                 if (! $@) {
102                                         debug("Markdown: Markdown::Markdown()");
103                                         $markdown_sub=\&Markdown::Markdown;
104                                 }
105                                 else {
106                                         error(sprintf(gettext("failed to load Markdown.pm perl module (%s) or $config{markdown_path} (%s)"), $@, $!));
107                                 }
108                         }
109                 }
110                 
111                 require Encode;
112         }
113         
114         # Workaround for perl bug (#376329)
115         $content=Encode::encode_utf8($content);
116         eval {$content=&$markdown_sub($content)};
117         if ($@) {
118                 eval {$content=&$markdown_sub($content)};
119                 print STDERR $@ if $@;
120         }
121         $content=Encode::decode_utf8($content);
122
123         return $content;
124 }
125
126 1