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};
95 # Workaround for discount binding bug
96 # https://rt.cpan.org/Ticket/Display.html?id=73657
97 return "" if $t=~/^\s*$/;
101 # Disable Pandoc-style % Title, % Author, % Date
102 # Use the meta plugin instead
103 $flags |= Text::Markdown::Discount::MKD_NOHEADER();
105 # Disable Unicodification of quote marks, em dashes...
106 # Use the typography plugin instead
107 $flags |= Text::Markdown::Discount::MKD_NOPANTS();
109 if ($config{mdwn_footnotes}) {
110 $flags |= Text::Markdown::Discount::MKD_EXTRA_FOOTNOTE();
113 unless ($config{mdwn_alpha_lists}) {
114 $flags |= Text::Markdown::Discount::MKD_NOALPHALIST();
117 # Workaround for discount's eliding
119 # https://rt.cpan.org/Ticket/Display.html?id=74016
120 if (Text::Markdown::Discount->can("MKD_NOSTYLE")) {
121 $flags |= Text::Markdown::Discount::MKD_NOSTYLE();
124 # This is correct for the libmarkdown.so.2 ABI
125 $flags |= 0x00400000;
128 # Enable fenced code blocks in libmarkdown >= 2.2.0
129 # https://bugs.debian.org/888055
130 if (Text::Markdown::Discount->can("MKD_FENCEDCODE")) {
131 $flags |= Text::Markdown::Discount::MKD_FENCEDCODE();
134 $flags |= 0x02000000;
137 # PHP Markdown Extra-style term\n: definition -> <dl>
138 if (Text::Markdown::Discount->can("MKD_DLEXTRA")) {
139 $flags |= Text::Markdown::Discount::MKD_DLEXTRA();
142 $flags |= 0x01000000;
145 # Allow dashes and underscores in tag names
146 if (Text::Markdown::Discount->can("MKD_GITHUBTAGS")) {
147 $flags |= Text::Markdown::Discount::MKD_GITHUBTAGS();
150 $flags |= 0x08000000;
153 return Text::Markdown::Discount::markdown($t, $flags);
157 if (! defined $markdown_sub) {
158 eval q{use Text::Markdown};
160 if (Text::Markdown->can('markdown')) {
161 $markdown_sub=\&Text::Markdown::markdown;
164 $markdown_sub=\&Text::Markdown::Markdown;
168 eval q{use Markdown};
170 $markdown_sub=\&Markdown::Markdown;
174 do "/usr/bin/markdown" ||
175 error(sprintf(gettext("failed to load Markdown.pm perl module (%s) or /usr/bin/markdown (%s)"), $error, $!));
176 $markdown_sub=\&Markdown::Markdown;
184 # Workaround for perl bug (#376329)
185 $content=Encode::encode_utf8($content);
186 eval {$content=&$markdown_sub($content)};
188 eval {$content=&$markdown_sub($content)};
189 print STDERR $@ if $@;
191 $content=Encode::decode_utf8($content);