1 |
package Frey::Web; |
package Frey::Web; |
2 |
use Moose::Role; |
use Moose::Role; |
3 |
|
|
4 |
with 'Frey::Backtrace'; |
with 'Frey::Session'; |
5 |
|
|
|
use Frey::Types; |
|
|
|
|
|
use Continuity::Widget::DomNode; |
|
6 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
7 |
use Carp qw/confess cluck/; |
use Carp qw/confess cluck carp/; |
8 |
use File::Slurp; |
use File::Slurp; |
9 |
|
use Text::Tabs; # expand, unexpand |
10 |
|
|
11 |
|
use lib 'lib'; |
12 |
|
|
13 |
|
use Frey::Types; |
14 |
|
|
15 |
use Frey::Bookmarklet; |
use Frey::Bookmarklet; |
16 |
use Frey::ClassBrowser; |
use Frey::Class::Browser; |
17 |
|
use Frey::INC; |
18 |
|
|
19 |
use Frey::SVK; |
use Frey::SVK; |
20 |
|
|
21 |
has 'head' => ( |
our @head; |
22 |
is => 'rw', |
sub head { @head } |
|
isa => 'ArrayRef[Str]', |
|
|
default => sub { [ 'static/frey.css' ] }, |
|
|
); |
|
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 => sub { |
29 |
|
carp "undefined request_url"; |
30 |
|
'/'; |
31 |
|
}, |
32 |
); |
); |
33 |
|
|
34 |
has 'title' => ( |
has 'title' => ( |
52 |
is => 'rw', |
is => 'rw', |
53 |
isa => 'Int', |
isa => 'Int', |
54 |
default => 4096, |
default => 4096, |
55 |
documentation => 'Maximum dump size sent to browser before truncation', |
documentation => 'maximum dump size sent to browser before truncation', |
56 |
); |
); |
57 |
|
|
58 |
has 'inline_smaller_than' => ( |
has 'inline_smaller_than' => ( |
59 |
is => 'rw', |
is => 'rw', |
60 |
isa => 'Int', |
isa => 'Int', |
61 |
default => 10240, |
default => 10240, |
62 |
documentation => 'Inline JavaScript and CSS to reduce round-trips', |
documentation => 'inline JavaScript and CSS to reduce round-trips', |
63 |
); |
); |
64 |
|
|
65 |
has 'chop_warning' => ( |
has 'html_dump_width' => ( |
66 |
|
documentation => 'crop longer lines in dumps', |
67 |
is => 'rw', |
is => 'rw', |
68 |
isa => 'Int', |
isa => 'Int', |
69 |
default => 80, |
# required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them |
70 |
documentation => 'Crop lines longer', |
default => 250, |
71 |
|
); |
72 |
|
|
73 |
|
has 'wrap_in_page' => ( |
74 |
|
documentation => 'wrap full html page with status bar around content', |
75 |
|
is => 'rw', |
76 |
|
isa => 'Bool', |
77 |
|
default => 1, |
78 |
); |
); |
79 |
|
|
80 |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
82 |
|
|
83 |
sub html_escape { |
sub html_escape { |
84 |
my ( $self, $html ) = @_; |
my ( $self, $html ) = @_; |
85 |
|
return '' unless defined $html; |
86 |
$html =~ s/($escape_re)/$escape{$1}/g; |
$html =~ s/($escape_re)/$escape{$1}/g; |
87 |
return $html; |
return $html; |
88 |
} |
} |
89 |
|
|
90 |
|
# from Mojo::ByteStream |
91 |
|
sub url_escape { |
92 |
|
my ( $self, $url, $pattern ) = @_; |
93 |
|
$pattern ||= 'A-Za-z0-9\-\.\_\~'; |
94 |
|
$url =~ s/([^$pattern])/sprintf('%%%02X',ord($1))/ge; |
95 |
|
return $url; |
96 |
|
} |
97 |
|
|
98 |
sub html_dump { |
sub html_dump { |
99 |
my $self = shift; |
my ( $self, $dump ) = @_; |
100 |
$self->html_escape( dump( @_ ) ); |
$dump = dump( $dump ) if ref($dump); |
101 |
|
my $width = $self->html_dump_width; |
102 |
|
$dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs; |
103 |
|
$dump = $self->html_escape( $dump ); |
104 |
|
$dump =~ s{\Q...\E}{…}gs; |
105 |
|
# $dump =~ $self->html_links( $dump ); # FIXME include this |
106 |
|
return "<code>$dump</code>"; |
107 |
} |
} |
108 |
|
|
109 |
sub dom2html { |
sub popup { my $self = shift; $self->popup_dropdown('popup', @_); } |
110 |
# warn "## dom2html ",dump( @_ ); |
sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); } |
111 |
return Continuity::Widget::DomNode->create( @_ )->to_string; |
|
112 |
|
our $re_html = qr{<(?:!--|(\w+)|[^>]+)/?>}s; # relaxed html check for one semi-valid tag |
113 |
|
|
114 |
|
sub popup_dropdown { |
115 |
|
my ( $self, $type, $name, $content, $full ) = @_; |
116 |
|
|
117 |
|
$content = $self->html_dump($content) if ref $content; |
118 |
|
|
119 |
|
$content = qq|<span>$content</span>| unless $content =~ m{^\s*<(span|a|code).+?/\1>\s*}; |
120 |
|
|
121 |
|
$content =~ s{<span>(<code>[^<]+</code>)</span>}{$1} && $self->TODO("code wrapped in span"); |
122 |
|
|
123 |
|
warn "## $type [$name] = ", length( $content ), " bytes"; # if $self->debug; # FIXME |
124 |
|
|
125 |
|
if ( $name =~ m{::} && $name !~ $re_html ) { |
126 |
|
return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|; |
127 |
|
} elsif ( $name =~ s{^\s*(<a)\s+}{$1 class="frey-$type"} ) { |
128 |
|
return qq|$name $content\n|; |
129 |
|
} else { |
130 |
|
return qq|<span class="frey-$type">$name $content</span>\n|; |
131 |
|
} |
132 |
} |
} |
133 |
|
|
134 |
sub _inline_path { |
sub _inline { |
135 |
my ( $self, $path ) = @_; |
my ( $self, $path ) = @_; |
136 |
-s $path < $self->inline_smaller_than; |
return unless defined $path; |
137 |
|
warn "# _inline $path"; |
138 |
|
-e $path && -s $path < $self->inline_smaller_than && -s $path; |
139 |
} |
} |
140 |
|
|
141 |
sub _head_html { |
sub _head_html { |
142 |
my $self = shift; |
my $self = shift; |
143 |
my $out = ''; |
my $out = ''; |
144 |
foreach my $path ( @{ $self->head } ) { |
foreach my $path ( @head ) { |
145 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
146 |
if ( $path =~ m/\.js$/ ) { |
if ( $path =~ m/\.js$/ ) { |
147 |
$out .= $self->_inline_path( $path ) ? |
my $size; |
148 |
qq|<!-- $path --><script type="text/javascript">\n| . read_file($path) . qq|\n</script>| : |
$out .= $size = _inline( $path ) ? |
149 |
|
qq|<script type="text/javascript">\n/* inline $path $size bytes */\n\n| . read_file($path) . qq|\n</script>| : |
150 |
qq|<script type="text/javascript" src="/$path"></script>|; |
qq|<script type="text/javascript" src="/$path"></script>|; |
151 |
} elsif ( $path =~ m/\.css$/ ) { |
} elsif ( $path =~ m/\.css$/ ) { |
152 |
$out .= $self->_inline_path( $path ) ? |
my $size; |
153 |
qq|<!-- $path --><style type="text/css">\n| . read_file( $path ) . qq|\n</style>| : |
$out .= $size = _inline( $path ) ? |
154 |
|
qq|<style type="text/css">\n/* inline $path $size bytes */\n\n| . read_file( $path ) . qq|\n</style>| : |
155 |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
156 |
} elsif ( $path =~ m{<.+>}s ) { |
} elsif ( $path =~ m{<.+>}s ) { |
157 |
$out .= $path; |
$out .= $path; |
178 |
return if ! defined $path || $path eq ''; |
return if ! defined $path || $path eq ''; |
179 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
180 |
|
|
181 |
if ( $path =~ m{<.*>}s ) { |
if ( $path =~ $re_html ) { |
182 |
push @{ $self->head }, $path; |
push @head, $path; |
183 |
} elsif ( -e $path ) { |
} elsif ( -e $path ) { |
184 |
if ( $path =~ m/\.(?:js|css)$/ ) { |
if ( $path =~ m/\.(?:js|css)$/ ) { |
185 |
push @{ $self->head }, $path; |
push @head, $path; |
186 |
} else { |
} else { |
187 |
confess "can't add_head( $path ) it's not js or css"; |
confess "can't add_head( $path ) it's not js or css"; |
188 |
} |
} |
193 |
|
|
194 |
} |
} |
195 |
|
|
196 |
|
sub _add_css_js { |
197 |
|
my ( $self, $what, $content ) = @_; |
198 |
|
|
199 |
|
my $tag = $what eq 'css' ? 'style' : 'script'; |
200 |
|
my $type = $what eq 'css' ? 'text/css' : 'text/javascript'; |
201 |
|
my $head; |
202 |
|
|
203 |
|
my ( $package, $path, $line ) = caller(1); |
204 |
|
|
205 |
|
$content = "/$content" if $content !~ m{[\n\r]} && -e $content; |
206 |
|
if ( $content =~ $re_html ) { |
207 |
|
$head = qq| |
208 |
|
$content |
209 |
|
<!-- $type via $package at $path line $line --> |
210 |
|
|; |
211 |
|
} elsif ( $content =~ m{^(/\w+|https?://)} && $content !~ m{[\n\r]} ) { |
212 |
|
if ( $what eq 'js' ) { |
213 |
|
$head = qq| |
214 |
|
<$tag type="$type" src="$content"> |
215 |
|
/* $what via $package at $path line $line */ |
216 |
|
</$tag> |
217 |
|
|; |
218 |
|
} else { |
219 |
|
$head = qq| |
220 |
|
<link rel="stylesheet" type="$type" href="$content"> |
221 |
|
<!-- $what via $package at $path line $line --> |
222 |
|
|; |
223 |
|
} |
224 |
|
} else { |
225 |
|
$head = qq| |
226 |
|
<$tag type="$type"> |
227 |
|
/* via $package at $path line $line */ |
228 |
|
$content |
229 |
|
</$tag> |
230 |
|
|; |
231 |
|
}; |
232 |
|
$self->add_head( $head ); |
233 |
|
} |
234 |
|
|
235 |
|
sub add_css { |
236 |
|
my ($self,$css) = @_; |
237 |
|
$self->_add_css_js( 'css', $css ); |
238 |
|
} |
239 |
|
|
240 |
|
sub add_js { |
241 |
|
my ($self,$js) = @_; |
242 |
|
$self->_add_css_js( 'js', $js ); |
243 |
|
} |
244 |
|
|
245 |
our $reload_counter = 0; |
our $reload_counter = 0; |
246 |
|
|
247 |
|
|
248 |
=head2 page |
=head2 html_page |
249 |
|
|
250 |
$self->page( |
$self->html_page( |
251 |
title => 'page title', |
title => 'page title', |
252 |
head => '<!-- optional head markup -->', |
head => '<!-- optional head markup -->', |
253 |
body => '<b>Page Body</b>', |
body => '<b>Page Body</b>', |
260 |
|
|
261 |
our $icon_html; |
our $icon_html; |
262 |
|
|
263 |
sub popup { |
sub html_page { |
|
my ( $self, $name, $content, $full ) = @_; |
|
|
|
|
|
if ( ref($content) ) { |
|
|
$content = '<code>' . dump($content) . '</code>'; |
|
|
my $l = length($content); |
|
|
$content = qq|<span>$l bytes</span>| if ! $full && $l > $self->dump_max_bytes; |
|
|
} else { |
|
|
$content = qq|<span>$content</span>|; |
|
|
} |
|
|
|
|
|
warn "## popup [$name] = ", length( $content ), " bytes" if $self->debug; |
|
|
return qq|<span class="frey-popup">$name $content</span>\n|; |
|
|
} |
|
|
|
|
|
sub page { |
|
264 |
my $self = shift; |
my $self = shift; |
265 |
my $a = {@_}; |
my $a = {@_}; |
266 |
|
|
|
warn "## page ",dump($a); |
|
|
|
|
267 |
$reload_counter++; |
$reload_counter++; |
268 |
|
|
269 |
my $status_line = ''; |
my $status_line = ''; |
270 |
|
|
|
unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup }; |
|
|
# unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }; |
|
|
|
|
271 |
foreach my $part ( @status ) { |
foreach my $part ( @status ) { |
272 |
foreach my $name ( keys %$part ) { |
foreach my $name ( keys %$part ) { |
273 |
$status_line .= $self->popup( $name, $part->{$name} ); |
$status_line .= $self->popup( $name, $part->{$name} ); |
278 |
$url =~ s{\?reload=\d+}{}; |
$url =~ s{\?reload=\d+}{}; |
279 |
|
|
280 |
my $body = $a->{body}; |
my $body = $a->{body}; |
281 |
$body ||= $self->as_markup if $self->can('as_markup'); |
if ( ! $body ) { |
282 |
if ( $self->content_type !~ m{html} ) { |
my $run = $a->{run} || 'as_markup'; |
283 |
|
warn "# no body, invoke $self->$run on ", ref($self); |
284 |
|
$body = $self->$run; |
285 |
|
} |
286 |
|
if ( $self->content_type !~ m{html} || ! $self->wrap_in_page ) { |
287 |
warn "# return only $self body ", $self->content_type; |
warn "# return only $self body ", $self->content_type; |
288 |
return $body |
return $body |
289 |
} elsif ( ! defined $body ) { |
} elsif ( ! defined $body ) { |
293 |
|
|
294 |
$status_line .= $self->warnings_html; |
$status_line .= $self->warnings_html; |
295 |
|
|
|
my $inc_html; |
|
|
{ |
|
|
my $inc; |
|
|
map { |
|
|
s{.pm$}{}; |
|
|
my $class = $_; |
|
|
s[/][}->{]g; |
|
|
$class =~ s[/][::]g; |
|
|
eval '$inc->{' . $_ . '} = $class'; |
|
|
} sort keys %INC; |
|
|
$inc_html = dump( $inc ); |
|
|
$inc_html =~ s{\s+=>\s+\d+}{}gs; |
|
|
$inc_html =~ s{(['"]?)(\w+)\1\s+=>\s+(['"]?)([\w:]*\2)\3}{<a target="$4" href="/$4" title="$4">$2</a>}gs; |
|
|
$inc_html =~ s{\s+=>\s+}{ }gs; |
|
|
$inc_html =~ s{,}{}gs; |
|
|
} |
|
|
|
|
|
$status_line .= $self->popup( INC => "<small>$inc_html</small>" ); |
|
|
|
|
296 |
my ($exit,$description) = ('exit','stop server'); |
my ($exit,$description) = ('exit','stop server'); |
297 |
($exit,$description) = ('restart','restart server') |
($exit,$description) = ('restart','restart server') |
298 |
if $ENV{FREY_RESTART}; # tune labels on exit link |
if $ENV{FREY_RESTART}; # tune labels on exit link |
299 |
|
|
300 |
my $right = |
my $right = |
301 |
qq| |
qq| |
302 |
<span class="right"> |
<span class="frey-status-right"> |
303 |
<a title="reload $url" href="/reload$url">reload</a> |
<a title="reload $url" href="/reload$url">reload</a> |
304 |
<a title="$description" href="/exit$url">$exit</a> |
<a title="$description" href="/exit$url" target="exit">$exit</a> |
305 |
</span> |
</span> |
306 |
|; |
|; |
307 |
|
|
308 |
my $info = Frey::SVK->info; |
my $svk = Frey::SVK->new; |
309 |
my $revision = Frey::SVK->info->{Revision} || ''; |
my $info = $svk->info; |
310 |
|
my $revision = $svk->info->{Revision} || ''; |
311 |
$revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)}; |
$revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)}; |
312 |
|
|
313 |
$self->add_icon unless $icon_html; |
$self->add_icon unless $icon_html; |
314 |
|
|
315 |
|
my $title = undef |
316 |
|
|| $a->{title} |
317 |
|
|| $self->title |
318 |
|
|| ref($self) |
319 |
|
; |
320 |
|
|
321 |
|
# $title =~ s{(\w)\w+::}{$1:}g; # XXX compress names of classes |
322 |
|
|
323 |
|
$self->add_css(qq| |
324 |
|
body { |
325 |
|
padding-bottom: 3em; /* don't overlap status line */ |
326 |
|
} |
327 |
|
|); |
328 |
|
|
329 |
my $html = join("\n", |
my $html = join("\n", |
330 |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
331 |
$self->_head_html, |
$self->_head_html, |
332 |
'<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>', |
qq|<title>$title</title>|, |
333 |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
334 |
( $icon_html || '<!-- no icon -->' ), |
( $icon_html || '<!-- no icon -->' ), |
335 |
( $a->{head} || '' ), |
( $a->{head} || '' ), |
371 |
qq|>$class</a>|; |
qq|>$class</a>|; |
372 |
} |
} |
373 |
|
|
374 |
=head2 editor_links |
=head2 html_links |
375 |
|
|
376 |
Create HTML links to editor for perl error message |
Create HTML links to editor for perl error message |
377 |
|
|
378 |
my $html = $self->editor_links( $error ) |
my $html = $self->html_links( $error ) |
379 |
|
|
380 |
=cut |
=cut |
381 |
|
|
382 |
sub editor_links { |
sub html_links { |
383 |
my ( $self, $error ) = @_; |
my ( $self, $error ) = @_; |
384 |
|
|
385 |
|
$error = $self->strip_full_path( $error ); |
386 |
|
|
387 |
|
# $error =~ s[(bless\({\s+.+?\s+},\s+)("[^"]+")(\) at)][<span class="frey-dropdown">$1<code>$2</code>$3</span>]gs; # FIXME insert bless hiding back |
388 |
|
|
389 |
|
# perl's backtrace |
390 |
$error =~ s{at\s+(\S+)\s+line\s+(\d+)} |
$error =~ s{at\s+(\S+)\s+line\s+(\d+)} |
391 |
{at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm; |
{at <a target="editor" href="/editor+$1+$2" title="vi $1 +$2">$1</a> line $2}gsm; |
392 |
|
|
393 |
|
$error =~ s{(via (?:package)\s+"?)([\w:]+)("?)} |
394 |
|
{$1<a target="$2" href="/$2" title="introspect $2">$2</a>$3}gsm |
395 |
|
|| # or anything that looks like "Class::Name" |
396 |
|
$error =~ s{"(\w+(?:::\w+)+)"} |
397 |
|
{"<a target="$1" href="/$1" title="introspect $1">$1</a>"}gsm; |
398 |
|
|
399 |
|
# method error messages |
400 |
|
# FIXME replace with link to Frey::Introspect data |
401 |
|
$error =~ s{(method ")(\w+)(" via)} |
402 |
|
{$1<a target="$2" href="/Frey::Shell::Grep/as_markup?pattern=$2" title="grep $2">$2</a>$3}gsm; |
403 |
|
|
404 |
|
# link paths to editor |
405 |
|
$error =~ s{((?:lib|t)/[\S]+)\s+(\d+\s+bytes)} |
406 |
|
{<a target="editor" href="/editor+$1+1" title="vi $1 [$2]">$1</a>}gsm; |
407 |
|
|
408 |
$error =~ s{(via package ")([\w:]+)(")} |
$error =~ s{(class ")([\w:]+)(")} |
409 |
{$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm; |
{$1<a target="$2" href="/$2" title="introspect $2">$2</a>$3}gsm; |
410 |
|
|
411 |
return $error; |
return $error; |
412 |
} |
} |
413 |
|
|
414 |
|
sub html_self { |
415 |
|
my $self = shift; |
416 |
|
my $html = $self; |
417 |
|
$html =~ s{([\w:]+)=}{<a target="$1" href="/$1" title="introspect $1">$1</a>=}gsm; |
418 |
|
return $html; |
419 |
|
} |
420 |
|
|
421 |
|
=head2 error |
422 |
|
|
423 |
|
This method will return error to browser and backtrace unless |
424 |
|
error message ends with LF C<\n> just like L<warn> |
425 |
|
|
426 |
|
=cut |
427 |
|
|
428 |
sub error { |
sub error { |
429 |
my $self = shift; |
my $self = shift; |
430 |
my $error = join(" ", @_); |
my $error = join(" ", @_); |
431 |
|
|
432 |
my @backtrace = $self->backtrace; |
my $fatal = ''; |
433 |
$error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace; |
my $backtrace = ''; |
434 |
|
|
435 |
|
if ( $error !~ m{\n$} ) { |
436 |
|
if ( my @backtrace = $self->backtrace ) { |
437 |
|
$backtrace = |
438 |
|
"\n" . $self->html_self . "->error backtrace\n\t" |
439 |
|
. $self->html_links( join( "\n\t", @backtrace ) ) |
440 |
|
; |
441 |
|
$fatal = qq| frey-fatal|; |
442 |
|
} |
443 |
|
} |
444 |
|
|
445 |
warn "ERROR: $error\n"; |
warn "ERROR: $error\n"; |
446 |
return |
$self->add_icon('error'); |
447 |
qq|<pre class="frey-error">| |
$error = $self->html_links( $error ); |
448 |
. $self->editor_links( $error ) . |
return qq|<pre class="frey-error$fatal">$error $backtrace</pre>| ; |
|
qq|</pre>| |
|
|
; |
|
449 |
} |
} |
450 |
|
|
451 |
|
=head1 Status line |
452 |
|
|
453 |
|
=head2 add_status |
454 |
|
|
455 |
|
$self->add_status( { name => { some => 'data' } } ); |
456 |
|
|
457 |
|
$self->add_status( "append to last status popup" ); |
458 |
|
|
459 |
|
=cut |
460 |
|
|
461 |
sub add_status { |
sub add_status { |
462 |
my ( $self, $data ) = @_; |
my ( $self, $data ) = @_; |
463 |
push @status, $data; |
die "no data" unless $data; |
464 |
|
if ( ref $data ) { |
465 |
|
push @status, $data; |
466 |
|
} else { |
467 |
|
if ( defined $status[ $#status ] ) { |
468 |
|
$status[ $#status ]->{ '+' } = $data; |
469 |
|
} else { |
470 |
|
push @status, { '+' => $data }; |
471 |
|
} |
472 |
|
} |
473 |
} |
} |
474 |
|
|
475 |
|
=head2 clean_status |
476 |
|
|
477 |
|
Called at beginning of each request |
478 |
|
|
479 |
|
$self->clean_status; |
480 |
|
|
481 |
|
=cut |
482 |
|
|
483 |
sub clean_status { |
sub clean_status { |
484 |
@status = (); |
my ($self) = shift; |
485 |
|
@head = ( 'static/frey.css' ); |
486 |
|
@status = ( |
487 |
|
{ 'ClassBrowser' => Frey::Class::Browser->new( usage_sort => 1, usage_on_top => 0 )->as_markup }, |
488 |
|
{ 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }, |
489 |
|
{ 'INC' => Frey::INC->new->as_markup }, |
490 |
|
); |
491 |
$icon_html = ''; |
$icon_html = ''; |
492 |
} |
} |
493 |
|
|
494 |
|
=head2 status_parts |
495 |
|
|
496 |
|
Dump all status line parts |
497 |
|
|
498 |
|
$self->status_parts |
499 |
|
|
500 |
|
=cut |
501 |
|
|
502 |
sub status_parts { |
sub status_parts { |
503 |
warn "## status parts ", dump( map { keys %$_ } @status ); |
warn "## status parts ", dump( map { keys %$_ } @status ); |
504 |
} |
} |
505 |
|
|
506 |
|
=for debug |
507 |
|
|
508 |
sub DEMOLISH { |
sub DEMOLISH { |
509 |
my ( $self ) = @_; |
my ( $self ) = @_; |
510 |
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; |
511 |
} |
} |
512 |
|
|
513 |
|
=cut |
514 |
|
|
515 |
=head2 add_icon |
=head2 add_icon |
516 |
|
|
517 |
Frey::Foo->add_icon; # /static/icons/Frey/Foo.png |
Frey::Foo->add_icon; # /static/icons/Frey/Foo.png |
521 |
|
|
522 |
sub icon_path { |
sub icon_path { |
523 |
my ($self,$class,$variant) = @_; |
my ($self,$class,$variant) = @_; |
524 |
my $icon = $class; |
|
525 |
$icon =~ s{::}{/}g; |
sub icon_exists { |
526 |
$icon .= "/$variant" if $variant; |
my $class = shift; |
527 |
my $path = 'static/icons/' . $icon . '.png'; |
$class =~ s{::}{/}g; |
528 |
if ( -e $path ) { |
$class .= "/$variant" if $variant; |
529 |
warn "# $class from $self icon_path $path"; |
my $icon_path = 'static/icons/' . $class . '.png'; |
530 |
return $path; |
return $icon_path if -e $icon_path; |
531 |
} else { |
return; |
532 |
warn "TODO: add $path icon for $class"; |
} |
533 |
|
|
534 |
|
my $path = icon_exists( $class ); |
535 |
|
if ( ! $path ) { |
536 |
|
my $super_class = $class; |
537 |
|
while ( $super_class =~ s{::[^:]+$}{} && ! $path ) { |
538 |
|
$path = icon_exists( $super_class ) unless $super_class eq 'Frey'; # don't default on Frey icon |
539 |
|
} |
540 |
|
} |
541 |
|
|
542 |
|
if ( ! $path ) { |
543 |
|
$self->TODO( "add icon for $class" . ( $variant ? " variant $variant" : '' ) ); |
544 |
return undef; |
return undef; |
545 |
} |
} |
546 |
|
|
547 |
|
warn "# $class from $self icon_path $path" if $self->debug; |
548 |
|
return $path; |
549 |
} |
} |
550 |
|
|
551 |
sub add_icon { |
sub add_icon { |
552 |
my ($self,$variant) = @_; |
my ($self,$variant) = @_; |
553 |
|
|
554 |
my $class = ref($self); |
my $class = $self->class if $self->can('class'); |
555 |
$class = $self->class if $self->can('class'); |
#$class ||= $self->title; |
556 |
|
$class ||= ref($self); |
557 |
my $icon_path = $self->icon_path( $class, $variant ) || return; |
my $icon_path = $self->icon_path( $class, $variant ) || return; |
558 |
|
|
559 |
$icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|; |
$icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|; |
586 |
'"' => '"', |
'"' => '"', |
587 |
}; |
}; |
588 |
|
|
589 |
|
=for later |
590 |
|
|
591 |
my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']'; |
my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']'; |
592 |
warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re"; |
warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re"; |
593 |
|
|
594 |
|
=cut |
595 |
|
|
596 |
sub log_path { |
sub log_path { |
597 |
$Frey::Bootstrap::log_path || warn "no log_path?"; |
$Frey::Bootstrap::log_path || die "no log_path?"; |
598 |
|
} |
599 |
|
|
600 |
|
our $pwd = `pwd`; |
601 |
|
chomp $pwd; |
602 |
|
|
603 |
|
sub strip_full_path { |
604 |
|
my ($self, $msg) = @_; |
605 |
|
# Mojo seems to expand warn messages to full path which is annoying |
606 |
|
$msg =~ s{/[^/]+/\.\./}{/}gs; |
607 |
|
$msg =~ s{$pwd/*}{}gs; |
608 |
|
return $msg; |
609 |
} |
} |
610 |
|
|
611 |
|
our $last_log_pos = 0; |
612 |
|
our $last_log_line = 0; |
613 |
|
|
614 |
sub warnings_html { |
sub warnings_html { |
615 |
my ($self,$level) = shift; |
my ($self,$level) = shift; |
616 |
$level ||= $self->debug, |
$level ||= $self->debug, |
617 |
my $path = $self->log_path; |
my $path = $self->log_path; |
618 |
|
|
619 |
my $warnings; |
my $max = 30; |
620 |
my $line = 0; |
my $pos = 0; |
621 |
|
my @warnings = ( '' x $max ); # XXX circualar buffer for 50 lines |
622 |
|
my $line = $last_log_line; |
623 |
my $multiline_end; |
my $multiline_end; |
624 |
|
|
625 |
open(my $log, '<', $path) || die "can't open $path: $!"; |
# XXX do we really want to do this every time? |
626 |
|
my $css = qq|/* short css classes for levels */\n|; |
627 |
|
my $level_to_class; |
628 |
|
foreach ( keys %$warn_colors ) { |
629 |
|
my $l = length($_); |
630 |
|
my $class = 'l' . $l; |
631 |
|
$css .= qq|.$class { color: $warn_colors->{$_} }\n|; |
632 |
|
$level_to_class->{ $_ } = $class; |
633 |
|
} |
634 |
|
$self->add_css( $css ); |
635 |
|
|
636 |
|
open(my $log, '<', $path) || die "can't open $path: $!"; |
637 |
|
seek($log, $last_log_pos, 0) || warn "can't seek: $!"; |
638 |
while(<$log>) { |
while(<$log>) { |
639 |
chomp; |
chomp; |
640 |
$line++; |
$line++; |
641 |
|
|
642 |
|
next if m{^\s+(Mojo|Class::MOP|Moose)::}; |
643 |
|
|
644 |
my $style = ''; |
my $style = ''; |
645 |
|
|
646 |
|
=for filter |
647 |
|
|
648 |
if ( $multiline_end ) { |
if ( $multiline_end ) { |
649 |
if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) { |
if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) { |
650 |
# warn "## $line end of $multiline_end in '$_'\n"; |
# warn "## $line end of $multiline_end in '$_'\n"; |
661 |
next; |
next; |
662 |
} |
} |
663 |
|
|
664 |
$style = $warn_colors->{$1} |
=cut |
665 |
? ' style="color:' . $warn_colors->{$1} . '"' |
if ( m{^(#*)} ) { |
666 |
: ''; |
|
667 |
|
my $level = $1; |
668 |
|
my $msg = $self->strip_full_path( $_ ); |
669 |
|
|
|
my $msg = $self->html_escape( $_ ); |
|
670 |
my $spacer = ' '; |
my $spacer = ' '; |
671 |
if ( length($msg) > $self->chop_warning ) { |
my $real_msg = expand( $msg ); |
672 |
$msg = substr( $msg, 0, $self->chop_warning ); |
if ( length($real_msg) > $self->html_dump_width ) { |
673 |
|
|
674 |
|
$real_msg = substr( $msg, 0, $self->html_dump_width ); |
675 |
|
$msg = unexpand( $real_msg ); |
676 |
$spacer = '…' |
$spacer = '…' |
677 |
} |
} |
678 |
$msg =~ s{^\s}{ }g; |
|
679 |
$warnings .= qq|<tt$style>$msg</tt>$spacer<small><a target="editor" href="/editor+$path+$line">+$line</a></small><br/>|; |
$msg = $self->html_escape( $msg ); |
680 |
# FIXME <tt> should be <code> but CSS hates me |
|
681 |
|
if ( my $class = $level_to_class->{ $level } ) { |
682 |
|
$msg = qq|<span class="$class">$msg</span>|; |
683 |
|
} |
684 |
|
|
685 |
|
#$msg .= $spacer . qq|<a target="editor" href="/editor+$path+$line" style="float: right;">$line</a>\n|; |
686 |
|
$msg = qq|<a target="editor" href="/editor+$path+$line" style="float: right;">$line</a>$msg| |
687 |
|
. ( $spacer ? $spacer : '' ) |
688 |
|
. "\n"; # XXX <pre> needs this |
689 |
|
|
690 |
|
$warnings[ $pos++ % $max ] = $msg; |
691 |
} |
} |
692 |
} |
} |
693 |
|
$last_log_pos = tell($log); |
694 |
|
$last_log_line = $line; |
695 |
|
warn "log has $line lines tell position $last_log_pos"; |
696 |
close($log) || die "can't close $path: $!"; |
close($log) || die "can't close $path: $!"; |
697 |
|
|
698 |
|
my $size = -s $path; |
699 |
|
|
700 |
|
my $warnings = join("", |
701 |
|
map { $warnings[ ( $pos + $_ ) % $max ] || '' } 0 .. ( $max - 1 ) |
702 |
|
); |
703 |
|
|
704 |
|
my $s = length($warnings); |
705 |
|
|
706 |
return |
return |
707 |
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 |
708 |
. $self->editor_links( $warnings ) |
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>| |
709 |
. qq|</span></span>| |
. $self->html_links( $warnings ) |
710 |
|
. qq|</code></span>| |
711 |
; |
; |
712 |
} |
} |
713 |
|
|
714 |
|
|
715 |
|
=head2 backtrace |
716 |
|
|
717 |
|
Show backtrace with links to editor |
718 |
|
|
719 |
|
my @backtrace = $self->backtrace; |
720 |
|
|
721 |
|
=cut |
722 |
|
|
723 |
|
sub backtrace { |
724 |
|
my ($self) = @_; |
725 |
|
|
726 |
|
my @backtrace; |
727 |
|
foreach ( 1 .. 5 ) { # 0 = backtrace |
728 |
|
my ( |
729 |
|
$package,$path,$line |
730 |
|
# subroutine hasargs |
731 |
|
# wantarray evaltext is_require |
732 |
|
# hints bitmask hinthash |
733 |
|
) = caller($_) or last; |
734 |
|
|
735 |
|
push @backtrace, |
736 |
|
qq|via "$package" at $path line $line|; |
737 |
|
} |
738 |
|
#warn "# backtrace: ", dump( @backtrace ) if @backtrace; |
739 |
|
return @backtrace; |
740 |
|
} |
741 |
|
|
742 |
|
=head2 checkbox |
743 |
|
|
744 |
|
Generate checkbox html markup from some attribute |
745 |
|
|
746 |
|
my $html = $self->checkbox('attribute_name', $value); |
747 |
|
|
748 |
|
=cut |
749 |
|
|
750 |
|
sub checkbox { |
751 |
|
my ($self,$name,$value) = @_; |
752 |
|
my $checked = ''; |
753 |
|
my $all_checkboxes = eval { $self->$name }; |
754 |
|
warn "ERROR tried to get checkbox value for '$name' which is unknown: $@" if $@; |
755 |
|
$all_checkboxes = [ $all_checkboxes ] unless ref($all_checkboxes) eq 'ARRAY'; # sigh, too chatty |
756 |
|
$checked = ' checked' if grep { defined $_ && $_ eq $value } @$all_checkboxes; |
757 |
|
warn "# checkbox $name $value $checked\t", $self->dump( $self->$name ); |
758 |
|
qq|<input name="$name" value="$value" type="checkbox"$checked>|; |
759 |
|
} |
760 |
|
|
761 |
|
=head2 strip |
762 |
|
|
763 |
|
Strip whitespace around content |
764 |
|
|
765 |
|
my $stripped = strip(' no more whitespace around this '); |
766 |
|
|
767 |
|
=cut |
768 |
|
|
769 |
|
sub strip { |
770 |
|
my $t = shift; |
771 |
|
$t =~ s{^\s+}{}gs; |
772 |
|
$t =~ s{>\s+<}{><}gs; |
773 |
|
$t =~ s{\s+$}{}gs; |
774 |
|
return $t; |
775 |
|
} |
776 |
|
|
777 |
1; |
1; |