]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/mdwn.pm
e142fec4633272d67c7feb79778f08b465d2e2cc
[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 => "checkconfig", id => "mdwn", call => \&checkconfig);
11         hook(type => "getsetup", id => "mdwn", call => \&getsetup);
12         hook(type => "htmlize", id => "mdwn", call => \&htmlize, longname => "Markdown");
13         hook(type => "htmlize", id => "md", call => \&htmlize, longname => "Markdown (popular file extension)", nocreate => 1);
14 }
16 sub getsetup () {
17         return
18                 plugin => {
19                         safe => 1,
20                         rebuild => 1, # format plugin
21                         section => "format",
22                 },
23                 multimarkdown => {
24                         type => "boolean",
25                         example => 0,
26                         description => "enable multimarkdown features?",
27                         safe => 1,
28                         rebuild => 1,
29                 },
30                 nodiscount => {
31                         type => "boolean",
32                         example => 0,
33                         description => "disable use of markdown discount?",
34                         safe => 1,
35                         rebuild => 1,
36                 },
37                 mdwn_footnotes => {
38                         type => "boolean",
39                         example => 1,
40                         description => "enable footnotes in Markdown (where supported)?",
41                         safe => 1,
42                         rebuild => 1,
43                 },
44 }
46 sub checkconfig () {
47         $config{mdwn_footnotes} = 1 unless defined $config{mdwn_footnotes};
48 }
50 my $markdown_sub;
51 sub htmlize (@) {
52         my %params=@_;
53         my $content = $params{content};
55         if (! defined $markdown_sub) {
56                 # Markdown is forked and splintered upstream and can be
57                 # available in a variety of forms. Support them all.
58                 no warnings 'once';
59                 $blosxom::version="is a proper perl module too much to ask?";
60                 use warnings 'all';
62                 if (exists $config{multimarkdown} && $config{multimarkdown}) {
63                         eval q{use Text::MultiMarkdown};
64                         if ($@) {
65                                 debug(gettext("multimarkdown is enabled, but Text::MultiMarkdown is not installed"));
66                         }
67                         else {
68                                 $markdown_sub=sub {
69                                         my %flags=( use_metadata => 0 );
71                                         if ($config{mdwn_footnotes}) {
72                                                 $flags{disable_footnotes}=1;
73                                         }
75                                         Text::MultiMarkdown::markdown(shift, \%flags);
76                                 }
77                         }
78                 }
79                 if (! defined $markdown_sub &&
80                     (! exists $config{nodiscount} || ! $config{nodiscount})) {
81                         eval q{use Text::Markdown::Discount};
82                         if (! $@) {
83                                 $markdown_sub=sub {
84                                         my $t=shift;
86                                         # Workaround for discount binding bug
87                                         # https://rt.cpan.org/Ticket/Display.html?id=73657
88                                         return "" if $t=~/^\s*$/;
90                                         my $flags=0;
92                                         # Disable Pandoc-style % Title, % Author, % Date
93                                         # Use the meta plugin instead
94                                         $flags |= Text::Markdown::Discount::MKD_NOHEADER();
96                                         # Disable Unicodification of quote marks, em dashes...
97                                         # Use the typography plugin instead
98                                         $flags |= Text::Markdown::Discount::MKD_NOPANTS();
100                                         if ($config{mdwn_footnotes}) {
101                                                 $flags |= Text::Markdown::Discount::MKD_EXTRA_FOOTNOTE();
102                                         }
104                                         # Workaround for discount's eliding
105                                         # of <style> blocks.
106                                         # https://rt.cpan.org/Ticket/Display.html?id=74016
107                                         if (Text::Markdown::Discount->can("MKD_NOSTYLE")) {
108                                                 $flags |= Text::Markdown::Discount::MKD_NOSTYLE();
109                                         }
110                                         else {
111                                                 # This is correct for the libmarkdown.so.2 ABI
112                                                 $flags |= 0x00400000;
113                                         }
115                                         return Text::Markdown::Discount::markdown($t, $flags);
116                                 }
117                         }
118                 }
119                 if (! defined $markdown_sub) {
120                         eval q{use Text::Markdown};
121                         if (! $@) {
122                                 if (Text::Markdown->can('markdown')) {
123                                         $markdown_sub=\&Text::Markdown::markdown;
124                                 }
125                                 else {
126                                         $markdown_sub=\&Text::Markdown::Markdown;
127                                 }
128                         }
129                         else {
130                                 eval q{use Markdown};
131                                 if (! $@) {
132                                         $markdown_sub=\&Markdown::Markdown;
133                                 }
134                                 else {
135                                         my $error = $@;
136                                         do "/usr/bin/markdown" ||
137                                                 error(sprintf(gettext("failed to load Markdown.pm perl module (%s) or /usr/bin/markdown (%s)"), $error, $!));
138                                         $markdown_sub=\&Markdown::Markdown;
139                                 }
140                         }
141                 }
142                 
143                 require Encode;
144         }
145         
146         # Workaround for perl bug (#376329)
147         $content=Encode::encode_utf8($content);
148         eval {$content=&$markdown_sub($content)};
149         if ($@) {
150                 eval {$content=&$markdown_sub($content)};
151                 print STDERR $@ if $@;
152         }
153         $content=Encode::decode_utf8($content);
155         return $content;