]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/mdwn.pm
436f2461da9399cd7d80aaf09d42f06c2391f59a
[git.ikiwiki.info.git] / IkiWiki / Plugin / mdwn.pm
1 #!/usr/bin/perl
2 # Markdown markup language
3 package IkiWiki::Plugin::mdwn;
5 use warnings;
6 use strict;
7 use IkiWiki 3.00;
9 sub import {
10         hook(type => "getsetup", id => "mdwn", call => \&getsetup);
11         hook(type => "htmlize", id => "mdwn", call => \&htmlize, longname => "Markdown");
12         hook(type => "htmlize", id => "md", call => \&htmlize, longname => "Markdown (popular file extension)", nocreate => 1);
13 }
15 sub getsetup () {
16         return
17                 plugin => {
18                         safe => 1,
19                         rebuild => 1, # format plugin
20                         section => "format",
21                 },
22                 multimarkdown => {
23                         type => "boolean",
24                         example => 0,
25                         description => "enable multimarkdown features?",
26                         safe => 1,
27                         rebuild => 1,
28                 },
29                 nodiscount => {
30                         type => "boolean",
31                         example => 0,
32                         description => "disable use of markdown discount?",
33                         safe => 1,
34                         rebuild => 1,
35                 },
36 }
38 my $markdown_sub;
39 sub htmlize (@) {
40         my %params=@_;
41         my $content = $params{content};
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';
50                 if (exists $config{multimarkdown} && $config{multimarkdown}) {
51                         eval q{use Text::MultiMarkdown};
52                         if ($@) {
53                                 debug(gettext("multimarkdown is enabled, but Text::MultiMarkdown is not installed"));
54                         }
55                         else {
56                                 $markdown_sub=sub {
57                                         Text::MultiMarkdown::markdown(shift, {use_metadata => 0});
58                                 }
59                         }
60                 }
61                 if (! defined $markdown_sub &&
62                     (! exists $config{nodiscount} || ! $config{nodiscount})) {
63                         eval q{use Text::Markdown::Discount};
64                         if (! $@) {
65                                 $markdown_sub=sub {
66                                         my $t=shift;
68                                         # Workaround for discount binding bug
69                                         # https://rt.cpan.org/Ticket/Display.html?id=73657
70                                         return "" if $t=~/^\s*$/;
72                                         my $flags=0;
74                                         # Disable Pandoc-style % Title, % Author, % Date
75                                         # Use the meta plugin instead
76                                         $flags |= Text::Markdown::Discount::MKD_NOHEADER();
78                                         # Disable Unicodification of quote marks, em dashes...
79                                         # Use the typography plugin instead
80                                         $flags |= Text::Markdown::Discount::MKD_NOPANTS();
82                                         # Workaround for discount's eliding
83                                         # of <style> blocks.
84                                         # https://rt.cpan.org/Ticket/Display.html?id=74016
85                                         $t=~s/<style/<elyts/ig;
86                                         my $r=Text::Markdown::Discount::markdown($t, $flags);
87                                         $r=~s/<elyts/<style/ig;
88                                         return $r;
89                                 }
90                         }
91                 }
92                 if (! defined $markdown_sub) {
93                         eval q{use Text::Markdown};
94                         if (! $@) {
95                                 if (Text::Markdown->can('markdown')) {
96                                         $markdown_sub=\&Text::Markdown::markdown;
97                                 }
98                                 else {
99                                         $markdown_sub=\&Text::Markdown::Markdown;
100                                 }
101                         }
102                         else {
103                                 eval q{use Markdown};
104                                 if (! $@) {
105                                         $markdown_sub=\&Markdown::Markdown;
106                                 }
107                                 else {
108                                         my $error = $@;
109                                         do "/usr/bin/markdown" ||
110                                                 error(sprintf(gettext("failed to load Markdown.pm perl module (%s) or /usr/bin/markdown (%s)"), $error, $!));
111                                         $markdown_sub=\&Markdown::Markdown;
112                                 }
113                         }
114                 }
115                 
116                 require Encode;
117         }
118         
119         # Workaround for perl bug (#376329)
120         $content=Encode::encode_utf8($content);
121         eval {$content=&$markdown_sub($content)};
122         if ($@) {
123                 eval {$content=&$markdown_sub($content)};
124                 print STDERR $@ if $@;
125         }
126         $content=Encode::decode_utf8($content);
128         return $content;