]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/mdwn.pm
mdwn: Don't mangle <style> into <elyts> under some circumstances
[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                                         if (Text::Markdown::Discount->can("MKD_NOSTYLE")) {
86                                                 $flags |= Text::Markdown::Discount::MKD_NOSTYLE();
87                                         }
88                                         else {
89                                                 # This is correct for the libmarkdown.so.2 ABI
90                                                 $flags |= 0x00400000;
91                                         }
93                                         return Text::Markdown::Discount::markdown($t, $flags);
94                                 }
95                         }
96                 }
97                 if (! defined $markdown_sub) {
98                         eval q{use Text::Markdown};
99                         if (! $@) {
100                                 if (Text::Markdown->can('markdown')) {
101                                         $markdown_sub=\&Text::Markdown::markdown;
102                                 }
103                                 else {
104                                         $markdown_sub=\&Text::Markdown::Markdown;
105                                 }
106                         }
107                         else {
108                                 eval q{use Markdown};
109                                 if (! $@) {
110                                         $markdown_sub=\&Markdown::Markdown;
111                                 }
112                                 else {
113                                         my $error = $@;
114                                         do "/usr/bin/markdown" ||
115                                                 error(sprintf(gettext("failed to load Markdown.pm perl module (%s) or /usr/bin/markdown (%s)"), $error, $!));
116                                         $markdown_sub=\&Markdown::Markdown;
117                                 }
118                         }
119                 }
120                 
121                 require Encode;
122         }
123         
124         # Workaround for perl bug (#376329)
125         $content=Encode::encode_utf8($content);
126         eval {$content=&$markdown_sub($content)};
127         if ($@) {
128                 eval {$content=&$markdown_sub($content)};
129                 print STDERR $@ if $@;
130         }
131         $content=Encode::decode_utf8($content);
133         return $content;