Debian Codegraph @

lib/Text/Markup/Markdown.pm

raw
1 package Text::Markup::Markdown;
2
3 use 5.8.1;
4 use strict;
5 use warnings;
6 use Text::Markup;
7 use File::BOM qw(open_bom);
8 use Text::Markdown::Discount;
9
10 our $VERSION = '0.41';
11
12 sub import {
13 # Replace the regex if passed one.
14 Text::Markup->register( markdown => $_[1] ) if $_[1];
15 }
16
17 sub parser {
18 my ($file, $encoding, $opts) = @_;
19 my %params = @{ $opts };
20
21 unless (defined $params{flags}) {
22 $params{flags} = Text::Markdown::Discount::MKD_NOHEADER
23 | Text::Markdown::Discount::MKD_TOC
24 | Text::Markdown::Discount::MKD_DLEXTRA
25 | Text::Markdown::Discount::MKD_FENCEDCODE
26 | Text::Markdown::Discount::MKD_EXTRA_FOOTNOTE
27 | Text::Markdown::Discount::MKD_IDANCHOR;
28 }
29 open_bom my $fh, $file, ":encoding($encoding)";
30 my $html = Text::Markdown::Discount::markdown(join('', <$fh>), $params{flags});
31 return unless $html =~ /\S/;
32 utf8::encode($html);
33 return $html if $params{raw};
34 return qq{<html>
35 <head>
36 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
37 </head>
38 <body>
39 $html
40 </body>
41 </html>
42 };
43
44 }
45
46 1;
47 __END__
48
49 =head1 Name
50
51 Text::Markup::Markdown - Markdown parser for Text::Markup
52
53 =head1 Synopsis
54
55 my $html = Text::Markup->new->parse(file => 'README.md');
56 my $raw = Text::Markup->new->parse(
57 file => 'README.md',
58 options => [ raw => 1 ],
59 );
60
61 =head1 Description
62
63 This is the L<Markdown|https://daringfireball.net/projects/markdown/> parser
64 for L<Text::Markup>. It reads in the file (relying on a
65 L<BOM|https://www.unicode.org/unicode/faq/utf_bom.html#BOM>), hands it off to
66 L<Text::Markdown::Discount> for parsing, and then returns the generated HTML as an
67 encoded UTF-8 string with an C<http-equiv="Content-Type"> element identifying
68 the encoding as UTF-8.
69
70 It recognizes files with the following extensions as Markdown:
71
72 =over
73
74 =item F<.md>
75
76 =item F<.mkd>
77
78 =item F<.mkdn>
79
80 =item F<.mdown>
81
82 =item F<.markdown>
83
84 =back
85
86 To change it the files it recognizes, load this module directly and pass a
87 regular expression matching the desired extension(s), like so:
88
89 use Text::Markup::Markdown qr{markd?};
90
91 Normally this module returns the output wrapped in a minimal HTML document
92 skeleton. If you would like the raw output without the skeleton, you can pass
93 the C<raw> parameter to C<parse>.
94
95 The Text::Markup::Markdown C<parse> method supports an additional parameter,
96 C<flags>, a bitmask of
97 L<Text::Markdown::Discount options|https://github.com/Songmu/text-markdown-discount/blob/d6b1325/lib/Text/Markdown/Discount.xs#L16-L46>.
98 Use this parameter to replace the default, which is:
99
100 MKD_NOHEADER | MKD_TOC | MKD_DLEXTRA | MKD_FENCEDCODE | MKD_EXTRA_FOOTNOTE | MKD_IDANCHOR
101
102 =head1 See Also
103
104 L<National Funk Congress Deadlocked On Get Up/Get Down Issue|https://www.theonion.com/national-funk-congress-deadlocked-on-get-up-get-down-is-1819565355>.
105 MarkI<up> or MarkI<down>?
106
107 =head1 Author
108
109 David E. Wheeler <[email protected]>
110
111 =head1 Copyright and License
112
113 Copyright (c) 2011-2025 David E. Wheeler. Some Rights Reserved.
114
115 This module is free software; you can redistribute it and/or modify it under
116 the same terms as Perl itself.
117
118 =cut