File Coverage

blib/lib/MojoMojo/Formatter/Markdown.pm
Criterion Covered Total %
statement 10 16 62.5
branch 1 2 50.0
condition n/a
subroutine 3 4 75.0
pod 2 2 100.0
total 16 24 66.6


line stmt bran cond sub pod time code
1             package MojoMojo::Formatter::Markdown;
2              
3 27     27   233230 use parent qw/MojoMojo::Formatter/;
  27         733  
  27         151  
4              
5             my $markdown;
6 27     27   13076 eval "use Text::MultiMarkdown";
  27         576136  
  27         993  
7             unless ($@) {
8             $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             sub esc {
42 0     0 1 0 my %replace_map = (
43             '&' => '&amp;',
44             '<' => '&lt;',
45             '>' => '&gt;'
46             );
47              
48 0         0 my $source = shift;
49 0         0 $source =~ s/([&<>])/$replace_map{$1}/eg;
  0         0  
50 0         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             sub main_format_content {
74 109     109 1 29564 my ( $class, $content ) = @_;
75 109 50       765 return unless $markdown;
76              
77 109         549 $$content =~ s/```(.+?)\n(.+?)```/"<pre><code class=\"language-$1\">\n".esc($2)."<\/code><\/pre>";/seg;
  0         0  
78 109         686 $$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             1;