1 |
package Frey::Web; |
package Frey::Web; |
2 |
use Moose::Role; |
use Moose::Role; |
3 |
|
|
4 |
with 'Frey::Backtrace'; |
with 'Frey::Session'; |
5 |
|
|
6 |
use Frey::Types; |
use Frey::Types; |
7 |
|
|
8 |
use Continuity::Widget::DomNode; |
#use Continuity::Widget::DomNode; |
9 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
10 |
use Carp qw/confess/; |
use Carp qw/confess cluck/; |
11 |
use File::Slurp; |
use File::Slurp; |
12 |
|
|
13 |
use Frey::Bookmarklet; |
use Frey::Bookmarklet; |
14 |
use Frey::ClassBrowser; |
use Frey::ClassBrowser; |
15 |
|
use Frey::INC; |
16 |
|
|
17 |
has 'head' => ( |
use Frey::SVK; |
|
is => 'rw', |
|
|
isa => 'ArrayRef[Str]', |
|
|
default => sub { [ 'static/frey.css' ] }, |
|
|
); |
|
18 |
|
|
19 |
has 'status' => ( |
our @head; |
20 |
is => 'rw', |
sub head { @head } |
|
isa => 'ArrayRef[HashRef[Str]]', |
|
|
lazy => 1, |
|
|
default => sub { [ |
|
|
{ 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }, |
|
|
{ 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup }, |
|
|
] }, |
|
|
); |
|
21 |
|
|
22 |
has 'request_url' => ( |
has 'request_url' => ( |
23 |
is => 'rw', |
is => 'rw', |
39 |
is => 'rw', |
is => 'rw', |
40 |
isa => 'Str', |
isa => 'Str', |
41 |
default => 'text/html', |
default => 'text/html', |
42 |
|
documentation => 'Content-type header', |
43 |
); |
); |
44 |
|
|
45 |
=head2 inline_smaller_than |
has 'dump_max_bytes' => ( |
46 |
|
is => 'rw', |
47 |
Inline JavaScript and CSS smaller than this size into page reducing |
isa => 'Int', |
48 |
round-trips to server. |
default => 4096, |
49 |
|
documentation => 'maximum dump size sent to browser before truncation', |
50 |
=cut |
); |
51 |
|
|
52 |
has 'inline_smaller_than' => ( |
has 'inline_smaller_than' => ( |
53 |
is => 'rw', |
is => 'rw', |
54 |
isa => 'Int', |
isa => 'Int', |
55 |
default => 10240, |
default => 10240, |
56 |
|
documentation => 'inline JavaScript and CSS to reduce round-trips', |
57 |
|
); |
58 |
|
|
59 |
|
has 'html_dump_width' => ( |
60 |
|
documentation => 'crop longer lines in dumps', |
61 |
|
is => 'rw', |
62 |
|
isa => 'Int', |
63 |
|
# required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them |
64 |
|
default => 128, |
65 |
); |
); |
66 |
|
|
67 |
sub dom2html { |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
68 |
# warn "## dom2html ",dump( @_ ); |
my $escape_re = join '|' => keys %escape; |
69 |
return Continuity::Widget::DomNode->create( @_ )->to_string; |
|
70 |
|
sub html_escape { |
71 |
|
my ( $self, $html ) = @_; |
72 |
|
$html =~ s/($escape_re)/$escape{$1}/g; |
73 |
|
return $html; |
74 |
|
} |
75 |
|
|
76 |
|
sub html_dump { |
77 |
|
my ( $self, $dump ) = @_; |
78 |
|
$dump = dump( $dump ) if ref($dump); |
79 |
|
my $width = $self->html_dump_width; |
80 |
|
$dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs; |
81 |
|
$dump = $self->html_escape( $dump ); |
82 |
|
$dump =~ s{\Q...\E}{…}gs; |
83 |
|
# $dump =~ $self->editor_links( $dump ); # FIXME include this |
84 |
|
return "<code>$dump</code>"; |
85 |
|
} |
86 |
|
|
87 |
|
sub popup { my $self = shift; $self->popup_dropdown('popup', @_); } |
88 |
|
sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); } |
89 |
|
|
90 |
|
our $re_html = qr{<(?:!--.+?--|(\w+).+?/\1)>}s; # relaxed html check for one semi-valid tag |
91 |
|
|
92 |
|
sub popup_dropdown { |
93 |
|
my ( $self, $type, $name, $content, $full ) = @_; |
94 |
|
|
95 |
|
$content = $self->html_dump($content) if ref $content; |
96 |
|
|
97 |
|
$content = qq|<span>$content</span>| unless $content =~ m{^\s*<(span|a|code).+?/\1>\s*}; |
98 |
|
|
99 |
|
$content =~ s{<span>(<code>[^<]+</code>)</span>}{$1} && $self->TODO("code wrapped in span"); |
100 |
|
|
101 |
|
warn "## $type [$name] = ", length( $content ), " bytes" if $self->debug; |
102 |
|
|
103 |
|
if ( $name =~ m{::} && $name !~ $re_html ) { |
104 |
|
return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|; |
105 |
|
} else { |
106 |
|
return qq|<span class="frey-$type">$name $content</span>\n|; |
107 |
|
} |
108 |
} |
} |
109 |
|
|
110 |
sub _inline_path { |
sub _inline_path { |
115 |
sub _head_html { |
sub _head_html { |
116 |
my $self = shift; |
my $self = shift; |
117 |
my $out = ''; |
my $out = ''; |
118 |
foreach my $path ( @{ $self->head } ) { |
foreach my $path ( @head ) { |
119 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
120 |
if ( $path =~ m/\.js$/ ) { |
if ( $path =~ m/\.js$/ ) { |
121 |
$out .= $self->_inline_path( $path ) ? |
$out .= $self->_inline_path( $path ) ? |
150 |
return if ! defined $path || $path eq ''; |
return if ! defined $path || $path eq ''; |
151 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
152 |
|
|
153 |
if ( $path =~ m{<.*>}s ) { |
if ( $path =~ $re_html ) { |
154 |
push @{ $self->head }, $path; |
push @head, $path; |
155 |
} elsif ( -e $path ) { |
} elsif ( -e $path ) { |
156 |
if ( $path =~ m/\.(?:js|css)$/ ) { |
if ( $path =~ m/\.(?:js|css)$/ ) { |
157 |
push @{ $self->head }, $path; |
push @head, $path; |
158 |
} else { |
} else { |
159 |
confess "can't add_head( $path ) it's not js or css"; |
confess "can't add_head( $path ) it's not js or css"; |
160 |
} |
} |
165 |
|
|
166 |
} |
} |
167 |
|
|
168 |
|
sub add_css { |
169 |
|
my ($self,$css) = @_; |
170 |
|
my ( $package, $path, $line ) = caller; |
171 |
|
$self->add_head( qq| |
172 |
|
<style type="text/css"> |
173 |
|
/* via $package at $path line $line */ |
174 |
|
$css |
175 |
|
</style> |
176 |
|
| ); |
177 |
|
} |
178 |
|
|
179 |
our $reload_counter = 0; |
our $reload_counter = 0; |
180 |
|
|
181 |
|
|
189 |
|
|
190 |
=cut |
=cut |
191 |
|
|
192 |
|
our @status; |
193 |
|
sub status { @status }; |
194 |
|
|
195 |
|
our $icon_html; |
196 |
|
|
197 |
sub page { |
sub page { |
198 |
my $self = shift; |
my $self = shift; |
199 |
my $a = {@_}; |
my $a = {@_}; |
200 |
|
|
201 |
|
warn "## page ",dump($a); |
202 |
|
|
203 |
$reload_counter++; |
$reload_counter++; |
204 |
|
|
205 |
my $status_line = ''; |
my $status_line = ''; |
206 |
foreach my $part ( @{ $self->status } ) { |
|
207 |
if ( ref($part) ne 'HASH' ) { |
foreach my $part ( @status ) { |
|
warn "part not hash ",dump( $part ) ; |
|
|
#$self->status( $part ); |
|
|
next; |
|
|
} |
|
208 |
foreach my $name ( keys %$part ) { |
foreach my $name ( keys %$part ) { |
209 |
my $content = $part->{$name}; |
$status_line .= $self->popup( $name, $part->{$name} ); |
|
if ( ref($content) ) { |
|
|
$content = '<code>' . dump($content) . '</code>'; |
|
|
my $l = length($content); |
|
|
$content = qq|<span>$l bytes</span>| if $l > 1024; |
|
|
} else { |
|
|
$content = qq|<span>$content</span>|; |
|
|
} |
|
|
warn "### part [$name] = ", length( $content ), " bytes" if $self->debug; |
|
|
$status_line .= qq|<span class="frey-popup">$name $content</span>\n|; |
|
210 |
} |
} |
211 |
} |
} |
212 |
|
|
223 |
$body = '<!-- no body -->'; |
$body = '<!-- no body -->'; |
224 |
} |
} |
225 |
|
|
226 |
$status_line |
$status_line .= $self->warnings_html; |
227 |
.= qq|<span class="frey-popup">warn<code>| |
|
228 |
. $self->editor_links( join("", $self->warnings ) ) |
my ($exit,$description) = ('exit','stop server'); |
229 |
. qq|</code></span>| |
($exit,$description) = ('restart','restart server') |
230 |
if $self->warnings; |
if $ENV{FREY_RESTART}; # tune labels on exit link |
231 |
|
|
232 |
|
my $right = |
233 |
|
qq| |
234 |
|
<span class="right"> |
235 |
|
<a title="reload $url" href="/reload$url">reload</a> |
236 |
|
<a title="$description" href="/exit$url">$exit</a> |
237 |
|
</span> |
238 |
|
|; |
239 |
|
|
240 |
|
my $svk = Frey::SVK->new; |
241 |
|
my $info = $svk->info; |
242 |
|
my $revision = $svk->info->{Revision} || ''; |
243 |
|
$revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)}; |
244 |
|
|
245 |
|
$self->add_icon unless $icon_html; |
246 |
|
|
247 |
my $html = join("\n", |
my $html = join("\n", |
248 |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
249 |
$self->_head_html, |
$self->_head_html, |
250 |
'<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>', |
'<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>', |
251 |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
252 |
|
( $icon_html || '<!-- no icon -->' ), |
253 |
( $a->{head} || '' ), |
( $a->{head} || '' ), |
254 |
qq| |
qq| |
255 |
</head><body> |
</head><body> |
256 |
$body |
$body |
257 |
<div class="frey-status-line"> |
<div class="frey-status-line"> |
258 |
<a href="/">Frey</a> $Frey::VERSION |
<a href="/">Frey</a> $Frey::VERSION $revision |
|
<a href="?reload=$reload_counter"><code>$url</code></a> |
|
259 |
$status_line |
$status_line |
260 |
|
$right |
261 |
</div> |
</div> |
262 |
</body></html> |
</body></html> |
263 |
|, |
|, |
268 |
return $html; |
return $html; |
269 |
} |
} |
270 |
|
|
271 |
|
=head2 editor |
272 |
|
|
273 |
|
Create HTML editor link with optional line and title |
274 |
|
|
275 |
|
my $html = $self->editor( $class, $line, $title ); |
276 |
|
|
277 |
|
=cut |
278 |
|
|
279 |
|
sub editor { |
280 |
|
my ( $self, $class, $line, $title ) = @_; |
281 |
|
confess "need class" unless $class; |
282 |
|
if ( ! defined $title ) { |
283 |
|
$title = "edit $class"; |
284 |
|
$title .= " line $line" if $line; |
285 |
|
} |
286 |
|
$line ||= 1; |
287 |
|
qq|<a target="editor" href="/editor+$class+$line"| . |
288 |
|
( $title ? qq| title="$title"| : '' ) . |
289 |
|
qq|>$class</a>|; |
290 |
|
} |
291 |
|
|
292 |
|
=head2 editor_links |
293 |
|
|
294 |
|
Create HTML links to editor for perl error message |
295 |
|
|
296 |
|
my $html = $self->editor_links( $error ) |
297 |
|
|
298 |
|
=cut |
299 |
|
|
300 |
sub editor_links { |
sub editor_links { |
301 |
my ( $self, $error ) = @_; |
my ( $self, $error ) = @_; |
302 |
|
|
303 |
$error =~ s{at\s+(\S+)\s+line\s+(\d+)} |
$error =~ s{at\s+(\S+)\s+line\s+(\d+)} |
304 |
{at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm; |
{at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm; |
305 |
|
|
306 |
$error =~ s{(via package ")([\w:]+)(")} |
$error =~ s{(via (?:package) "?)([\w:]+)("?)} |
307 |
{$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm; |
{$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm; |
308 |
|
|
309 |
return $error; |
return $error; |
324 |
; |
; |
325 |
} |
} |
326 |
|
|
327 |
|
=head1 Status line |
328 |
|
|
329 |
|
=head2 add_status |
330 |
|
|
331 |
|
$self->add_status( { name => { some => 'data' } } ); |
332 |
|
|
333 |
|
$self->add_status( "append to last status popup" ); |
334 |
|
|
335 |
|
=cut |
336 |
|
|
337 |
|
sub add_status { |
338 |
|
my ( $self, $data ) = @_; |
339 |
|
push @status, { 'X' => [ $self->backtrace ] }; |
340 |
|
if ( ref($data) ) { |
341 |
|
push @status, $data; |
342 |
|
} else { |
343 |
|
if ( defined $status[ $#status ] ) { |
344 |
|
$status[ $#status ]->{ '+' } = $data; |
345 |
|
} else { |
346 |
|
push @status, { '+' => $data }; |
347 |
|
} |
348 |
|
} |
349 |
|
} |
350 |
|
|
351 |
|
=head2 clean_status |
352 |
|
|
353 |
|
Called at beginning of each request |
354 |
|
|
355 |
|
$self->clean_status; |
356 |
|
|
357 |
|
=cut |
358 |
|
|
359 |
|
sub clean_status { |
360 |
|
my ($self) = shift; |
361 |
|
@head = ( 'static/frey.css' ); |
362 |
|
@status = ( |
363 |
|
{ 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup }, |
364 |
|
{ 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }, |
365 |
|
{ 'INC' => Frey::INC->new->as_markup }, |
366 |
|
); |
367 |
|
$icon_html = ''; |
368 |
|
} |
369 |
|
|
370 |
|
=head2 status_parts |
371 |
|
|
372 |
|
Dump all status line parts |
373 |
|
|
374 |
|
$self->status_parts |
375 |
|
|
376 |
|
=cut |
377 |
|
|
378 |
|
sub status_parts { |
379 |
|
warn "## status parts ", dump( map { keys %$_ } @status ); |
380 |
|
} |
381 |
|
|
382 |
|
=for debug |
383 |
|
|
384 |
|
sub DEMOLISH { |
385 |
|
my ( $self ) = @_; |
386 |
|
warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status; |
387 |
|
} |
388 |
|
|
389 |
|
=cut |
390 |
|
|
391 |
|
=head2 add_icon |
392 |
|
|
393 |
|
Frey::Foo->add_icon; # /static/icons/Frey/Foo.png |
394 |
|
Frey::Foo->add_icon('warning'); # /static/icons/Frey/Foo/warning.png |
395 |
|
|
396 |
|
=cut |
397 |
|
|
398 |
|
sub icon_path { |
399 |
|
my ($self,$class,$variant) = @_; |
400 |
|
my $icon = $class; |
401 |
|
$icon =~ s{::}{/}g; |
402 |
|
$icon .= "/$variant" if $variant; |
403 |
|
my $path = 'static/icons/' . $icon . '.png'; |
404 |
|
if ( -e $path ) { |
405 |
|
warn "# $class from $self icon_path $path" if $self->debug; |
406 |
|
return $path; |
407 |
|
} else { |
408 |
|
$self->TODO( "add $path icon for $class" ); |
409 |
|
return undef; |
410 |
|
} |
411 |
|
} |
412 |
|
|
413 |
|
sub add_icon { |
414 |
|
my ($self,$variant) = @_; |
415 |
|
|
416 |
|
my $class = ref($self); |
417 |
|
$class = $self->class if $self->can('class'); |
418 |
|
my $icon_path = $self->icon_path( $class, $variant ) || return; |
419 |
|
|
420 |
|
$icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|; |
421 |
|
warn "# using icon $icon_path"; |
422 |
|
|
423 |
|
=for later |
424 |
|
|
425 |
|
# FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work! |
426 |
|
my $ico_path = $icon_path; |
427 |
|
$ico_path =~ s{png$}{ico}; |
428 |
|
if ( ! -e $ico_path ) { |
429 |
|
system "convert $icon_path $ico_path"; |
430 |
|
warn "# convert $icon_path $ico_path : $@"; |
431 |
|
} |
432 |
|
$icon_html .= qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path">| if -e $ico_path; |
433 |
|
|
434 |
|
=cut |
435 |
|
|
436 |
|
} |
437 |
|
|
438 |
|
my $warn_colors = { |
439 |
|
'#' => '#444', |
440 |
|
'##' => '#888', |
441 |
|
}; |
442 |
|
|
443 |
|
my $multiline_markers = { |
444 |
|
'(' => ')', |
445 |
|
'{' => '}', |
446 |
|
'[' => ']', |
447 |
|
'"' => '"', |
448 |
|
}; |
449 |
|
|
450 |
|
my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']'; |
451 |
|
warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re"; |
452 |
|
|
453 |
|
sub log_path { |
454 |
|
$Frey::Bootstrap::log_path || warn "no log_path?"; |
455 |
|
} |
456 |
|
|
457 |
|
sub warnings_html { |
458 |
|
my ($self,$level) = shift; |
459 |
|
$level ||= $self->debug, |
460 |
|
my $path = $self->log_path; |
461 |
|
|
462 |
|
my $warnings; |
463 |
|
my $line = 0; |
464 |
|
my $multiline_end; |
465 |
|
|
466 |
|
open(my $log, '<', $path) || die "can't open $path: $!"; |
467 |
|
while(<$log>) { |
468 |
|
chomp; |
469 |
|
$line++; |
470 |
|
|
471 |
|
my $style = ''; |
472 |
|
|
473 |
|
if ( $multiline_end ) { |
474 |
|
if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) { |
475 |
|
# warn "## $line end of $multiline_end in '$_'\n"; |
476 |
|
undef $multiline_end; |
477 |
|
} else { |
478 |
|
# warn "## $line skipped\n"; |
479 |
|
} |
480 |
|
} elsif ( m{^(#*)\s+} ) { |
481 |
|
my $l = $1 ? length($1) : 0; |
482 |
|
if ( $l > $level ) { |
483 |
|
undef $multiline_end; |
484 |
|
$multiline_end = $multiline_markers->{$1} if m{($multiline_re)$}; |
485 |
|
# warn "## $line start $1 .. $multiline_end level $l > $level for '$_'\n" if $multiline_end; |
486 |
|
next; |
487 |
|
} |
488 |
|
|
489 |
|
$style = $warn_colors->{$1} |
490 |
|
? ' style="color:' . $warn_colors->{$1} . '"' |
491 |
|
: ''; |
492 |
|
|
493 |
|
my $msg = $self->html_escape( $_ ); |
494 |
|
my $spacer = ' '; |
495 |
|
if ( length($msg) > $self->html_dump_width ) { |
496 |
|
$msg = substr( $msg, 0, $self->html_dump_width ); |
497 |
|
$spacer = '…' |
498 |
|
} |
499 |
|
$msg =~ s{^\s}{ }g; |
500 |
|
$warnings .= qq|<tt$style>$msg</tt>$spacer<small><a target="editor" href="/editor+$path+$line">+$line</a></small><br/>|; |
501 |
|
# FIXME <tt> should be <code> but CSS hates me |
502 |
|
} |
503 |
|
} |
504 |
|
close($log) || die "can't close $path: $!"; |
505 |
|
|
506 |
|
return |
507 |
|
qq|<span class="frey-popup"><a target="editor" href="/editor+$path+$line" title="open $path level $level">warn</a><span>| |
508 |
|
. $self->editor_links( $warnings ) |
509 |
|
. qq|</span></span>| |
510 |
|
; |
511 |
|
} |
512 |
|
|
513 |
|
|
514 |
|
=head2 backtrace |
515 |
|
|
516 |
|
Show backtrace with links to editor |
517 |
|
|
518 |
|
my @backtrace = $self->backtrace; |
519 |
|
|
520 |
|
=cut |
521 |
|
|
522 |
|
sub backtrace { |
523 |
|
my ($self) = @_; |
524 |
|
|
525 |
|
my @backtrace; |
526 |
|
foreach ( 0 .. 5 ) { |
527 |
|
my ( |
528 |
|
$package,$path,$line |
529 |
|
# subroutine hasargs |
530 |
|
# wantarray evaltext is_require |
531 |
|
# hints bitmask hinthash |
532 |
|
) = caller($_) or last; |
533 |
|
|
534 |
|
push @backtrace, |
535 |
|
qq|via $package at $path line $line|; |
536 |
|
} |
537 |
|
warn "# backtrace: ", dump( @backtrace ) if @backtrace; |
538 |
|
return @backtrace; |
539 |
|
} |
540 |
|
|
541 |
1; |
1; |