]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/mdwn.pm
Process .md like .mdwn, but disallow web creation.
[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;
67                                         # Workaround for discount binding bug
68                                         # https://rt.cpan.org/Ticket/Display.html?id=73657
69                                         return "" if $t=~/^\s*$/;
70                                         # Workaround for discount's eliding
71                                         # of <style> blocks.
72                                         # https://rt.cpan.org/Ticket/Display.html?id=74016
73                                         $t=~s/<style/<elyts/ig;
74                                         my $r=Text::Markdown::Discount::markdown($t);
75                                         $r=~s/<elyts/<style/ig;
76                                         return $r;
77                                 }
78                         }
79                 }
80                 if (! defined $markdown_sub) {
81                         eval q{use Text::Markdown};
82                         if (! $@) {
83                                 if (Text::Markdown->can('markdown')) {
84                                         $markdown_sub=\&Text::Markdown::markdown;
85                                 }
86                                 else {
87                                         $markdown_sub=\&Text::Markdown::Markdown;
88                                 }
89                         }
90                         else {
91                                 eval q{use Markdown};
92                                 if (! $@) {
93                                         $markdown_sub=\&Markdown::Markdown;
94                                 }
95                                 else {
96                                         my $error = $@;
97                                         do "/usr/bin/markdown" ||
98                                                 error(sprintf(gettext("failed to load Markdown.pm perl module (%s) or /usr/bin/markdown (%s)"), $error, $!));
99                                         $markdown_sub=\&Markdown::Markdown;
100                                 }
101                         }
102                 }
103                 
104                 require Encode;
105         }
106         
107         # Workaround for perl bug (#376329)
108         $content=Encode::encode_utf8($content);
109         eval {$content=&$markdown_sub($content)};
110         if ($@) {
111                 eval {$content=&$markdown_sub($content)};
112                 print STDERR $@ if $@;
113         }
114         $content=Encode::decode_utf8($content);
116         return $content;