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 if ($config{mdwn_footnotes}) {
81 $flags{disable_footnotes}=1;
84 Text::MultiMarkdown::markdown(shift, \%flags);
88 if (! defined $markdown_sub &&
89 (! exists $config{nodiscount} || ! $config{nodiscount})) {
90 eval q{use Text::Markdown::Discount};
92 my $markdown = \&Text::Markdown::Discount::markdown;
95 # Disable Pandoc-style % Title, % Author, % Date
96 # Use the meta plugin instead
97 $always_flags |= Text::Markdown::Discount::MKD_NOHEADER();
99 # Disable Unicodification of quote marks, em dashes...
100 # Use the typography plugin instead
101 $always_flags |= Text::Markdown::Discount::MKD_NOPANTS();
103 # Workaround for discount's eliding of <style> blocks.
104 # https://rt.cpan.org/Ticket/Display.html?id=74016
105 if (Text::Markdown::Discount->can('MKD_NOSTYLE')) {
106 $always_flags |= Text::Markdown::Discount::MKD_NOSTYLE();
108 elsif ($markdown->('<style>x</style>', 0) !~ '<style>' &&
109 $markdown->('<style>x</style>', 0x00400000) =~ m{<style>x</style>}) {
110 $always_flags |= 0x00400000;
113 # Enable fenced code blocks in libmarkdown >= 2.2.0
114 # https://bugs.debian.org/888055
115 if (Text::Markdown::Discount->can('MKD_FENCEDCODE')) {
116 $always_flags |= Text::Markdown::Discount::MKD_FENCEDCODE();
118 elsif ($markdown->("~~~\nx\n~~~", 0) !~ m{<pre\b} &&
119 $markdown->("~~~\nx\n~~~", 0x02000000) =~ m{<pre\b}) {
120 $always_flags |= 0x02000000;
123 # PHP Markdown Extra-style term\n: definition -> <dl>
124 if (Text::Markdown::Discount->can('MKD_DLEXTRA')) {
125 $always_flags |= Text::Markdown::Discount::MKD_DLEXTRA();
127 elsif ($markdown->("term\n: def\n", 0) !~ m{<dl>} &&
128 $markdown->("term\n: def\n", 0x01000000) =~ m{<dl>}) {
129 $always_flags |= 0x01000000;
132 # Allow dashes and underscores in tag names
133 if (Text::Markdown::Discount->can('MKD_GITHUBTAGS')) {
134 $always_flags |= Text::Markdown::Discount::MKD_GITHUBTAGS();
136 elsif ($markdown->('<foo_bar>', 0) !~ m{<foo_bar} &&
137 $markdown->('<foo_bar>', 0x08000000) =~ m{<foo_bar\b}) {
138 $always_flags |= 0x08000000;
144 # Workaround for discount binding bug
145 # https://rt.cpan.org/Ticket/Display.html?id=73657
146 return "" if $t=~/^\s*$/;
148 my $flags=$always_flags;
150 if ($config{mdwn_footnotes}) {
151 $flags |= Text::Markdown::Discount::MKD_EXTRA_FOOTNOTE();
154 unless ($config{mdwn_alpha_lists}) {
155 $flags |= Text::Markdown::Discount::MKD_NOALPHALIST();
158 return Text::Markdown::Discount::markdown($t, $flags);
162 if (! defined $markdown_sub) {
163 eval q{use Text::Markdown};
165 if (Text::Markdown->can('markdown')) {
166 $markdown_sub=\&Text::Markdown::markdown;
169 $markdown_sub=\&Text::Markdown::Markdown;
173 eval q{use Markdown};
175 $markdown_sub=\&Markdown::Markdown;
179 do "/usr/bin/markdown" ||
180 error(sprintf(gettext("failed to load Markdown.pm perl module (%s) or /usr/bin/markdown (%s)"), $error, $!));
181 $markdown_sub=\&Markdown::Markdown;
189 # Workaround for perl bug (#376329)
190 $content=Encode::encode_utf8($content);
191 eval {$content=&$markdown_sub($content)};
193 eval {$content=&$markdown_sub($content)};
194 print STDERR $@ if $@;
196 $content=Encode::decode_utf8($content);