Markdown/Perl/Util.pm

filter: Statementstmt 99.0% 104 / 105 Branchbran 93.3% 28 / 30 Conditioncond 94.1% 16 / 17 MC/DCmcdc 88.8% 8 / 9 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 8 totaltotal 93.0% 175 / 188 CC 26 SCAR 18.9
CC
26
Cov
97%
remove_prefix_spaces
15.0
indented
4.0
tabs_to_space
4.0
CRAP 6.6
« Markdown/Perl/Options.pm ↑ Summary Markdown/Perl/HTML.pm »
1 package Markdown::Perl::Util;
2  
3 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 32 ▶use feature ':5.24';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 32 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 32 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 32 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 32 ▶use List::MoreUtils 'first_index';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 32 ▶use List::Util 'max', 'min';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 32 ▶use Unicode::CaseFold 'fc';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 32 our $VERSION = 0.01;
16  
17 32 our @EXPORT_OK =
18 qw(split_while remove_prefix_spaces indent_size indented_one_tab horizontal_size normalize_label indented tabs_to_space);
19 32 our %EXPORT_TAGS = (all => \@EXPORT_OK);
20  
21 # Partition a list into a continuous chunk for which the given code evaluates to
22 # true, and the rest of the list. Returns a list of two array-ref.
23 sub split_while : prototype(&@) { ## no critic (RequireArgUnpacking)
24 2 ▶ my $test = shift;
Subroutine split_while: called CC=2 SCAR=6.9
Pod undocumented
25 2 ▶ my $i = first_index { !$test->($_) } @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
26 2 ▶ return (\@_, []) if $i < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i < 0 1 1 2
27 1 my @pass = splice(@_, 0, $i);
28 1 return (\@pass, \@_);
29 }
30  
31 # Removes the equivalent of n spaces at the beginning of the line. Tabs are
32 # matched to a tab-stop of size 4.
33 # Removes all the spaces if there is less than that.
34 # If needed, tabs are converted into 4 spaces.
35 # In list context, also returns how many spaces were actually matched.
36 sub remove_prefix_spaces {
37 53216 ▶ my ($n, $text, $preserve_tabs) = @_;
Subroutine remove_prefix_spaces: called CC=15 SCAR=27.1
Pod undocumented
38 53216 ▶ $preserve_tabs //= 1; # when not specified we do preserve tabs
Condition100%2/2
$preserve_tabs //= 1
l!l
20853008
MC/DC100%1/1
$preserve_tabs //= 1
$preserve_tabs
Truth table100%2/2
A
A= $preserve_tabs
Aresult
11
00
39 53216 ▶ if (!$preserve_tabs) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($preserve_tabs) 41 53175 53216
40 41 my $s = indent_size($text); # this sets pos($text);
41 41 my $ret = (' ' x max(0, $s - $n)).(substr $text, pos($text));
42 41 ▶ return $ret unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 41 0 41
43 0 return ($ret, min($s, $n));
44 }
45 53175 my $t = int($n / 4);
46 53175 my $s = $n % 4;
47 53175 my $m = 0; # How many spaces we have matched.
48 53175 for my $i (1 .. $t) {
49 2246 ▶ if ($text =~ m/^( {0,3}\t| {4})/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^( {0,3}\t| {4})/u) { } 2064 182 2246
50 # We remove one full tab-stop from the string.
51 2064 substr $text, 0, length($1), '';
52 2064 $m += 4;
53 } else {
54 # We didn’t have a full tab-stop, so we remove as many spaces as we had.
55 182 ▶ $text =~ m/^( {0,3})/ or confess 'Unexpected match failure';
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /^( {0,3})/u or confess("Unexpected match failure") 0 182 182
56 182 $m += $LAST_MATCH_END[0] - $LAST_MATCH_START[0];
57 182 ▶ return substr $text, length($1) unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 164 18 182
58 18 return ((substr $text, length($1)), $m);
59 }
60 }
61 52993 ▶ if ($s != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s != 0) 29782 23211 52993
62 29782 $text =~ m/^(?<p>\ {0,3}\t|\ {4})*?(?<l>\ {0,3}\t|\ {4})?(?<s>\ {0,3})(?<e>[^ \t].*|$)/xs; ## no critic (ProhibitComplexRegexes)
63 29782 my $ns = length $+{s};
64 29782 ▶ if ($ns >= $s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ns >= $s) { } 16596 13186 29782
elsif (length $+{'l'}) { } 851 12335 13186
65 16596 ▶ $text = ($+{p} // '').($+{l} // '').(' ' x ($ns - $s)).$+{e};
Condition100%4/4
$+{"p"} // ""
l!l
716589
$+{"l"} // ""
l!l
25916337
MC/DC100%1/1
$+{"p"} // ""
$+{"p"}
MC/DC100%1/1
$+{"l"} // ""
$+{"l"}
Truth table100%2/2
A
A= $+{"p"}
Aresult
11
00
Truth table100%2/2
A
A= $+{"l"}
Aresult
11
00
66 16596 $m += $s;
67 } elsif (length($+{l})) {
68 851 ▶ $text = ($+{p} // '').(' ' x (4 + $ns - $s)).$+{e};
Condition100%2/2
$+{"p"} // ""
l!l
92759
MC/DC100%1/1
$+{"p"} // ""
$+{"p"}
Truth table100%2/2
A
A= $+{"p"}
Aresult
11
00
69 851 $m += $s;
70 } else {
71 12335 $text = $+{e};
72 12335 $m += $ns;
73 }
74 }
75 52993 ▶ return $text unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 39621 13372 52993
76 13372 return ($text, $m);
77 }
78  
79 # Return the indentation of the given text
80 # indent_size($str, $prev_indent)
81 #
82 # Sets pos($_[0]) to the first non-whitespace character.
83 # $prev_indent can be passed if the $str is not the beginning of the logical
84 # line, to properly compute the tab stops.
85 # TODO: this feature is used when parsing list_items, but could be used in many
86 # other places too.
87 sub indent_size { ## no critic (RequireArgUnpacking)
88 47532 ▶ pos($_[0]) = 0;
Subroutine indent_size: called CC=3 SCAR=11.0
Pod undocumented
89 47532 my $t = () = $_[0] =~ m/\G( {0,3}\t| {4})/gc; # Forcing list context.
90 47532 $_[0] =~ m/\G( *)/g;
91 47532 my $s = length($1); ## no critic (ProhibitCaptureWithoutTest)
92 47532 ▶ if (substr($_[0], 0, 1) eq "\t" && @_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($_[0], 0, 1) eq "\t" and @_ > 1) 43 47489 47532
Condition100%3/3
substr($_[0], 0, 1) eq "\t" and @_ > 1
!ll&&!rl&&r
474048543
MC/DC100%2/2
substr($_[0], 0, 1) eq "\t" and @_ > 1
substr($_[0], 0, 1) eq "\t" @_ > 1
Truth table100%3/3
A and B
A= substr($_[0], 0, 1) eq "\t"B= @_ > 1
ABresult
0X0
100
111
93 43 $s -= $_[1] % 4;
94 }
95 47532 return $t * 4 + $s;
96 }
97  
98 # Compute the horizontal size of a given string (similar to indent_size, but
99 # all characters count, not just tabs and space).
100 sub horizontal_size {
101 5 ▶ my ($text) = @_;
Subroutine horizontal_size: called CC=2 SCAR=6.9
Pod undocumented
102 5 my $t = () = $text =~ m/\G([^\t]{0,3}\t|[^\t]{4})/gc; # Forcing list context.
103 5 ▶ my $s = length($text) - (pos($text) // 0);
Condition100%2/2
pos $text // 0
l!l
41
MC/DC100%1/1
pos $text // 0
pos $text
Truth table100%2/2
A
A= pos $text
Aresult
11
00
104 5 return $t * 4 + $s;
105 }
106  
107 # Returns true if the text is indented by at least one tab-stop.
108 sub indented_one_tab {
109 6 ▶ return indented(4, $_[0]);
Subroutine indented_one_tab: called CC=1 SCAR=0.0
Pod undocumented
110 }
111  
112 sub indented {
113 16077 ▶ my ($n, $text) = @_;
Subroutine indented: called CC=4 SCAR=13.9
Pod undocumented
114 16077 my $t = int($n / 4);
115 16077 my $s = $n % 4;
116 16077 for my $i (1 .. $t) {
117 16070 ▶ return unless $text =~ m/\G(?: {0,3}\t| {4})/g;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\G(?: {0,3}\t| {4})/gu 15326 744 16070
118 }
119 751 ▶ return 1 if $text =~ m/\G(?: {$s}| *\t)/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\G(?: {$s}| *\t)/u 280 471 751
120 471 return;
121 }
122  
123 # Performs the normalization described in:
124 # https://spec.commonmark.org/0.31.2/#matches
125 sub normalize_label {
126 19001 ▶ my ($label) = @_;
Subroutine normalize_label: called CC=2 SCAR=7.0
Pod undocumented
127 19001 ▶ $label = fc($label) // ''; # fc returns undef for empty label.
Condition50%1/2
&Markdown::Perl::Util::fc($label) // ""
l!l
190010
MC/DC0%0/1
&Markdown::Perl::Util::fc($label) // ""
&Markdown::Perl::Util::fc($label)
Truth table50%1/2
A
A= &Markdown::Perl::Util::fc($label)
Aresult
11
00
128 19001 $label =~ s/^[ \t\n]+|[ \t\n]+$//g;
129 19001 $label =~ s/[ \t\n]+|[\t\n]/ /g;
130 19001 return $label;
131 }
132  
133 # Convert tabs to space in the given string. Assuming $prefix horizontal spaces
134 # before the string.
135 sub tabs_to_space { ## no critic (RequireArgUnpacking)
136 219 ▶ my ($str, $prefix) = @_;
Subroutine tabs_to_space: called CC=4 SCAR=13.9
Pod undocumented
137 219 ▶ $prefix //= 0;
Condition100%2/2
$prefix //= 0
l!l
2154
MC/DC100%1/1
$prefix //= 0
$prefix
Truth table100%2/2
A
A= $prefix
Aresult
11
00
138 219 ▶ while ($str =~ m/\G[^\t]*\t/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\G[^\t]*\t/gu) 120 219 339
139 120 $prefix += $LAST_MATCH_END[0] - $LAST_MATCH_START[0] - 1;
140 120 my $nb_space = 4 - $prefix % 4;
141 120 substr $str, $LAST_MATCH_END[0] - 1, 1, ' ' x $nb_space;
142 120 pos($str) = $LAST_MATCH_END[0] - 1 + $nb_space;
143 120 $prefix = 0; # By definition we are now aligned with a tab stop.
144 }
145 219 ▶ return $str if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 4 215 219
146 215 $_[0] = $str;
147 215 return;
148 }
149  
150 32 1;
« Markdown/Perl/Options.pm ↑ Summary Markdown/Perl/HTML.pm »