Highlight.pm

filter: Statementstmt 13.3% 28 / 210 Branchbran 0.0% 0 / 108 Conditioncond 0.0% 0 / 78 MC/DCmcdc 0.0% 0 / 44 Subroutinesub 29.1% 7 / 24 Podpod 66.6% 8 / 12 totaltotal 9.0% 43 / 476 CC 92 SCAR 46.1
CC
92
Cov
7%
markdown
342.0
interpreter
110.0
run
73.9
CRAP 100.8
↑ Summary Highlight/sourcehighlight.pm »
1 package Text::ANSI::Highlight;
2  
3 4 our $VERSION = "0.01";
4  
5 4 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # The highlighter which is used when none is given
13 4 our $HIGHLIGHTER = 'pandoc';
14  
15 # Short names of the highlighters
16 4 our %SHORT = (
17 gnu => 'sourcehighlight',
18 sky => 'skylighting',
19 );
20  
21 # A file larger than this is left as it is, 0 for no limit. The
22 # colored text is many times larger than the file, and takes time to
23 # show.
24 4 our $MAX_SIZE = 256 * 1024;
25  
26 # Other names of the languages: extensions of files, and the names
27 # people write after a fence. The highlighter is asked for the name
28 # as it is first, and then for the name here.
29 4 our %ALIAS = (
30 pl => 'perl', pm => 'perl', t => 'perl', psgi => 'perl',
31 sh => 'bash', shell => 'bash', ksh => 'bash', dash => 'bash',
32 bashrc => 'bash', profile => 'bash',
33 py => 'python', python3 => 'python',
34 rb => 'ruby',
35 js => 'javascript', mjs => 'javascript', cjs => 'javascript',
36 node => 'javascript',
37 jsx => 'javascriptreact',
38 ts => 'typescript', tsx => 'typescriptreact',
39 yml => 'yaml',
40 h => 'c',
41 'c++' => 'cpp', cc => 'cpp', cxx => 'cpp', hpp => 'cpp', hh => 'cpp',
42 'c#' => 'cs', csharp => 'cs',
43 'objective-c' => 'objectivec', m => 'objectivec',
44 rs => 'rust',
45 hs => 'haskell',
46 kt => 'kotlin',
47 md => 'markdown', mkd => 'markdown',
48 mk => 'makefile', make => 'makefile', gnumakefile => 'makefile',
49 tex => 'latex',
50 htm => 'html',
51 el => 'commonlisp', lisp => 'commonlisp',
52 ex => 'elixir', exs => 'elixir',
53 erl => 'erlang',
54 patch => 'diff',
55 conf => 'ini', cfg => 'ini',
56 tclsh => 'tcl',
57 );
58  
59 # The parameter is "with", the name of the highlighter. With an odd
60 # number of arguments, the first is taken for it: new('pandoc') is
61 # new(with => 'pandoc').
62 0 ▶sub new {
Subroutine new: not called CC=8 SCAR=42.8
Pod documented
63 0 my $class = shift;
64 0 ▶ unshift @_, 'with' if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 0 0
65 0 my %arg = @_;
66 0 ▶ my $name = delete $arg{with} // $HIGHLIGHTER;
Condition0%0/3
delete $arg{"with"} // $Text::ANSI::Highlight::HIGHLIGHTER
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $arg{"with"} // $Text::ANSI::Highlight::HIGHLIGHTER
delete $arg{"with"} $Text::ANSI::Highlight::HIGHLIGHTER
Truth table0%0/3
A // B
A= delete $arg{"with"}B= $Text::ANSI::Highlight::HIGHLIGHTER
ABresult
1X1
011
000
67 0 ▶ $name = $SHORT{$name} // $name;
Condition0%0/3
$Text::ANSI::Highlight::SHORT{$name} // $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$Text::ANSI::Highlight::SHORT{$name} // $name
$Text::ANSI::Highlight::SHORT{$name} $name
Truth table0%0/3
A // B
A= $Text::ANSI::Highlight::SHORT{$name}B= $name
ABresult
1X1
011
000
68 0 ▶ croak join(', ', sort keys %arg) . ': unknown parameter' if %arg;
Branch: 1 branch
BranchTrueFalseTotal
if %arg 0 0 0
69 0 ▶ $name =~ /\A\w+\z/ or croak "$name: invalid highlighter";
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\A\w+\z/u or croak("${name}: invalid highlighter") 0 0 0
70 0 my $module = __PACKAGE__ . "::$name";
71 0 my $file = $module =~ s{::}{/}gr . '.pm';
72 0 ▶ unless (eval { require $file; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require $file; 1 } }) 0 0 0
73 0 ▶ croak $@ unless $@ =~ /^Can't locate \Q$file\E /;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /^Can't locate \Q$file\E /u 0 0 0
74 0 croak "$name: no such highlighter";
75 }
76 0 bless { name => $name, module => $module }, $class;
77 }
78  
79 0 ▶sub name { $_[0]{name} }
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
80  
81 # The functions of the highlighter
82 0 ▶sub _call {
Subroutine _call: not called CC=1 SCAR=6.9
83 0 my($obj, $func) = splice @_, 0, 2;
84 0 $obj->{module}->can($func)->(@_);
85 }
86  
87 # The name of a language for the highlighter, or undef when it does
88 # not know it. Markdown is not a language here: a Markdown text is
89 # colored by the markdown method.
90 0 ▶sub language {
Subroutine language: not called CC=7 SCAR=40.3
Pod documented
91 0 my($obj, $name) = @_;
92 0 ▶ $name = lc($name // '');
Condition0%0/2
$name // ""
l!l
00
MC/DC0%0/1
$name // ""
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
93 0 ▶ return undef if $name eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "" 0 0 0
94 0 ▶ for ($name, $ALIAS{$name} // ()) {
Condition0%0/3
$Text::ANSI::Highlight::ALIAS{$name} // ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$Text::ANSI::Highlight::ALIAS{$name} // ()
$Text::ANSI::Highlight::ALIAS{$name} ()
Truth table0%0/3
A // B
A= $Text::ANSI::Highlight::ALIAS{$name}B= ()
ABresult
1X1
011
000
95 0 ▶ next if $_ eq 'markdown';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "markdown" 0 0 0
96 0 my $lang = $obj->_call(language => $_);
97 0 ▶ return $lang if defined $lang;
Branch: 1 branch
BranchTrueFalseTotal
if defined $lang 0 0 0
98 }
99 0 undef;
100 }
101  
102 # Color texts. Takes [ language, text ] of each, and returns as many
103 # texts, undef for one which is not colored: one with a language which
104 # the highlighter does not know, an empty one, and one which has an
105 # escape sequence already. All are undef when the command of the
106 # highlighter is not there.
107 #
108 # Only the colors are taken from the highlighter: when the text it
109 # gives is not that of the input, the input is left as it is.
110 #
111 # Texts of characters give texts of characters, and those of bytes
112 # (UTF-8) give bytes.
113 0 ▶sub highlight {
Subroutine highlight: not called CC=8 SCAR=42.8
Pod documented
114 0 my $obj = shift;
115 0 my @code = @_;
116 0 my @ret = (undef) x @code;
117 0 my $chars = grep { utf8::is_utf8($_->[1]) } @code;
118 0 my(@index, @pair, @crlf);
119 0 for my $i (0 .. $#code) {
120 0 my($name, $text) = @{$code[$i]};
121 0 ▶ if (lc($name // '') =~ /\A(?:markdown|md|mkd)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc($name // "") =~ /\A(?:markdown|md|mkd)\z/u) 0 0 0
Condition0%0/2
$name // ""
l!l
00
MC/DC0%0/1
$name // ""
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
122 0 $ret[$i] = $obj->markdown($text);
123 0 next;
124 }
125 0 ▶ my $lang = $obj->language($name) // next;
Condition0%0/2
$obj->language($name) // (next)
l!l
00
MC/DC0%0/1
$obj->language($name) // (next)
$obj->language($name)
Truth table0%0/2
A
A= $obj->language($name)
Aresult
11
00
126 0 ▶ my $crlf = _prepare(\$text, $chars) // next;
Condition0%0/2
_prepare(\$text, $chars) // (next)
l!l
00
MC/DC0%0/1
_prepare(\$text, $chars) // (next)
_prepare(\$text, $chars)
Truth table0%0/2
A
A= _prepare(\$text, $chars)
Aresult
11
00
127 0 push @index, $i;
128 0 push @pair, [ $lang, $text ];
129 0 push @crlf, $crlf;
130 }
131 0 ▶ my @out = @pair ? $obj->_call(highlight => @pair) : ();
Branch: 1 branch
BranchTrueFalseTotal
@pair ? : 0 0 0
132 0 for my $j (0 .. $#pair) {
133 0 $ret[$index[$j]] = _accept($out[$j], $pair[$j][1], $crlf[$j], $chars);
134 }
135 0 @ret;
136 }
137  
138 # Make a text ready for the highlighter: bytes, and LF for CR LF.
139 # Returns undef for a text which is not to be colored, or whether it
140 # had CR LF.
141 0 ▶sub _prepare {
Subroutine _prepare: not called CC=8 SCAR=42.8
142 0 my($text, $chars) = @_;
143 0 ▶ return undef if $$text eq '' or $$text =~ /\e/;
Branch: 1 branch
BranchTrueFalseTotal
if $$text eq "" or $$text =~ /\e/u 0 0 0
Condition0%0/3
$$text eq "" or $$text =~ /\e/u
l!l&&r!l&&!r
000
MC/DC0%0/2
$$text eq "" or $$text =~ /\e/u
$$text eq "" $$text =~ /\e/u
Truth table0%0/3
A or B
A= $$text eq ""B= $$text =~ /\e/u
ABresult
1X1
011
000
144 0 ▶ utf8::encode($$text) if $chars;
Branch: 1 branch
BranchTrueFalseTotal
if $chars 0 0 0
145 # a highlighter can give LF for CR LF: take CR away, when every
146 # line has it
147 0 ▶ my $crlf = $$text =~ /\r\n/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$$text =~ /\r\n/u ? : 0 0 0
148 0 ▶ if ($crlf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($crlf) 0 0 0
149 0 ▶ return undef if $$text =~ /(?<!\r)\n/;
Branch: 1 branch
BranchTrueFalseTotal
if $$text =~ /(?<!\r)\n/u 0 0 0
150 0 $$text =~ s/\r\n/\n/g;
151 }
152 0 ▶ return undef if $$text =~ /\r/;
Branch: 1 branch
BranchTrueFalseTotal
if $$text =~ /\r/u 0 0 0
153 0 $crlf;
154 }
155  
156 # Take what the highlighter gave, when it is the text in colors
157 0 ▶sub _accept {
Subroutine _accept: not called CC=8 SCAR=42.8
158 0 my($color, $text, $crlf, $chars) = @_;
159 0 ▶ defined $color or return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $color or return undef 0 0 0
160 # the newlines at the ends are those of the input
161 0 $color =~ s/\A\n+//;
162 0 $color =~ s/\n*\z//;
163 0 ▶ $color = $1 . $color if $text =~ /\A(\n+)(?=.)/s;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\A(\n+)(?=.)/su 0 0 0
164 0 ▶ $color .= $1 if $text =~ /(\n+)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /(\n+)\z/u 0 0 0
165 # only the colors are taken
166 0 ▶ return undef unless $color =~ s/\e\[[\d;]*m//gr eq $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $color =~ s/\e\[[\d;]*m//gru eq $text 0 0 0
167 0 ▶ return undef unless $color =~ /\e\[[\d;]*[1-9][\d;]*m/;
Branch: 1 branch
BranchTrueFalseTotal
unless $color =~ /\e\[[\d;]*[1-9][\d;]*m/u 0 0 0
168 0 ▶ if ($crlf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($crlf) 0 0 0
169 0 $color =~ s/((?:\e\[[\d;]*m)*)\n/$1\r\n/g;
170 }
171 0 ▶ utf8::decode($color) if $chars;
Branch: 1 branch
BranchTrueFalseTotal
if $chars 0 0 0
172 0 $color;
173 }
174  
175 # A fenced code block of Markdown: the closing fence is of the same
176 # character as the opening one, and at least as long (CommonMark).
177 # $4 is the info string, $5 the code. A fence which is not closed is
178 # not a block.
179 4 my $FENCE = qr{^( {0,3})((`|~)\3{2,})(.*)\n((?s:.*?))^( {0,3})(\2\3*)(\h*)$}m;
180  
181 # Color a Markdown text: the code in the fences, each by the language
182 # of its fence, and the text around them as Markdown, when the
183 # highlighter knows Markdown. The lines of the fences are as they
184 # are. Returns undef when nothing is colored.
185 #
186 # The text around the code is given to the highlighter in pieces
187 # which have no fence in them. A definition of Markdown which finds
188 # the fences by itself can take the rest of the text for the code of
189 # a fence, as that of skylighting does. A piece which has a fence,
190 # one which is not closed, is left as it is.
191 0 ▶sub markdown {
Subroutine markdown: not called CC=18 SCAR=58.3
Pod documented
192 0 my($obj, $text) = @_;
193 0 my $chars = utf8::is_utf8($text);
194 # the fences are found in a text of LF; CR comes back at the end,
195 # when every line has it
196 0 ▶ my $crlf = $text =~ /\r\n/ && $text !~ /(?<!\r)\n/;
Condition0%0/3
$text =~ /\r\n/u && !($text =~ /(?<!\r)\n/u)
!ll&&!rl&&r
000
MC/DC0%0/2
$text =~ /\r\n/u && !($text =~ /(?<!\r)\n/u)
$text =~ /\r\n/u !($text =~ /(?<!\r)\n/u)
Truth table0%0/3
A && not(B)
A= $text =~ /\r\n/uB= !($text =~ /(?<!\r)\n/u)
ABresult
0X0
100
111
197 0 ▶ $text =~ s/\r\n/\n/g if $crlf;
Branch: 1 branch
BranchTrueFalseTotal
if $crlf 0 0 0
198 0 my(@piece, @pair, @kind);
199 0 ▶ my $prose = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
200 0 my $piece = shift;
201 0 push @piece, $piece;
202 # a fence, or front matter which is not closed at the top
203 0 ▶ my $skip = $piece =~ /^ {0,3}(?:`{3,}|~{3,})/m
Condition0%0/9
$piece =~ /^ {0,3}(?:`{3,}|~{3,})/mu || @piece == 1 && $piece =~ /\A---\h*\n/u && !($piece =~ /\A.*\n(?s:.*?)^(?:---|\.\.\.)\h*$/mu)
l!l&&r!l&&!r
000
@piece == 1 and $piece =~ /\A---\h*\n/u and not $piece =~ /\A.*\n(?s:.*?)^(?:---|\.\.\.)\h*$/mu
!ll&&!rl&&r
000
@piece == 1 and $piece =~ /\A---\h*\n/u
!ll&&!rl&&r
000
MC/DC0%0/4
$piece =~ /^ {0,3}(?:`{3,}|~{3,})/mu || @piece == 1 && $piece =~ /\A---\h*\n/u && !($piece =~ /\A.*\n(?s:.*?)^(?:---|\.\.\.)\h*$/mu)
$piece =~ /^ {0,3}(?:`{3,}|~{3,})/mu @piece == 1 $piece =~ /\A---\h*\n/u not $piece =~ /\A.*\n(?s:.*?)^(?:---|\.\.\.)\h*$/mu
Truth table0%0/5
A || B and C and not(D)
A= $piece =~ /^ {0,3}(?:`{3,}|~{3,})/muB= @piece == 1C= $piece =~ /\A---\h*\n/uD= not $piece =~ /\A.*\n(?s:.*?)^(?:---|\.\.\.)\h*$/mu
ABCDresult
1XXX1
01111
00XX0
010X0
01100
204 || (@piece == 1 && $piece =~ /\A---\h*\n/
205 && $piece !~ /\A.*\n(?s:.*?)^(?:---|\.\.\.)\h*$/m);
206 0 ▶ push @kind, [ $skip ? 'skip' : 'text' ];
Branch: 1 branch
BranchTrueFalseTotal
$skip ? : 0 0 0
207 0 push @pair, $piece;
208 0 };
209 0 my $pos = 0;
210 0 ▶ while ($text =~ /$FENCE/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /$FENCE/gu) 0 0 0
211 0 my($start, $end, $from, $to, $info) = ($-[0], $+[0], $-[5], $+[5], $4);
212 0 $prose->(substr $text, $pos, $start - $pos);
213 0 push @piece,
214 substr($text, $start, $from - $start),
215 substr($text, $from, $to - $from),
216 substr($text, $to, $end - $to);
217 0 ▶ push @kind, [ code => $info =~ /^\h*([\w+#.-]+)/ ? $1 : '' ];
Branch: 1 branch
BranchTrueFalseTotal
$info =~ /^\h*([\w+#.-]+)/u ? : 0 0 0
218 0 push @pair, $piece[-2];
219 0 $pos = $end;
220 }
221 0 $prose->(substr $text, $pos);
222  
223 # the pieces of Markdown go to the highlighter with the code, all
224 # at once; the code of a language goes through highlight
225 0 my $markdown = $obj->_call('markdown');
226 0 my(@code, @which);
227 0 my $colored = 0;
228 0 for my $i (0 .. $#pair) {
229 0 my($kind, $name) = @{$kind[$i]};
230 0 my $lang;
231 0 ▶ if ($kind eq 'text') {
Branch: 2 branches
BranchTrueFalseTotal
if ($kind eq 'text') { } 0 0 0
elsif ($kind eq 'code') { } 0 0 0
232 0 ▶ $lang = 'markdown' if $markdown;
Branch: 1 branch
BranchTrueFalseTotal
if $markdown 0 0 0
233 } elsif ($kind eq 'code') {
234 # Markdown in a fence is a Markdown text of its own
235 0 ▶ if (lc($name) =~ /\A(?:markdown|md|mkd)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc($name) =~ /\A(?:markdown|md|mkd)\z/u) 0 0 0
236 0 ▶ my $color = $obj->markdown($pair[$i]) // next;
Condition0%0/2
$obj->markdown($pair[$i]) // (next)
l!l
00
MC/DC0%0/1
$obj->markdown($pair[$i]) // (next)
$obj->markdown($pair[$i])
Truth table0%0/2
A
A= $obj->markdown($pair[$i])
Aresult
11
00
237 0 $piece[$i * 2] = $color;
238 0 $colored++;
239 0 next;
240 }
241 0 $lang = $obj->language($name);
242 }
243 0 ▶ defined $lang or next;
Branch: 1 branch
BranchTrueFalseTotal
defined $lang or next 0 0 0
244 0 my $piece = $pair[$i];
245 0 ▶ defined _prepare(\$piece, $chars) or next;
Branch: 1 branch
BranchTrueFalseTotal
defined _prepare(\$piece, $chars) or next 0 0 0
246 0 push @code, [ $lang, $piece ];
247 0 push @which, $i;
248 }
249 0 ▶ my @out = @code ? $obj->_call(highlight => @code) : ();
Branch: 1 branch
BranchTrueFalseTotal
@code ? : 0 0 0
250 0 for my $j (0 .. $#code) {
251 0 ▶ my $color = _accept($out[$j], $code[$j][1], 0, $chars) // next;
Condition0%0/2
_accept($out[$j], $code[$j][1], 0, $chars) // (next)
l!l
00
MC/DC0%0/1
_accept($out[$j], $code[$j][1], 0, $chars) // (next)
_accept($out[$j], $code[$j][1], 0, $chars)
Truth table0%0/2
A
A= _accept($out[$j], $code[$j][1], 0, $chars)
Aresult
11
00
252 0 $piece[ $which[$j] * 2 ] = $color;
253 0 $colored++;
254 }
255 0 ▶ $colored or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$colored or return undef 0 0 0
256 0 my $result = join '', @piece;
257 0 ▶ $result =~ s/((?:\e\[[\d;]*m)*)\n/$1\r\n/g if $crlf;
Branch: 1 branch
BranchTrueFalseTotal
if $crlf 0 0 0
258 0 $result;
259 }
260  
261 # The names of interpreters which are not the names of a language
262 4 my %INTERPRETER = (
263 node => 'javascript',
264 tclsh => 'tcl',
265 ksh => 'bash',
266 dash => 'bash',
267 );
268  
269 # The language of a script, by the interpreter on its first line
270 # (#!/usr/bin/env perl, #!/usr/bin/python3): the name of the command,
271 # after env and its options and variables, without the version number.
272 0 ▶sub interpreter {
Subroutine interpreter: not called CC=10 SCAR=47.0
Pod undocumented
273 0 ▶ my $line = shift // '';
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
274 0 ▶ $line =~ /\A#!\s*(\S.*)/ or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /\A#!\s*(\S.*)/u or return undef 0 0 0
275 0 my @word = split ' ', $1;
276 0 ▶ my $command = shift @word // return undef;
Condition0%0/2
shift @word // (return undef)
l!l
00
MC/DC0%0/1
shift @word // (return undef)
shift @word
Truth table0%0/2
A
A= shift @word
Aresult
11
00
277 0 ▶ if ($command =~ m{(?:\A|/)env\z}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command =~ m[(?:\A|/)env\z]u) 0 0 0
278 0 ▶ shift @word while @word and $word[0] =~ /\A-|=/;
Branch: 1 branch
BranchTrueFalseTotal
@word and $word[0] =~ /\A-|=/u and do { shift @word } 0 0 0
Condition0%0/3
@word and $word[0] =~ /\A-|=/u
!ll&&!rl&&r
000
MC/DC0%0/2
@word and $word[0] =~ /\A-|=/u
@word $word[0] =~ /\A-|=/u
Truth table0%0/3
A and B
A= @wordB= $word[0] =~ /\A-|=/u
ABresult
0X0
100
111
279 0 ▶ $command = shift @word // return undef;
Condition0%0/2
shift @word // (return undef)
l!l
00
MC/DC0%0/1
shift @word // (return undef)
shift @word
Truth table0%0/2
A
A= shift @word
Aresult
11
00
280 }
281 0 my $name = $command =~ s{.*/}{}r =~ s/[\d.]+\z//r;
282 0 ▶ return undef if $name eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "" 0 0 0
283 0 ▶ $INTERPRETER{$name} // $name;
Condition0%0/3
$INTERPRETER{$name} // $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$INTERPRETER{$name} // $name
$INTERPRETER{$name} $name
Truth table0%0/3
A // B
A= $INTERPRETER{$name}B= $name
ABresult
1X1
011
000
284 }
285  
286 0 ▶sub is_markdown { $_[0] =~ /\.(?:md|markdown|mkd)\z/i }
Subroutine is_markdown: not called CC=1 SCAR=6.9
Pod undocumented
287  
288 # The language of a file: by its extension, by its name, and by the
289 # interpreter on its first line.
290 0 ▶sub file_language {
Subroutine file_language: not called CC=5 SCAR=34.0
Pod undocumented
291 0 my($obj, $file, $content) = @_;
292 0 my $base = $file =~ s{.*/}{}r;
293 0 my($ext) = $base =~ /\.([^.]+)\z/;
294 0 my($line) = $content =~ /\A(.*)/;
295 0 ▶ for ($ext // (), $base, interpreter($line) // ()) {
Condition0%0/6
$ext // ()
l!l&&r!l&&!r
000
interpreter($line) // ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$ext // ()
$ext ()
MC/DC0%0/2
interpreter($line) // ()
interpreter($line) ()
Truth table0%0/3
A // B
A= $extB= ()
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= interpreter($line)B= ()
ABresult
1X1
011
000
296 0 my $lang = $obj->language($_);
297 0 ▶ return $lang if defined $lang;
Branch: 1 branch
BranchTrueFalseTotal
if defined $lang 0 0 0
298 }
299 0 undef;
300 }
301  
302 # Color the content of a file, by the language its name tells; a
303 # Markdown file by the markdown method. The language can be given.
304 # Returns undef when it is not colored: a file whose language is not
305 # known, one which has an escape character, an empty one, and one
306 # larger than $MAX_SIZE.
307 0 ▶sub file {
Subroutine file: not called CC=2 SCAR=17.9
Pod documented
308 0 my($obj, $file, %opt) = @_;
309 0 ▶ my $content = _content($file) // return undef;
Condition0%0/2
_content($file) // (return undef)
l!l
00
MC/DC0%0/1
_content($file) // (return undef)
_content($file)
Truth table0%0/2
A
A= _content($file)
Aresult
11
00
310 0 $obj->text($content, name => $file, %opt);
311 }
312  
313 # Color a text. The language is given by language, or told from the
314 # name of a file given by name.
315 0 ▶sub text {
Subroutine text: not called CC=6 SCAR=37.4
Pod documented
316 0 my($obj, $text, %opt) = @_;
317 0 my $lang = $opt{language};
318 0 ▶ my $name = $opt{name} // '';
Condition0%0/2
$opt{"name"} // ""
l!l
00
MC/DC0%0/1
$opt{"name"} // ""
$opt{"name"}
Truth table0%0/2
A
A= $opt{"name"}
Aresult
11
00
319 0 ▶ if (defined $lang ? lc($lang) =~ /\A(?:markdown|md|mkd)\z/ : is_markdown($name)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $lang ? lc($lang) =~ /\A(?:markdown|md|mkd)\z/u : is_markdown($name)) 0 0 0
defined $lang ? : 0 0 0
320 0 return $obj->markdown($text);
321 }
322 0 ▶ $lang //= $obj->file_language($name, $text) // return undef;
Condition0%0/4
$lang //= $obj->file_language($name, $text) // (return undef)
l!l
00
$obj->file_language($name, $text) // (return undef)
l!l
00
MC/DC0%0/1
$lang //= $obj->file_language($name, $text) // (return undef)
$lang
Truth table0%0/2
A
A= $lang
Aresult
11
00
323 0 ($obj->highlight([ $lang, $text ]))[0];
324 }
325  
326 0 ▶sub _content {
Subroutine _content: not called CC=6 SCAR=37.4
327 0 my $file = shift;
328 0 ▶ return undef unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
329 0 ▶ my $size = -s _ or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $size = -s _ or return undef 0 0 0
330 0 ▶ return undef if $MAX_SIZE and $size > $MAX_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if $Text::ANSI::Highlight::MAX_SIZE and $size > $Text::ANSI::Highlight::MAX_SIZE 0 0 0
Condition0%0/3
$Text::ANSI::Highlight::MAX_SIZE and $size > $Text::ANSI::Highlight::MAX_SIZE
!ll&&!rl&&r
000
MC/DC0%0/2
$Text::ANSI::Highlight::MAX_SIZE and $size > $Text::ANSI::Highlight::MAX_SIZE
$Text::ANSI::Highlight::MAX_SIZE $size > $Text::ANSI::Highlight::MAX_SIZE
Truth table0%0/3
A and B
A= $Text::ANSI::Highlight::MAX_SIZEB= $size > $Text::ANSI::Highlight::MAX_SIZE
ABresult
0X0
100
111
331 0 ▶ open my $fh, '<:raw', $file or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or return undef 0 0 0
332 0 local $/;
333 0 <$fh>;
334 }
335  
336 # Read the standard input and print it in colors, or as it is when it
337 # is not colored. The arguments are the options of the highlighter
338 # (-w, --with) and of the language (-l, --language), which is
339 # Markdown by default.
340 #
341 # This is to be called as a filter: by the tee module of greple, which
342 # gives the parts of a file to a function and puts back what it prints
343 # (greple -Mtee '&Text::ANSI::Highlight::filter' -- ...). The lines
344 # are those of the input, and only colors are added, so tee can put
345 # them back by the number of lines; and code blocks of Markdown, given
346 # with their fences, are colored each by itself.
347 0 ▶sub filter {
Subroutine filter: not called CC=5 SCAR=34.0
Pod documented
348 0 require Getopt::Long;
349 0 my %opt = (language => 'markdown');
350 0 ▶ Getopt::Long::GetOptionsFromArray(
Branch: 1 branch
BranchTrueFalseTotal
Getopt::Long::GetOptionsFromArray([@_], \%opt, "with|w=s", "language|l=s") or croak("filter: invalid option") 0 0 0
351 [ @_ ], \%opt, 'with|w=s', 'language|l=s')
352 or croak 'filter: invalid option';
353 0 ▶ my $hl = __PACKAGE__->new(defined $opt{with} ? $opt{with} : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'with'} ? : 0 0 0
354 0 ▶ my $text = do { local $/; <STDIN> } // '';
Condition0%0/2
do { local $/; readline STDIN } // ""
l!l
00
MC/DC0%0/1
do { local $/; readline STDIN } // ""
do { local $/; readline STDIN }
Truth table0%0/2
A
A= do { local $/; readline STDIN }
Aresult
11
00
355 0 ▶ print $hl->text($text, language => $opt{language}) // $text;
Condition0%0/3
$hl->text($text, "language", $opt{"language"}) // $text
l!l&&r!l&&!r
000
MC/DC0%0/2
$hl->text($text, "language", $opt{"language"}) // $text
$hl->text($text, "language", $opt{"language"}) $text
Truth table0%0/3
A // B
A= $hl->text($text, "language", $opt{"language"})B= $text
ABresult
1X1
011
000
356 }
357  
358 # Run a command with the text as its input, and return what it
359 # writes, or undef when it fails. Its messages are thrown away.
360 # For the highlighters.
361 0 ▶sub run {
Subroutine run: not called CC=9 SCAR=43.0
Pod undocumented
362 0 my($command, $input) = @_;
363 0 my $tmp = File::Temp->new;
364 0 binmode $tmp;
365 0 print $tmp $input;
366 0 $tmp->flush;
367 0 my $pid = open my $fh, '-|';
368 0 ▶ defined $pid or return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $pid or return undef 0 0 0
369 0 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 0 0 0
370 # by the file descriptors: STDIN of the caller may not be on 0,
371 # as when it is a handle on a string
372 0 ▶ open my $in, '<', $tmp->filename or POSIX::_exit(126);
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<", $tmp->filename or POSIX::_exit(126) 0 0 0
373 0 ▶ open my $null, '>', File::Spec->devnull or POSIX::_exit(126);
Branch: 1 branch
BranchTrueFalseTotal
open my $null, ">", "File::Spec"->devnull or POSIX::_exit(126) 0 0 0
374 0 ▶ POSIX::dup2(fileno $in, 0) // POSIX::_exit(126);
Condition0%0/3
POSIX::dup2(fileno $in, 0) // POSIX::_exit(126)
l!l&&r!l&&!r
000
MC/DC0%0/2
POSIX::dup2(fileno $in, 0) // POSIX::_exit(126)
POSIX::dup2(fileno $in, 0) POSIX::_exit(126)
Truth table0%0/3
A // B
A= POSIX::dup2(fileno $in, 0)B= POSIX::_exit(126)
ABresult
1X1
011
000
375 0 ▶ POSIX::dup2(fileno $null, 2) // POSIX::_exit(126);
Condition0%0/3
POSIX::dup2(fileno $null, 2) // POSIX::_exit(126)
l!l&&r!l&&!r
000
MC/DC0%0/2
POSIX::dup2(fileno $null, 2) // POSIX::_exit(126)
POSIX::dup2(fileno $null, 2) POSIX::_exit(126)
Truth table0%0/3
A // B
A= POSIX::dup2(fileno $null, 2)B= POSIX::_exit(126)
ABresult
1X1
011
000
376 0 ▶ { no warnings 'exec'; exec @$command }
Subroutine BEGIN: called CC=1 SCAR=0.6
377 0 POSIX::_exit(127);
378 }
379 0 binmode $fh;
380 0 my $out = do { local $/; <$fh> };
381 0 ▶ close $fh or return undef;
Branch: 1 branch
BranchTrueFalseTotal
close $fh or return undef 0 0 0
382 0 ▶ $out // '';
Condition0%0/2
$out // ""
l!l
00
MC/DC0%0/1
$out // ""
$out
Truth table0%0/2
A
A= $out
Aresult
11
00
383 }
384  
385 4 1;
386  
387 __END__
↑ Summary Highlight/sourcehighlight.pm »