| 1 |
package Text::Markup::CommonMark; |
| 2 |
|
| 3 |
use 5.8.1; |
| 4 |
use strict; |
| 5 |
use warnings; |
| 6 |
use CommonMark; |
| 7 |
use Text::Markup; |
| 8 |
use File::BOM qw(open_bom); |
| 9 |
|
| 10 |
our $VERSION = '0.42'; |
| 11 |
|
| 12 |
sub import { |
| 13 |
# Replace Text::Markup::Markdown. |
| 14 |
Text::Markup->register( markdown => $_[1] || qr{m(?:d(?:own)?|kdn?|arkdown)} ); |
| 15 |
} |
| 16 |
|
| 17 |
sub parser { |
| 18 |
my ($file, $encoding, $opts) = @_; |
| 19 |
open_bom my $fh, $file, ":encoding($encoding)"; |
| 20 |
my %params = @{ $opts }; |
| 21 |
my $html = CommonMark->parse( |
| 22 |
smart => 1, |
| 23 |
unsafe => 1, |
| 24 |
%params, |
| 25 |
string => join( '', <$fh>), |
| 26 |
)->render( %params, format => 'html' ); |
| 27 |
return unless $html =~ /\S/; |
| 28 |
utf8::encode($html); |
| 29 |
return $html if $params{raw}; |
| 30 |
return qq{<html> |
| 31 |
<head> |
| 32 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| 33 |
</head> |
| 34 |
<body> |
| 35 |
$html |
| 36 |
</body> |
| 37 |
</html> |
| 38 |
}; |
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
1; |
| 43 |
__END__ |
| 44 |
|
| 45 |
=head1 Name |
| 46 |
|
| 47 |
Text::Markup::CommonMark - CommonMark Markdown parser for Text::Markup |
| 48 |
|
| 49 |
=head1 Synopsis |
| 50 |
|
| 51 |
use Text::Markup::CommonMark; |
| 52 |
my $html = Text::Markup->new->parse(file => 'README.md'); |
| 53 |
my $raw = Text::Markup->new->parse( |
| 54 |
file => 'README.md', |
| 55 |
options => [ raw => 1 ], |
| 56 |
); |
| 57 |
|
| 58 |
=head1 Description |
| 59 |
|
| 60 |
This is the L<CommonMark|https://commonmark.org> parser |
| 61 |
for L<Text::Markup>. On load, it replaces the default L<Text::Markup::Markdown> |
| 62 |
parser for parsing L<Markdown|https://daringfireball.net/projects/markdown/>. |
| 63 |
Note that L<Text::Markup> does not load this module by default, but when |
| 64 |
loaded manually will be the preferred Markdown parser. |
| 65 |
|
| 66 |
Text::Markup::CommonMark reads in the file (relying on a |
| 67 |
L<BOM|https://www.unicode.org/unicode/faq/utf_bom.html#BOM>), hands it off to |
| 68 |
L<CommonMark> for parsing, and then returns the generated HTML as an |
| 69 |
encoded UTF-8 string with an C<http-equiv="Content-Type"> element identifying |
| 70 |
the encoding as UTF-8. |
| 71 |
|
| 72 |
It recognizes files with the following extensions as CommonMark Markdown: |
| 73 |
|
| 74 |
=over |
| 75 |
|
| 76 |
=item F<.md> |
| 77 |
|
| 78 |
=item F<.mkd> |
| 79 |
|
| 80 |
=item F<.mkdn> |
| 81 |
|
| 82 |
=item F<.mdown> |
| 83 |
|
| 84 |
=item F<.markdown> |
| 85 |
|
| 86 |
=back |
| 87 |
|
| 88 |
To change it the files it recognizes, load this module directly and pass a |
| 89 |
regular expression matching the desired extension(s), like so: |
| 90 |
|
| 91 |
use Text::Markup::CommonMark qr{markd?}; |
| 92 |
|
| 93 |
Normally this module returns the output wrapped in a minimal HTML document |
| 94 |
skeleton. If you would like the raw output without the skeleton, you can pass |
| 95 |
the C<raw> option to C<parse>. |
| 96 |
|
| 97 |
In addition Text::CommonMark supports all of the CommonMark |
| 98 |
L<parse options|CommonMark/parse> and L<render options|CommonMark::Node/render>, |
| 99 |
including: |
| 100 |
|
| 101 |
=over |
| 102 |
|
| 103 |
=item C<smart> |
| 104 |
|
| 105 |
When true, convert straight quotes to curly, --- to em dashes, -- to en |
| 106 |
dashes. Enabled by default. |
| 107 |
|
| 108 |
=item C<sourcepos> |
| 109 |
|
| 110 |
When true, include a data-sourcepos attribute on all block elements. Disabled |
| 111 |
by default. |
| 112 |
|
| 113 |
=item C<hardbreaks> |
| 114 |
|
| 115 |
When true, render soft-break elements as hard line breaks. Disabled by default. |
| 116 |
|
| 117 |
=item C<nobreaks> |
| 118 |
|
| 119 |
When true, render soft-break elements as spaces. Disabled by default. |
| 120 |
|
| 121 |
=item C<validate_utf8> |
| 122 |
|
| 123 |
When true, validate UTF-8 in the input before parsing, replacing illegal |
| 124 |
sequences with the replacement character C<U+FFFD>. Disabled by default. |
| 125 |
|
| 126 |
=item C<unsafe> |
| 127 |
|
| 128 |
Render raw HTML and unsafe links (C<javascript:>, C<vbscript:>, C<file:>, and |
| 129 |
C<data:>, except for C<image/png>, C<image/gif>, C<image/jpeg>, or |
| 130 |
C<image/webp> mime types). Raw HTML is replaced by a placeholder HTML comment. |
| 131 |
Unsafe links are replaced by empty strings. Enabled by default. |
| 132 |
|
| 133 |
=back |
| 134 |
|
| 135 |
=head1 Author |
| 136 |
|
| 137 |
David E. Wheeler <[email protected]> |
| 138 |
|
| 139 |
=head1 Copyright and License |
| 140 |
|
| 141 |
Copyright (c) 2011-2025 David E. Wheeler. Some Rights Reserved. |
| 142 |
|
| 143 |
This module is free software; you can redistribute it and/or modify it under |
| 144 |
the same terms as Perl itself. |
| 145 |
|
| 146 |
=cut |