| 1 | package Text::ANSI::Highlight; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | our $VERSION = "0.03"; | |||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use v5.16; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use File::Temp (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use POSIX (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | # The highlighter which is used when none is given | ||||||||||||||||||||||||||||||||||||||||||
| 13 | 5 | our $HIGHLIGHTER = 'pandoc'; | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | # Short names of the highlighters | ||||||||||||||||||||||||||||||||||||||||||
| 16 | 5 | 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 | 5 | 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 | 5 | 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 name of a highlighter for a name or a short name, or undef when | ||||||||||||||||||||||||||||||||||||||||||
| 60 | # there is no such highlighter. The module is looked for, and not | ||||||||||||||||||||||||||||||||||||||||||
| 61 | # loaded: a highlighter is a module which has the function highlight. | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub highlighter { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
highlighter: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | my($class, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | $name =~ s/:.*//s; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | $name =~ /\A\w+\z/ or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | my $file = __PACKAGE__ =~ s{::}{/}gr . "/$name.pm"; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | for my $dir (grep { !ref } @INC) { | |||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | open my $fh, '<', "$dir/$file" or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | local $/; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | return <$fh> =~ /^sub highlight\b/m ? $name : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | undef; | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | # The parameter is "with", the name of the highlighter. With an odd | ||||||||||||||||||||||||||||||||||||||||||
| 77 | # number of arguments, the first is taken for it: new('pandoc') is | ||||||||||||||||||||||||||||||||||||||||||
| 78 | # new(with => 'pandoc'). | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | unshift @_, 'with' if @_ % 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | my %arg = @_; | |||||||||||||||||||||||||||||||||||||||||
| 83 | # with => "name:style" is the highlighter and its style; the style | ||||||||||||||||||||||||||||||||||||||||||
| 84 | # parameter wins over it | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | $style = delete $arg{style} if exists $arg{style}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 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 ""
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 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 ""
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | croak join(', ', sort keys %arg) . ': unknown parameter' if %arg; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | $name =~ /\A\w+\z/ or croak "$name: invalid highlighter"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | my $module = __PACKAGE__ . "::$name"; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | my $file = $module =~ s{::}{/}gr . '.pm'; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | unless (eval { require $file; 1 }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | croak $@ unless $@ =~ /^Can't locate \Q$file\E /; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | croak "$name: no such highlighter"; | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | bless { name => $name, module => $module, style => $style }, $class; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub name { $_[0]{name} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | sub style { $_[0]{style} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
style: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | # The functions of the highlighter. The style is given in the | ||||||||||||||||||||||||||||||||||||||||||
| 105 | # variable $STYLE of its package, for the time of the call: the | ||||||||||||||||||||||||||||||||||||||||||
| 106 | # highlighter makes the option of its command from it. | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | sub _call { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_call: not called CC=1 SCAR=1.7
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | my($obj, $func) = splice @_, 0, 2; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | local ${"$obj->{module}::STYLE"} = $obj->{style}; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | $obj->{module}->can($func)->(@_); | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | # The name of a language for the highlighter, or undef when it does | ||||||||||||||||||||||||||||||||||||||||||
| 115 | # not know it. Markdown is not a language here: a Markdown text is | ||||||||||||||||||||||||||||||||||||||||||
| 116 | # colored by the markdown method. | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub language { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
language: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | my($obj, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | $name = lc($name // ''); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$name // ""
MC/DC0%0/1
$name // ""
$name
Truth table0%0/2
A
A= $name
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | return undef if $name eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | # the name here first: a highlighter can take the same name for | ||||||||||||||||||||||||||||||||||||||||||
| 122 | # another language, as source-highlight takes pl for Prolog | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 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= ()
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | next if $_ eq 'markdown'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | my $lang = $obj->_call(language => $_); | |||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | return $lang if defined $lang; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | undef; | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | # Color texts. Takes [ language, text ] of each, and returns as many | ||||||||||||||||||||||||||||||||||||||||||
| 132 | # texts, undef for one which is not colored: one with a language which | ||||||||||||||||||||||||||||||||||||||||||
| 133 | # the highlighter does not know, an empty one, and one which has an | ||||||||||||||||||||||||||||||||||||||||||
| 134 | # escape sequence already. All are undef when the command of the | ||||||||||||||||||||||||||||||||||||||||||
| 135 | # highlighter is not there. | ||||||||||||||||||||||||||||||||||||||||||
| 136 | # | ||||||||||||||||||||||||||||||||||||||||||
| 137 | # Only the colors are taken from the highlighter: when the text it | ||||||||||||||||||||||||||||||||||||||||||
| 138 | # gives is not that of the input, the input is left as it is. | ||||||||||||||||||||||||||||||||||||||||||
| 139 | # | ||||||||||||||||||||||||||||||||||||||||||
| 140 | # Texts of characters give texts of characters, and those of bytes | ||||||||||||||||||||||||||||||||||||||||||
| 141 | # (UTF-8) give bytes. | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub highlight { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
highlight: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | my $obj = shift; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | my @code = @_; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | my @ret = (undef) x @code; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | my $chars = grep { utf8::is_utf8($_->[1]) } @code; | |||||||||||||||||||||||||||||||||||||||||
| 147 | # All go to the highlighter in one call: the texts, and the pieces | ||||||||||||||||||||||||||||||||||||||||||
| 148 | # of those which are Markdown | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | my(@job, %markdown); | |||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | for my $i (0 .. $#code) { | |||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | my($name, $text) = @{$code[$i]}; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | if (lc($name // '') =~ /\A(?:markdown|md|mkd)\z/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/2
$name // ""
MC/DC0%0/1
$name // ""
$name
Truth table0%0/2
A
A= $name
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | $markdown{$i} = $obj->_markdown_plan($text, \@job, $chars); | |||||||||||||||||||||||||||||||||||||||||
| 154 | } elsif (defined(my $lang = $obj->language($name))) { | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | push @job, { lang => $lang, text => $text, slot => \$ret[$i] }; | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | $obj->_run(\@job, $chars); | |||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | $ret[$_] = $markdown{$_}->() for keys %markdown; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | @ret; | |||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | # Give the texts of the jobs to the highlighter in one call, and put | ||||||||||||||||||||||||||||||||||||||||||
| 164 | # what comes back in the slot of each job, when it is the text in | ||||||||||||||||||||||||||||||||||||||||||
| 165 | # colors. A job is { lang, text, slot }, and gets ok when it is done. | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | sub _run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_run: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | my($obj, $job, $chars) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | my(@todo, @pair, @crlf); | |||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | for my $j (@$job) { | |||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | my $text = $j->{text}; | |||||||||||||||||||||||||||||||||||||||||
| 171 | 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)
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | push @todo, $j; | |||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | push @pair, [ $j->{lang}, $text ]; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | push @crlf, $crlf; | |||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | @pair or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | my @out = $obj->_call(highlight => @pair); | |||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | for my $i (0 .. $#todo) { | |||||||||||||||||||||||||||||||||||||||||
| 179 | 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)
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | ${ $todo[$i]{slot} } = $color; | |||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | $todo[$i]{ok} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | # Make a text ready for the highlighter: bytes, and LF for CR LF. | ||||||||||||||||||||||||||||||||||||||||||
| 186 | # Returns undef for a text which is not to be colored, or whether it | ||||||||||||||||||||||||||||||||||||||||||
| 187 | # had CR LF. | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | sub _prepare { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_prepare: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | my($text, $chars) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 190 | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | ▶ | utf8::encode($$text) if $chars; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | # a highlighter can give LF for CR LF: take CR away, when every | ||||||||||||||||||||||||||||||||||||||||||
| 193 | # line has it | ||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | ▶ | my $crlf = $$text =~ /\r\n/ ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | ▶ | if ($crlf) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | ▶ | return undef if $$text =~ /(?<!\r)\n/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | $$text =~ s/\r\n/\n/g; | |||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | return undef if $$text =~ /\r/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | $crlf; | |||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | # Take what the highlighter gave, when it is the text in colors | ||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | ▶ | sub _accept { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_accept: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | my($color, $text, $crlf, $chars) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | ▶ | defined $color or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | # the newlines at the ends are those of the input | ||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | $color =~ s/\A\n+//; | |||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | $color =~ s/\n*\z//; | |||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | ▶ | $color = $1 . $color if $text =~ /\A(\n+)(?=.)/s; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | ▶ | $color .= $1 if $text =~ /(\n+)\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | # only the colors are taken | ||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | ▶ | return undef unless $color =~ s/\e\[[\d;]*m//gr eq $text; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | ▶ | return undef unless $color =~ /\e\[[\d;]*[1-9][\d;]*m/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | ▶ | if ($crlf) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | $color =~ s/((?:\e\[[\d;]*m)*)\n/$1\r\n/g; | |||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | ▶ | utf8::decode($color) if $chars; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | $color; | |||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | # A fenced code block of Markdown: the closing fence is of the same | ||||||||||||||||||||||||||||||||||||||||||
| 223 | # character as the opening one, and at least as long (CommonMark). | ||||||||||||||||||||||||||||||||||||||||||
| 224 | # $4 is the info string, $5 the code. A fence which is not closed is | ||||||||||||||||||||||||||||||||||||||||||
| 225 | # not a block. | ||||||||||||||||||||||||||||||||||||||||||
| 226 | 5 | my $FENCE = qr{^( {0,3})((`|~)\3{2,})(.*)\n((?s:.*?))^( {0,3})(\2\3*)(\h*)$}m; | |||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | # Color a Markdown text: the code in the fences, each by the language | ||||||||||||||||||||||||||||||||||||||||||
| 229 | # of its fence, and the text around them as Markdown, when the | ||||||||||||||||||||||||||||||||||||||||||
| 230 | # highlighter knows Markdown. The lines of the fences are as they | ||||||||||||||||||||||||||||||||||||||||||
| 231 | # are. Returns undef when nothing is colored. | ||||||||||||||||||||||||||||||||||||||||||
| 232 | # | ||||||||||||||||||||||||||||||||||||||||||
| 233 | # The text around the code is given to the highlighter in pieces | ||||||||||||||||||||||||||||||||||||||||||
| 234 | # which have no fence in them. A definition of Markdown which finds | ||||||||||||||||||||||||||||||||||||||||||
| 235 | # the fences by itself can take the rest of the text for the code of | ||||||||||||||||||||||||||||||||||||||||||
| 236 | # a fence, as that of skylighting does. A piece which has a fence, | ||||||||||||||||||||||||||||||||||||||||||
| 237 | # one which is not closed, is left as it is. | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | ▶ | sub markdown { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
markdown: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | my($obj, $text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | my @job; | |||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | my $build = $obj->_markdown_plan($text, \@job, utf8::is_utf8($text)); | |||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | $obj->_run(\@job, utf8::is_utf8($text)); | |||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | $build->(); | |||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | # Divide a Markdown text into pieces, add the jobs for them to the | ||||||||||||||||||||||||||||||||||||||||||
| 247 | # list, and return a function which puts the text together again when | ||||||||||||||||||||||||||||||||||||||||||
| 248 | # the jobs are done: the text with colors, or undef when nothing of it | ||||||||||||||||||||||||||||||||||||||||||
| 249 | # is colored. Markdown in a fence is divided in the same way, and its | ||||||||||||||||||||||||||||||||||||||||||
| 250 | # pieces go to the same list. | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | ▶ | sub _markdown_plan { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_markdown_plan: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | my($obj, $text, $job, $chars) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 253 | # the fences are found in a text of LF; CR comes back at the end, | ||||||||||||||||||||||||||||||||||||||||||
| 254 | # when every line has it | ||||||||||||||||||||||||||||||||||||||||||
| 255 | 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)
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | ▶ | $text =~ s/\r\n/\n/g if $crlf; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | my $markdown = $obj->_call('markdown'); | |||||||||||||||||||||||||||||||||||||||||
| 258 | # the pieces: the text, the opening fence, the code, the closing | ||||||||||||||||||||||||||||||||||||||||||
| 259 | # fence, and so on, to the text at the end | ||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | my(@piece, @mine, @nested); | |||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | ▶ | my $add = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | my($lang, $slot) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | push @$job, my $j = { lang => $lang, text => $$slot, slot => $slot }; | |||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | push @mine, $j; | |||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | my $prose = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | push @piece, shift; | |||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | ▶ | return unless $markdown; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 269 | # not a piece which has a fence, or front matter which is not | ||||||||||||||||||||||||||||||||||||||||||
| 270 | # closed at the top | ||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | ▶ | return if $piece[-1] =~ /^ {0,3}(?:`{3,}|~{3,})/m; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | and $piece[-1] !~ /\A.*\n(?s:.*?)^(?:---|\.\.\.)\h*$/m; | ||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | $add->(markdown => \$piece[-1]); | |||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | my $pos = 0; | |||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | ▶ | while ($text =~ /$FENCE/g) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | my($start, $end, $from, $to, $info) = ($-[0], $+[0], $-[5], $+[5], $4); | |||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | $prose->(substr $text, $pos, $start - $pos); | |||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | push @piece, | |||||||||||||||||||||||||||||||||||||||||
| 281 | substr($text, $start, $from - $start), | ||||||||||||||||||||||||||||||||||||||||||
| 282 | substr($text, $from, $to - $from), | ||||||||||||||||||||||||||||||||||||||||||
| 283 | substr($text, $to, $end - $to); | ||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | $pos = $end; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | my $code = \$piece[-2]; | |||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | ▶ | my $name = $info =~ /^\h*([\w+#.-]+)/ ? $1 : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | ▶ | if (lc($name) =~ /\A(?:markdown|md|mkd)\z/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | # Markdown in a fence is a Markdown text of its own | ||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | push @nested, [ $code, $obj->_markdown_plan($$code, $job, $chars) ]; | |||||||||||||||||||||||||||||||||||||||||
| 290 | } elsif (defined(my $lang = $obj->language($name))) { | ||||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | $add->($lang => $code); | |||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | $prose->(substr $text, $pos); | |||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | my $colored = grep { $_->{ok} } @mine; | |||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | for (@nested) { | |||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | my($code, $build) = @$_; | |||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | ▶ | my $color = $build->() // next; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
&$build() // (next)
MC/DC0%0/1
&$build() // (next)
&$build()
Truth table0%0/2
A
A= &$build()
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | $$code = $color; | |||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | $colored++; | |||||||||||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||||||||||
| 304 | 0 | ▶ | $colored or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | my $result = join '', @piece; | |||||||||||||||||||||||||||||||||||||||||
| 306 | 0 | ▶ | $result =~ s/((?:\e\[[\d;]*m)*)\n/$1\r\n/g if $crlf; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | 0 | $result; | |||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | # The names of interpreters which are not the names of a language | ||||||||||||||||||||||||||||||||||||||||||
| 312 | 5 | my %INTERPRETER = ( | |||||||||||||||||||||||||||||||||||||||||
| 313 | node => 'javascript', | ||||||||||||||||||||||||||||||||||||||||||
| 314 | tclsh => 'tcl', | ||||||||||||||||||||||||||||||||||||||||||
| 315 | ksh => 'bash', | ||||||||||||||||||||||||||||||||||||||||||
| 316 | dash => 'bash', | ||||||||||||||||||||||||||||||||||||||||||
| 317 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | # The language of a script, by the interpreter on its first line | ||||||||||||||||||||||||||||||||||||||||||
| 320 | # (#!/usr/bin/env perl, #!/usr/bin/python3): the name of the command, | ||||||||||||||||||||||||||||||||||||||||||
| 321 | # after env and its options and variables, without the version number. | ||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | ▶ | sub interpreter { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
interpreter: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | ▶ | my $line = shift // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() // ""
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | ▶ | $line =~ /\A#!\s*(\S.*)/ or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | my @word = split ' ', $1; | |||||||||||||||||||||||||||||||||||||||||
| 326 | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 327 | 0 | ▶ | if ($command =~ m{(?:\A|/)env\z}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 328 | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | my $name = $command =~ s{.*/}{}r =~ s/[\d.]+\z//r; | |||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | ▶ | return undef if $name eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 333 | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | ▶ | sub is_markdown { $_[0] =~ /\.(?:md|markdown|mkd)\z/i } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_markdown: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | # The language of a file: by its extension, by its name, and by the | ||||||||||||||||||||||||||||||||||||||||||
| 339 | # interpreter on its first line. | ||||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | ▶ | sub file_language { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
file_language: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | my($obj, $file, $content) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | my $base = $file =~ s{.*/}{}r; | |||||||||||||||||||||||||||||||||||||||||
| 343 | 0 | my($ext) = $base =~ /\.([^.]+)\z/; | |||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | my($line) = $content =~ /\A(.*)/; | |||||||||||||||||||||||||||||||||||||||||
| 345 | 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= ()
| |||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | my $lang = $obj->language($_); | |||||||||||||||||||||||||||||||||||||||||
| 347 | 0 | ▶ | return $lang if defined $lang; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | undef; | |||||||||||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | # Color the content of a file, by the language its name tells; a | ||||||||||||||||||||||||||||||||||||||||||
| 353 | # Markdown file by the markdown method. The language can be given. | ||||||||||||||||||||||||||||||||||||||||||
| 354 | # Returns undef when it is not colored: a file whose language is not | ||||||||||||||||||||||||||||||||||||||||||
| 355 | # known, one which has an escape character, an empty one, and one | ||||||||||||||||||||||||||||||||||||||||||
| 356 | # larger than $MAX_SIZE. | ||||||||||||||||||||||||||||||||||||||||||
| 357 | 0 | ▶ | sub file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
file: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 358 | 0 | my($obj, $file, %opt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 359 | 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)
| |||||||||||||||||||||||||||||||||||||||||||
| 360 | 0 | $obj->text($content, name => $file, %opt); | |||||||||||||||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||
| 363 | # Color a text. The language is given by language, or told from the | ||||||||||||||||||||||||||||||||||||||||||
| 364 | # name of a file given by name. | ||||||||||||||||||||||||||||||||||||||||||
| 365 | 0 | ▶ | sub text { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
text: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 366 | 0 | my($obj, $text, %opt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 367 | 0 | my $lang = $opt{language}; | |||||||||||||||||||||||||||||||||||||||||
| 368 | 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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 369 | 0 | ▶ | if (defined $lang ? lc($lang) =~ /\A(?:markdown|md|mkd)\z/ : is_markdown($name)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | return $obj->markdown($text); | |||||||||||||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||||||||||||
| 372 | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | ($obj->highlight([ $lang, $text ]))[0]; | |||||||||||||||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | ▶ | sub _content { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_content: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||
| 377 | 0 | my $file = shift; | |||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | ▶ | return undef unless -f $file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 379 | 0 | ▶ | my $size = -s _ or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 380 | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 381 | 0 | ▶ | open my $fh, '<:raw', $file or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 382 | 0 | local $/; | |||||||||||||||||||||||||||||||||||||||||
| 383 | 0 | <$fh>; | |||||||||||||||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | # Read the standard input and print it in colors, or as it is when it | ||||||||||||||||||||||||||||||||||||||||||
| 387 | # is not colored. The arguments are the options of the highlighter | ||||||||||||||||||||||||||||||||||||||||||
| 388 | # (-w, --with) and of the language (-l, --language), which is | ||||||||||||||||||||||||||||||||||||||||||
| 389 | # Markdown by default. | ||||||||||||||||||||||||||||||||||||||||||
| 390 | # | ||||||||||||||||||||||||||||||||||||||||||
| 391 | # This is to be called as a filter: by the tee module of greple, which | ||||||||||||||||||||||||||||||||||||||||||
| 392 | # gives the parts of a file to a function and puts back what it prints | ||||||||||||||||||||||||||||||||||||||||||
| 393 | # (greple -Mtee '&Text::ANSI::Highlight::filter' -- ...). The lines | ||||||||||||||||||||||||||||||||||||||||||
| 394 | # are those of the input, and only colors are added, so tee can put | ||||||||||||||||||||||||||||||||||||||||||
| 395 | # them back by the number of lines; and code blocks of Markdown, given | ||||||||||||||||||||||||||||||||||||||||||
| 396 | # with their fences, are colored each by itself. | ||||||||||||||||||||||||||||||||||||||||||
| 397 | 0 | ▶ | sub filter { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
filter: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 398 | 0 | require Getopt::Long; | |||||||||||||||||||||||||||||||||||||||||
| 399 | 0 | my %opt = (language => 'markdown'); | |||||||||||||||||||||||||||||||||||||||||
| 400 | 0 | ▶ | Getopt::Long::GetOptionsFromArray( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 401 | [ @_ ], \%opt, 'with|w=s', 'style|s=s', 'language|l=s') | ||||||||||||||||||||||||||||||||||||||||||
| 402 | or croak 'filter: invalid option'; | ||||||||||||||||||||||||||||||||||||||||||
| 403 | my $hl = __PACKAGE__->new( | ||||||||||||||||||||||||||||||||||||||||||
| 404 | with => $opt{with} // '', | ||||||||||||||||||||||||||||||||||||||||||
| 405 | 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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 406 | 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
}
| |||||||||||||||||||||||||||||||||||||||||||
| 407 | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | # Run a command with the text as its input, and return what it | ||||||||||||||||||||||||||||||||||||||||||
| 411 | # writes, or undef when it fails. Its messages are thrown away. | ||||||||||||||||||||||||||||||||||||||||||
| 412 | # For the highlighters. | ||||||||||||||||||||||||||||||||||||||||||
| 413 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: not called CC=9 SCAR=43.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 414 | 0 | my($command, $input) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | my $tmp = File::Temp->new; | |||||||||||||||||||||||||||||||||||||||||
| 416 | 0 | binmode $tmp; | |||||||||||||||||||||||||||||||||||||||||
| 417 | 0 | print $tmp $input; | |||||||||||||||||||||||||||||||||||||||||
| 418 | 0 | $tmp->flush; | |||||||||||||||||||||||||||||||||||||||||
| 419 | 0 | my $pid = open my $fh, '-|'; | |||||||||||||||||||||||||||||||||||||||||
| 420 | 0 | ▶ | defined $pid or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 421 | 0 | ▶ | if ($pid == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 422 | # by the file descriptors: STDIN of the caller may not be on 0, | ||||||||||||||||||||||||||||||||||||||||||
| 423 | # as when it is a handle on a string | ||||||||||||||||||||||||||||||||||||||||||
| 424 | 0 | ▶ | open my $in, '<', $tmp->filename or POSIX::_exit(126); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 425 | 0 | ▶ | open my $null, '>', File::Spec->devnull or POSIX::_exit(126); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 426 | 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)
| |||||||||||||||||||||||||||||||||||||||||||
| 427 | 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)
| |||||||||||||||||||||||||||||||||||||||||||
| 428 | 0 | ▶ | { no warnings 'exec'; exec @$command } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.6
| |||||||||||||||||||||||||||||||||||||||||||
| 429 | 0 | POSIX::_exit(127); | |||||||||||||||||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||||||||||||||||
| 431 | 0 | binmode $fh; | |||||||||||||||||||||||||||||||||||||||||
| 432 | 0 | my $out = do { local $/; <$fh> }; | |||||||||||||||||||||||||||||||||||||||||
| 433 | 0 | ▶ | close $fh or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 434 | 0 | ▶ | $out // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$out // ""
MC/DC0%0/1
$out // ""
$out
Truth table0%0/2
A
A= $out
| |||||||||||||||||||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||
| 439 | __END__ | ||||||||||||||||||||||||||||||||||||||||||