| 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.43'; | ||
| 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__ |