]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/mdwn.pm
Merge branch 'master' of git://git.ikiwiki.info
[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 }
14 sub getsetup () {
15         return
16                 plugin => {
17                         safe => 1,
18                         rebuild => 1, # format plugin
19                         section => "format",
20                 },
21                 multimarkdown => {
22                         type => "boolean",
23                         example => 0,
24                         description => "enable multimarkdown features?",
25                         safe => 1,
26                         rebuild => 1,
27                 },
28                 nodiscount => {
29                         type => "boolean",
30                         example => 0,
31                         description => "disable use of markdown discount?",
32                         safe => 1,
33                         rebuild => 1,
34                 },
35 }
37 my $markdown_sub;
38 sub htmlize (@) {
39         my %params=@_;
40         my $content = $params{content};
42         if (! defined $markdown_sub) {
43                 # Markdown is forked and splintered upstream and can be
44                 # available in a variety of forms. Support them all.
45                 no warnings 'once';
46                 $blosxom::version="is a proper perl module too much to ask?";
47                 use warnings 'all';
49                 if (exists $config{multimarkdown} && $config{multimarkdown}) {
50                         eval q{use Text::MultiMarkdown};
51                         if ($@) {
52                                 debug(gettext("multimarkdown is enabled, but Text::MultiMarkdown is not installed"));
53                         }
54                         else {
55                                 $markdown_sub=sub {
56                                         Text::MultiMarkdown::markdown(shift, {use_metadata => 0});
57                                 }
58                         }
59                 }
60                 if (! defined $markdown_sub &&
61                     exists $config{nodiscount} && ! $config{nodiscount}) {
62                         eval q{use Text::Markdown::Discount};
63                         if (! $@) {
64                                 $markdown_sub=sub {
65                                         my $t=shift;
66                                         # Workaround for discount binding bug
67                                         # https://rt.cpan.org/Ticket/Display.html?id=73657
68                                         return "" if $t=~/^\s*$/;
69                                         # Workaround for discount's eliding
70                                         # of <style> blocks.
71                                         # https://rt.cpan.org/Ticket/Display.html?id=74016
72                                         $t=~s/<style/<elyts/ig;
73                                         my $r=Text::Markdown::Discount::markdown($t);
74                                         $r=~s/<elyts/<style/ig;
75                                         return $r;
76                                 }
77                         }
78                 }
79                 if (! defined $markdown_sub) {
80                         eval q{use Text::Markdown};
81                         if (! $@) {
82                                 if (Text::Markdown->can('markdown')) {
83                                         $markdown_sub=\&Text::Markdown::markdown;
84                                 }
85                                 else {
86                                         $markdown_sub=\&Text::Markdown::Markdown;
87                                 }
88                         }
89                         else {
90                                 eval q{use Markdown};
91                                 if (! $@) {
92                                         $markdown_sub=\&Markdown::Markdown;
93                                 }
94                                 else {
95                                         do "/usr/bin/markdown" ||
96                                                 error(sprintf(gettext("failed to load Markdown.pm perl module (%s) or /usr/bin/markdown (%s)"), $@, $!));
97                                         $markdown_sub=\&Markdown::Markdown;
98                                 }
99                         }
100                 }
101                 
102                 require Encode;
103         }
104         
105         # Workaround for perl bug (#376329)
106         $content=Encode::encode_utf8($content);
107         eval {$content=&$markdown_sub($content)};
108         if ($@) {
109                 eval {$content=&$markdown_sub($content)};
110                 print STDERR $@ if $@;
111         }
112         $content=Encode::decode_utf8($content);
114         return $content;