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