Markdown/Perl.pm

filter: Statementstmt 97.8% 185 / 189 Branchbran 88.4% 69 / 78 Conditioncond 91.6% 22 / 24 MC/DCmcdc 85.7% 12 / 14 Subroutinesub 100% 28 / 28 Podpod 100% 5 / 5 totaltotal 94.9% 321 / 338 CC 50 SCAR 30.4
CC
50
Cov
95%
_emit_html
39.1
set_hooks
5.3
_get_this_and_args
5.1
CRAP 20.9
↑ Summary Markdown/Perl/BlockParser.pm »
1 package Markdown::Perl;
2  
3 31 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 31 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 31 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 31 ▶use feature ':5.24';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 31 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 31 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 31 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 31 ▶use Hash::Util 'lock_keys';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 31 ▶use List::Util 'none';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 31 ▶use List::MoreUtils 'pairwise';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 31 ▶use Markdown::Perl::BlockParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 31 ▶use Markdown::Perl::Inlines;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 31 ▶use Markdown::Perl::HTML 'html_escape', 'decode_entities', 'parse_attributes';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 31 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 31 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 31 ▶use parent 'Markdown::Perl::Options';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 31 our $VERSION = '1.14'; # remember to bump in App::pmarkdown too.
23  
24 31 our @EXPORT_OK = qw(convert set_options set_mode set_hooks);
25 31 our %EXPORT_TAGS = (all => \@EXPORT_OK);
26  
27 47 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
28 47 my ($class, @options) = @_;
29  
30 47 my $this = $class->SUPER::new(
31 mode => undef,
32 options => {},
33 local_options => {},
34 hooks => {});
35 47 $this->SUPER::set_options(options => @options);
36 47 lock_keys(%{$this});
37  
38 47 return $this;
39 }
40  
41 1 ▶sub set_options {
Subroutine set_options: called CC=1 SCAR=0.0
Pod documented
42 1 my ($this, @options) = &_get_this_and_args; ## no critic (ProhibitAmpersandSigils)
43 1 $this->SUPER::set_options(options => @options);
44 1 return;
45 }
46  
47 2 ▶sub set_mode {
Subroutine set_mode: called CC=1 SCAR=0.0
Pod documented
48 2 my ($this, $mode) = &_get_this_and_args; ## no critic (ProhibitAmpersandSigils)
49 2 $this->SUPER::set_mode(options => $mode);
50 2 return;
51 }
52  
53 31 Readonly::Array my @VALID_HOOKS => qw(resolve_link_ref yaml_metadata);
54  
55 9 ▶sub set_hooks {
Subroutine set_hooks: called CC=5 SCAR=16.7
Pod documented
56 9 my ($this, %hooks) = &_get_this_and_args; ## no critic (ProhibitAmpersandSigils)
57 9 ▶ while (my ($k, $v) = each %hooks) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %hooks) 9 9 18
58 9 ▶ if (none { $_ eq $k } @VALID_HOOKS) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 3 branches
BranchTrueFalseTotal
if (&none(sub { $_ eq $k; } , @VALID_HOOKS)) { } 0 9 9
elsif (not defined $v) { } 1 8 9
elsif (ref $v ne 'CODE') { } 0 8 8
59 0 croak "Invalid hook name: ${k}";
60 } elsif (!defined $v) {
61 1 delete $this->{hooks}{$k};
62 } elsif (ref $v ne 'CODE') {
63 0 carp 'Hook target must be a CODE reference';
64 } else {
65 8 $this->{hooks}{$k} = $v;
66 }
67 }
68 9 return;
69 }
70  
71 # Returns @_, unless the first argument is not blessed as a Markdown::Perl
72 # object, in which case it returns a default object.
73 31 my $default_this = Markdown::Perl->new();
74  
75 35159 ▶sub _get_this_and_args { ## no critic (RequireArgUnpacking)
Subroutine _get_this_and_args: called CC=5 SCAR=16.2
76 35159 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 35159 35159
77 # We could use `$this isa Markdown::Perl` that does not require to test
78 # blessedness first. However this requires 5.31.6 which is not in Debian
79 # stable as of writing this.
80 35159 ▶ if (!blessed($_[0]) || !$_[0]->isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)) 206 34953 35159
Condition66%2/3
builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
206034953
MC/DC50%1/2
builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa(__PACKAGE__)
ABresult
0X0
100
111
81 206 unshift @_, $default_this;
82 }
83 35159 ▶ return @_ if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 17584 17575 35159
84 17575 return;
85 }
86  
87 # Takes a string and converts it to HTML. Can be called as a free function or as
88 # class method. In the latter case, provided options override those set in the
89 # class constructor.
90 # Both the input and output are unicode strings.
91 17575 ▶sub convert { ## no critic (RequireArgUnpacking)
Subroutine convert: called CC=1 SCAR=0.0
Pod documented
92 17575 &_get_this_and_args; ## no critic (ProhibitAmpersandSigils)
93 17575 my $this = shift @_;
94 17575 my $md = \(shift @_); # Taking a reference to avoid copying the input. is it useful?
95 17575 $this->SUPER::set_options(local_options => @_);
96  
97 # TODO: introduce an HtmlRenderer object that carries the $linkrefs states
98 # around (instead of having to pass it in all the calls).
99 17572 my ($blocks, $linkrefs) = $this->_parse($md);
100 17571 my $out = $this->_emit_html(0, 'root', $linkrefs, @{$blocks});
101 17571 $this->{local_options} = {};
102 17571 return $out;
103 }
104  
105 # This is an internal call for now because the structure of the parse tree is
106 # not defined.
107 # Note that while convert() takes care not to copy the md argument, this is not
108 # the case of this method, however, it can receive a scalar ref instead of a
109 # scalar, to avoid the copy.
110 # TODO: create a BlockTree class and document it, then make this be public.
111 17572 ▶sub _parse {
Subroutine _parse: called CC=3 SCAR=11.8
112 17572 my ($this, $md_or_ref) = &_get_this_and_args; ## no critic (ProhibitAmpersandSigils)
113 17572 ▶ my $md = ref($md_or_ref) ? $md_or_ref : \$md_or_ref;
Branch: 1 branch
BranchTrueFalseTotal
ref $md_or_ref ? : 17572 0 17572
114  
115 17572 my $parser = Markdown::Perl::BlockParser->new($this, $md);
116 17572 my ($linkrefs, $blocks) = $parser->process();
117 17571 ▶ return ($blocks, $linkrefs) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 17571 0 17571
118 0 return $blocks;
119 }
120  
121 44053 ▶sub _render_inlines {
Subroutine _render_inlines: called CC=1 SCAR=0.0
122 44053 my ($this, $linkrefs, @lines) = @_;
123 44053 return Markdown::Perl::Inlines::render($this, $linkrefs, @lines);
124 }
125  
126 # TODO: move this to a separate package and split the method in smaller chunks.
127 43522 ▶sub _emit_html { ## no critic (ProhibitExcessComplexity)
Subroutine _emit_html: called CC=39 SCAR=36.7
128 43522 my ($this, $tight_block, $parent_type, $linkrefs, @blocks) = @_;
129 43522 my $out = '';
130 43522 my $block_index = 0;
131 43522 for my $bl (@blocks) {
132 75289 $block_index++;
133 75289 ▶ if ($bl->{type} eq 'break') {
Branch: 9 branches
BranchTrueFalseTotal
if ($bl->{'type'} eq 'break') { } 250 75039 75289
elsif ($bl->{'type'} eq 'heading') { } 1032 74007 75039
elsif ($bl->{'type'} eq 'code') { } 4870 69137 74007
elsif ($bl->{'type'} eq 'html') { } 2656 66481 69137
elsif ($bl->{'type'} eq 'paragraph') { } 42994 23487 66481
elsif ($bl->{'type'} eq 'quotes') { } 9507 13980 23487
elsif ($bl->{'type'} eq 'list') { } 13900 80 13980
elsif ($bl->{'type'} eq 'table') { } 7 73 80
elsif ($bl->{'type'} eq 'directive') { } 73 0 73
134 250 $out .= "<hr />\n";
135 } elsif ($bl->{type} eq 'heading') {
136 1032 my $l = $bl->{level};
137 1032 my $c = $bl->{content};
138 1032 ▶ $c = $this->_render_inlines($linkrefs, ref $c eq 'ARRAY' ? @{$c} : $c);
Branch: 1 branch
BranchTrueFalseTotal
ref $c eq 'ARRAY' ? : 527 505 1032
139 1032 $c =~ s/^[ \t]+|[ \t]+$//g; # Only the setext headings spec asks for this, but this can’t hurt atx heading where this can’t change anything.
140 1032 $out .= "<h${l}>$c</h${l}>\n";
141 } elsif ($bl->{type} eq 'code') {
142 4870 my $c = $bl->{content};
143 4870 html_escape($c, $this->get_html_escaped_code_characters);
144 4870 my $i = '';
145 4870 ▶ if ($this->get_code_blocks_info eq 'language' && $bl->{info}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->get_code_blocks_info eq "language" and $bl->{"info"}) 3181 1689 4870
Condition66%2/3
$this->get_code_blocks_info eq "language" and $bl->{"info"}
!ll&&!rl&&r
016893181
MC/DC50%1/2
$this->get_code_blocks_info eq "language" and $bl->{"info"}
$this->get_code_blocks_info eq "language" $bl->{"info"}
Truth table66%2/3
A and B
A= $this->get_code_blocks_info eq "language"B= $bl->{"info"}
ABresult
0X0
100
111
146 3181 my $l = $bl->{info} =~ s/\s.*//r; # The spec does not really cover this behavior so we’re using Perl notion of whitespace here.
147 3181 decode_entities($l);
148 3181 html_escape($l, $this->get_html_escaped_characters);
149 3181 $i = " class=\"language-${l}\"";
150 }
151 4870 $out .= "<pre><code${i}>$c</code></pre>\n";
152 } elsif ($bl->{type} eq 'html') {
153 2656 $out .= $bl->{content};
154 } elsif ($bl->{type} eq 'paragraph') {
155 42994 my $html = '';
156 42994 ▶ if ((
Branch: 1 branch
BranchTrueFalseTotal
if ($this->get_allow_task_list_markers eq "list" and $parent_type eq "list" and $block_index == 1 or $this->get_allow_task_list_markers eq "always") 7908 35086 42994
Condition100%9/9
$this->get_allow_task_list_markers eq "list" and $parent_type eq "list" and $block_index == 1 or $this->get_allow_task_list_markers eq "always"
l!l&&r!l&&!r
7906235086
$this->get_allow_task_list_markers eq "list" and $parent_type eq "list" and $block_index == 1
!ll&&!rl&&r
35006827906
$this->get_allow_task_list_markers eq "list" and $parent_type eq "list"
!ll&&!rl&&r
19381156257988
MC/DC100%4/4
$this->get_allow_task_list_markers eq "list" and $parent_type eq "list" and $block_index == 1 or $this->get_allow_task_list_markers eq "always"
$this->get_allow_task_list_markers eq "list" $parent_type eq "list" $block_index == 1 $this->get_allow_task_list_markers eq "always"
Truth table71%5/7
A and B and C or D
A= $this->get_allow_task_list_markers eq "list"B= $parent_type eq "list"C= $block_index == 1D= $this->get_allow_task_list_markers eq "always"
ABCDresult
111X1
0XX11
10X11
11011
0XX00
10X00
11000
157 $this->get_allow_task_list_markers eq 'list'
158 && $parent_type eq 'list'
159 && $block_index == 1)
160 || $this->get_allow_task_list_markers eq 'always'
161 ) {
162 7908 ▶ if ($bl->{content}[0] =~ m/ ^ \s* \[ (?<marker> [ xX] ) \] (?<space> \s | $ ) /x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bl->{"content"}[0] =~ / ^ \s* \[ (?<marker> [ xX] ) \] (?<space> \s | $ ) /ux) 9 7899 7908
163 $html =
164 '<input '
165 .($LAST_PAREN_MATCH{marker} eq ' ' ? '' : 'checked="" ')
166 .'disabled="" type="checkbox">'
167 9 ▶ .($LAST_PAREN_MATCH{space} eq ' ' ? ' ' : "\n");
Branch: 2 branches
BranchTrueFalseTotal
$Markdown::Perl::LAST_PAREN_MATCH{'marker'} eq ' ' ? : 6 3 9
$Markdown::Perl::LAST_PAREN_MATCH{'space'} eq ' ' ? : 9 0 9
168 9 substr $bl->{content}[0], 0, $LAST_MATCH_END[0], '';
169 }
170 }
171 42994 $html .= $this->_render_inlines($linkrefs, @{$bl->{content}});
172 42994 ▶ if ($tight_block) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tight_block) { } 11994 31000 42994
elsif ($this->get_render_naked_paragraphs) { } 5 30995 31000
173 11994 $out .= $html;
174 } elsif ($this->get_render_naked_paragraphs) {
175 5 $out .= "${html}\n";
176 } else {
177 30995 $out .= "<p>${html}</p>\n";
178 }
179 } elsif ($bl->{type} eq 'quotes') {
180 9507 my $c = $this->_emit_html(0, 'quotes', $linkrefs, @{$bl->{content}});
181 9507 $out .= "<blockquote>\n${c}</blockquote>\n";
182 } elsif ($bl->{type} eq 'list') {
183 13900 my $type = $bl->{style}; # 'ol' or 'ul'
184 13900 my $start = '';
185 13900 my $num = $bl->{start_num};
186 13900 my $loose = $bl->{loose};
187 13900 ▶ $start = " start=\"${num}\"" if $type eq 'ol' && $num != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "ol" and $num != 1 20 13880 13900
Condition100%3/3
$type eq "ol" and $num != 1
!ll&&!rl&&r
7231664920
MC/DC100%2/2
$type eq "ol" and $num != 1
$type eq "ol" $num != 1
Truth table100%3/3
A and B
A= $type eq "ol"B= $num != 1
ABresult
0X0
100
111
188 $out .= "<${type}${start}>\n<li>"
189 .join("</li>\n<li>",
190 13900 map { $this->_emit_html(!$loose, 'list', $linkrefs, @{$_->{content}}) } @{$bl->{items}})
191 ."</li>\n</${type}>\n";
192 } elsif ($bl->{type} eq 'table') {
193 7 $out .= '<table><thead><tr>';
194 7 ▶ my @align = map { $_ ? " align=\"${_}\"" : '' } @{$bl->{content}{align}};
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 2 11 13
195 7 my @h = map { $this->_render_inlines($linkrefs, $_) } @{$bl->{content}{headers}};
196 7 ▶ $out .= join('', pairwise { "<th${a}>${b}</th>" } @align, @h);
Subroutine __ANON__: called CC=1 SCAR=0.0
197 7 $out .= '</tr></thead>';
198 7 ▶ if (@{$bl->{content}{table}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$bl->{"content"}{"table"};}) 6 1 7
199 6 $out .= '<tbody>';
200 6 my $ms = $this->get_table_blocks_have_cells_for_missing_data;
201 6 for my $l (@{$bl->{content}{table}}) {
202 9 $out .= '<tr>';
203 9 ▶ my @d = map { defined ? $this->_render_inlines($linkrefs, $_) : $ms ? '' : undef } @{$l};
Branch: 2 branches
BranchTrueFalseTotal
defined $_ ? : 14 2 16
$ms ? : 2 0 2
204 9 ▶ $out .= join('', pairwise { defined $b ? "<td${a}>${b}</td>" : '' } @align, @d);
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
defined $b ? : 16 0 16
205 9 $out .= '</tr>';
206 }
207 6 $out .= '</tbody>';
208 }
209 7 $out .= '</table>';
210 } elsif ($bl->{type} eq 'directive') {
211 73 my $c = $this->_emit_html(0, 'directive', $linkrefs, @{$bl->{content}});
212 73 ▶ my %attr = parse_attributes($bl->{attributes}) if defined $bl->{attributes}; ## no critic (ProhibitConditionalDeclaration)
Branch: 1 branch
BranchTrueFalseTotal
if defined $bl->{"attributes"} 11 62 73
213 73 my @attr = ('div');
214 73 ▶ push @attr, 'id="'.$attr{id}.'"' if exists $attr{id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attr{"id"} 4 69 73
215 73 ▶ unshift @{$attr{class}}, lc($bl->{name}) if defined $bl->{name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $bl->{"name"} 18 55 73
216 73 ▶ push @attr, 'class="'.join(' ', @{$attr{class}}).'"' if exists $attr{class};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attr{"class"} 52 21 73
217 73 ▶ push @attr, map { sprintf 'data-%s="%s"', @{$_} } @{$attr{keys}} if exists $attr{keys};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attr{"keys"} 2 71 73
218 73 my $tag = join(' ', @attr);
219 # TODO: the inline content is ignored for now
220 # TODO we should add a hook to process the directive in custom cases.
221 73 $out .= "<${tag}>\n${c}</div>\n";
222 73 ▶ if (defined $bl->{inline} && $this->get_warn_for_unused_input()) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bl->{"inline"} and $this->get_warn_for_unused_input) 1 72 73
Condition100%3/3
defined $bl->{"inline"} and $this->get_warn_for_unused_input
!ll&&!rl&&r
7111
MC/DC100%2/2
defined $bl->{"inline"} and $this->get_warn_for_unused_input
defined $bl->{"inline"} $this->get_warn_for_unused_input
Truth table100%3/3
A and B
A= defined $bl->{"inline"}B= $this->get_warn_for_unused_input
ABresult
0X0
100
111
223 1 carp 'Unused inline content in a directive block: '.$bl->{inline};
224 }
225 73 ▶ if (defined $attr{junk} && $this->get_warn_for_unused_input()) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attr{"junk"} and $this->get_warn_for_unused_input) 1 72 73
Condition100%3/3
defined $attr{"junk"} and $this->get_warn_for_unused_input
!ll&&!rl&&r
7021
MC/DC100%2/2
defined $attr{"junk"} and $this->get_warn_for_unused_input
defined $attr{"junk"} $this->get_warn_for_unused_input
Truth table100%3/3
A and B
A= defined $attr{"junk"}B= $this->get_warn_for_unused_input
ABresult
0X0
100
111
226 1 carp 'Unused attribute content in a directive block: '.$attr{junk};
227 }
228 } else {
229 0 confess 'Unexpected block type when rendering HTML output: '.$bl->{type};
230 }
231 }
232 # Note: a final new line should always be appended to $out. This is not
233 # guaranteed when the last element is HTML and the input file did not contain
234 # a final new line, unless the option force_final_new_line is set.
235 43522 return $out;
236 }
237  
238 31 1;
239  
240 __END__
↑ Summary Markdown/Perl/BlockParser.pm »