1 |
package Frey::Web; |
package Frey::Web; |
2 |
use Moose::Role; |
use Moose::Role; |
3 |
|
|
|
with 'Frey::Backtrace'; |
|
|
|
|
4 |
use Frey::Types; |
use Frey::Types; |
5 |
|
|
6 |
use Continuity::Widget::DomNode; |
use Continuity::Widget::DomNode; |
10 |
|
|
11 |
use Frey::Bookmarklet; |
use Frey::Bookmarklet; |
12 |
use Frey::ClassBrowser; |
use Frey::ClassBrowser; |
13 |
|
use Frey::INC; |
14 |
|
|
15 |
use Frey::SVK; |
use Frey::SVK; |
16 |
|
|
17 |
has 'head' => ( |
has 'head' => ( |
47 |
is => 'rw', |
is => 'rw', |
48 |
isa => 'Int', |
isa => 'Int', |
49 |
default => 4096, |
default => 4096, |
50 |
documentation => 'Maximum dump size sent to browser before truncation', |
documentation => 'maximum dump size sent to browser before truncation', |
51 |
); |
); |
52 |
|
|
|
=head2 inline_smaller_than |
|
|
|
|
|
Inline JavaScript and CSS smaller than this size into page reducing |
|
|
round-trips to server. |
|
|
|
|
|
=cut |
|
|
|
|
53 |
has 'inline_smaller_than' => ( |
has 'inline_smaller_than' => ( |
54 |
is => 'rw', |
is => 'rw', |
55 |
isa => 'Int', |
isa => 'Int', |
56 |
default => 10240, |
default => 10240, |
57 |
|
documentation => 'inline JavaScript and CSS to reduce round-trips', |
58 |
|
); |
59 |
|
|
60 |
|
has 'html_dump_width' => ( |
61 |
|
documentation => 'crop longer lines in dumps', |
62 |
|
is => 'rw', |
63 |
|
isa => 'Int', |
64 |
|
# required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them |
65 |
|
default => 128, |
66 |
); |
); |
67 |
|
|
68 |
|
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
69 |
|
my $escape_re = join '|' => keys %escape; |
70 |
|
|
71 |
|
sub html_escape { |
72 |
|
my ( $self, $html ) = @_; |
73 |
|
$html =~ s/($escape_re)/$escape{$1}/g; |
74 |
|
return $html; |
75 |
|
} |
76 |
|
|
77 |
|
sub html_dump { |
78 |
|
my ( $self, $dump ) = @_; |
79 |
|
$dump = dump( $dump ) if ref($dump); |
80 |
|
my $width = $self->html_dump_width; |
81 |
|
$dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs; |
82 |
|
$dump = $self->html_escape( $dump ); |
83 |
|
$dump =~ s{\Q...\E}{…}gs; |
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 |
|
warn "## $type [$name] = ", length( $content ), " bytes" if $self->debug; |
100 |
|
|
101 |
|
if ( $name =~ m{::} && $name !~ $re_html ) { |
102 |
|
return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|; |
103 |
|
} else { |
104 |
|
return qq|<span class="frey-$type">$name $content</span>\n|; |
105 |
|
} |
106 |
|
} |
107 |
|
|
108 |
sub dom2html { |
sub dom2html { |
109 |
# warn "## dom2html ",dump( @_ ); |
# warn "## dom2html ",dump( @_ ); |
110 |
return Continuity::Widget::DomNode->create( @_ )->to_string; |
return Continuity::Widget::DomNode->create( @_ )->to_string; |
153 |
return if ! defined $path || $path eq ''; |
return if ! defined $path || $path eq ''; |
154 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
155 |
|
|
156 |
if ( $path =~ m{<.*>}s ) { |
if ( $path =~ $re_html ) { |
157 |
push @{ $self->head }, $path; |
push @{ $self->head }, $path; |
158 |
} elsif ( -e $path ) { |
} elsif ( -e $path ) { |
159 |
if ( $path =~ m/\.(?:js|css)$/ ) { |
if ( $path =~ m/\.(?:js|css)$/ ) { |
197 |
my $status_line = ''; |
my $status_line = ''; |
198 |
|
|
199 |
unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup }; |
unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup }; |
200 |
unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }; |
# unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }; |
201 |
|
unshift @status, { 'INC' => Frey::INC->new->as_markup }; |
202 |
|
|
203 |
foreach my $part ( @status ) { |
foreach my $part ( @status ) { |
204 |
foreach my $name ( keys %$part ) { |
foreach my $name ( keys %$part ) { |
205 |
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|; |
|
206 |
} |
} |
207 |
} |
} |
208 |
|
|
219 |
$body = '<!-- no body -->'; |
$body = '<!-- no body -->'; |
220 |
} |
} |
221 |
|
|
222 |
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; |
|
223 |
|
|
224 |
my ($exit,$description) = ('exit','stop server'); |
my ($exit,$description) = ('exit','stop server'); |
225 |
($exit,$description) = ('restart','restart server') |
($exit,$description) = ('restart','restart server') |
385 |
|
|
386 |
} |
} |
387 |
|
|
388 |
|
my $warn_colors = { |
389 |
|
'#' => '#444', |
390 |
|
'##' => '#888', |
391 |
|
}; |
392 |
|
|
393 |
|
my $multiline_markers = { |
394 |
|
'(' => ')', |
395 |
|
'{' => '}', |
396 |
|
'[' => ']', |
397 |
|
'"' => '"', |
398 |
|
}; |
399 |
|
|
400 |
|
my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']'; |
401 |
|
warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re"; |
402 |
|
|
403 |
|
sub log_path { |
404 |
|
$Frey::Bootstrap::log_path || warn "no log_path?"; |
405 |
|
} |
406 |
|
|
407 |
|
sub warnings_html { |
408 |
|
my ($self,$level) = shift; |
409 |
|
$level ||= $self->debug, |
410 |
|
my $path = $self->log_path; |
411 |
|
|
412 |
|
my $warnings; |
413 |
|
my $line = 0; |
414 |
|
my $multiline_end; |
415 |
|
|
416 |
|
open(my $log, '<', $path) || die "can't open $path: $!"; |
417 |
|
while(<$log>) { |
418 |
|
chomp; |
419 |
|
$line++; |
420 |
|
|
421 |
|
my $style = ''; |
422 |
|
|
423 |
|
if ( $multiline_end ) { |
424 |
|
if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) { |
425 |
|
# warn "## $line end of $multiline_end in '$_'\n"; |
426 |
|
undef $multiline_end; |
427 |
|
} else { |
428 |
|
# warn "## $line skipped\n"; |
429 |
|
} |
430 |
|
} elsif ( m{^(#*)\s+} ) { |
431 |
|
my $l = $1 ? length($1) : 0; |
432 |
|
if ( $l > $level ) { |
433 |
|
undef $multiline_end; |
434 |
|
$multiline_end = $multiline_markers->{$1} if m{($multiline_re)$}; |
435 |
|
# warn "## $line start $1 .. $multiline_end level $l > $level for '$_'\n" if $multiline_end; |
436 |
|
next; |
437 |
|
} |
438 |
|
|
439 |
|
$style = $warn_colors->{$1} |
440 |
|
? ' style="color:' . $warn_colors->{$1} . '"' |
441 |
|
: ''; |
442 |
|
|
443 |
|
my $msg = $self->html_escape( $_ ); |
444 |
|
my $spacer = ' '; |
445 |
|
if ( length($msg) > $self->html_dump_width ) { |
446 |
|
$msg = substr( $msg, 0, $self->html_dump_width ); |
447 |
|
$spacer = '…' |
448 |
|
} |
449 |
|
$msg =~ s{^\s}{ }g; |
450 |
|
$warnings .= qq|<tt$style>$msg</tt>$spacer<small><a target="editor" href="/editor+$path+$line">+$line</a></small><br/>|; |
451 |
|
# FIXME <tt> should be <code> but CSS hates me |
452 |
|
} |
453 |
|
} |
454 |
|
close($log) || die "can't close $path: $!"; |
455 |
|
|
456 |
|
return |
457 |
|
qq|<span class="frey-popup"><a target="editor" href="/editor+$path+$line" title="open $path level $level">warn</a><span>| |
458 |
|
. $self->editor_links( $warnings ) |
459 |
|
. qq|</span></span>| |
460 |
|
; |
461 |
|
} |
462 |
|
|
463 |
|
|
464 |
|
=head2 backtrace |
465 |
|
|
466 |
|
Show backtrace with links to editor |
467 |
|
|
468 |
|
my @backtrace = $self->backtrace; |
469 |
|
|
470 |
|
=cut |
471 |
|
|
472 |
|
sub backtrace { |
473 |
|
my ($self) = @_; |
474 |
|
|
475 |
|
my @backtrace; |
476 |
|
foreach ( 0 .. 5 ) { |
477 |
|
my ( |
478 |
|
$package,$path,$line |
479 |
|
# subroutine hasargs |
480 |
|
# wantarray evaltext is_require |
481 |
|
# hints bitmask hinthash |
482 |
|
) = caller($_) or last; |
483 |
|
|
484 |
|
push @backtrace, |
485 |
|
qq|via $package from $path <a target="editor" href="/editor+$path+$line">$path</a>|; |
486 |
|
} |
487 |
|
warn "# backtrace: ", dump( @backtrace ) if @backtrace; |
488 |
|
return @backtrace; |
489 |
|
} |
490 |
|
|
491 |
1; |
1; |