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