Markdown/Perl/Util.pm

filter: Statementstmt 99.0% 104 / 105 Branchbran 93.3% 28 / 30 Conditioncond 100% 17 / 17 MC/DCmcdc 100% 9 / 9 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 8 totaltotal 94.1% 177 / 188 CC 26 SCAR 18.9
CC
26
Cov
98%
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 2 ▶sub split_while : prototype(&@) { ## no critic (RequireArgUnpacking)
Subroutine split_while: called CC=2 SCAR=6.9
Pod undocumented
24 2 my $test = shift;
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 53149 ▶sub remove_prefix_spaces {
Subroutine remove_prefix_spaces: called CC=15 SCAR=27.1
Pod undocumented
37 53149 my ($n, $text, $preserve_tabs) = @_;
38 53149 ▶ $preserve_tabs //= 1; # when not specified we do preserve tabs
Condition100%2/2
$preserve_tabs //= 1
l!l
20252947
MC/DC100%1/1
$preserve_tabs //= 1
$preserve_tabs
Truth table100%2/2
A
A= $preserve_tabs
Aresult
11
00
39 53149 ▶ if (!$preserve_tabs) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($preserve_tabs) 38 53111 53149
40 38 my $s = indent_size($text); # this sets pos($text);
41 38 my $ret = (' ' x max(0, $s - $n)).(substr $text, pos($text));
42 38 ▶ return $ret unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 38 0 38
43 0 return ($ret, min($s, $n));
44 }
45 53111 my $t = int($n / 4);
46 53111 my $s = $n % 4;
47 53111 my $m = 0; # How many spaces we have matched.
48 53111 for my $i (1 .. $t) {
49 2297 ▶ if ($text =~ m/^( {0,3}\t| {4})/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^( {0,3}\t| {4})/u) { } 2112 185 2297
50 # We remove one full tab-stop from the string.
51 2112 substr $text, 0, length($1), '';
52 2112 $m += 4;
53 } else {
54 # We didn’t have a full tab-stop, so we remove as many spaces as we had.
55 185 ▶ $text =~ m/^( {0,3})/ or confess 'Unexpected match failure';
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /^( {0,3})/u or confess("Unexpected match failure") 185 0 185
56 185 $m += $LAST_MATCH_END[0] - $LAST_MATCH_START[0];
57 185 ▶ return substr $text, length($1) unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 158 27 185
58 27 return ((substr $text, length($1)), $m);
59 }
60 }
61 52926 ▶ if ($s != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s != 0) 30002 22924 52926
62 30002 $text =~ m/^(?<p>\ {0,3}\t|\ {4})*?(?<l>\ {0,3}\t|\ {4})?(?<s>\ {0,3})(?<e>[^ \t].*|$)/xs; ## no critic (ProhibitComplexRegexes)
63 30002 my $ns = length $+{s};
64 30002 ▶ if ($ns >= $s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ns >= $s) { } 16493 13509 30002
elsif (length $+{'l'}) { } 823 12686 13509
65 16493 ▶ $text = ($+{p} // '').($+{l} // '').(' ' x ($ns - $s)).$+{e};
Condition100%4/4
$+{"p"} // ""
l!l
416489
$+{"l"} // ""
l!l
27816215
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 16493 $m += $s;
67 } elsif (length($+{l})) {
68 823 ▶ $text = ($+{p} // '').(' ' x (4 + $ns - $s)).$+{e};
Condition100%2/2
$+{"p"} // ""
l!l
64759
MC/DC100%1/1
$+{"p"} // ""
$+{"p"}
Truth table100%2/2
A
A= $+{"p"}
Aresult
11
00
69 823 $m += $s;
70 } else {
71 12686 $text = $+{e};
72 12686 $m += $ns;
73 }
74 }
75 52926 ▶ return $text unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 39371 13555 52926
76 13555 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 47720 ▶sub indent_size { ## no critic (RequireArgUnpacking)
Subroutine indent_size: called CC=3 SCAR=11.0
Pod undocumented
88 47720 pos($_[0]) = 0;
89 47720 my $t = () = $_[0] =~ m/\G( {0,3}\t| {4})/gc; # Forcing list context.
90 47720 $_[0] =~ m/\G( *)/g;
91 47720 my $s = length($1); ## no critic (ProhibitCaptureWithoutTest)
92 47720 ▶ if (substr($_[0], 0, 1) eq "\t" && @_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($_[0], 0, 1) eq "\t" and @_ > 1) 59 47661 47720
Condition100%3/3
substr($_[0], 0, 1) eq "\t" and @_ > 1
!ll&&!rl&&r
4750515659
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 59 $s -= $_[1] % 4;
94 }
95 47720 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 5 ▶sub horizontal_size {
Subroutine horizontal_size: called CC=2 SCAR=6.9
Pod undocumented
101 5 my ($text) = @_;
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 6 ▶sub indented_one_tab {
Subroutine indented_one_tab: called CC=1 SCAR=0.0
Pod undocumented
109 6 return indented(4, $_[0]);
110 }
111  
112 16086 ▶sub indented {
Subroutine indented: called CC=4 SCAR=13.9
Pod undocumented
113 16086 my ($n, $text) = @_;
114 16086 my $t = int($n / 4);
115 16086 my $s = $n % 4;
116 16086 for my $i (1 .. $t) {
117 16079 ▶ return unless $text =~ m/\G(?: {0,3}\t| {4})/g;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\G(?: {0,3}\t| {4})/gu 15386 693 16079
118 }
119 700 ▶ return 1 if $text =~ m/\G(?: {$s}| *\t)/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\G(?: {$s}| *\t)/u 270 430 700
120 430 return;
121 }
122  
123 # Performs the normalization described in:
124 # https://spec.commonmark.org/0.31.2/#matches
125 19240 ▶sub normalize_label {
Subroutine normalize_label: called CC=2 SCAR=6.9
Pod undocumented
126 19240 my ($label) = @_;
127 19240 ▶ $label = fc($label) // ''; # fc returns undef for empty label.
Condition100%2/2
&Markdown::Perl::Util::fc($label) // ""
l!l
1922416
MC/DC100%1/1
&Markdown::Perl::Util::fc($label) // ""
&Markdown::Perl::Util::fc($label)
Truth table100%2/2
A
A= &Markdown::Perl::Util::fc($label)
Aresult
11
00
128 19240 $label =~ s/^[ \t\n]+|[ \t\n]+$//g;
129 19240 $label =~ s/[ \t\n]+|[\t\n]/ /g;
130 19240 return $label;
131 }
132  
133 # Convert tabs to space in the given string. Assuming $prefix horizontal spaces
134 # before the string.
135 233 ▶sub tabs_to_space { ## no critic (RequireArgUnpacking)
Subroutine tabs_to_space: called CC=4 SCAR=13.9
Pod undocumented
136 233 my ($str, $prefix) = @_;
137 233 ▶ $prefix //= 0;
Condition100%2/2
$prefix //= 0
l!l
2294
MC/DC100%1/1
$prefix //= 0
$prefix
Truth table100%2/2
A
A= $prefix
Aresult
11
00
138 233 ▶ while ($str =~ m/\G[^\t]*\t/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\G[^\t]*\t/gu) 141 233 374
139 141 $prefix += $LAST_MATCH_END[0] - $LAST_MATCH_START[0] - 1;
140 141 my $nb_space = 4 - $prefix % 4;
141 141 substr $str, $LAST_MATCH_END[0] - 1, 1, ' ' x $nb_space;
142 141 pos($str) = $LAST_MATCH_END[0] - 1 + $nb_space;
143 141 $prefix = 0; # By definition we are now aligned with a tab stop.
144 }
145 233 ▶ return $str if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 4 229 233
146 229 $_[0] = $str;
147 229 return;
148 }
149  
150 32 1;
« Markdown/Perl/Options.pm ↑ Summary Markdown/Perl/HTML.pm »