2006-07-04 00:08:04 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
# Markdown markup language
|
|
|
|
package IkiWiki::Plugin::mdwn;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.00;
|
2006-07-04 00:08:04 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2008-07-25 23:52:00 +02:00
|
|
|
hook(type => "getsetup", id => "mdwn", call => \&getsetup);
|
2009-05-16 14:59:27 +02:00
|
|
|
hook(type => "htmlize", id => "mdwn", call => \&htmlize, longname => "Markdown");
|
2016-03-08 20:31:15 +01:00
|
|
|
hook(type => "htmlize", id => "md", call => \&htmlize, longname => "Markdown (popular file extension)", nocreate => 1);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-07-04 00:08:04 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getsetup () {
|
2008-07-25 23:52:00 +02:00
|
|
|
return
|
2008-08-03 22:40:12 +02:00
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => 1, # format plugin
|
2010-02-12 07:10:36 +01:00
|
|
|
section => "format",
|
2008-08-03 22:40:12 +02:00
|
|
|
},
|
2008-07-25 23:52:00 +02:00
|
|
|
multimarkdown => {
|
|
|
|
type => "boolean",
|
2008-07-27 03:07:15 +02:00
|
|
|
example => 0,
|
2008-07-26 20:43:47 +02:00
|
|
|
description => "enable multimarkdown features?",
|
2008-07-25 23:52:00 +02:00
|
|
|
safe => 1,
|
|
|
|
rebuild => 1,
|
|
|
|
},
|
2012-01-16 18:42:30 +01:00
|
|
|
nodiscount => {
|
|
|
|
type => "boolean",
|
|
|
|
example => 0,
|
|
|
|
description => "disable use of markdown discount?",
|
|
|
|
safe => 1,
|
|
|
|
rebuild => 1,
|
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-07-25 23:52:00 +02:00
|
|
|
|
2006-09-16 03:50:29 +02:00
|
|
|
my $markdown_sub;
|
2008-12-17 21:22:16 +01:00
|
|
|
sub htmlize (@) {
|
2006-08-28 20:17:59 +02:00
|
|
|
my %params=@_;
|
|
|
|
my $content = $params{content};
|
2006-07-04 00:08:04 +02:00
|
|
|
|
2006-09-16 03:50:29 +02:00
|
|
|
if (! defined $markdown_sub) {
|
|
|
|
# Markdown is forked and splintered upstream and can be
|
2008-05-13 18:43:25 +02:00
|
|
|
# available in a variety of forms. Support them all.
|
2006-07-04 00:08:04 +02:00
|
|
|
no warnings 'once';
|
|
|
|
$blosxom::version="is a proper perl module too much to ask?";
|
|
|
|
use warnings 'all';
|
2006-08-10 06:11:58 +02:00
|
|
|
|
2008-05-13 18:43:25 +02:00
|
|
|
if (exists $config{multimarkdown} && $config{multimarkdown}) {
|
|
|
|
eval q{use Text::MultiMarkdown};
|
|
|
|
if ($@) {
|
2008-07-25 23:52:00 +02:00
|
|
|
debug(gettext("multimarkdown is enabled, but Text::MultiMarkdown is not installed"));
|
2008-05-13 18:43:25 +02:00
|
|
|
}
|
2009-10-26 16:56:42 +01:00
|
|
|
else {
|
|
|
|
$markdown_sub=sub {
|
|
|
|
Text::MultiMarkdown::markdown(shift, {use_metadata => 0});
|
|
|
|
}
|
2008-05-13 18:43:25 +02:00
|
|
|
}
|
2006-09-16 03:50:29 +02:00
|
|
|
}
|
2012-01-16 18:42:30 +01:00
|
|
|
if (! defined $markdown_sub &&
|
2012-01-23 23:15:14 +01:00
|
|
|
(! exists $config{nodiscount} || ! $config{nodiscount})) {
|
discount support
mdwn: Can use the discount markdown library, via the
Text::Markdown::Discount perl module.
This is preferred if available since it's the fastest currently supported
markdown library, speeding up markdown rendering by a factor of 40.
That is to say, when only rendering a lot of markdown, discount is 40x
faster. When building a ikiwiki site, ikiwiki's other overhead gets in the
way, but I still see significant speedups. Building the ikiwiki docwiki
dropped from 62 to 45 seconds, for example.
However, when multimarkdown is enabled, Text::Markdown::Multimarkdown is
still used.
While discount contains some nonstandard markdown extensions,
including tables and footnotes, AFAICS most of them are not
enabled by default in the perl bindings.
I consider sticking to non-extended markdown a desirable thing, since this
is probably not the last markdown engine. In particular, sundown is waiting
in the wings to get packaged and get a perl binding.
----
Reviewing all the showdown extensions, here are the ones that are enabled:
centered paragraphs:
->centered<-
image sizes: [dust mite](http://dust.mite =150x150)
<style>..</style> blocks are eaten. The perl binding does not provide
access to the gathered CSS. This is not legal html anyway, so unlikely
to cause breakage.
2012-01-01 21:56:32 +01:00
|
|
|
eval q{use Text::Markdown::Discount};
|
|
|
|
if (! $@) {
|
2012-01-01 22:24:21 +01:00
|
|
|
$markdown_sub=sub {
|
2012-01-15 21:19:22 +01:00
|
|
|
my $t=shift;
|
2017-05-14 18:34:12 +02:00
|
|
|
|
2012-01-01 22:24:21 +01:00
|
|
|
# Workaround for discount binding bug
|
|
|
|
# https://rt.cpan.org/Ticket/Display.html?id=73657
|
2012-01-15 21:19:22 +01:00
|
|
|
return "" if $t=~/^\s*$/;
|
2017-05-14 18:34:12 +02:00
|
|
|
|
|
|
|
my $flags=0;
|
|
|
|
|
|
|
|
# Disable Pandoc-style % Title, % Author, % Date
|
|
|
|
# Use the meta plugin instead
|
|
|
|
$flags |= Text::Markdown::Discount::MKD_NOHEADER();
|
|
|
|
|
|
|
|
# Disable Unicodification of quote marks, em dashes...
|
|
|
|
# Use the typography plugin instead
|
|
|
|
$flags |= Text::Markdown::Discount::MKD_NOPANTS();
|
|
|
|
|
2012-01-15 21:19:22 +01:00
|
|
|
# Workaround for discount's eliding
|
|
|
|
# of <style> blocks.
|
|
|
|
# https://rt.cpan.org/Ticket/Display.html?id=74016
|
|
|
|
$t=~s/<style/<elyts/ig;
|
2017-05-14 18:34:12 +02:00
|
|
|
my $r=Text::Markdown::Discount::markdown($t, $flags);
|
2012-01-15 21:19:22 +01:00
|
|
|
$r=~s/<elyts/<style/ig;
|
|
|
|
return $r;
|
2012-01-01 22:24:21 +01:00
|
|
|
}
|
discount support
mdwn: Can use the discount markdown library, via the
Text::Markdown::Discount perl module.
This is preferred if available since it's the fastest currently supported
markdown library, speeding up markdown rendering by a factor of 40.
That is to say, when only rendering a lot of markdown, discount is 40x
faster. When building a ikiwiki site, ikiwiki's other overhead gets in the
way, but I still see significant speedups. Building the ikiwiki docwiki
dropped from 62 to 45 seconds, for example.
However, when multimarkdown is enabled, Text::Markdown::Multimarkdown is
still used.
While discount contains some nonstandard markdown extensions,
including tables and footnotes, AFAICS most of them are not
enabled by default in the perl bindings.
I consider sticking to non-extended markdown a desirable thing, since this
is probably not the last markdown engine. In particular, sundown is waiting
in the wings to get packaged and get a perl binding.
----
Reviewing all the showdown extensions, here are the ones that are enabled:
centered paragraphs:
->centered<-
image sizes: [dust mite](http://dust.mite =150x150)
<style>..</style> blocks are eaten. The perl binding does not provide
access to the gathered CSS. This is not legal html anyway, so unlikely
to cause breakage.
2012-01-01 21:56:32 +01:00
|
|
|
}
|
|
|
|
}
|
2008-07-25 23:52:00 +02:00
|
|
|
if (! defined $markdown_sub) {
|
2006-09-16 03:14:30 +02:00
|
|
|
eval q{use Text::Markdown};
|
2006-09-16 03:50:29 +02:00
|
|
|
if (! $@) {
|
2008-03-05 02:29:52 +01:00
|
|
|
if (Text::Markdown->can('markdown')) {
|
|
|
|
$markdown_sub=\&Text::Markdown::markdown;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$markdown_sub=\&Text::Markdown::Markdown;
|
|
|
|
}
|
2006-09-16 03:50:29 +02:00
|
|
|
}
|
|
|
|
else {
|
2008-05-13 18:43:25 +02:00
|
|
|
eval q{use Markdown};
|
|
|
|
if (! $@) {
|
|
|
|
$markdown_sub=\&Markdown::Markdown;
|
|
|
|
}
|
|
|
|
else {
|
protect $@ whenever a block using $@ is non-trivial
As noted in the Try::Tiny man page, eval/$@ can be quite awkward in
corner cases, because $@ has the same properties and problems as C's
errno. While writing a regression test for definetemplate
in which it couldn't find an appropriate template, I received
<span class="error">Error: failed to process template
<span class="createlink">deftmpl</span> </span>
instead of the intended
<span class="error">Error: failed to process template
<span class="createlink">deftmpl</span> template deftmpl not
found</span>
which turned out to be because the "catch"-analogous block called
gettext before it used $@, and gettext can call define_gettext,
which uses eval.
This commit alters all current "catch"-like blocks that use $@, except
those that just do trivial things with $@ (string interpolation, string
concatenation) and call a function (die, error, print, etc.)
2014-02-21 18:06:36 +01:00
|
|
|
my $error = $@;
|
2008-05-13 18:43:25 +02:00
|
|
|
do "/usr/bin/markdown" ||
|
protect $@ whenever a block using $@ is non-trivial
As noted in the Try::Tiny man page, eval/$@ can be quite awkward in
corner cases, because $@ has the same properties and problems as C's
errno. While writing a regression test for definetemplate
in which it couldn't find an appropriate template, I received
<span class="error">Error: failed to process template
<span class="createlink">deftmpl</span> </span>
instead of the intended
<span class="error">Error: failed to process template
<span class="createlink">deftmpl</span> template deftmpl not
found</span>
which turned out to be because the "catch"-analogous block called
gettext before it used $@, and gettext can call define_gettext,
which uses eval.
This commit alters all current "catch"-like blocks that use $@, except
those that just do trivial things with $@ (string interpolation, string
concatenation) and call a function (die, error, print, etc.)
2014-02-21 18:06:36 +01:00
|
|
|
error(sprintf(gettext("failed to load Markdown.pm perl module (%s) or /usr/bin/markdown (%s)"), $error, $!));
|
2008-05-13 18:43:25 +02:00
|
|
|
$markdown_sub=\&Markdown::Markdown;
|
|
|
|
}
|
2006-09-16 03:14:30 +02:00
|
|
|
}
|
2006-08-10 06:11:58 +02:00
|
|
|
}
|
2008-05-13 18:43:25 +02:00
|
|
|
|
2006-07-04 00:08:04 +02:00
|
|
|
require Encode;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Workaround for perl bug (#376329)
|
|
|
|
$content=Encode::encode_utf8($content);
|
2007-11-27 22:36:37 +01:00
|
|
|
eval {$content=&$markdown_sub($content)};
|
|
|
|
if ($@) {
|
|
|
|
eval {$content=&$markdown_sub($content)};
|
|
|
|
print STDERR $@ if $@;
|
|
|
|
}
|
2006-07-04 00:08:04 +02:00
|
|
|
$content=Encode::decode_utf8($content);
|
|
|
|
|
|
|
|
return $content;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-07-04 00:08:04 +02:00
|
|
|
|
|
|
|
1
|