]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/mdwn.pm
hack to support media:content
[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                 mdwn_alpha_lists => {
45                         type => "boolean",
46                         example => 0,
47                         description => "interpret line like 'A. First item' as ordered list when using Discount?",
48                         advanced => 1,
49                         safe => 1,
50                         rebuild => 1,
51                 },
52 }
54 sub checkconfig () {
55         $config{mdwn_footnotes} = 1 unless defined $config{mdwn_footnotes};
56         $config{mdwn_alpha_lists} = 0 unless defined $config{mdwn_alpha_lists};
57 }
59 our $markdown_sub;
60 sub htmlize (@) {
61         my %params=@_;
62         my $content = $params{content};
64         if (! defined $markdown_sub) {
65                 # Markdown is forked and splintered upstream and can be
66                 # available in a variety of forms. Support them all.
67                 no warnings 'once';
68                 $blosxom::version="is a proper perl module too much to ask?";
69                 use warnings 'all';
71                 if (exists $config{multimarkdown} && $config{multimarkdown}) {
72                         eval q{use Text::MultiMarkdown};
73                         if ($@) {
74                                 debug(gettext("multimarkdown is enabled, but Text::MultiMarkdown is not installed"));
75                         }
76                         else {
77                                 $markdown_sub=sub {
78                                         my %flags=( use_metadata => 0 );
80                                         $flags{disable_footnotes}=not $config{mdwn_footnotes};
82                                         Text::MultiMarkdown::markdown(shift, \%flags);
83                                 }
84                         }
85                 }
86                 if (! defined $markdown_sub &&
87                     (! exists $config{nodiscount} || ! $config{nodiscount})) {
88                         eval q{use Text::Markdown::Discount};
89                         if (! $@) {
90                                 my $markdown = \&Text::Markdown::Discount::markdown;
91                                 my $always_flags = 0;
93                                 # Disable Pandoc-style % Title, % Author, % Date
94                                 # Use the meta plugin instead
95                                 $always_flags |= Text::Markdown::Discount::MKD_NOHEADER();
97                                 # Disable Unicodification of quote marks, em dashes...
98                                 # Use the typography plugin instead
99                                 $always_flags |= Text::Markdown::Discount::MKD_NOPANTS();
101                                 # Workaround for discount's eliding of <style> blocks.
102                                 # https://rt.cpan.org/Ticket/Display.html?id=74016
103                                 if (Text::Markdown::Discount->can('MKD_NOSTYLE')) {
104                                         $always_flags |= Text::Markdown::Discount::MKD_NOSTYLE();
105                                 }
106                                 elsif ($markdown->('<style>x</style>', 0) !~ '<style>' &&
107                                         $markdown->('<style>x</style>', 0x00400000) =~ m{<style>x</style>}) {
108                                         $always_flags |= 0x00400000;
109                                 }
111                                 # Enable fenced code blocks in libmarkdown >= 2.2.0
112                                 # https://bugs.debian.org/888055
113                                 if (Text::Markdown::Discount->can('MKD_FENCEDCODE')) {
114                                         $always_flags |= Text::Markdown::Discount::MKD_FENCEDCODE();
115                                 }
116                                 elsif ($markdown->("~~~\nx\n~~~", 0) !~ m{<pre\b} &&
117                                         $markdown->("~~~\nx\n~~~", 0x02000000) =~ m{<pre\b}) {
118                                         $always_flags |= 0x02000000;
119                                 }
121                                 # PHP Markdown Extra-style term\n: definition -> <dl>
122                                 if (Text::Markdown::Discount->can('MKD_DLEXTRA')) {
123                                         $always_flags |= Text::Markdown::Discount::MKD_DLEXTRA();
124                                 }
125                                 elsif ($markdown->("term\n: def\n", 0) !~ m{<dl>} &&
126                                         $markdown->("term\n: def\n", 0x01000000) =~ m{<dl>}) {
127                                         $always_flags |= 0x01000000;
128                                 }
130                                 # Allow dashes and underscores in tag names
131                                 if (Text::Markdown::Discount->can('MKD_GITHUBTAGS')) {
132                                         $always_flags |= Text::Markdown::Discount::MKD_GITHUBTAGS();
133                                 }
134                                 elsif ($markdown->('<foo_bar>', 0) !~ m{<foo_bar} &&
135                                         $markdown->('<foo_bar>', 0x08000000) =~ m{<foo_bar\b}) {
136                                         $always_flags |= 0x08000000;
137                                 }
139                                 $markdown_sub=sub {
140                                         my $t=shift;
142                                         # Workaround for discount binding bug
143                                         # https://rt.cpan.org/Ticket/Display.html?id=73657
144                                         return "" if $t=~/^\s*$/;
146                                         my $flags=$always_flags;
148                                         if ($config{mdwn_footnotes}) {
149                                                 $flags |= Text::Markdown::Discount::MKD_EXTRA_FOOTNOTE();
150                                         }
152                                         unless ($config{mdwn_alpha_lists}) {
153                                                 $flags |= Text::Markdown::Discount::MKD_NOALPHALIST();
154                                         }
156                                         return Text::Markdown::Discount::markdown($t, $flags);
157                                 }
158                         }
159                 }
160                 if (! defined $markdown_sub) {
161                         eval q{use Text::Markdown};
162                         if (! $@) {
163                                 if (Text::Markdown->can('markdown')) {
164                                         $markdown_sub=\&Text::Markdown::markdown;
165                                 }
166                                 else {
167                                         $markdown_sub=\&Text::Markdown::Markdown;
168                                 }
169                         }
170                         else {
171                                 eval q{use Markdown};
172                                 if (! $@) {
173                                         $markdown_sub=\&Markdown::Markdown;
174                                 }
175                                 else {
176                                         my $error = $@;
177                                         do "/usr/bin/markdown" ||
178                                                 error(sprintf(gettext("failed to load Markdown.pm perl module (%s) or /usr/bin/markdown (%s)"), $error, $!));
179                                         $markdown_sub=\&Markdown::Markdown;
180                                 }
181                         }
182                 }
183                 
184                 require Encode;
185         }
186         
187         # Workaround for perl bug (#376329)
188         $content=Encode::encode_utf8($content);
189         eval {$content=&$markdown_sub($content)};
190         if ($@) {
191                 eval {$content=&$markdown_sub($content)};
192                 print STDERR $@ if $@;
193         }
194         $content=Encode::decode_utf8($content);
196         return $content;