16 |
|
|
17 |
use Frey::SVK; |
use Frey::SVK; |
18 |
|
|
19 |
has 'head' => ( |
use Text::Tabs; # expand, unexpand |
20 |
is => 'rw', |
|
21 |
isa => 'ArrayRef[Str]', |
our @head; |
22 |
default => sub { [ 'static/frey.css' ] }, |
sub head { @head } |
|
); |
|
23 |
|
|
24 |
has 'request_url' => ( |
has 'request_url' => ( |
25 |
is => 'rw', |
is => 'rw', |
26 |
isa => 'Uri', coerce => 1, |
isa => 'Uri', coerce => 1, |
27 |
default => '/', |
required => 1, |
28 |
|
# default => '/', |
29 |
); |
); |
30 |
|
|
31 |
has 'title' => ( |
has 'title' => ( |
64 |
is => 'rw', |
is => 'rw', |
65 |
isa => 'Int', |
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 |
# 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, |
default => 120, |
68 |
); |
); |
69 |
|
|
70 |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
83 |
$dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs; |
$dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs; |
84 |
$dump = $self->html_escape( $dump ); |
$dump = $self->html_escape( $dump ); |
85 |
$dump =~ s{\Q...\E}{…}gs; |
$dump =~ s{\Q...\E}{…}gs; |
86 |
|
# $dump =~ $self->editor_links( $dump ); # FIXME include this |
87 |
return "<code>$dump</code>"; |
return "<code>$dump</code>"; |
88 |
} |
} |
89 |
|
|
90 |
sub popup { my $self = shift; $self->popup_dropdown('popup', @_); } |
sub popup { my $self = shift; $self->popup_dropdown('popup', @_); } |
91 |
sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); } |
sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); } |
92 |
|
|
93 |
our $re_html = qr{<(?:!--.+?--|(\w+).+?/\1)>}s; # relaxed html check for one semi-valid tag |
our $re_html = qr{<(?:!--.+?--|(\w+).+?/\1|[^>]+/)>}s; # relaxed html check for one semi-valid tag |
94 |
|
|
95 |
sub popup_dropdown { |
sub popup_dropdown { |
96 |
my ( $self, $type, $name, $content, $full ) = @_; |
my ( $self, $type, $name, $content, $full ) = @_; |
99 |
|
|
100 |
$content = qq|<span>$content</span>| unless $content =~ m{^\s*<(span|a|code).+?/\1>\s*}; |
$content = qq|<span>$content</span>| unless $content =~ m{^\s*<(span|a|code).+?/\1>\s*}; |
101 |
|
|
102 |
warn "## $type [$name] = ", length( $content ), " bytes" if $self->debug; |
$content =~ s{<span>(<code>[^<]+</code>)</span>}{$1} && $self->TODO("code wrapped in span"); |
103 |
|
|
104 |
|
warn "## $type [$name] = ", length( $content ), " bytes"; # if $self->debug; # FIXME |
105 |
|
|
106 |
if ( $name =~ m{::} && $name !~ $re_html ) { |
if ( $name =~ m{::} && $name !~ $re_html ) { |
107 |
return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|; |
return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|; |
108 |
|
} elsif ( $name =~ s{^\s*(<a)\s+}{$1 class="frey-$type"} ) { |
109 |
|
return qq|$name $content\n|; |
110 |
} else { |
} else { |
111 |
return qq|<span class="frey-$type">$name $content</span>\n|; |
return qq|<span class="frey-$type">$name $content</span>\n|; |
112 |
} |
} |
120 |
sub _head_html { |
sub _head_html { |
121 |
my $self = shift; |
my $self = shift; |
122 |
my $out = ''; |
my $out = ''; |
123 |
foreach my $path ( @{ $self->head } ) { |
foreach my $path ( @head ) { |
124 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
125 |
if ( $path =~ m/\.js$/ ) { |
if ( $path =~ m/\.js$/ ) { |
126 |
$out .= $self->_inline_path( $path ) ? |
$out .= $self->_inline_path( $path ) ? |
127 |
qq|<!-- $path --><script type="text/javascript">\n| . read_file($path) . qq|\n</script>| : |
qq|<script type="text/javascript">\n/* inline $path */\n\n| . read_file($path) . qq|\n</script>| : |
128 |
qq|<script type="text/javascript" src="/$path"></script>|; |
qq|<script type="text/javascript" src="/$path"></script>|; |
129 |
} elsif ( $path =~ m/\.css$/ ) { |
} elsif ( $path =~ m/\.css$/ ) { |
130 |
$out .= $self->_inline_path( $path ) ? |
$out .= $self->_inline_path( $path ) ? |
131 |
qq|<!-- $path --><style type="text/css">\n| . read_file( $path ) . qq|\n</style>| : |
qq|<style type="text/css">\n/* inline $path */\n\n| . read_file( $path ) . qq|\n</style>| : |
132 |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
133 |
} elsif ( $path =~ m{<.+>}s ) { |
} elsif ( $path =~ m{<.+>}s ) { |
134 |
$out .= $path; |
$out .= $path; |
156 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
157 |
|
|
158 |
if ( $path =~ $re_html ) { |
if ( $path =~ $re_html ) { |
159 |
push @{ $self->head }, $path; |
push @head, $path; |
160 |
} elsif ( -e $path ) { |
} elsif ( -e $path ) { |
161 |
if ( $path =~ m/\.(?:js|css)$/ ) { |
if ( $path =~ m/\.(?:js|css)$/ ) { |
162 |
push @{ $self->head }, $path; |
push @head, $path; |
163 |
} else { |
} else { |
164 |
confess "can't add_head( $path ) it's not js or css"; |
confess "can't add_head( $path ) it's not js or css"; |
165 |
} |
} |
170 |
|
|
171 |
} |
} |
172 |
|
|
173 |
|
sub add_css { |
174 |
|
my ($self,$css) = @_; |
175 |
|
my ( $package, $path, $line ) = caller; |
176 |
|
$self->add_head( qq| |
177 |
|
<style type="text/css"> |
178 |
|
/* via $package at $path line $line */ |
179 |
|
$css |
180 |
|
</style> |
181 |
|
| ); |
182 |
|
} |
183 |
|
|
184 |
our $reload_counter = 0; |
our $reload_counter = 0; |
185 |
|
|
186 |
|
|
209 |
|
|
210 |
my $status_line = ''; |
my $status_line = ''; |
211 |
|
|
|
unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup }; |
|
|
# unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }; |
|
|
unshift @status, { 'INC' => Frey::INC->new->as_markup }; |
|
|
|
|
212 |
foreach my $part ( @status ) { |
foreach my $part ( @status ) { |
213 |
foreach my $name ( keys %$part ) { |
foreach my $name ( keys %$part ) { |
214 |
$status_line .= $self->popup( $name, $part->{$name} ); |
$status_line .= $self->popup( $name, $part->{$name} ); |
219 |
$url =~ s{\?reload=\d+}{}; |
$url =~ s{\?reload=\d+}{}; |
220 |
|
|
221 |
my $body = $a->{body}; |
my $body = $a->{body}; |
222 |
$body ||= $self->as_markup if $self->can('as_markup'); |
if ( ! $body ) { |
223 |
|
my $run = $a->{run} || 'as_markup'; |
224 |
|
warn "# no body, invoke $self->$run on ", ref($self); |
225 |
|
$body = $self->$run; |
226 |
|
} |
227 |
if ( $self->content_type !~ m{html} ) { |
if ( $self->content_type !~ m{html} ) { |
228 |
warn "# return only $self body ", $self->content_type; |
warn "# return only $self body ", $self->content_type; |
229 |
return $body |
return $body |
246 |
</span> |
</span> |
247 |
|; |
|; |
248 |
|
|
249 |
my $info = Frey::SVK->info; |
my $svk = Frey::SVK->new; |
250 |
my $revision = Frey::SVK->info->{Revision} || ''; |
my $info = $svk->info; |
251 |
|
my $revision = $svk->info->{Revision} || ''; |
252 |
$revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)}; |
$revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)}; |
253 |
|
|
254 |
$self->add_icon unless $icon_html; |
$self->add_icon unless $icon_html; |
309 |
sub editor_links { |
sub editor_links { |
310 |
my ( $self, $error ) = @_; |
my ( $self, $error ) = @_; |
311 |
|
|
312 |
|
# $error =~ s[(bless\({\s+.+?\s+},\s+)("[^"]+")(\) at)][<span class="frey-dropdown">$1<code>$2</code>$3</span>]gs; # FIXME insert bless hiding back |
313 |
|
|
314 |
$error =~ s{at\s+(\S+)\s+line\s+(\d+)} |
$error =~ s{at\s+(\S+)\s+line\s+(\d+)} |
315 |
{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; |
316 |
|
|
317 |
$error =~ s{(via package ")([\w:]+)(")} |
$error =~ s{(via (?:package) "?)([\w:]+)("?)} |
318 |
{$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm; |
{$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm; |
319 |
|
|
320 |
return $error; |
return $error; |
335 |
; |
; |
336 |
} |
} |
337 |
|
|
338 |
|
=head1 Status line |
339 |
|
|
340 |
|
=head2 add_status |
341 |
|
|
342 |
|
$self->add_status( { name => { some => 'data' } } ); |
343 |
|
|
344 |
|
$self->add_status( "append to last status popup" ); |
345 |
|
|
346 |
|
=cut |
347 |
|
|
348 |
sub add_status { |
sub add_status { |
349 |
my ( $self, $data ) = @_; |
my ( $self, $data ) = @_; |
350 |
push @status, $data; |
push @status, { 'X' => [ $self->backtrace ] }; |
351 |
|
if ( ref($data) ) { |
352 |
|
push @status, $data; |
353 |
|
} else { |
354 |
|
if ( defined $status[ $#status ] ) { |
355 |
|
$status[ $#status ]->{ '+' } = $data; |
356 |
|
} else { |
357 |
|
push @status, { '+' => $data }; |
358 |
|
} |
359 |
|
} |
360 |
} |
} |
361 |
|
|
362 |
|
=head2 clean_status |
363 |
|
|
364 |
|
Called at beginning of each request |
365 |
|
|
366 |
|
$self->clean_status; |
367 |
|
|
368 |
|
=cut |
369 |
|
|
370 |
sub clean_status { |
sub clean_status { |
371 |
@status = (); |
my ($self) = shift; |
372 |
|
@head = ( 'static/frey.css' ); |
373 |
|
my $params = { request_url => $self->request_url }; |
374 |
|
@status = ( |
375 |
|
{ 'ClassBrowser' => Frey::ClassBrowser->new( %$params, usage_on_top => 0 )->as_markup }, |
376 |
|
{ 'Bookmarklets' => Frey::Bookmarklet->new( %$params )->as_markup }, |
377 |
|
{ 'INC' => Frey::INC->new( %$params )->as_markup }, |
378 |
|
); |
379 |
$icon_html = ''; |
$icon_html = ''; |
380 |
} |
} |
381 |
|
|
382 |
|
=head2 status_parts |
383 |
|
|
384 |
|
Dump all status line parts |
385 |
|
|
386 |
|
$self->status_parts |
387 |
|
|
388 |
|
=cut |
389 |
|
|
390 |
sub status_parts { |
sub status_parts { |
391 |
warn "## status parts ", dump( map { keys %$_ } @status ); |
warn "## status parts ", dump( map { keys %$_ } @status ); |
392 |
} |
} |
393 |
|
|
394 |
|
=for debug |
395 |
|
|
396 |
sub DEMOLISH { |
sub DEMOLISH { |
397 |
my ( $self ) = @_; |
my ( $self ) = @_; |
398 |
warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status; |
warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status; |
399 |
} |
} |
400 |
|
|
401 |
|
=cut |
402 |
|
|
403 |
=head2 add_icon |
=head2 add_icon |
404 |
|
|
405 |
Frey::Foo->add_icon; # /static/icons/Frey/Foo.png |
Frey::Foo->add_icon; # /static/icons/Frey/Foo.png |
414 |
$icon .= "/$variant" if $variant; |
$icon .= "/$variant" if $variant; |
415 |
my $path = 'static/icons/' . $icon . '.png'; |
my $path = 'static/icons/' . $icon . '.png'; |
416 |
if ( -e $path ) { |
if ( -e $path ) { |
417 |
warn "# $class from $self icon_path $path"; |
warn "# $class from $self icon_path $path" if $self->debug; |
418 |
return $path; |
return $path; |
419 |
} else { |
} else { |
420 |
$self->TODO( "add $path icon for $class" ); |
$self->TODO( "add $path icon for $class" ); |
471 |
$level ||= $self->debug, |
$level ||= $self->debug, |
472 |
my $path = $self->log_path; |
my $path = $self->log_path; |
473 |
|
|
474 |
my $warnings; |
my $max = 30; |
475 |
|
my $pos = 0; |
476 |
|
my @warnings = ( '' x $max ); # XXX circualar buffer for 50 lines |
477 |
my $line = 0; |
my $line = 0; |
478 |
my $multiline_end; |
my $multiline_end; |
479 |
|
|
480 |
|
# XXX do we really want to do this every time? |
481 |
|
my $css = qq|/* short css classes for levels */\n|; |
482 |
|
my $level_to_class; |
483 |
|
foreach ( keys %$warn_colors ) { |
484 |
|
my $l = length($_); |
485 |
|
my $class = 'l' . $l; |
486 |
|
$css .= qq|.$class { color: $warn_colors->{$_} }\n|; |
487 |
|
$level_to_class->{ $_ } = $class; |
488 |
|
} |
489 |
|
$self->add_css( $css ); |
490 |
|
|
491 |
open(my $log, '<', $path) || die "can't open $path: $!"; |
open(my $log, '<', $path) || die "can't open $path: $!"; |
492 |
while(<$log>) { |
while(<$log>) { |
493 |
chomp; |
chomp; |
495 |
|
|
496 |
my $style = ''; |
my $style = ''; |
497 |
|
|
498 |
|
=for filter |
499 |
|
|
500 |
if ( $multiline_end ) { |
if ( $multiline_end ) { |
501 |
if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) { |
if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) { |
502 |
# warn "## $line end of $multiline_end in '$_'\n"; |
# warn "## $line end of $multiline_end in '$_'\n"; |
513 |
next; |
next; |
514 |
} |
} |
515 |
|
|
516 |
$style = $warn_colors->{$1} |
=cut |
517 |
? ' style="color:' . $warn_colors->{$1} . '"' |
if ( m{^(#*)} ) { |
518 |
: ''; |
|
519 |
|
my $level = $1; |
520 |
|
my $msg = $_; |
521 |
|
|
|
my $msg = $self->html_escape( $_ ); |
|
522 |
my $spacer = ' '; |
my $spacer = ' '; |
523 |
if ( length($msg) > $self->html_dump_width ) { |
my $real_msg = expand( $msg ); |
524 |
$msg = substr( $msg, 0, $self->html_dump_width ); |
if ( length($real_msg) > $self->html_dump_width ) { |
525 |
|
|
526 |
|
$real_msg = substr( $msg, 0, $self->html_dump_width ); |
527 |
|
$msg = unexpand( $real_msg ); |
528 |
$spacer = '…' |
$spacer = '…' |
529 |
} |
} |
530 |
$msg =~ s{^\s}{ }g; |
|
531 |
$warnings .= qq|<tt$style>$msg</tt>$spacer<small><a target="editor" href="/editor+$path+$line">+$line</a></small><br/>|; |
$msg = $self->html_escape( $msg ); |
532 |
# FIXME <tt> should be <code> but CSS hates me |
|
533 |
|
if ( my $class = $level_to_class->{ $level } ) { |
534 |
|
$msg = qq|<span class="$class">$msg</span>|; |
535 |
|
} |
536 |
|
|
537 |
|
#$msg .= $spacer . qq|<a target="editor" href="/editor+$path+$line" style="float: right;">$line</a>\n|; |
538 |
|
$msg = qq|<a target="editor" href="/editor+$path+$line" style="float: right;">$line</a>$msg| |
539 |
|
. ( $spacer ? $spacer : '' ) |
540 |
|
. "\n"; # XXX <pre> needs this |
541 |
|
|
542 |
|
$warnings[ $pos++ % $max ] = $msg; |
543 |
} |
} |
544 |
} |
} |
545 |
|
warn "log has $line lines tell position ",tell($log); |
546 |
close($log) || die "can't close $path: $!"; |
close($log) || die "can't close $path: $!"; |
547 |
|
|
548 |
|
my $size = -s $path; |
549 |
|
|
550 |
|
my $warnings = join("", |
551 |
|
map { $warnings[ ( $pos + $_ ) % $max ] || '' } 0 .. ( $max - 1 ) |
552 |
|
); |
553 |
|
|
554 |
|
my $s = length($warnings); |
555 |
|
|
556 |
return |
return |
557 |
qq|<span class="frey-popup"><a target="editor" href="/editor+$path+$line" title="open $path level $level">warn</a><span>| |
# need to wrap editor link into span so we can have links in warnings |
558 |
|
qq|<span class="frey-popup"><a target="editor" href="/editor+$path+$line" title="$path \| $size -> $s bytes \| $line -> $pos lines \| level $level">warn</a><code>| |
559 |
. $self->editor_links( $warnings ) |
. $self->editor_links( $warnings ) |
560 |
. qq|</span></span>| |
. qq|</code></span></a>| |
561 |
; |
; |
562 |
} |
} |
563 |
|
|
583 |
) = caller($_) or last; |
) = caller($_) or last; |
584 |
|
|
585 |
push @backtrace, |
push @backtrace, |
586 |
qq|via $package from $path <a target="editor" href="/editor+$path+$line">$path</a>|; |
qq|via $package at $path line $line|; |
587 |
} |
} |
588 |
warn "# backtrace: ", dump( @backtrace ) if @backtrace; |
#warn "# backtrace: ", dump( @backtrace ) if @backtrace; |
589 |
return @backtrace; |
return @backtrace; |
590 |
} |
} |
591 |
|
|