lib/MojoMojo/Formatter/Markdown.pm

filter: Statementstmt 71.4% 15 / 21 Branchbran 50.0% 2 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 3 / 4 Podpod 100% 2 / 2 totaltotal 70.9% 22 / 31 CC 2 SCAR 4.9
CC
2
Cov
68%
main_format_content
2.1
esc
2.0
BEGIN
1.0
CRAP 1.6
« lib/MojoMojo/Formatter.pm ↑ Summary lib/MojoMojo/Formatter/File/DocBook.pm »
1 package MojoMojo::Formatter::Markdown;
2  
3 27 ▶use parent qw/MojoMojo::Formatter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 27 my $markdown;
6 27 ▶eval "use Text::MultiMarkdown";
Subroutine BEGIN: called CC=1 SCAR=0.0
7 27 ▶unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 27 0 27
8 27 $markdown = Text::MultiMarkdown->new(
9 markdown_in_html_blocks => 0, # Allow Markdown syntax within HTML blocks.
10 use_metadata =>
11 0, # Remove MultiMarkdown behavior change to make the top of the document metadata.
12 heading_ids => 0, # Remove MultiMarkdown behavior change in <hX> tags.
13 img_ids => 0, # Remove MultiMarkdown behavior change in <img> tags.
14 );
15 }
16  
17 =head1 NAME
18  
19 MojoMojo::Formatter::Markdown - MultiMarkdown formatting for your content.
20 MultiMarkdown is an extension of Markdown, adding support for tables,
21 footnotes, bibliography, automatic cross-references, glossaries, appendices,
22 definition lists, math syntax, anchor and image attributes, and document metadata.
23  
24 Markdown syntax: L<http://daringfireball.net/projects/markdown/syntax>
25 MultiMarkdown syntax: L<http://fletcherpenney.net/multimarkdown/users_guide/multimarkdown_syntax_guide/>
26  
27 =head1 DESCRIPTION
28  
29 This formatter processes content using L<Text::MultiMarkdown> This is a
30 syntax for writing human-friendly formatted text.
31  
32 =head1 METHODS
33  
34  
35 =head2 esc
36  
37 Escape HTML special characters given as a replace map.
38  
39 =cut
40  
41 0 ▶sub esc {
Subroutine esc: not called CC=1 SCAR=6.9
Pod documented
42 0 my %replace_map = (
43 '&' => '&amp;',
44 '<' => '&lt;',
45 '>' => '&gt;'
46 );
47  
48 0 my $source = shift;
49 0 $source =~ s/([&<>])/$replace_map{$1}/eg;
50 0 return $source;
51 }
52  
53  
54 =head2 main_format_content
55  
56 Calls the formatter. Takes a ref to the content as well as the
57 context object. Note that this is different from the format_content method
58 of non-main formatters. This is because we don't want all main formatters
59 to be called when iterating over pluggable modules in
60 L<MojoMojo::Schema::ResultSet::Content::format_content>.
61  
62 C<main_format_content> will only be called by <MojoMojo::Formatter::Main>.
63  
64 Note that L<Text::Markdown> ensures that the output always ends with B<one>
65 newline. The fact that multiple newlines are collapsed into one makese sense,
66 because this is the behavior of HTML towards whispace. The fact that there's
67 always a newline at the end makes sense again, given that the output will always
68 be nested in a B<block>-level element, be it a C<< <p> >> (most often),
69 C<< <table> >>, or C<< <div> >> (when passing HTML through).
70  
71 =cut
72  
73 109 ▶sub main_format_content {
Subroutine main_format_content: called CC=2 SCAR=7.4
Pod documented
74 109 my ( $class, $content ) = @_;
75 109 ▶ return unless $markdown;
Branch: 1 branch
BranchTrueFalseTotal
unless $markdown 0 109 109
76  
77 0 $$content =~ s/```(.+?)\n(.+?)```/"<pre><code class=\"language-$1\">\n".esc($2)."<\/code><\/pre>";/seg;
78 109 $$content = $markdown->markdown($$content);
79 }
80  
81 =head1 SEE ALSO
82  
83 L<MojoMojo>, L<Module::Pluggable::Ordered>, L<Text::MultiMarkdown>
84  
85 =head1 AUTHORS
86  
87 Marcus Ramberg <mramberg@cpan.org>
88  
89 =head1 LICENSE
90  
91 This library is free software. You can redistribute it and/or modify
92 it under the same terms as Perl itself.
93  
94 =cut
95  
96 27 1;
« lib/MojoMojo/Formatter.pm ↑ Summary lib/MojoMojo/Formatter/File/DocBook.pm »