| 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
| |||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$preserve_tabs //= 1
$preserve_tabs
Truth table100%2/2
A
A= $preserve_tabs
| |||||||||||||||||||||||||||||
| 39 | 53149 | ▶ | if (!$preserve_tabs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 56 | 185 | $m += $LAST_MATCH_END[0] - $LAST_MATCH_START[0]; | |||||||||||||||||||||||||||
| 57 | 185 | ▶ | return substr $text, length($1) unless wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 27 | return ((substr $text, length($1)), $m); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 52926 | ▶ | if ($s != 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 65 | 16493 | ▶ | $text = ($+{p} // '').($+{l} // '').(' ' x ($ns - $s)).$+{e}; | ||||||||||||||||||||||||||
|
Condition100%4/4
$+{"p"} // ""
$+{"l"} // ""
MC/DC100%1/1
$+{"p"} // ""
$+{"p"}
MC/DC100%1/1
$+{"l"} // ""
$+{"l"}
Truth table100%2/2
A
A= $+{"p"}
Truth table100%2/2
A
A= $+{"l"}
| |||||||||||||||||||||||||||||
| 66 | 16493 | $m += $s; | |||||||||||||||||||||||||||
| 67 | } elsif (length($+{l})) { | ||||||||||||||||||||||||||||
| 68 | 823 | ▶ | $text = ($+{p} // '').(' ' x (4 + $ns - $s)).$+{e}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$+{"p"} // ""
MC/DC100%1/1
$+{"p"} // ""
$+{"p"}
Truth table100%2/2
A
A= $+{"p"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
substr($_[0], 0, 1) eq "\t" and @_ > 1
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
| |||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
pos $text // 0
pos $text
Truth table100%2/2
A
A= pos $text
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 700 | ▶ | return 1 if $text =~ m/\G(?: {$s}| *\t)/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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) // ""
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)
| |||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$prefix //= 0
$prefix
Truth table100%2/2
A
A= $prefix
| |||||||||||||||||||||||||||||
| 138 | 233 | ▶ | while ($str =~ m/\G[^\t]*\t/g) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 146 | 229 | $_[0] = $str; | |||||||||||||||||||||||||||
| 147 | 229 | return; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 32 | 1; | |||||||||||||||||||||||||||