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 cluck/; |
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 |
use Frey::SVK; |
use Frey::SVK; |
18 |
|
|
19 |
has 'head' => ( |
our @head = ( 'static/frey.css' ); |
|
is => 'rw', |
|
|
isa => 'ArrayRef[Str]', |
|
|
default => sub { [ 'static/frey.css' ] }, |
|
|
); |
|
20 |
|
|
21 |
has 'request_url' => ( |
has 'request_url' => ( |
22 |
is => 'rw', |
is => 'rw', |
45 |
is => 'rw', |
is => 'rw', |
46 |
isa => 'Int', |
isa => 'Int', |
47 |
default => 4096, |
default => 4096, |
48 |
documentation => 'Maximum dump size sent to browser before truncation', |
documentation => 'maximum dump size sent to browser before truncation', |
49 |
); |
); |
50 |
|
|
|
=head2 inline_smaller_than |
|
|
|
|
|
Inline JavaScript and CSS smaller than this size into page reducing |
|
|
round-trips to server. |
|
|
|
|
|
=cut |
|
|
|
|
51 |
has 'inline_smaller_than' => ( |
has 'inline_smaller_than' => ( |
52 |
is => 'rw', |
is => 'rw', |
53 |
isa => 'Int', |
isa => 'Int', |
54 |
default => 10240, |
default => 10240, |
55 |
|
documentation => 'inline JavaScript and CSS to reduce round-trips', |
56 |
|
); |
57 |
|
|
58 |
|
has 'html_dump_width' => ( |
59 |
|
documentation => 'crop longer lines in dumps', |
60 |
|
is => 'rw', |
61 |
|
isa => 'Int', |
62 |
|
# required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them |
63 |
|
default => 128, |
64 |
); |
); |
65 |
|
|
66 |
sub dom2html { |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
67 |
# warn "## dom2html ",dump( @_ ); |
my $escape_re = join '|' => keys %escape; |
68 |
return Continuity::Widget::DomNode->create( @_ )->to_string; |
|
69 |
|
sub html_escape { |
70 |
|
my ( $self, $html ) = @_; |
71 |
|
$html =~ s/($escape_re)/$escape{$1}/g; |
72 |
|
return $html; |
73 |
|
} |
74 |
|
|
75 |
|
sub html_dump { |
76 |
|
my ( $self, $dump ) = @_; |
77 |
|
$dump = dump( $dump ) if ref($dump); |
78 |
|
my $width = $self->html_dump_width; |
79 |
|
$dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs; |
80 |
|
$dump = $self->html_escape( $dump ); |
81 |
|
$dump =~ s{\Q...\E}{…}gs; |
82 |
|
return "<code>$dump</code>"; |
83 |
|
} |
84 |
|
|
85 |
|
sub popup { my $self = shift; $self->popup_dropdown('popup', @_); } |
86 |
|
sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); } |
87 |
|
|
88 |
|
our $re_html = qr{<(?:!--.+?--|(\w+).+?/\1)>}s; # relaxed html check for one semi-valid tag |
89 |
|
|
90 |
|
sub popup_dropdown { |
91 |
|
my ( $self, $type, $name, $content, $full ) = @_; |
92 |
|
|
93 |
|
$content = $self->html_dump($content) if ref $content; |
94 |
|
|
95 |
|
$content = qq|<span>$content</span>| unless $content =~ m{^\s*<(span|a|code).+?/\1>\s*}; |
96 |
|
|
97 |
|
warn "## $type [$name] = ", length( $content ), " bytes" if $self->debug; |
98 |
|
|
99 |
|
if ( $name =~ m{::} && $name !~ $re_html ) { |
100 |
|
return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|; |
101 |
|
} else { |
102 |
|
return qq|<span class="frey-$type">$name $content</span>\n|; |
103 |
|
} |
104 |
} |
} |
105 |
|
|
106 |
sub _inline_path { |
sub _inline_path { |
111 |
sub _head_html { |
sub _head_html { |
112 |
my $self = shift; |
my $self = shift; |
113 |
my $out = ''; |
my $out = ''; |
114 |
foreach my $path ( @{ $self->head } ) { |
foreach my $path ( @head ) { |
115 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
116 |
if ( $path =~ m/\.js$/ ) { |
if ( $path =~ m/\.js$/ ) { |
117 |
$out .= $self->_inline_path( $path ) ? |
$out .= $self->_inline_path( $path ) ? |
146 |
return if ! defined $path || $path eq ''; |
return if ! defined $path || $path eq ''; |
147 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
148 |
|
|
149 |
if ( $path =~ m{<.*>}s ) { |
if ( $path =~ $re_html ) { |
150 |
push @{ $self->head }, $path; |
push @head, $path; |
151 |
} elsif ( -e $path ) { |
} elsif ( -e $path ) { |
152 |
if ( $path =~ m/\.(?:js|css)$/ ) { |
if ( $path =~ m/\.(?:js|css)$/ ) { |
153 |
push @{ $self->head }, $path; |
push @head, $path; |
154 |
} else { |
} else { |
155 |
confess "can't add_head( $path ) it's not js or css"; |
confess "can't add_head( $path ) it's not js or css"; |
156 |
} |
} |
161 |
|
|
162 |
} |
} |
163 |
|
|
164 |
|
sub add_css { |
165 |
|
my ($self,$css) = @_; |
166 |
|
my ( $package, $path, $line ) = caller; |
167 |
|
$self->add_head( qq| |
168 |
|
<style type="text/css"> |
169 |
|
/* via package $package at $path line $line */ |
170 |
|
$css |
171 |
|
</style> |
172 |
|
| ); |
173 |
|
} |
174 |
|
|
175 |
our $reload_counter = 0; |
our $reload_counter = 0; |
176 |
|
|
177 |
|
|
188 |
our @status; |
our @status; |
189 |
sub status { @status }; |
sub status { @status }; |
190 |
|
|
191 |
|
our $icon_html; |
192 |
|
|
193 |
sub page { |
sub page { |
194 |
my $self = shift; |
my $self = shift; |
195 |
my $a = {@_}; |
my $a = {@_}; |
201 |
my $status_line = ''; |
my $status_line = ''; |
202 |
|
|
203 |
unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup }; |
unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup }; |
204 |
unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }; |
# unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }; |
205 |
|
unshift @status, { 'INC' => Frey::INC->new->as_markup }; |
206 |
|
|
207 |
foreach my $part ( @status ) { |
foreach my $part ( @status ) { |
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 > $self->dump_max_bytes; |
|
|
} 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 |
my $warn_colors = { |
$status_line .= $self->warnings_html; |
|
'#' => '#444', |
|
|
'##' => '#888', |
|
|
}; |
|
|
|
|
|
$status_line |
|
|
.= qq|<span class="frey-popup">warn<span>| |
|
|
. $self->editor_links( |
|
|
join("", map { |
|
|
warn "# $_"; |
|
|
my $style = ''; |
|
|
$style = $warn_colors->{$1} |
|
|
? ' style="color:' . $warn_colors->{$1} . '"' |
|
|
: '' |
|
|
if m{^(#+)}; |
|
|
qq|<tt$style>$_</tt><br/>|; # XXX <tt> should be <code> but CSS hates me |
|
|
} $self->warnings ) |
|
|
) |
|
|
. qq|</span></span>| |
|
|
if $self->warnings; |
|
227 |
|
|
228 |
my ($exit,$description) = ('exit','stop server'); |
my ($exit,$description) = ('exit','stop server'); |
229 |
($exit,$description) = ('restart','restart server') |
($exit,$description) = ('restart','restart server') |
237 |
</span> |
</span> |
238 |
|; |
|; |
239 |
|
|
240 |
my $info = Frey::SVK->info; |
my $svk = Frey::SVK->new; |
241 |
my $revision = Frey::SVK->info->{Revision} || ''; |
my $info = $svk->info; |
242 |
|
my $revision = $svk->info->{Revision} || ''; |
243 |
$revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)}; |
$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> |
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 { |
sub add_status { |
338 |
my ( $self, $data ) = @_; |
my ( $self, $data ) = @_; |
339 |
push @status, $data; |
if ( ref($data) ) { |
340 |
|
push @status, $data; |
341 |
|
} else { |
342 |
|
$status[ $#status ]->{added} = $data; |
343 |
|
} |
344 |
} |
} |
345 |
|
|
346 |
|
=head2 clean_status |
347 |
|
|
348 |
|
Called at beginning of each request |
349 |
|
|
350 |
|
$self->clean_status; |
351 |
|
|
352 |
|
=cut |
353 |
|
|
354 |
sub clean_status { |
sub clean_status { |
355 |
|
my ($self) = shift; |
356 |
|
@head = (); |
357 |
@status = (); |
@status = (); |
358 |
|
$icon_html = ''; |
359 |
} |
} |
360 |
|
|
361 |
|
=head2 status_parts |
362 |
|
|
363 |
|
Dump all status line parts |
364 |
|
|
365 |
|
$self->status_parts |
366 |
|
|
367 |
|
=cut |
368 |
|
|
369 |
sub status_parts { |
sub status_parts { |
370 |
warn "## status parts ", dump( map { keys %$_ } @status ); |
warn "## status parts ", dump( map { keys %$_ } @status ); |
371 |
} |
} |
372 |
|
|
373 |
|
=for debug |
374 |
|
|
375 |
sub DEMOLISH { |
sub DEMOLISH { |
376 |
my ( $self ) = @_; |
my ( $self ) = @_; |
377 |
cluck "## DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status; |
warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status; |
378 |
|
} |
379 |
|
|
380 |
|
=cut |
381 |
|
|
382 |
|
=head2 add_icon |
383 |
|
|
384 |
|
Frey::Foo->add_icon; # /static/icons/Frey/Foo.png |
385 |
|
Frey::Foo->add_icon('warning'); # /static/icons/Frey/Foo/warning.png |
386 |
|
|
387 |
|
=cut |
388 |
|
|
389 |
|
sub icon_path { |
390 |
|
my ($self,$class,$variant) = @_; |
391 |
|
my $icon = $class; |
392 |
|
$icon =~ s{::}{/}g; |
393 |
|
$icon .= "/$variant" if $variant; |
394 |
|
my $path = 'static/icons/' . $icon . '.png'; |
395 |
|
if ( -e $path ) { |
396 |
|
warn "# $class from $self icon_path $path" if $self->debug; |
397 |
|
return $path; |
398 |
|
} else { |
399 |
|
$self->TODO( "add $path icon for $class" ); |
400 |
|
return undef; |
401 |
|
} |
402 |
|
} |
403 |
|
|
404 |
|
sub add_icon { |
405 |
|
my ($self,$variant) = @_; |
406 |
|
|
407 |
|
my $class = ref($self); |
408 |
|
$class = $self->class if $self->can('class'); |
409 |
|
my $icon_path = $self->icon_path( $class, $variant ) || return; |
410 |
|
|
411 |
|
$icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|; |
412 |
|
warn "# using icon $icon_path"; |
413 |
|
|
414 |
|
=for later |
415 |
|
|
416 |
|
# FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work! |
417 |
|
my $ico_path = $icon_path; |
418 |
|
$ico_path =~ s{png$}{ico}; |
419 |
|
if ( ! -e $ico_path ) { |
420 |
|
system "convert $icon_path $ico_path"; |
421 |
|
warn "# convert $icon_path $ico_path : $@"; |
422 |
|
} |
423 |
|
$icon_html .= qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path">| if -e $ico_path; |
424 |
|
|
425 |
|
=cut |
426 |
|
|
427 |
|
} |
428 |
|
|
429 |
|
my $warn_colors = { |
430 |
|
'#' => '#444', |
431 |
|
'##' => '#888', |
432 |
|
}; |
433 |
|
|
434 |
|
my $multiline_markers = { |
435 |
|
'(' => ')', |
436 |
|
'{' => '}', |
437 |
|
'[' => ']', |
438 |
|
'"' => '"', |
439 |
|
}; |
440 |
|
|
441 |
|
my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']'; |
442 |
|
warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re"; |
443 |
|
|
444 |
|
sub log_path { |
445 |
|
$Frey::Bootstrap::log_path || warn "no log_path?"; |
446 |
|
} |
447 |
|
|
448 |
|
sub warnings_html { |
449 |
|
my ($self,$level) = shift; |
450 |
|
$level ||= $self->debug, |
451 |
|
my $path = $self->log_path; |
452 |
|
|
453 |
|
my $warnings; |
454 |
|
my $line = 0; |
455 |
|
my $multiline_end; |
456 |
|
|
457 |
|
open(my $log, '<', $path) || die "can't open $path: $!"; |
458 |
|
while(<$log>) { |
459 |
|
chomp; |
460 |
|
$line++; |
461 |
|
|
462 |
|
my $style = ''; |
463 |
|
|
464 |
|
if ( $multiline_end ) { |
465 |
|
if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) { |
466 |
|
# warn "## $line end of $multiline_end in '$_'\n"; |
467 |
|
undef $multiline_end; |
468 |
|
} else { |
469 |
|
# warn "## $line skipped\n"; |
470 |
|
} |
471 |
|
} elsif ( m{^(#*)\s+} ) { |
472 |
|
my $l = $1 ? length($1) : 0; |
473 |
|
if ( $l > $level ) { |
474 |
|
undef $multiline_end; |
475 |
|
$multiline_end = $multiline_markers->{$1} if m{($multiline_re)$}; |
476 |
|
# warn "## $line start $1 .. $multiline_end level $l > $level for '$_'\n" if $multiline_end; |
477 |
|
next; |
478 |
|
} |
479 |
|
|
480 |
|
$style = $warn_colors->{$1} |
481 |
|
? ' style="color:' . $warn_colors->{$1} . '"' |
482 |
|
: ''; |
483 |
|
|
484 |
|
my $msg = $self->html_escape( $_ ); |
485 |
|
my $spacer = ' '; |
486 |
|
if ( length($msg) > $self->html_dump_width ) { |
487 |
|
$msg = substr( $msg, 0, $self->html_dump_width ); |
488 |
|
$spacer = '…' |
489 |
|
} |
490 |
|
$msg =~ s{^\s}{ }g; |
491 |
|
$warnings .= qq|<tt$style>$msg</tt>$spacer<small><a target="editor" href="/editor+$path+$line">+$line</a></small><br/>|; |
492 |
|
# FIXME <tt> should be <code> but CSS hates me |
493 |
|
} |
494 |
|
} |
495 |
|
close($log) || die "can't close $path: $!"; |
496 |
|
|
497 |
|
return |
498 |
|
qq|<span class="frey-popup"><a target="editor" href="/editor+$path+$line" title="open $path level $level">warn</a><span>| |
499 |
|
. $self->editor_links( $warnings ) |
500 |
|
. qq|</span></span>| |
501 |
|
; |
502 |
|
} |
503 |
|
|
504 |
|
|
505 |
|
=head2 backtrace |
506 |
|
|
507 |
|
Show backtrace with links to editor |
508 |
|
|
509 |
|
my @backtrace = $self->backtrace; |
510 |
|
|
511 |
|
=cut |
512 |
|
|
513 |
|
sub backtrace { |
514 |
|
my ($self) = @_; |
515 |
|
|
516 |
|
my @backtrace; |
517 |
|
foreach ( 0 .. 5 ) { |
518 |
|
my ( |
519 |
|
$package,$path,$line |
520 |
|
# subroutine hasargs |
521 |
|
# wantarray evaltext is_require |
522 |
|
# hints bitmask hinthash |
523 |
|
) = caller($_) or last; |
524 |
|
|
525 |
|
push @backtrace, |
526 |
|
qq|via package $package at $path line $line|; |
527 |
|
} |
528 |
|
warn "# backtrace: ", dump( @backtrace ) if @backtrace; |
529 |
|
return @backtrace; |
530 |
} |
} |
531 |
|
|
532 |
1; |
1; |