| 1 | package Text::ANSI::Highlight; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 6 | our $VERSION = "0.04"; | |||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use v5.16; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use File::Temp (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use POSIX (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | # The highlighter which is used when none is given | ||||||||||||||||||||||||||||||||||||||||||
| 13 | 6 | our $HIGHLIGHTER = 'pandoc'; | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | # Short names of the highlighters | ||||||||||||||||||||||||||||||||||||||||||
| 16 | 6 | 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 | 6 | 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 | # here first, and then for the name as it is. | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 6 | our %ALIAS = ( | |||||||||||||||||||||||||||||||||||||||||
| 30 | pl => 'perl', pm => 'perl', t => 'perl', psgi => 'perl', | ||||||||||||||||||||||||||||||||||||||||||
| 31 | cpanfile => 'perl', | ||||||||||||||||||||||||||||||||||||||||||
| 32 | sh => 'bash', shell => 'bash', ksh => 'bash', dash => 'bash', | ||||||||||||||||||||||||||||||||||||||||||
| 33 | bashrc => 'bash', profile => 'bash', | ||||||||||||||||||||||||||||||||||||||||||
| 34 | py => 'python', python3 => 'python', | ||||||||||||||||||||||||||||||||||||||||||
| 35 | rb => 'ruby', | ||||||||||||||||||||||||||||||||||||||||||
| 36 | js => 'javascript', mjs => 'javascript', cjs => 'javascript', | ||||||||||||||||||||||||||||||||||||||||||
| 37 | node => 'javascript', | ||||||||||||||||||||||||||||||||||||||||||
| 38 | jsx => 'javascriptreact', | ||||||||||||||||||||||||||||||||||||||||||
| 39 | ts => 'typescript', tsx => 'typescriptreact', | ||||||||||||||||||||||||||||||||||||||||||
| 40 | yml => 'yaml', | ||||||||||||||||||||||||||||||||||||||||||
| 41 | h => 'c', | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 'c++' => 'cpp', cc => 'cpp', cxx => 'cpp', hpp => 'cpp', hh => 'cpp', | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 'c#' => 'cs', csharp => 'cs', | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 'objective-c' => 'objectivec', m => 'objectivec', | ||||||||||||||||||||||||||||||||||||||||||
| 45 | rs => 'rust', | ||||||||||||||||||||||||||||||||||||||||||
| 46 | hs => 'haskell', | ||||||||||||||||||||||||||||||||||||||||||
| 47 | kt => 'kotlin', | ||||||||||||||||||||||||||||||||||||||||||
| 48 | md => 'markdown', mkd => 'markdown', | ||||||||||||||||||||||||||||||||||||||||||
| 49 | mk => 'makefile', make => 'makefile', gnumakefile => 'makefile', | ||||||||||||||||||||||||||||||||||||||||||
| 50 | tex => 'latex', | ||||||||||||||||||||||||||||||||||||||||||
| 51 | htm => 'html', | ||||||||||||||||||||||||||||||||||||||||||
| 52 | el => 'commonlisp', lisp => 'commonlisp', | ||||||||||||||||||||||||||||||||||||||||||
| 53 | ex => 'elixir', exs => 'elixir', | ||||||||||||||||||||||||||||||||||||||||||
| 54 | erl => 'erlang', | ||||||||||||||||||||||||||||||||||||||||||
| 55 | patch => 'diff', | ||||||||||||||||||||||||||||||||||||||||||
| 56 | conf => 'ini', cfg => 'ini', | ||||||||||||||||||||||||||||||||||||||||||
| 57 | tclsh => 'tcl', | ||||||||||||||||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | # The name of a highlighter for a name or a short name, or undef when | ||||||||||||||||||||||||||||||||||||||||||
| 61 | # there is no such highlighter. The module is looked for, and not | ||||||||||||||||||||||||||||||||||||||||||
| 62 | # loaded: a highlighter is a module which has the function highlight. | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub highlighter { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
highlighter: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | my($class, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | $name =~ s/:.*//s; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | $name = $SHORT{$name} // $name; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$Text::ANSI::Highlight::SHORT{$name} // $name
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
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | $name =~ /\A\w+\z/ or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | my $file = __PACKAGE__ =~ s{::}{/}gr . "/$name.pm"; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | for my $dir (grep { !ref } @INC) { | |||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | open my $fh, '<', "$dir/$file" or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | local $/; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | return <$fh> =~ /^sub highlight\b/m ? $name : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | undef; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | # The parameter is "with", the name of the highlighter. With an odd | ||||||||||||||||||||||||||||||||||||||||||
| 78 | # number of arguments, the first is taken for it: new('pandoc') is | ||||||||||||||||||||||||||||||||||||||||||
| 79 | # new(with => 'pandoc'). | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=14 SCAR=53.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | unshift @_, 'with' if @_ % 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | my %arg = @_; | |||||||||||||||||||||||||||||||||||||||||
| 84 | # with => "name:style" is the highlighter and its style; the style | ||||||||||||||||||||||||||||||||||||||||||
| 85 | # parameter wins over it | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | my($name, $style) = split /:/, delete $arg{with} // '', 2; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
delete $arg{"with"} // ""
MC/DC0%0/1
delete $arg{"with"} // ""
delete $arg{"with"}
Truth table0%0/2
A
A= delete $arg{"with"}
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | $style = delete $arg{style} if exists $arg{style}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | undef $style if defined $style and $style eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $style and $style eq ""
MC/DC0%0/2
defined $style and $style eq ""
defined $style
$style eq ""
Truth table0%0/3
A and B
A= defined $styleB= $style eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | # background => 0: the colors of the background are taken out | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | my $background = exists $arg{background} ? delete $arg{background} : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | $name = $HIGHLIGHTER if not defined $name or $name eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $name or $name eq ""
MC/DC0%0/2
not defined $name or $name eq ""
not defined $name
$name eq ""
Truth table0%0/3
not(A) or B
A= not defined $nameB= $name eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | $name = $SHORT{$name} // $name; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$Text::ANSI::Highlight::SHORT{$name} // $name
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
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | croak join(', ', sort keys %arg) . ': unknown parameter' if %arg; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | $name =~ /\A\w+\z/ or croak "$name: invalid highlighter"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | my $module = __PACKAGE__ . "::$name"; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | my $file = $module =~ s{::}{/}gr . '.pm'; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | unless (eval { require $file; 1 }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | croak $@ unless $@ =~ /^Can't locate \Q$file\E /; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | croak "$name: no such highlighter"; | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | bless { name => $name, module => $module, style => $style, | |||||||||||||||||||||||||||||||||||||||||
| 102 | background => $background }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub name { $_[0]{name} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | sub style { $_[0]{style} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
style: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | # The functions of the highlighter. The style is given in the | ||||||||||||||||||||||||||||||||||||||||||
| 109 | # variable $STYLE of its package, for the time of the call: the | ||||||||||||||||||||||||||||||||||||||||||
| 110 | # highlighter makes the option of its command from it. | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub _call { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_call: not called CC=1 SCAR=1.7
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | my($obj, $func) = splice @_, 0, 2; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 6 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | local ${"$obj->{module}::STYLE"} = $obj->{style}; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | $obj->{module}->can($func)->(@_); | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | # The name of a language for the highlighter, or undef when it does | ||||||||||||||||||||||||||||||||||||||||||
| 119 | # not know it. Markdown is not a language here: a Markdown text is | ||||||||||||||||||||||||||||||||||||||||||
| 120 | # colored by the markdown method. | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | sub language { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
language: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | my($obj, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | $name = lc($name // ''); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$name // ""
MC/DC0%0/1
$name // ""
$name
Truth table0%0/2
A
A= $name
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | return undef if $name eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | # the name here first: a highlighter can take the same name for | ||||||||||||||||||||||||||||||||||||||||||
| 126 | # another language, as source-highlight takes pl for Prolog | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | for ($ALIAS{$name} // (), $name) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$Text::ANSI::Highlight::ALIAS{$name} // ()
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= ()
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | next if $_ eq 'markdown'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | my $lang = $obj->_call(language => $_); | |||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | return $lang if defined $lang; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | undef; | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | # Color texts. Takes [ language, text ] of each, and returns as many | ||||||||||||||||||||||||||||||||||||||||||
| 136 | # texts, undef for one which is not colored: one with a language which | ||||||||||||||||||||||||||||||||||||||||||
| 137 | # the highlighter does not know, an empty one, and one which has an | ||||||||||||||||||||||||||||||||||||||||||
| 138 | # escape sequence already. All are undef when the command of the | ||||||||||||||||||||||||||||||||||||||||||
| 139 | # highlighter is not there. | ||||||||||||||||||||||||||||||||||||||||||
| 140 | # | ||||||||||||||||||||||||||||||||||||||||||
| 141 | # Only the colors are taken from the highlighter: when the text it | ||||||||||||||||||||||||||||||||||||||||||
| 142 | # gives is not that of the input, the input is left as it is. | ||||||||||||||||||||||||||||||||||||||||||
| 143 | # | ||||||||||||||||||||||||||||||||||||||||||
| 144 | # Texts of characters give texts of characters, and those of bytes | ||||||||||||||||||||||||||||||||||||||||||
| 145 | # (UTF-8) give bytes. | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | sub highlight { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
highlight: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | my $obj = shift; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | my @code = @_; | |||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | my $chars = grep { utf8::is_utf8($_->[1]) } @code; | |||||||||||||||||||||||||||||||||||||||||
| 150 | # All go to the highlighter in one call: the texts, and the pieces | ||||||||||||||||||||||||||||||||||||||||||
| 151 | # of those which are Markdown | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | my(@job, @build); | |||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | for my $i (0 .. $#code) { | |||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | my($info, $text) = @{$code[$i]}; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | $build[$i] = $obj->_code_plan($info, $text, \@job, $chars); | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | $obj->_run(\@job, $chars); | |||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | map { $_->() } @build; | |||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | # The parts of the text of a block, by the line of its fence: a list | ||||||||||||||||||||||||||||||||||||||||||
| 162 | # of [ language, text, label ]. It is one part with the name of the | ||||||||||||||||||||||||||||||||||||||||||
| 163 | # fence for the language, when there is no module for a block of that | ||||||||||||||||||||||||||||||||||||||||||
| 164 | # name. The label is what the module calls the part, or undef. | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 5 | ▶ | sub parts { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parts: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 5 | my($class, $info, $text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 5 | ▶ | my $name = ($info // '') =~ /^\h*(?:\{\h*\.)?([\w+#.-]+)/ ? $1 : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$info // ""
MC/DC100%1/1
$info // ""
$info
Truth table100%2/2
A
A= $info
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 5 | ▶ | if (my $parts = _block($name)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 2 | my @part = $parts->($info, $text); | |||||||||||||||||||||||||||||||||||||||||
| 170 | 2 | ▶ | return @part if join('', map { $_->[1] } @part) eq $text; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 3 | [ $name, $text ]; | |||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | # Add the jobs for a text to the list, by the line of its fence, the | ||||||||||||||||||||||||||||||||||||||||||
| 176 | # name of a language or more, and return a function which gives the | ||||||||||||||||||||||||||||||||||||||||||
| 177 | # text in colors when the jobs are done, or undef. | ||||||||||||||||||||||||||||||||||||||||||
| 178 | # | ||||||||||||||||||||||||||||||||||||||||||
| 179 | # The name is the first word, or the first class in braces as in | ||||||||||||||||||||||||||||||||||||||||||
| 180 | # ```{.perl .numberLines}. Markdown is divided into its pieces. A | ||||||||||||||||||||||||||||||||||||||||||
| 181 | # block which has a module for its name is divided into the parts the | ||||||||||||||||||||||||||||||||||||||||||
| 182 | # module tells, each of its language. | ||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | sub _code_plan { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_code_plan: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | my($obj, $info, $text, $job, $chars) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | my $name = ($info // '') =~ /^\h*(?:\{\h*\.)?([\w+#.-]+)/ ? $1 : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$info // ""
MC/DC0%0/1
$info // ""
$info
Truth table0%0/2
A
A= $info
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | if (lc($name) =~ /\A(?:markdown|md|mkd)\z/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | return $obj->_markdown_plan($text, $job, $chars); | |||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | my @part = parts(undef, $info, $text); | |||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | my @text = map { $_->[1] } @part; | |||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | my @mine; | |||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | for my $i (keys @part) { | |||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | my $lang = $obj->language($part[$i][0]) // next; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$obj->language($part[$i][0]) // (next)
MC/DC0%0/1
$obj->language($part[$i][0]) // (next)
$obj->language($part[$i][0])
Truth table0%0/2
A
A= $obj->language($part[$i][0])
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | push @$job, { lang => $lang, text => $text[$i], slot => \$text[$i] }; | |||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | push @mine, $job->[-1]; | |||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | ▶ | sub { (grep { $_->{ok} } @mine) ? join('', @text) : undef }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | # Give the texts of the jobs to the highlighter in one call, and put | ||||||||||||||||||||||||||||||||||||||||||
| 201 | # what comes back in the slot of each job, when it is the text in | ||||||||||||||||||||||||||||||||||||||||||
| 202 | # colors. A job is { lang, text, slot }, and gets ok when it is done. | ||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | sub _run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_run: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | my($obj, $job, $chars) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | my(@todo, @pair, @crlf); | |||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | for my $j (@$job) { | |||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | my $text = $j->{text}; | |||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | ▶ | my $crlf = _prepare(\$text, $chars) // next; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
_prepare(\$text, $chars) // (next)
MC/DC0%0/1
_prepare(\$text, $chars) // (next)
_prepare(\$text, $chars)
Truth table0%0/2
A
A= _prepare(\$text, $chars)
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | push @todo, $j; | |||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | push @pair, [ $j->{lang}, $text ]; | |||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | push @crlf, $crlf; | |||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | ▶ | @pair or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | my @out = $obj->_call(highlight => @pair); | |||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | for my $i (0 .. $#todo) { | |||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | ▶ | my $color = _accept($out[$i], $pair[$i][1], $crlf[$i], $chars) // next; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
_accept($out[$i], $pair[$i][1], $crlf[$i], $chars) // (next)
MC/DC0%0/1
_accept($out[$i], $pair[$i][1], $crlf[$i], $chars) // (next)
_accept($out[$i], $pair[$i][1], $crlf[$i], $chars)
Truth table0%0/2
A
A= _accept($out[$i], $pair[$i][1], $crlf[$i], $chars)
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | $color = _no_background($color) unless $obj->{background}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | ${ $todo[$i]{slot} } = $color; | |||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | $todo[$i]{ok} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | # Take the colors of the background out of the SGR sequences of a | ||||||||||||||||||||||||||||||||||||||||||
| 224 | # text: 40-47, 100-107, 48 with its color (48;5;N, 48;2;R;G;B, and | ||||||||||||||||||||||||||||||||||||||||||
| 225 | # with colons 48:5:N, 48:2::R:G:B), and 49, which puts the default | ||||||||||||||||||||||||||||||||||||||||||
| 226 | # one back. | ||||||||||||||||||||||||||||||||||||||||||
| 227 | # A sequence which has nothing left is gone. | ||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | ▶ | sub _no_background { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_no_background: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | $_[0] =~ s{\e\[([\d;:]*)m}{ | |||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | my @in = split /;/, $1, -1; | |||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | my @out; | |||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | ▶ | while (@in) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | my $p = shift @in; | |||||||||||||||||||||||||||||||||||||||||
| 234 | # the color after 38 and 48: 5;N or 2;R;G;B. With colons | ||||||||||||||||||||||||||||||||||||||||||
| 235 | # it is in the parameter itself, as 48:2::R:G:B | ||||||||||||||||||||||||||||||||||||||||||
| 236 | my $n = $p =~ /\A[34]8\z/ | ||||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | ▶ | ? { 5 => 2, 2 => 4 }->{ $in[0] // '' } // 0 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/4
{5, 2, 2, 4}->{$in[0] // ""} // 0
$in[0] // ""
MC/DC0%0/1
{5, 2, 2, 4}->{$in[0] // ""} // 0
{5, 2, 2, 4}->{$in[0] // ""}
MC/DC0%0/1
$in[0] // ""
$in[0]
Truth table0%0/2
A
A= {5, 2, 2, 4}->{$in[0] // ""}
Truth table0%0/2
A
A= $in[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | my @color = splice @in, 0, $n; | |||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | ▶ | next if $p =~ /\A(?:4[0-9]|10[0-7])(?::|\z)/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | push @out, $p, @color; | |||||||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | ▶ | @out || $1 eq '' ? "\e[" . join(';', @out) . 'm' : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@out or $1 eq ""
MC/DC0%0/2
@out or $1 eq ""
@out
$1 eq ""
Truth table0%0/3
A or B
A= @outB= $1 eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | }ger; | ||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | # Make a text ready for the highlighter: bytes, and LF for CR LF. | ||||||||||||||||||||||||||||||||||||||||||
| 247 | # Returns undef for a text which is not to be colored, or whether it | ||||||||||||||||||||||||||||||||||||||||||
| 248 | # had CR LF. | ||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | ▶ | sub _prepare { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_prepare: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | my($text, $chars) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | ▶ | return undef if $$text eq '' or $$text =~ /\e/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$$text eq "" or $$text =~ /\e/u
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
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | ▶ | utf8::encode($$text) if $chars; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | # a highlighter can give LF for CR LF: take CR away, when every | ||||||||||||||||||||||||||||||||||||||||||
| 254 | # line has it | ||||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | ▶ | my $crlf = $$text =~ /\r\n/ ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | ▶ | if ($crlf) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | ▶ | return undef if $$text =~ /(?<!\r)\n/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | $$text =~ s/\r\n/\n/g; | |||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | ▶ | return undef if $$text =~ /\r/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | $crlf; | |||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | # Take what the highlighter gave, when it is the text in colors | ||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | ▶ | sub _accept { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_accept: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | my($color, $text, $crlf, $chars) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | ▶ | defined $color or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | # the newlines at the ends are those of the input | ||||||||||||||||||||||||||||||||||||||||||
| 269 | 0 | $color =~ s/\A\n+//; | |||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | $color =~ s/\n*\z//; | |||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | ▶ | $color = $1 . $color if $text =~ /\A(\n+)(?=.)/s; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | ▶ | $color .= $1 if $text =~ /(\n+)\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | # only the colors are taken | ||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | ▶ | return undef unless $color =~ s/\e\[[\d;:]*m//gr eq $text; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | ▶ | return undef unless $color =~ /\e\[[\d;:]*[1-9][\d;:]*m/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | ▶ | if ($crlf) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | $color =~ s/((?:\e\[[\d;:]*m)*)\n/$1\r\n/g; | |||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | ▶ | utf8::decode($color) if $chars; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | $color; | |||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | # A fenced code block of Markdown: the closing fence is of the same | ||||||||||||||||||||||||||||||||||||||||||
| 284 | # character as the opening one, and at least as long (CommonMark). | ||||||||||||||||||||||||||||||||||||||||||
| 285 | # $4 is the info string, $5 the code. A fence which is not closed is | ||||||||||||||||||||||||||||||||||||||||||
| 286 | # not a block. | ||||||||||||||||||||||||||||||||||||||||||
| 287 | 6 | my $FENCE = qr{^( {0,3})((`|~)\3{2,})(.*)\n((?s:.*?))^( {0,3})(\2\3*)(\h*)$}m; | |||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | # Color a Markdown text: the code in the fences, each by the language | ||||||||||||||||||||||||||||||||||||||||||
| 290 | # of its fence, and the text around them as Markdown, when the | ||||||||||||||||||||||||||||||||||||||||||
| 291 | # highlighter knows Markdown. The lines of the fences are as they | ||||||||||||||||||||||||||||||||||||||||||
| 292 | # are. Returns undef when nothing is colored. | ||||||||||||||||||||||||||||||||||||||||||
| 293 | # | ||||||||||||||||||||||||||||||||||||||||||
| 294 | # The text around the code is given to the highlighter in pieces | ||||||||||||||||||||||||||||||||||||||||||
| 295 | # which have no fence in them. A definition of Markdown which finds | ||||||||||||||||||||||||||||||||||||||||||
| 296 | # the fences by itself can take the rest of the text for the code of | ||||||||||||||||||||||||||||||||||||||||||
| 297 | # a fence, as that of skylighting does. A piece which has a fence, | ||||||||||||||||||||||||||||||||||||||||||
| 298 | # one which is not closed, is left as it is. | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | ▶ | sub markdown { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
markdown: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | my($obj, $text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | my @job; | |||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | my $build = $obj->_markdown_plan($text, \@job, utf8::is_utf8($text)); | |||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | $obj->_run(\@job, utf8::is_utf8($text)); | |||||||||||||||||||||||||||||||||||||||||
| 304 | 0 | $build->(); | |||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | # The function which gives the parts of a block of that name, when | ||||||||||||||||||||||||||||||||||||||||||
| 308 | # there is a module for it: Text::ANSI::Highlight::Block::NAME, with a | ||||||||||||||||||||||||||||||||||||||||||
| 309 | # function parts. It takes the line of the fence after the marks, and | ||||||||||||||||||||||||||||||||||||||||||
| 310 | # the text of the block, and returns a list of [ language, text ], | ||||||||||||||||||||||||||||||||||||||||||
| 311 | # whose texts are that of the block when they are put together. The | ||||||||||||||||||||||||||||||||||||||||||
| 312 | # language is undef for a part which has none. | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 6 | my %BLOCK; | |||||||||||||||||||||||||||||||||||||||||
| 314 | 5 | ▶ | sub _block { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_block: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||
| 315 | 5 | my $name = shift; | |||||||||||||||||||||||||||||||||||||||||
| 316 | 5 | ▶ | $name =~ /\A\w+\z/ or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | 3 | ▶ | $BLOCK{$name} //= do { | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$BLOCK{$name} //= do {
my $module = 'Text::ANSI::Highlight' . "::Block::$name";
eval "require $module; 1" and $module->can("parts") or 0
}
MC/DC0%0/2
$BLOCK{$name} //= do {
my $module = 'Text::ANSI::Highlight' . "::Block::$name";
eval "require $module; 1" and $module->can("parts") or 0
}
$BLOCK{$name}
do {
my $module = 'Text::ANSI::Highlight' . "::Block::$name";
eval "require $module; 1" and $module->can("parts") or 0
}
Truth table66%2/3
A //= B
A= $BLOCK{$name}B= do {
my $module = 'Text::ANSI::Highlight' . "::Block::$name";
eval "require $module; 1" and $module->can("parts") or 0
}
| |||||||||||||||||||||||||||||||||||||||||||
| 318 | 2 | my $module = __PACKAGE__ . "::Block::$name"; | |||||||||||||||||||||||||||||||||||||||||
| 319 | 2 | ▶ | (eval "require $module; 1" && $module->can('parts')) || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition80%4/5
eval "require $module; 1" && $module->can('parts') || 0
eval "require $module; 1" and $module->can("parts")
MC/DC50%1/2
eval "require $module; 1" && $module->can('parts') || 0
eval "require $module; 1"
$module->can("parts")
Truth table66%2/3
A and B
A= eval "require $module; 1"B= $module->can("parts")
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | # Divide a Markdown text into pieces, add the jobs for them to the | ||||||||||||||||||||||||||||||||||||||||||
| 324 | # list, and return a function which puts the text together again when | ||||||||||||||||||||||||||||||||||||||||||
| 325 | # the jobs are done: the text with colors, or undef when nothing of it | ||||||||||||||||||||||||||||||||||||||||||
| 326 | # is colored. Markdown in a fence is divided in the same way, and its | ||||||||||||||||||||||||||||||||||||||||||
| 327 | # pieces go to the same list. | ||||||||||||||||||||||||||||||||||||||||||
| 328 | 0 | ▶ | sub _markdown_plan { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_markdown_plan: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | 0 | my($obj, $text, $job, $chars) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 330 | # the fences are found in a text of LF; CR comes back at the end, | ||||||||||||||||||||||||||||||||||||||||||
| 331 | # when every line has it | ||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | ▶ | my $crlf = $text =~ /\r\n/ && $text !~ /(?<!\r)\n/; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$text =~ /\r\n/u && !($text =~ /(?<!\r)\n/u)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | ▶ | $text =~ s/\r\n/\n/g if $crlf; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | my $markdown = $obj->_call('markdown'); | |||||||||||||||||||||||||||||||||||||||||
| 335 | # the pieces: the text, the opening fence, the code, the closing | ||||||||||||||||||||||||||||||||||||||||||
| 336 | # fence, and so on, to the text at the end | ||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | my(@piece, @mine, @nested); | |||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | ▶ | my $add = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | my($lang, $slot) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | push @$job, my $j = { lang => $lang, text => $$slot, slot => $slot }; | |||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | push @mine, $j; | |||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 343 | 0 | ▶ | my $prose = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | push @piece, shift; | |||||||||||||||||||||||||||||||||||||||||
| 345 | 0 | ▶ | return unless $markdown; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 346 | # not a piece which has a fence, or front matter which is not | ||||||||||||||||||||||||||||||||||||||||||
| 347 | # closed at the top | ||||||||||||||||||||||||||||||||||||||||||
| 348 | 0 | ▶ | return if $piece[-1] =~ /^ {0,3}(?:`{3,}|~{3,})/m; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | ▶ | return if @piece == 1 and $piece[-1] =~ /\A---\h*\n/ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
@piece == 1 and $piece[-1] =~ /\A---\h*\n/u and not $piece[-1] =~ /\A.*\n(?s:.*?)^(?:---|\.\.\.)\h*$/mu
@piece == 1 and $piece[-1] =~ /\A---\h*\n/u
MC/DC0%0/3
@piece == 1 and $piece[-1] =~ /\A---\h*\n/u and not $piece[-1] =~ /\A.*\n(?s:.*?)^(?:---|\.\.\.)\h*$/mu
@piece == 1
$piece[-1] =~ /\A---\h*\n/u
not $piece[-1] =~ /\A.*\n(?s:.*?)^(?:---|\.\.\.)\h*$/mu
Truth table0%0/4
A and B and not(C)
A= @piece == 1B= $piece[-1] =~ /\A---\h*\n/uC= not $piece[-1] =~ /\A.*\n(?s:.*?)^(?:---|\.\.\.)\h*$/mu
| |||||||||||||||||||||||||||||||||||||||||||
| 350 | and $piece[-1] !~ /\A.*\n(?s:.*?)^(?:---|\.\.\.)\h*$/m; | ||||||||||||||||||||||||||||||||||||||||||
| 351 | 0 | $add->(markdown => \$piece[-1]); | |||||||||||||||||||||||||||||||||||||||||
| 352 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 353 | 0 | my $pos = 0; | |||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | ▶ | while ($text =~ /$FENCE/g) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 355 | 0 | my($start, $end, $from, $to, $info) = ($-[0], $+[0], $-[5], $+[5], $4); | |||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | $prose->(substr $text, $pos, $start - $pos); | |||||||||||||||||||||||||||||||||||||||||
| 357 | 0 | push @piece, | |||||||||||||||||||||||||||||||||||||||||
| 358 | substr($text, $start, $from - $start), | ||||||||||||||||||||||||||||||||||||||||||
| 359 | substr($text, $from, $to - $from), | ||||||||||||||||||||||||||||||||||||||||||
| 360 | substr($text, $to, $end - $to); | ||||||||||||||||||||||||||||||||||||||||||
| 361 | 0 | $pos = $end; | |||||||||||||||||||||||||||||||||||||||||
| 362 | 0 | my $code = \$piece[-2]; | |||||||||||||||||||||||||||||||||||||||||
| 363 | 0 | push @nested, [ $code, $obj->_code_plan($info, $$code, $job, $chars) ]; | |||||||||||||||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||||||||||||||||
| 365 | 0 | $prose->(substr $text, $pos); | |||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | 0 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||
| 368 | 0 | my $colored = grep { $_->{ok} } @mine; | |||||||||||||||||||||||||||||||||||||||||
| 369 | 0 | for (@nested) { | |||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | my($code, $build) = @$_; | |||||||||||||||||||||||||||||||||||||||||
| 371 | 0 | ▶ | my $color = $build->() // next; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
&$build() // (next)
MC/DC0%0/1
&$build() // (next)
&$build()
Truth table0%0/2
A
A= &$build()
| |||||||||||||||||||||||||||||||||||||||||||
| 372 | 0 | $$code = $color; | |||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | $colored++; | |||||||||||||||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | ▶ | $colored or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | my $result = join '', @piece; | |||||||||||||||||||||||||||||||||||||||||
| 377 | 0 | ▶ | $result =~ s/((?:\e\[[\d;:]*m)*)\n/$1\r\n/g if $crlf; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | $result; | |||||||||||||||||||||||||||||||||||||||||
| 379 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | # The names of interpreters which are not the names of a language | ||||||||||||||||||||||||||||||||||||||||||
| 383 | 6 | my %INTERPRETER = ( | |||||||||||||||||||||||||||||||||||||||||
| 384 | node => 'javascript', | ||||||||||||||||||||||||||||||||||||||||||
| 385 | tclsh => 'tcl', | ||||||||||||||||||||||||||||||||||||||||||
| 386 | ksh => 'bash', | ||||||||||||||||||||||||||||||||||||||||||
| 387 | dash => 'bash', | ||||||||||||||||||||||||||||||||||||||||||
| 388 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||
| 390 | # The language of a script, by the interpreter on its first line | ||||||||||||||||||||||||||||||||||||||||||
| 391 | # (#!/usr/bin/env perl, #!/usr/bin/python3): the name of the command, | ||||||||||||||||||||||||||||||||||||||||||
| 392 | # after env and its options and variables, without the version number. | ||||||||||||||||||||||||||||||||||||||||||
| 393 | 0 | ▶ | sub interpreter { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
interpreter: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 394 | 0 | ▶ | my $line = shift // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() // ""
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 395 | 0 | ▶ | $line =~ /\A#!\s*(\S.*)/ or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 396 | 0 | my @word = split ' ', $1; | |||||||||||||||||||||||||||||||||||||||||
| 397 | 0 | ▶ | my $command = shift @word // return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift @word // (return undef)
MC/DC0%0/1
shift @word // (return undef)
shift @word
Truth table0%0/2
A
A= shift @word
| |||||||||||||||||||||||||||||||||||||||||||
| 398 | 0 | ▶ | if ($command =~ m{(?:\A|/)env\z}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 399 | 0 | ▶ | shift @word while @word and $word[0] =~ /\A-|=/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@word and $word[0] =~ /\A-|=/u
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
| |||||||||||||||||||||||||||||||||||||||||||
| 400 | 0 | ▶ | $command = shift @word // return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift @word // (return undef)
MC/DC0%0/1
shift @word // (return undef)
shift @word
Truth table0%0/2
A
A= shift @word
| |||||||||||||||||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||||||||||||||||
| 402 | 0 | my $name = $command =~ s{.*/}{}r =~ s/[\d.]+\z//r; | |||||||||||||||||||||||||||||||||||||||||
| 403 | 0 | ▶ | return undef if $name eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 404 | 0 | ▶ | $INTERPRETER{$name} // $name; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$INTERPRETER{$name} // $name
MC/DC0%0/2
$INTERPRETER{$name} // $name
$INTERPRETER{$name}
$name
Truth table0%0/3
A // B
A= $INTERPRETER{$name}B= $name
| |||||||||||||||||||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||
| 407 | 0 | ▶ | sub is_markdown { $_[0] =~ /\.(?:md|markdown|mkd)\z/i } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_markdown: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||
| 409 | # The language of a file: by its extension, by its name, and by the | ||||||||||||||||||||||||||||||||||||||||||
| 410 | # interpreter on its first line. | ||||||||||||||||||||||||||||||||||||||||||
| 411 | 0 | ▶ | sub file_language { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
file_language: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 412 | 0 | my($obj, $file, $content) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 413 | 0 | my $base = $file =~ s{.*/}{}r; | |||||||||||||||||||||||||||||||||||||||||
| 414 | 0 | my($ext) = $base =~ /\.([^.]+)\z/; | |||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | my($line) = $content =~ /\A(.*)/; | |||||||||||||||||||||||||||||||||||||||||
| 416 | 0 | ▶ | for ($ext // (), $base, interpreter($line) // ()) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
$ext // ()
interpreter($line) // ()
MC/DC0%0/2
$ext // ()
$ext
()
MC/DC0%0/2
interpreter($line) // ()
interpreter($line)
()
Truth table0%0/3
A // B
A= $extB= ()
Truth table0%0/3
A // B
A= interpreter($line)B= ()
| |||||||||||||||||||||||||||||||||||||||||||
| 417 | 0 | my $lang = $obj->language($_); | |||||||||||||||||||||||||||||||||||||||||
| 418 | 0 | ▶ | return $lang if defined $lang; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||||||||||||
| 420 | 0 | undef; | |||||||||||||||||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||
| 423 | # Color the content of a file, by the language its name tells; a | ||||||||||||||||||||||||||||||||||||||||||
| 424 | # Markdown file by the markdown method. The language can be given. | ||||||||||||||||||||||||||||||||||||||||||
| 425 | # Returns undef when it is not colored: a file whose language is not | ||||||||||||||||||||||||||||||||||||||||||
| 426 | # known, one which has an escape character, an empty one, and one | ||||||||||||||||||||||||||||||||||||||||||
| 427 | # larger than $MAX_SIZE. | ||||||||||||||||||||||||||||||||||||||||||
| 428 | 0 | ▶ | sub file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
file: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 429 | 0 | my($obj, $file, %opt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 430 | 0 | ▶ | my $content = _content($file) // return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
_content($file) // (return undef)
MC/DC0%0/1
_content($file) // (return undef)
_content($file)
Truth table0%0/2
A
A= _content($file)
| |||||||||||||||||||||||||||||||||||||||||||
| 431 | 0 | $obj->text($content, name => $file, %opt); | |||||||||||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | # Color a text. The language is given by language, or told from the | ||||||||||||||||||||||||||||||||||||||||||
| 435 | # name of a file given by name. | ||||||||||||||||||||||||||||||||||||||||||
| 436 | 0 | ▶ | sub text { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
text: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 437 | 0 | my($obj, $text, %opt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 438 | 0 | my $lang = $opt{language}; | |||||||||||||||||||||||||||||||||||||||||
| 439 | 0 | ▶ | my $name = $opt{name} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opt{"name"} // ""
MC/DC0%0/1
$opt{"name"} // ""
$opt{"name"}
Truth table0%0/2
A
A= $opt{"name"}
| |||||||||||||||||||||||||||||||||||||||||||
| 440 | 0 | ▶ | if (defined $lang ? lc($lang) =~ /\A(?:markdown|md|mkd)\z/ : is_markdown($name)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 441 | 0 | return $obj->markdown($text); | |||||||||||||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||||||||||||
| 443 | 0 | ▶ | $lang //= $obj->file_language($name, $text) // return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/4
$lang //= $obj->file_language($name, $text) // (return undef)
$obj->file_language($name, $text) // (return undef)
MC/DC0%0/1
$lang //= $obj->file_language($name, $text) // (return undef)
$lang
Truth table0%0/2
A
A= $lang
| |||||||||||||||||||||||||||||||||||||||||||
| 444 | 0 | ($obj->highlight([ $lang, $text ]))[0]; | |||||||||||||||||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | 0 | ▶ | sub _content { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_content: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||
| 448 | 0 | my $file = shift; | |||||||||||||||||||||||||||||||||||||||||
| 449 | 0 | ▶ | return undef unless -f $file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 450 | 0 | ▶ | my $size = -s _ or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 451 | 0 | ▶ | return undef if $MAX_SIZE and $size > $MAX_SIZE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$Text::ANSI::Highlight::MAX_SIZE and $size > $Text::ANSI::Highlight::MAX_SIZE
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
| |||||||||||||||||||||||||||||||||||||||||||
| 452 | 0 | ▶ | open my $fh, '<:raw', $file or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 453 | 0 | local $/; | |||||||||||||||||||||||||||||||||||||||||
| 454 | 0 | <$fh>; | |||||||||||||||||||||||||||||||||||||||||
| 455 | } | ||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||
| 457 | # Read the standard input and print it in colors, or as it is when it | ||||||||||||||||||||||||||||||||||||||||||
| 458 | # is not colored. The arguments are the options of the highlighter | ||||||||||||||||||||||||||||||||||||||||||
| 459 | # (-w, --with) and of the language (-l, --language), which is | ||||||||||||||||||||||||||||||||||||||||||
| 460 | # Markdown by default. | ||||||||||||||||||||||||||||||||||||||||||
| 461 | # | ||||||||||||||||||||||||||||||||||||||||||
| 462 | # This is to be called as a filter: by the tee module of greple, which | ||||||||||||||||||||||||||||||||||||||||||
| 463 | # gives the parts of a file to a function and puts back what it prints | ||||||||||||||||||||||||||||||||||||||||||
| 464 | # (greple -Mtee '&Text::ANSI::Highlight::filter' -- ...). The lines | ||||||||||||||||||||||||||||||||||||||||||
| 465 | # are those of the input, and only colors are added, so tee can put | ||||||||||||||||||||||||||||||||||||||||||
| 466 | # them back by the number of lines; and code blocks of Markdown, given | ||||||||||||||||||||||||||||||||||||||||||
| 467 | # with their fences, are colored each by itself. | ||||||||||||||||||||||||||||||||||||||||||
| 468 | 0 | ▶ | sub filter { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
filter: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 469 | 0 | require Getopt::Long; | |||||||||||||||||||||||||||||||||||||||||
| 470 | 0 | my %opt = (language => 'markdown'); | |||||||||||||||||||||||||||||||||||||||||
| 471 | 0 | ▶ | Getopt::Long::GetOptionsFromArray( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 472 | [ @_ ], \%opt, 'with|w=s', 'style|s=s', 'language|l=s') | ||||||||||||||||||||||||||||||||||||||||||
| 473 | or croak 'filter: invalid option'; | ||||||||||||||||||||||||||||||||||||||||||
| 474 | my $hl = __PACKAGE__->new( | ||||||||||||||||||||||||||||||||||||||||||
| 475 | with => $opt{with} // '', | ||||||||||||||||||||||||||||||||||||||||||
| 476 | 0 | ▶ | defined $opt{style} ? (style => $opt{style}) : ()); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$opt{"with"} // ""
MC/DC0%0/1
$opt{"with"} // ""
$opt{"with"}
Truth table0%0/2
A
A= $opt{"with"}
| |||||||||||||||||||||||||||||||||||||||||||
| 477 | 0 | ▶ | my $text = do { local $/; <STDIN> } // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
do {
local $/;
readline STDIN
} // ""
MC/DC0%0/1
do {
local $/;
readline STDIN
} // ""
do {
local $/;
readline STDIN
}
Truth table0%0/2
A
A= do {
local $/;
readline STDIN
}
| |||||||||||||||||||||||||||||||||||||||||||
| 478 | 0 | ▶ | print $hl->text($text, language => $opt{language}) // $text; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$hl->text($text, "language", $opt{"language"}) // $text
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
| |||||||||||||||||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||
| 481 | # Run a command with the text as its input, and return what it | ||||||||||||||||||||||||||||||||||||||||||
| 482 | # writes, or undef when it fails. Its messages are thrown away. | ||||||||||||||||||||||||||||||||||||||||||
| 483 | # For the highlighters. | ||||||||||||||||||||||||||||||||||||||||||
| 484 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: not called CC=9 SCAR=43.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 485 | 0 | my($command, $input) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 486 | 0 | my $tmp = File::Temp->new; | |||||||||||||||||||||||||||||||||||||||||
| 487 | 0 | binmode $tmp; | |||||||||||||||||||||||||||||||||||||||||
| 488 | 0 | print $tmp $input; | |||||||||||||||||||||||||||||||||||||||||
| 489 | 0 | $tmp->flush; | |||||||||||||||||||||||||||||||||||||||||
| 490 | 0 | my $pid = open my $fh, '-|'; | |||||||||||||||||||||||||||||||||||||||||
| 491 | 0 | ▶ | defined $pid or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 492 | 0 | ▶ | if ($pid == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 493 | # by the file descriptors: STDIN of the caller may not be on 0, | ||||||||||||||||||||||||||||||||||||||||||
| 494 | # as when it is a handle on a string | ||||||||||||||||||||||||||||||||||||||||||
| 495 | 0 | ▶ | open my $in, '<', $tmp->filename or POSIX::_exit(126); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 496 | 0 | ▶ | open my $null, '>', File::Spec->devnull or POSIX::_exit(126); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 497 | 0 | ▶ | POSIX::dup2(fileno $in, 0) // POSIX::_exit(126); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
POSIX::dup2(fileno $in, 0) // POSIX::_exit(126)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 498 | 0 | ▶ | POSIX::dup2(fileno $null, 2) // POSIX::_exit(126); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
POSIX::dup2(fileno $null, 2) // POSIX::_exit(126)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 499 | 0 | ▶ | { no warnings 'exec'; exec @$command } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.6
| |||||||||||||||||||||||||||||||||||||||||||
| 500 | 0 | POSIX::_exit(127); | |||||||||||||||||||||||||||||||||||||||||
| 501 | } | ||||||||||||||||||||||||||||||||||||||||||
| 502 | 0 | binmode $fh; | |||||||||||||||||||||||||||||||||||||||||
| 503 | 0 | my $out = do { local $/; <$fh> }; | |||||||||||||||||||||||||||||||||||||||||
| 504 | 0 | ▶ | close $fh or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 505 | 0 | ▶ | $out // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$out // ""
MC/DC0%0/1
$out // ""
$out
Truth table0%0/2
A
A= $out
| |||||||||||||||||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||
| 508 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||
| 510 | __END__ | ||||||||||||||||||||||||||||||||||||||||||