Debian Codegraph @

lib/Markdown/Perl/Util.pm

raw
1 package Markdown::Perl::Util;
2
3 use strict;
4 use warnings;
5 use utf8;
6 use feature ':5.24';
7
8 use Carp;
9 use English;
10 use Exporter 'import';
11 use List::MoreUtils 'first_index';
12 use List::Util 'max', 'min';
13 use Unicode::CaseFold 'fc';
14
15 our $VERSION = 0.01;
16
17 our @EXPORT_OK =
18 qw(split_while remove_prefix_spaces indent_size indented_one_tab horizontal_size normalize_label indented tabs_to_space);
19 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 my $test = shift;
25 my $i = first_index { !$test->($_) } @_;
26 return (\@_, []) if $i < 0;
27 my @pass = splice(@_, 0, $i);
28 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 my ($n, $text, $preserve_tabs) = @_;
38 $preserve_tabs //= 1; # when not specified we do preserve tabs
39 if (!$preserve_tabs) {
40 my $s = indent_size($text); # this sets pos($text);
41 my $ret = (' ' x max(0, $s - $n)).(substr $text, pos($text));
42 return $ret unless wantarray;
43 return ($ret, min($s, $n));
44 }
45 my $t = int($n / 4);
46 my $s = $n % 4;
47 my $m = 0; # How many spaces we have matched.
48 for my $i (1 .. $t) {
49 if ($text =~ m/^( {0,3}\t| {4})/) {
50 # We remove one full tab-stop from the string.
51 substr $text, 0, length($1), '';
52 $m += 4;
53 } else {
54 # We didn’t have a full tab-stop, so we remove as many spaces as we had.
55 $text =~ m/^( {0,3})/ or confess 'Unexpected match failure';
56 $m += $LAST_MATCH_END[0] - $LAST_MATCH_START[0];
57 return substr $text, length($1) unless wantarray;
58 return ((substr $text, length($1)), $m);
59 }
60 }
61 if ($s != 0) {
62 $text =~ m/^(?<p>\ {0,3}\t|\ {4})*?(?<l>\ {0,3}\t|\ {4})?(?<s>\ {0,3})(?<e>[^ \t].*|$)/xs; ## no critic (ProhibitComplexRegexes)
63 my $ns = length $+{s};
64 if ($ns >= $s) {
65 $text = ($+{p} // '').($+{l} // '').(' ' x ($ns - $s)).$+{e};
66 $m += $s;
67 } elsif (length($+{l})) {
68 $text = ($+{p} // '').(' ' x (4 + $ns - $s)).$+{e};
69 $m += $s;
70 } else {
71 $text = $+{e};
72 $m += $ns;
73 }
74 }
75 return $text unless wantarray;
76 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 pos($_[0]) = 0;
89 my $t = () = $_[0] =~ m/\G( {0,3}\t| {4})/gc; # Forcing list context.
90 $_[0] =~ m/\G( *)/g;
91 my $s = length($1); ## no critic (ProhibitCaptureWithoutTest)
92 if (substr($_[0], 0, 1) eq "\t" && @_ > 1) {
93 $s -= $_[1] % 4;
94 }
95 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 my ($text) = @_;
102 my $t = () = $text =~ m/\G([^\t]{0,3}\t|[^\t]{4})/gc; # Forcing list context.
103 my $s = length($text) - (pos($text) // 0);
104 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 return indented(4, $_[0]);
110 }
111
112 sub indented {
113 my ($n, $text) = @_;
114 my $t = int($n / 4);
115 my $s = $n % 4;
116 for my $i (1 .. $t) {
117 return unless $text =~ m/\G(?: {0,3}\t| {4})/g;
118 }
119 return 1 if $text =~ m/\G(?: {$s}| *\t)/;
120 return;
121 }
122
123 # Performs the normalization described in:
124 # https://spec.commonmark.org/0.31.2/#matches
125 sub normalize_label {
126 my ($label) = @_;
127 $label = fc($label) // ''; # fc returns undef for empty label.
128 $label =~ s/^[ \t\n]+|[ \t\n]+$//g;
129 $label =~ s/[ \t\n]+|[\t\n]/ /g;
130 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 my ($str, $prefix) = @_;
137 $prefix //= 0;
138 while ($str =~ m/\G[^\t]*\t/g) {
139 $prefix += $LAST_MATCH_END[0] - $LAST_MATCH_START[0] - 1;
140 my $nb_space = 4 - $prefix % 4;
141 substr $str, $LAST_MATCH_END[0] - 1, 1, ' ' x $nb_space;
142 pos($str) = $LAST_MATCH_END[0] - 1 + $nb_space;
143 $prefix = 0; # By definition we are now aligned with a tab stop.
144 }
145 return $str if defined wantarray;
146 $_[0] = $str;
147 return;
148 }
149
150 1;