blib/lib/Spork/Hilite.pm

filter: Statementstmt 80.8% 97 / 120 Branchbran 78.8% 41 / 52 Conditioncond 54.5% 6 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 77.7% 14 / 18 Podpod 50.0% 1 / 2 totaltotal 76.6% 161 / 210 CC 34 SCAR 21.3
CC
34
Cov
79%
parse
15.1
detect_paragraphs
12.0
split
9.6
CRAP 8.4
↑ Summary
1 package Spork::Hilite;
2 3 our $VERSION = '0.11';
3  
4 3 ▶use Kwiki::Plugin -Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Kwiki::Installer -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 const class_title => 'Color Hiliting for Spork';
8 3 const class_id => 'hilite';
9 3 const css_file => 'hilite.css';
10  
11 0 ▶sub register {
Subroutine register: not called CC=1 SCAR=6.9
Pod undocumented
12 0 my $registry = shift;
13 0 $registry->add(preload => 'hilite');
14 0 $registry->add(wafl => hilite => 'Spork::Hilite::Wafl');
15 }
16  
17 0 ▶sub init {
Subroutine init: not called CC=1 SCAR=6.9
Pod documented
18 0 $self->hub->css->add_file($self->css_file);
19 }
20  
21 package Spork::Hilite::Wafl;
22 3 ▶use Spoon::Formatter;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use base 'Spoon::Formatter::WaflBlock';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 0 ▶sub QQQ {
Subroutine QQQ: not called CC=2 SCAR=10.9
26 3 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 0 ▶ XXX(@_) if ++$::xxx == 3;
Branch: 1 branch
BranchTrueFalseTotal
if ++$main::xxx == 3 0 0 0
28 0 @_;
29 }
30  
31 3 ▶sub to_html {
Subroutine to_html: called CC=1 SCAR=0.0
32 3 my ($code, $directives) = $self->split;
33 3 my $map = $self->parse($code, $directives);
34 3 my $formatted = $self->format($code, $map);
35 3 return $self->escape($formatted);
36 }
37  
38 3 my %color_map = (
39 r => 'red',
40 g => 'green',
41 b => 'blue',
42 c => 'cyan',
43 m => 'magenta',
44 y => 'yellow',
45 w => 'white',
46 );
47 3 ▶sub escape {
Subroutine escape: called CC=1 SCAR=3.1
48 3 require CGI;
49 0 my $output = CGI::escapeHTML(shift);
50 0 $output =~ s/~([rgbycmw])~/qq[<span class="hilite_$color_map{$1}">]/eg;
51 0 $output =~ s!~/~!</span>!g;
52 0 join '', map "$_\n", "<pre>", $output, "</pre>";
53 }
54  
55 3 ▶sub format {
Subroutine format: called CC=3 SCAR=11.0
56 3 my ($code, $map) = @_;
57 3 my @output;
58 3 ▶ for (my $i = 0; $i < @$code; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$code) 14 3 17
59 14 my $line = $code->[$i];
60 14 ▶ $line = $self->markup($line, $map->[$i])
Branch: 1 branch
BranchTrueFalseTotal
if defined $map->[$i] 8 6 14
61 if defined $map->[$i];
62 14 push @output, $line;
63 }
64 3 join "\n", @output;
65 }
66  
67 8 ▶sub markup {
Subroutine markup: called CC=7 SCAR=19.5
68 8 my ($line, $mark) = @_;
69 8 my $out = '';
70 8 my $current = '';
71 8 ▶ for (my $i = 0; $i < length($line); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $line) 71 8 79
72 71 ▶ my $hilite = $mark->[$i] ? $mark->[$i][0] : '';
Branch: 1 branch
BranchTrueFalseTotal
$mark->[$i] ? : 65 6 71
73 71 ▶ if ($current ne $hilite) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current ne $hilite) 16 55 71
74 16 ▶ $out .= '~/~' if $current;
Branch: 1 branch
BranchTrueFalseTotal
if $current 4 12 16
75 16 ▶ $out .= "~$hilite~"
Branch: 1 branch
BranchTrueFalseTotal
if $hilite 12 4 16
76 if $hilite;
77 16 $current = $hilite;
78 }
79 71 $out .= substr($line, $i, 1);
80 }
81 8 ▶ $out .= '~/~' if $current;
Branch: 1 branch
BranchTrueFalseTotal
if $current 8 0 8
82 8 return $out;
83 }
84  
85 0 ▶sub detect_paragraphs {
Subroutine detect_paragraphs: not called CC=3 SCAR=24.8
86 0 my ($line, $number, $code) = @_;
87 0 my $start = $number;
88 0 ▶ while ($number < @$code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number < @$code) 0 0 0
89 0 ▶ last if $code->[$number] =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $code->[$number] =~ /^\s*$/ 0 0 0
90 0 $number++;
91 }
92 map {
93 0 "$line$_";
94 } $start .. $number;
95 }
96  
97 3 ▶sub parse {
Subroutine parse: called CC=15 SCAR=27.1
98 3 my ($code, $directives) = @_;
99 my @directives = map {
100 3 /(.*?)(\d+)-(\d+)$/
101 ? do {
102 2 my ($line, $start, $end) = ($1, $2, $3);
103 map {
104 2 ▶ "$line$_";
Branch: 1 branch
BranchTrueFalseTotal
/(.*?)(\d+)\@$/ ? : 0 2 2
105 } $start .. $end;
106 }
107 : /(.*?)(\d+)\@$/
108 4 ▶ ? do {
Branch: 1 branch
BranchTrueFalseTotal
/(.*?)(\d+)-(\d+)$/ ? : 2 2 4
109 0 $self->detect_paragraphs($1, $2, $code);
110 }
111 : $_;
112 } @$directives;
113 3 my @map;
114 3 my $num = 0;
115 3 for my $line (@$code) {
116 14 $num += 1;
117 14 for (@directives) {
118 49 my $dir = $_;
119 49 ▶ next unless $dir =~ s/\s+$num$//;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir =~ s/\s+$num$// 41 8 49
120 8 my $repeat_char = '';
121 8 ▶ for (my $i = 0; $i < length($line); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $line) 71 8 79
122 3 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
123 71 ▶ my $char = substr($dir, $i, 1) || '';
Condition100%2/2
substr($dir, $i, 1) || ''
l!l
4130
MC/DC100%1/1
substr($dir, $i, 1) || ''
substr($dir, $i, 1)
Truth table100%2/2
A
A= substr($dir, $i, 1)
Aresult
11
00
124 71 ▶ $char = '' if $char eq ' ';
Branch: 1 branch
BranchTrueFalseTotal
if $char eq " " 6 65 71
125 71 ▶ $repeat_char = $char eq '+'
Branch: 2 branches
BranchTrueFalseTotal
$char eq '+' ? : 5 66 71
$char ? : 30 36 66
126 ? substr($dir, $i - 1, 1)
127 : $char
128 ? ''
129 : $repeat_char;
130 71 ▶ $char = undef if $char eq '+';
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "+" 5 66 71
131 71 ▶ $char ||= $repeat_char
Branch: 1 branch
BranchTrueFalseTotal
if $repeat_char 35 36 71
Condition33%1/3
$char ||= $repeat_char
l!l&&r!l&&!r
0350
MC/DC0%0/2
$char ||= $repeat_char
$char $repeat_char
Truth table33%1/3
A ||= B
A= $charB= $repeat_char
ABresult
1X1
011
000
132 if $repeat_char;
133 71 ▶ push @{$map[$num - 1]->[$i]}, $char
Branch: 1 branch
BranchTrueFalseTotal
if $char 65 6 71
134 if $char;
135 }
136 }
137 }
138 3 return \ @map;
139 }
140  
141 3 ▶sub split {
Subroutine split: called CC=9 SCAR=22.6
142 3 my $text = $self->text;
143 3 my @lines = map {chomp; $_} ($text =~ /^(.*\n?)/gm);
144 3 my(@code, @directives);
145
146 3 ▶ while (@lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) 20 0 20
147 20 ▶ last if $lines[0] =~ /^[rgbycmw\+\ ]+\d+(-\d+|\@)?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $lines[0] =~ /^[rgbycmw\+\ ]+\d+(-\d+|\@)?$/ 3 17 20
148 17 push @code, shift @lines;
149 }
150 3 ▶ pop @code while (@code and $code[-1] eq '');
Branch: 1 branch
BranchTrueFalseTotal
@code and $code[-1] eq "" and do { pop @code } 3 3 6
Condition66%2/3
@code and $code[-1] eq ""
!ll&&!rl&&r
033
MC/DC50%1/2
@code and $code[-1] eq ""
@code $code[-1] eq ""
Truth table66%2/3
A and B
A= @codeB= $code[-1] eq ""
ABresult
0X0
100
111
151 3 for (reverse @lines) {
152 4 ▶ next unless ($_ or @directives);
Branch: 1 branch
BranchTrueFalseTotal
unless $_ or @directives 0 4 4
Condition33%1/3
$_ or @directives
l!l&&r!l&&!r
400
MC/DC0%0/2
$_ or @directives
$_ @directives
Truth table33%1/3
A or B
A= $_B= @directives
ABresult
1X1
011
000
153 4 ▶ last unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 0 4 4
154 4 unshift @directives, $_;
155 }
156 3 return (\@code, \@directives);
157 }
158  
159 package Spork::Hilite;
160  
161 3 __DATA__
↑ Summary