lib/App/Dapper/Markdown.pm

filter: Statementstmt 100% 48 / 48 Branchbran 90.0% 9 / 10 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 98.7% 76 / 77 CC 8 SCAR 11.0
CC
8
Cov
98%
to_html
5.0
code_block
4.0
BEGIN
1.0
CRAP 3.0
« bin/dapper ↑ Summary lib/App/Dapper/Filters.pm »
1 package App::Dapper::Markdown;
2  
3 =head1 NAME
4  
5 App::Dapper::Markdown - Convert Markdown to HTML, with fenced code blocks.
6  
7 =head1 DESCRIPTION
8  
9 Dapper uses L<Text::MultiMarkdown> for Markdown. On top of it, this module
10 adds fenced code blocks, which MultiMarkdown does not understand:
11  
12 ```perl
13 print "Hello, world!\n";
14 ```
15  
16 A fenced block (with three or more backticks or tildes) becomes
17 C<< <pre><code class="language-perl"> >>, with its contents HTML-escaped and
18 left untouched by Markdown. The C<language-*> class is what client-side
19 highlighters such as Prism and highlight.js look for.
20  
21 It also expands components such as C<::: note> and C<::: youtube>; see
22 L<App::Dapper::Components>.
23  
24 =cut
25  
26 190 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 190 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 190 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 190 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 190 ▶use Text::MultiMarkdown ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32 190 ▶use App::Dapper::Components;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 =head2 to_html
35  
36 Takes Markdown text and returns HTML. Options (a hash reference) are passed
37 to L<App::Dapper::Components/expand>.
38  
39 =cut
40  
41 326 ▶sub to_html {
Subroutine to_html: called CC=5 SCAR=16.1
Pod documented
42 326 my ($text, $options) = @_;
43  
44 326 ▶ return '' unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 1 325 326
45 325 ▶ $options ||= {};
Condition100%2/2
$options ||= {}
l!l
122203
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
46  
47 325 my (@blocks, @fences);
48  
49 # Placeholders are made from a word that is not in the text, so the text
50 # can't be mistaken for one
51 325 my $tag = 'DAPPERBLOCK';
52 325 ▶ $tag .= 'Q' while index($text, $tag) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
index($text, $tag) != -1 and do { $tag .= 'Q' } 0 325 325
53 325 my ($code_tag, $component_tag) = ("${tag}C", "${tag}K");
54  
55 # Replace each fenced code block with a placeholder paragraph, so that
56 # Markdown leaves its contents alone, and put the HTML back afterwards
57 325 $text =~ s{
58 ^([ ]{0,3})((?>`{3,}|~{3,}))[ \t]*([\w+#.-]*)[^\n]*\n # opening fence and language
59 (.*?) # code
60 ^[ ]{0,3}\2[`~]*[ \t]*$ # closing fence
61 }{
62 64 push @fences, $&;
63 64 push @blocks, code_block($4, $3);
64 64 "\n\n$code_tag" . $#blocks . "\n\n";
65 }gemsx;
66  
67 # Components (::: note, ::: youtube, ...); the Markdown inside them is
68 # converted with its code blocks put back
69 325 my $components = [];
70 325 ▶ if ($text =~ /^[ ]{0,3}:{3,}[ \t]*[A-Za-z]/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^[ ]{0,3}:{3,}[ \t]*[A-Za-z]/m) 180 145 325
71 83 ▶ ($text, $components) = App::Dapper::Components::expand($text, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
72 83 my ($body, $opts) = @_;
73 83 $body =~ s{^\Q$code_tag\E(\d+)$}{$fences[$1]}gm;
74 83 return to_html($body, { %$opts, placeholder => undef });
75 180 }, { %$options, placeholder => $component_tag });
76 }
77  
78 324 my $html = Text::MultiMarkdown::markdown($text);
79  
80 324 $html =~ s{<p>\Q$component_tag\E(\d+)</p>}{$components->[$1]}g;
81 324 $html =~ s{\Q$component_tag\E(\d+)}{$components->[$1]}g;
82  
83 324 $html =~ s{<p>\Q$code_tag\E(\d+)</p>}{$blocks[$1]}g;
84 324 $html =~ s{\Q$code_tag\E(\d+)}{$blocks[$1]}g;
85  
86 324 return $html;
87 }
88  
89 =head2 code_block
90  
91 Takes the code and (optionally) its language, and returns the HTML for a code
92 block.
93  
94 =cut
95  
96 74 ▶sub code_block {
Subroutine code_block: called CC=4 SCAR=13.9
Pod documented
97 74 my ($code, $language) = @_;
98  
99 74 ▶ $code = '' unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 1 73 74
100 74 $code =~ s/&/&amp;/g;
101 74 $code =~ s/</&lt;/g;
102 74 $code =~ s/>/&gt;/g;
103  
104 74 ▶ my $class = defined $language && length $language ? qq{ class="language-$language"} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $language && length $language ? : 26 48 74
Condition100%3/3
defined $language and length $language
!ll&&!rl&&r
64226
MC/DC100%2/2
defined $language and length $language
defined $language length $language
Truth table100%3/3
A and B
A= defined $languageB= length $language
ABresult
0X0
100
111
105  
106 74 return "<pre><code$class>$code</code></pre>";
107 }
108  
109 190 1;
« bin/dapper ↑ Summary lib/App/Dapper/Filters.pm »