2 # Markdown markup language
3 package IkiWiki::Plugin::mdwn;
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);
20 rebuild => 1, # format plugin
26 description => "enable multimarkdown features?",
33 description => "disable use of markdown discount?",
40 description => "enable footnotes in Markdown (where supported)?",
47 description => "interpret line like 'A. First item' as ordered list when using Discount?",
55 $config{mdwn_footnotes} = 1 unless defined $config{mdwn_footnotes};
56 $config{mdwn_alpha_lists} = 0 unless defined $config{mdwn_alpha_lists};
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.
68 $blosxom::version="is a proper perl module too much to ask?";
71 if (exists $config{multimarkdown} && $config{multimarkdown}) {
72 eval q{use Text::MultiMarkdown};
74 debug(gettext("multimarkdown is enabled, but Text::MultiMarkdown is not installed"));
78 my %flags=( use_metadata => 0 );
80 $flags{disable_footnotes}=not $config{mdwn_footnotes};
82 Text::MultiMarkdown::markdown(shift, \%flags);
86 if (! defined $markdown_sub &&
87 (! exists $config{nodiscount} || ! $config{nodiscount})) {
88 eval q{use Text::Markdown::Discount};
90 my $markdown = \&Text::Markdown::Discount::markdown;
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();
106 elsif ($markdown->('<style>x</style>', 0) !~ '<style>' &&
107 $markdown->('<style>x</style>', 0x00400000) =~ m{<style>x</style>}) {
108 $always_flags |= 0x00400000;
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();
116 elsif ($markdown->("~~~\nx\n~~~", 0) !~ m{<pre\b} &&
117 $markdown->("~~~\nx\n~~~", 0x02000000) =~ m{<pre\b}) {
118 $always_flags |= 0x02000000;
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();
125 elsif ($markdown->("term\n: def\n", 0) !~ m{<dl>} &&
126 $markdown->("term\n: def\n", 0x01000000) =~ m{<dl>}) {
127 $always_flags |= 0x01000000;
130 # Allow dashes and underscores in tag names
131 if (Text::Markdown::Discount->can('MKD_GITHUBTAGS')) {
132 $always_flags |= Text::Markdown::Discount::MKD_GITHUBTAGS();
134 elsif ($markdown->('<foo_bar>', 0) !~ m{<foo_bar} &&
135 $markdown->('<foo_bar>', 0x08000000) =~ m{<foo_bar\b}) {
136 $always_flags |= 0x08000000;
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();
152 unless ($config{mdwn_alpha_lists}) {
153 $flags |= Text::Markdown::Discount::MKD_NOALPHALIST();
156 return Text::Markdown::Discount::markdown($t, $flags);
160 if (! defined $markdown_sub) {
161 eval q{use Text::Markdown};
163 if (Text::Markdown->can('markdown')) {
164 $markdown_sub=\&Text::Markdown::markdown;
167 $markdown_sub=\&Text::Markdown::Markdown;
171 eval q{use Markdown};
173 $markdown_sub=\&Markdown::Markdown;
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;
187 # Workaround for perl bug (#376329)
188 $content=Encode::encode_utf8($content);
189 eval {$content=&$markdown_sub($content)};
191 eval {$content=&$markdown_sub($content)};
192 print STDERR $@ if $@;
194 $content=Encode::decode_utf8($content);