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' => ( |
has 'head' => ( |
49 |
is => 'rw', |
is => 'rw', |
50 |
isa => 'Int', |
isa => 'Int', |
51 |
default => 4096, |
default => 4096, |
52 |
documentation => 'Maximum dump size sent to browser before truncation', |
documentation => 'maximum dump size sent to browser before truncation', |
53 |
); |
); |
54 |
|
|
|
=head2 inline_smaller_than |
|
|
|
|
|
Inline JavaScript and CSS smaller than this size into page reducing |
|
|
round-trips to server. |
|
|
|
|
|
=cut |
|
|
|
|
55 |
has 'inline_smaller_than' => ( |
has 'inline_smaller_than' => ( |
56 |
is => 'rw', |
is => 'rw', |
57 |
isa => 'Int', |
isa => 'Int', |
58 |
default => 10240, |
default => 10240, |
59 |
|
documentation => 'inline JavaScript and CSS to reduce round-trips', |
60 |
|
); |
61 |
|
|
62 |
|
has 'html_dump_width' => ( |
63 |
|
documentation => 'crop longer lines in dumps', |
64 |
|
is => 'rw', |
65 |
|
isa => 'Int', |
66 |
|
# required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them |
67 |
|
default => 128, |
68 |
); |
); |
69 |
|
|
70 |
sub dom2html { |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
71 |
# warn "## dom2html ",dump( @_ ); |
my $escape_re = join '|' => keys %escape; |
72 |
return Continuity::Widget::DomNode->create( @_ )->to_string; |
|
73 |
|
sub html_escape { |
74 |
|
my ( $self, $html ) = @_; |
75 |
|
$html =~ s/($escape_re)/$escape{$1}/g; |
76 |
|
return $html; |
77 |
|
} |
78 |
|
|
79 |
|
sub html_dump { |
80 |
|
my ( $self, $dump ) = @_; |
81 |
|
$dump = dump( $dump ) if ref($dump); |
82 |
|
my $width = $self->html_dump_width; |
83 |
|
$dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs; |
84 |
|
$dump = $self->html_escape( $dump ); |
85 |
|
$dump =~ s{\Q...\E}{…}gs; |
86 |
|
return "<code>$dump</code>"; |
87 |
|
} |
88 |
|
|
89 |
|
sub popup { my $self = shift; $self->popup_dropdown('popup', @_); } |
90 |
|
sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); } |
91 |
|
|
92 |
|
our $re_html = qr{<(?:!--.+?--|(\w+).+?/\1)>}s; # relaxed html check for one semi-valid tag |
93 |
|
|
94 |
|
sub popup_dropdown { |
95 |
|
my ( $self, $type, $name, $content, $full ) = @_; |
96 |
|
|
97 |
|
$content = $self->html_dump($content) if ref $content; |
98 |
|
|
99 |
|
$content = qq|<span>$content</span>| unless $content =~ m{^\s*<(span|a|code).+?/\1>\s*}; |
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 { |
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 @{ $self->head }, $path; |
155 |
} elsif ( -e $path ) { |
} elsif ( -e $path ) { |
156 |
if ( $path =~ m/\.(?:js|css)$/ ) { |
if ( $path =~ m/\.(?:js|css)$/ ) { |
181 |
our @status; |
our @status; |
182 |
sub status { @status }; |
sub status { @status }; |
183 |
|
|
184 |
|
our $icon_html; |
185 |
|
|
186 |
sub page { |
sub page { |
187 |
my $self = shift; |
my $self = shift; |
188 |
my $a = {@_}; |
my $a = {@_}; |
194 |
my $status_line = ''; |
my $status_line = ''; |
195 |
|
|
196 |
unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup }; |
unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup }; |
197 |
unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }; |
# unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }; |
198 |
|
unshift @status, { 'INC' => Frey::INC->new->as_markup }; |
199 |
|
|
200 |
foreach my $part ( @status ) { |
foreach my $part ( @status ) { |
201 |
foreach my $name ( keys %$part ) { |
foreach my $name ( keys %$part ) { |
202 |
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|; |
|
203 |
} |
} |
204 |
} |
} |
205 |
|
|
216 |
$body = '<!-- no body -->'; |
$body = '<!-- no body -->'; |
217 |
} |
} |
218 |
|
|
219 |
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; |
|
220 |
|
|
221 |
my ($exit,$description) = ('exit','stop server'); |
my ($exit,$description) = ('exit','stop server'); |
222 |
($exit,$description) = ('restart','restart server') |
($exit,$description) = ('restart','restart server') |
234 |
my $revision = Frey::SVK->info->{Revision} || ''; |
my $revision = Frey::SVK->info->{Revision} || ''; |
235 |
$revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)}; |
$revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)}; |
236 |
|
|
237 |
|
$self->add_icon unless $icon_html; |
238 |
|
|
239 |
my $html = join("\n", |
my $html = join("\n", |
240 |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
241 |
$self->_head_html, |
$self->_head_html, |
242 |
'<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>', |
'<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>', |
243 |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
244 |
|
( $icon_html || '<!-- no icon -->' ), |
245 |
( $a->{head} || '' ), |
( $a->{head} || '' ), |
246 |
qq| |
qq| |
247 |
</head><body> |
</head><body> |
323 |
|
|
324 |
sub clean_status { |
sub clean_status { |
325 |
@status = (); |
@status = (); |
326 |
|
$icon_html = ''; |
327 |
} |
} |
328 |
|
|
329 |
sub status_parts { |
sub status_parts { |
332 |
|
|
333 |
sub DEMOLISH { |
sub DEMOLISH { |
334 |
my ( $self ) = @_; |
my ( $self ) = @_; |
335 |
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; |
336 |
|
} |
337 |
|
|
338 |
|
=head2 add_icon |
339 |
|
|
340 |
|
Frey::Foo->add_icon; # /static/icons/Frey/Foo.png |
341 |
|
Frey::Foo->add_icon('warning'); # /static/icons/Frey/Foo/warning.png |
342 |
|
|
343 |
|
=cut |
344 |
|
|
345 |
|
sub icon_path { |
346 |
|
my ($self,$class,$variant) = @_; |
347 |
|
my $icon = $class; |
348 |
|
$icon =~ s{::}{/}g; |
349 |
|
$icon .= "/$variant" if $variant; |
350 |
|
my $path = 'static/icons/' . $icon . '.png'; |
351 |
|
if ( -e $path ) { |
352 |
|
warn "# $class from $self icon_path $path"; |
353 |
|
return $path; |
354 |
|
} else { |
355 |
|
$self->TODO( "add $path icon for $class" ); |
356 |
|
return undef; |
357 |
|
} |
358 |
|
} |
359 |
|
|
360 |
|
sub add_icon { |
361 |
|
my ($self,$variant) = @_; |
362 |
|
|
363 |
|
my $class = ref($self); |
364 |
|
$class = $self->class if $self->can('class'); |
365 |
|
my $icon_path = $self->icon_path( $class, $variant ) || return; |
366 |
|
|
367 |
|
$icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|; |
368 |
|
warn "# using icon $icon_path"; |
369 |
|
|
370 |
|
=for later |
371 |
|
|
372 |
|
# FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work! |
373 |
|
my $ico_path = $icon_path; |
374 |
|
$ico_path =~ s{png$}{ico}; |
375 |
|
if ( ! -e $ico_path ) { |
376 |
|
system "convert $icon_path $ico_path"; |
377 |
|
warn "# convert $icon_path $ico_path : $@"; |
378 |
|
} |
379 |
|
$icon_html .= qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path">| if -e $ico_path; |
380 |
|
|
381 |
|
=cut |
382 |
|
|
383 |
|
} |
384 |
|
|
385 |
|
my $warn_colors = { |
386 |
|
'#' => '#444', |
387 |
|
'##' => '#888', |
388 |
|
}; |
389 |
|
|
390 |
|
my $multiline_markers = { |
391 |
|
'(' => ')', |
392 |
|
'{' => '}', |
393 |
|
'[' => ']', |
394 |
|
'"' => '"', |
395 |
|
}; |
396 |
|
|
397 |
|
my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']'; |
398 |
|
warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re"; |
399 |
|
|
400 |
|
sub log_path { |
401 |
|
$Frey::Bootstrap::log_path || warn "no log_path?"; |
402 |
|
} |
403 |
|
|
404 |
|
sub warnings_html { |
405 |
|
my ($self,$level) = shift; |
406 |
|
$level ||= $self->debug, |
407 |
|
my $path = $self->log_path; |
408 |
|
|
409 |
|
my $warnings; |
410 |
|
my $line = 0; |
411 |
|
my $multiline_end; |
412 |
|
|
413 |
|
open(my $log, '<', $path) || die "can't open $path: $!"; |
414 |
|
while(<$log>) { |
415 |
|
chomp; |
416 |
|
$line++; |
417 |
|
|
418 |
|
my $style = ''; |
419 |
|
|
420 |
|
if ( $multiline_end ) { |
421 |
|
if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) { |
422 |
|
# warn "## $line end of $multiline_end in '$_'\n"; |
423 |
|
undef $multiline_end; |
424 |
|
} else { |
425 |
|
# warn "## $line skipped\n"; |
426 |
|
} |
427 |
|
} elsif ( m{^(#*)\s+} ) { |
428 |
|
my $l = $1 ? length($1) : 0; |
429 |
|
if ( $l > $level ) { |
430 |
|
undef $multiline_end; |
431 |
|
$multiline_end = $multiline_markers->{$1} if m{($multiline_re)$}; |
432 |
|
# warn "## $line start $1 .. $multiline_end level $l > $level for '$_'\n" if $multiline_end; |
433 |
|
next; |
434 |
|
} |
435 |
|
|
436 |
|
$style = $warn_colors->{$1} |
437 |
|
? ' style="color:' . $warn_colors->{$1} . '"' |
438 |
|
: ''; |
439 |
|
|
440 |
|
my $msg = $self->html_escape( $_ ); |
441 |
|
my $spacer = ' '; |
442 |
|
if ( length($msg) > $self->html_dump_width ) { |
443 |
|
$msg = substr( $msg, 0, $self->html_dump_width ); |
444 |
|
$spacer = '…' |
445 |
|
} |
446 |
|
$msg =~ s{^\s}{ }g; |
447 |
|
$warnings .= qq|<tt$style>$msg</tt>$spacer<small><a target="editor" href="/editor+$path+$line">+$line</a></small><br/>|; |
448 |
|
# FIXME <tt> should be <code> but CSS hates me |
449 |
|
} |
450 |
|
} |
451 |
|
close($log) || die "can't close $path: $!"; |
452 |
|
|
453 |
|
return |
454 |
|
qq|<span class="frey-popup"><a target="editor" href="/editor+$path+$line" title="open $path level $level">warn</a><span>| |
455 |
|
. $self->editor_links( $warnings ) |
456 |
|
. qq|</span></span>| |
457 |
|
; |
458 |
|
} |
459 |
|
|
460 |
|
|
461 |
|
=head2 backtrace |
462 |
|
|
463 |
|
Show backtrace with links to editor |
464 |
|
|
465 |
|
my @backtrace = $self->backtrace; |
466 |
|
|
467 |
|
=cut |
468 |
|
|
469 |
|
sub backtrace { |
470 |
|
my ($self) = @_; |
471 |
|
|
472 |
|
my @backtrace; |
473 |
|
foreach ( 0 .. 5 ) { |
474 |
|
my ( |
475 |
|
$package,$path,$line |
476 |
|
# subroutine hasargs |
477 |
|
# wantarray evaltext is_require |
478 |
|
# hints bitmask hinthash |
479 |
|
) = caller($_) or last; |
480 |
|
|
481 |
|
push @backtrace, |
482 |
|
qq|via $package from $path <a target="editor" href="/editor+$path+$line">$path</a>|; |
483 |
|
} |
484 |
|
warn "# backtrace: ", dump( @backtrace ) if @backtrace; |
485 |
|
return @backtrace; |
486 |
} |
} |
487 |
|
|
488 |
1; |
1; |