| 1 | package Plack::App::SourceViewer; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use parent qw/Plack::Component/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Plack::App::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Plack::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Syntax::Highlight::Engine::Kate; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Plack::Util::Accessor qw/ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | root | ||||||||||||||||||||||||||||
| 10 | encoding | ||||||||||||||||||||||||||||
| 11 | content_type | ||||||||||||||||||||||||||||
| 12 | ext_lang_map | ||||||||||||||||||||||||||||
| 13 | css | ||||||||||||||||||||||||||||
| 14 | 2 | /; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | my %EXT_LANG_MAP = ( | |||||||||||||||||||||||||||
| 19 | '.pm' => 'Perl', | ||||||||||||||||||||||||||||
| 20 | '.pl' => 'Perl', | ||||||||||||||||||||||||||||
| 21 | '.psgi' => 'Perl', | ||||||||||||||||||||||||||||
| 22 | '.t' => 'Perl', | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | our $DEFAULT_CSS = <<'_CSS_'; | |||||||||||||||||||||||||||
| 26 | body { font-size: 80%; font-family: "Consolas","Bitstream Vera Sans Mono","Courier New",Courier,monospace; } | ||||||||||||||||||||||||||||
| 27 | table { margin: 12px 0 32px 0; border-collapse: collapse; } | ||||||||||||||||||||||||||||
| 28 | td { white-space: nowrap; } | ||||||||||||||||||||||||||||
| 29 | .line-count { text-align: right; padding-right: 8px; } | ||||||||||||||||||||||||||||
| 30 | .alert { color: #0000ff; } | ||||||||||||||||||||||||||||
| 31 | .basen { color: #007f00; } | ||||||||||||||||||||||||||||
| 32 | .bstring { color: #c9a7ff; } | ||||||||||||||||||||||||||||
| 33 | .char { color: #ff00ff; } | ||||||||||||||||||||||||||||
| 34 | .comment { color: #7f7f7f; font-style: italic; } | ||||||||||||||||||||||||||||
| 35 | .datatype { color: #0000ff; } | ||||||||||||||||||||||||||||
| 36 | .decval { color: #00007f; } | ||||||||||||||||||||||||||||
| 37 | .error { color: #ff0000; font-weight: bold; font-style: italic; } | ||||||||||||||||||||||||||||
| 38 | .float { color: #00007f; } | ||||||||||||||||||||||||||||
| 39 | .function { color: #007f00; } | ||||||||||||||||||||||||||||
| 40 | .istring { color: #ff0000; } | ||||||||||||||||||||||||||||
| 41 | .keyword { font-weight: bold; } | ||||||||||||||||||||||||||||
| 42 | //.normal { color: #0000ff; } | ||||||||||||||||||||||||||||
| 43 | .operator { color: #eea000; } | ||||||||||||||||||||||||||||
| 44 | .others { color: #b03060; } | ||||||||||||||||||||||||||||
| 45 | .regionmaker { color: #96b9ff; font-style: italic; } | ||||||||||||||||||||||||||||
| 46 | .reserved { color: #9b30ff; font-weight: bold; } | ||||||||||||||||||||||||||||
| 47 | .string { color: #ff0000; } | ||||||||||||||||||||||||||||
| 48 | .variable { color: #0000ff; font-weight: bold; } | ||||||||||||||||||||||||||||
| 49 | .warning { color: #0000ff; font-weight: bold; font-style: italic; } | ||||||||||||||||||||||||||||
| 50 | _CSS_ | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 2 | our $SCRIPT = <<'_SCRIPT_'; | |||||||||||||||||||||||||||
| 53 | <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> | ||||||||||||||||||||||||||||
| 54 | <script> | ||||||||||||||||||||||||||||
| 55 | $(function(){ | ||||||||||||||||||||||||||||
| 56 | if ( location.hash.match(/\#L(\d+)$/) ) { | ||||||||||||||||||||||||||||
| 57 | var line = RegExp.$1; | ||||||||||||||||||||||||||||
| 58 | highlighter(line); | ||||||||||||||||||||||||||||
| 59 | window.scrollTo(0, $(window).scrollTop() - 100); | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | if ( location.hash.match(/\#L(\d+)\-L(\d+)$/) ) { | ||||||||||||||||||||||||||||
| 62 | var start = parseInt(RegExp.$1); | ||||||||||||||||||||||||||||
| 63 | var end = parseInt(RegExp.$2); | ||||||||||||||||||||||||||||
| 64 | if (start > end) { | ||||||||||||||||||||||||||||
| 65 | var tmp = start; start = end; end = tmp; | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | for (var i = start; i <= end; i++) { | ||||||||||||||||||||||||||||
| 68 | highlighter(i); | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | window.location.hash = 'L' + start; | ||||||||||||||||||||||||||||
| 71 | window.scrollTo(0, $(window).scrollTop() - 100); | ||||||||||||||||||||||||||||
| 72 | window.location.hash = 'L' + start + '-L' + end; | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | if ( location.hash.match(/\#L(\d+,[\d\,]+)$/) ) { | ||||||||||||||||||||||||||||
| 75 | var ft = RegExp.$1; | ||||||||||||||||||||||||||||
| 76 | var list = ft.split(','); | ||||||||||||||||||||||||||||
| 77 | for (var i in list) { | ||||||||||||||||||||||||||||
| 78 | highlighter(list[i]); | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | window.location.hash = 'L' + list.shift(); | ||||||||||||||||||||||||||||
| 81 | window.scrollTo(0, $(window).scrollTop() - 100); | ||||||||||||||||||||||||||||
| 82 | window.location.hash = 'L' + ft; | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | function highlighter(line) { | ||||||||||||||||||||||||||||
| 85 | if ($('#L'+line).length) { | ||||||||||||||||||||||||||||
| 86 | $('#L'+line).css('background-color', '#ffffcc'); | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | }); | ||||||||||||||||||||||||||||
| 90 | </script> | ||||||||||||||||||||||||||||
| 91 | _SCRIPT_ | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 3 | ▶ | sub prepare_app { | ||||||||||||||||||||||||||
|
Subroutine
prepare_app: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | $self->ext_lang_map({ | ||||||||||||||||||||||||||||
| 97 | %EXT_LANG_MAP, | ||||||||||||||||||||||||||||
| 98 | 3 | ▶ | %{$self->ext_lang_map || +{}}, | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->ext_lang_map || {}
MC/DC100%1/1
$self->ext_lang_map || {}
$self->ext_lang_map
Truth table100%2/2
A
A= $self->ext_lang_map
| |||||||||||||||||||||||||||||
| 99 | }); | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 3 | ▶ | if (!$self->css) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 1 | $self->css($DEFAULT_CSS); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 3 | ▶ | if (!$self->root) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 106 | 0 | $self->root(['.']); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | elsif (ref $self->root ne 'ARRAY') { | ||||||||||||||||||||||||||||
| 109 | 1 | $self->root([$self->root]); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 3 | ▶ | sub call { | ||||||||||||||||||||||||||
|
Subroutine
call: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 114 | 3 | my ($self, $env) = @_; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 3 | my $res; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 3 | for my $root (@{ $self->root }) { | |||||||||||||||||||||||||||
| 119 | 3 | ▶ | $self->{file}{$root} ||= Plack::App::File->new({ | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"file"}{$root} ||= "Plack::App::File"->new({"root", $root, "encoding", $self->encoding, "content_type", $self->content_type})
MC/DC0%0/2
$self->{"file"}{$root} ||= "Plack::App::File"->new({"root", $root, "encoding", $self->encoding, "content_type", $self->content_type})
$self->{"file"}{$root}
"Plack::App::File"->new({"root", $root, "encoding", $self->encoding, "content_type", $self->content_type})
Truth table66%2/3
A ||= B
A= $self->{"file"}{$root}B= "Plack::App::File"->new({"root", $root, "encoding", $self->encoding, "content_type", $self->content_type})
| |||||||||||||||||||||||||||||
| 120 | root => $root, | ||||||||||||||||||||||||||||
| 121 | encoding => $self->encoding, | ||||||||||||||||||||||||||||
| 122 | content_type => $self->content_type, | ||||||||||||||||||||||||||||
| 123 | }); | ||||||||||||||||||||||||||||
| 124 | 3 | $res = $self->{file}{$root}->call($env); | |||||||||||||||||||||||||||
| 125 | 3 | ▶ | if ($res && $res->[0] == 200) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$res and $res->[0] == 200
MC/DC50%1/2
$res and $res->[0] == 200
$res
$res->[0] == 200
Truth table66%2/3
A and B
A= $resB= $res->[0] == 200
| |||||||||||||||||||||||||||||
| 126 | 2 | $self->_filter_response($env, $res); | |||||||||||||||||||||||||||
| 127 | 2 | last; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 3 | return $res; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 2 | ▶ | sub _filter_response { | ||||||||||||||||||||||||||
|
Subroutine
_filter_response: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 135 | 2 | my ($self, $env, $res) = @_; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 2 | my $path = $env->{PATH_INFO}; | |||||||||||||||||||||||||||
| 138 | 2 | my ($ext) = ($path =~ m!.+(\..+)$!); | |||||||||||||||||||||||||||
| 139 | 2 | my $length; | |||||||||||||||||||||||||||
| 140 | my $body; | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 2 | ▶ | my $body_sub = sub { $body .= Plack::Util::encode_html($_[0]) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 143 | 2 | ▶ | if ( my $lang = $self->ext_lang_map->{$ext} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 1 | ▶ | $self->{highlighter}{$lang} ||= $self->_highlighter($lang); | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"highlighter"}{$lang} ||= $self->_highlighter($lang)
MC/DC0%0/2
$self->{"highlighter"}{$lang} ||= $self->_highlighter($lang)
$self->{"highlighter"}{$lang}
$self->_highlighter($lang)
Truth table33%1/3
A ||= B
A= $self->{"highlighter"}{$lang}B= $self->_highlighter($lang)
| |||||||||||||||||||||||||||||
| 145 | 1 | ▶ | $body_sub = sub { chomp $_[0]; $body .= $self->{highlighter}{$lang}->highlightText($_[0]) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 2 | Plack::Util::foreach($res->[2], $body_sub); | |||||||||||||||||||||||||||
| 149 | 2 | $body =~ s/\x0D\x0A|\x0D|\x0A/\n/g; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 2 | $res->[2] = []; | |||||||||||||||||||||||||||
| 152 | 2 | $length += $self->_body($res->[2], <<"_HTML_"); | |||||||||||||||||||||||||||
| 153 | <!DOCTYPE HTML><html> | ||||||||||||||||||||||||||||
| 154 | <head> | ||||||||||||||||||||||||||||
| 155 | 2 | <title>@{[Plack::Util::encode_html($path)]}</title> | |||||||||||||||||||||||||||
| 156 | <meta name="viewport" content="width=device-width, initial-scale=1;"> | ||||||||||||||||||||||||||||
| 157 | 2 | <style>@{[$self->css]}</style> | |||||||||||||||||||||||||||
| 158 | </head> | ||||||||||||||||||||||||||||
| 159 | <body> | ||||||||||||||||||||||||||||
| 160 | <table> | ||||||||||||||||||||||||||||
| 161 | _HTML_ | ||||||||||||||||||||||||||||
| 162 | 2 | my $line_count = 1; | |||||||||||||||||||||||||||
| 163 | 2 | for my $line ( split /\n/, $body ) { | |||||||||||||||||||||||||||
| 164 | 7 | $length += $self->_body( | |||||||||||||||||||||||||||
| 165 | $res->[2], | ||||||||||||||||||||||||||||
| 166 | qq|<tr id="L$line_count"><td class="line-count">$line_count</td><td>$line</td></tr>\n| | ||||||||||||||||||||||||||||
| 167 | ); | ||||||||||||||||||||||||||||
| 168 | 7 | $line_count++; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | 2 | $length += $self->_body( | |||||||||||||||||||||||||||
| 171 | $res->[2], | ||||||||||||||||||||||||||||
| 172 | "</table>$SCRIPT</body></html>", | ||||||||||||||||||||||||||||
| 173 | ); | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 2 | my $h = Plack::Util::headers($res->[1]); | |||||||||||||||||||||||||||
| 176 | 2 | $h->set('Content-Type', 'text/html'); | |||||||||||||||||||||||||||
| 177 | 2 | $h->set('Content-Length', $length); | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 11 | ▶ | sub _body { | ||||||||||||||||||||||||||
|
Subroutine
_body: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 181 | 11 | my ($self, $array, $html) = @_; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 11 | push @{$array}, $html; | |||||||||||||||||||||||||||
| 184 | 11 | return length $html; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 1 | ▶ | sub _highlighter { | ||||||||||||||||||||||||||
|
Subroutine
_highlighter: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 188 | 1 | my ($self, $language) = @_; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 1 | return Syntax::Highlight::Engine::Kate->new( | |||||||||||||||||||||||||||
| 191 | language => $language, | ||||||||||||||||||||||||||||
| 192 | substitutions => { | ||||||||||||||||||||||||||||
| 193 | "<" => "<", | ||||||||||||||||||||||||||||
| 194 | ">" => ">", | ||||||||||||||||||||||||||||
| 195 | "&" => "&", | ||||||||||||||||||||||||||||
| 196 | " " => " ", | ||||||||||||||||||||||||||||
| 197 | "\t" => " ", | ||||||||||||||||||||||||||||
| 198 | }, | ||||||||||||||||||||||||||||
| 199 | format_table => { | ||||||||||||||||||||||||||||
| 200 | Alert => [ qq|<span class="alert">|, "</span>" ], | ||||||||||||||||||||||||||||
| 201 | BaseN => [ qq|<span class="basen">|, "</span>" ], | ||||||||||||||||||||||||||||
| 202 | BString => [ qq|<span class="bstring">|, "</span>" ], | ||||||||||||||||||||||||||||
| 203 | Char => [ qq|<span class="cahr">|, "</span>" ], | ||||||||||||||||||||||||||||
| 204 | Comment => [ qq|<span class="comment"><i>|, "</span>" ], | ||||||||||||||||||||||||||||
| 205 | DataType => [ qq|<span class="datatype">|, "</span>" ], | ||||||||||||||||||||||||||||
| 206 | DecVal => [ qq|<span class="decval">|, "</span>" ], | ||||||||||||||||||||||||||||
| 207 | Error => [ qq|<span class="error">|, "</span>" ], | ||||||||||||||||||||||||||||
| 208 | Float => [ qq|<span class="float">|, "</span>" ], | ||||||||||||||||||||||||||||
| 209 | Function => [ qq|<span class="function">|, "</span>" ], | ||||||||||||||||||||||||||||
| 210 | IString => [ qq|<span class="istring">|, "</span>" ], | ||||||||||||||||||||||||||||
| 211 | Keyword => [ qq|<span class="keyword">|, "</span>" ], | ||||||||||||||||||||||||||||
| 212 | Normal => [ "", "" ], | ||||||||||||||||||||||||||||
| 213 | Operator => [ qq|<span class="operator">|, "</span>" ], | ||||||||||||||||||||||||||||
| 214 | Others => [ qq|<span class="others">|, "</span>" ], | ||||||||||||||||||||||||||||
| 215 | RegionMarker => [ qq|<span class="regionmaker">|, "</span>" ], | ||||||||||||||||||||||||||||
| 216 | Reserved => [ qq|<span class="reserved">|, "</span>" ], | ||||||||||||||||||||||||||||
| 217 | String => [ qq|<span class="string">|, "</span>"], | ||||||||||||||||||||||||||||
| 218 | Variable => [ qq|<span class="variable">|, "</span>" ], | ||||||||||||||||||||||||||||
| 219 | Warning => [ qq|<span class="warning">|, "</span>" ], | ||||||||||||||||||||||||||||
| 220 | }, | ||||||||||||||||||||||||||||
| 221 | ); | ||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 2 | 1; | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | __END__ | ||||||||||||||||||||||||||||