| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 59 | 14 | my $line = $code->[$i]; | |||||||||||||||||||||||||||
| 60 | 14 | ▶ | $line = $self->markup($line, $map->[$i]) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 72 | 71 | ▶ | my $hilite = $mark->[$i] ? $mark->[$i][0] : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 71 | ▶ | if ($current ne $hilite) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 16 | ▶ | $out .= '~/~' if $current; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 16 | ▶ | $out .= "~$hilite~" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | if $hilite; | ||||||||||||||||||||||||||||
| 77 | 16 | $current = $hilite; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 71 | $out .= substr($line, $i, 1); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 8 | ▶ | $out .= '~/~' if $current; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | last if $code->[$number] =~ /^\s*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 105 | } $start .. $end; | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | : /(.*?)(\d+)\@$/ | ||||||||||||||||||||||||||||
| 108 | 4 | ▶ | ? do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 120 | 8 | my $repeat_char = ''; | |||||||||||||||||||||||||||
| 121 | 8 | ▶ | for (my $i = 0; $i < length($line); $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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) || ''
MC/DC100%1/1
substr($dir, $i, 1) || ''
substr($dir, $i, 1)
Truth table100%2/2
A
A= substr($dir, $i, 1)
| |||||||||||||||||||||||||||||
| 124 | 71 | ▶ | $char = '' if $char eq ' '; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 71 | ▶ | $repeat_char = $char eq '+' | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 126 | ? substr($dir, $i - 1, 1) | ||||||||||||||||||||||||||||
| 127 | : $char | ||||||||||||||||||||||||||||
| 128 | ? '' | ||||||||||||||||||||||||||||
| 129 | : $repeat_char; | ||||||||||||||||||||||||||||
| 130 | 71 | ▶ | $char = undef if $char eq '+'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 71 | ▶ | $char ||= $repeat_char | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$char ||= $repeat_char
MC/DC0%0/2
$char ||= $repeat_char
$char
$repeat_char
Truth table33%1/3
A ||= B
A= $charB= $repeat_char
| |||||||||||||||||||||||||||||
| 132 | if $repeat_char; | ||||||||||||||||||||||||||||
| 133 | 71 | ▶ | push @{$map[$num - 1]->[$i]}, $char | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 147 | 20 | ▶ | last if $lines[0] =~ /^[rgbycmw\+\ ]+\d+(-\d+|\@)?$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 17 | push @code, shift @lines; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | 3 | ▶ | pop @code while (@code and $code[-1] eq ''); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@code and $code[-1] eq ""
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 ""
| |||||||||||||||||||||||||||||
| 151 | 3 | for (reverse @lines) { | |||||||||||||||||||||||||||
| 152 | 4 | ▶ | next unless ($_ or @directives); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_ or @directives
MC/DC0%0/2
$_ or @directives
$_
@directives
Truth table33%1/3
A or B
A= $_B= @directives
| |||||||||||||||||||||||||||||
| 153 | 4 | ▶ | last unless $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 4 | unshift @directives, $_; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | 3 | return (\@code, \@directives); | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | package Spork::Hilite; | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 3 | __DATA__ | |||||||||||||||||||||||||||