1 |
package Frey::Web; |
2 |
use Moose::Role; |
3 |
|
4 |
with 'Frey::Session'; |
5 |
|
6 |
use Frey::Types; |
7 |
|
8 |
#use Continuity::Widget::DomNode; |
9 |
use Data::Dump qw/dump/; |
10 |
use Carp qw/confess cluck/; |
11 |
use File::Slurp; |
12 |
|
13 |
use Frey::Bookmarklet; |
14 |
use Frey::ClassBrowser; |
15 |
use Frey::INC; |
16 |
|
17 |
use Frey::SVK; |
18 |
|
19 |
our @head; |
20 |
|
21 |
has 'request_url' => ( |
22 |
is => 'rw', |
23 |
isa => 'Uri', coerce => 1, |
24 |
default => '/', |
25 |
); |
26 |
|
27 |
has 'title' => ( |
28 |
is => 'rw', |
29 |
isa => 'Str', |
30 |
lazy => 1, |
31 |
default => sub { |
32 |
my ($self) = @_; |
33 |
ref($self); |
34 |
}, |
35 |
); |
36 |
|
37 |
has 'content_type' => ( |
38 |
is => 'rw', |
39 |
isa => 'Str', |
40 |
default => 'text/html', |
41 |
documentation => 'Content-type header', |
42 |
); |
43 |
|
44 |
has 'dump_max_bytes' => ( |
45 |
is => 'rw', |
46 |
isa => 'Int', |
47 |
default => 4096, |
48 |
documentation => 'maximum dump size sent to browser before truncation', |
49 |
); |
50 |
|
51 |
has 'inline_smaller_than' => ( |
52 |
is => 'rw', |
53 |
isa => 'Int', |
54 |
default => 10240, |
55 |
documentation => 'inline JavaScript and CSS to reduce round-trips', |
56 |
); |
57 |
|
58 |
has 'html_dump_width' => ( |
59 |
documentation => 'crop longer lines in dumps', |
60 |
is => 'rw', |
61 |
isa => 'Int', |
62 |
# required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them |
63 |
default => 128, |
64 |
); |
65 |
|
66 |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
67 |
my $escape_re = join '|' => keys %escape; |
68 |
|
69 |
sub html_escape { |
70 |
my ( $self, $html ) = @_; |
71 |
$html =~ s/($escape_re)/$escape{$1}/g; |
72 |
return $html; |
73 |
} |
74 |
|
75 |
sub html_dump { |
76 |
my ( $self, $dump ) = @_; |
77 |
$dump = dump( $dump ) if ref($dump); |
78 |
my $width = $self->html_dump_width; |
79 |
$dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs; |
80 |
$dump = $self->html_escape( $dump ); |
81 |
$dump =~ s{\Q...\E}{…}gs; |
82 |
# $dump =~ $self->editor_links( $dump ); # FIXME include this |
83 |
return "<code>$dump</code>"; |
84 |
} |
85 |
|
86 |
sub popup { my $self = shift; $self->popup_dropdown('popup', @_); } |
87 |
sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); } |
88 |
|
89 |
our $re_html = qr{<(?:!--.+?--|(\w+).+?/\1)>}s; # relaxed html check for one semi-valid tag |
90 |
|
91 |
sub popup_dropdown { |
92 |
my ( $self, $type, $name, $content, $full ) = @_; |
93 |
|
94 |
$content = $self->html_dump($content) if ref $content; |
95 |
|
96 |
$content = qq|<span>$content</span>| unless $content =~ m{^\s*<(span|a|code).+?/\1>\s*}; |
97 |
|
98 |
$content =~ s{<span>(<code>[^<]+</code>)</span>}{$1} && $self->TODO("code wrapped in span"); |
99 |
|
100 |
warn "## $type [$name] = ", length( $content ), " bytes" if $self->debug; |
101 |
|
102 |
if ( $name =~ m{::} && $name !~ $re_html ) { |
103 |
return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|; |
104 |
} else { |
105 |
return qq|<span class="frey-$type">$name $content</span>\n|; |
106 |
} |
107 |
} |
108 |
|
109 |
sub _inline_path { |
110 |
my ( $self, $path ) = @_; |
111 |
-s $path < $self->inline_smaller_than; |
112 |
} |
113 |
|
114 |
sub _head_html { |
115 |
my $self = shift; |
116 |
my $out = ''; |
117 |
foreach my $path ( @head ) { |
118 |
$path =~ s!^/!!; |
119 |
if ( $path =~ m/\.js$/ ) { |
120 |
$out .= $self->_inline_path( $path ) ? |
121 |
qq|<!-- $path --><script type="text/javascript">\n| . read_file($path) . qq|\n</script>| : |
122 |
qq|<script type="text/javascript" src="/$path"></script>|; |
123 |
} elsif ( $path =~ m/\.css$/ ) { |
124 |
$out .= $self->_inline_path( $path ) ? |
125 |
qq|<!-- $path --><style type="text/css">\n| . read_file( $path ) . qq|\n</style>| : |
126 |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
127 |
} elsif ( $path =~ m{<.+>}s ) { |
128 |
$out .= $path; |
129 |
} else { |
130 |
confess "don't know how to render $path"; |
131 |
} |
132 |
$out .= "\n"; |
133 |
} |
134 |
return $out; |
135 |
} |
136 |
|
137 |
=head2 add_head |
138 |
|
139 |
$o->add_head( 'path/to/external.js' ); |
140 |
|
141 |
my $size = $o->add_head( 'path/to/external.css' ); |
142 |
|
143 |
$o->add_head( '<!-- html content -->' ); |
144 |
|
145 |
=cut |
146 |
|
147 |
sub add_head { |
148 |
my ( $self, $path ) = @_; |
149 |
return if ! defined $path || $path eq ''; |
150 |
$path =~ s!^/!!; |
151 |
|
152 |
if ( $path =~ $re_html ) { |
153 |
push @head, $path; |
154 |
} elsif ( -e $path ) { |
155 |
if ( $path =~ m/\.(?:js|css)$/ ) { |
156 |
push @head, $path; |
157 |
} else { |
158 |
confess "can't add_head( $path ) it's not js or css"; |
159 |
} |
160 |
return -s $path; |
161 |
} else { |
162 |
confess "can't find $path: $!"; |
163 |
} |
164 |
|
165 |
} |
166 |
|
167 |
sub add_css { |
168 |
my ($self,$css) = @_; |
169 |
my ( $package, $path, $line ) = caller; |
170 |
$self->add_head( qq| |
171 |
<style type="text/css"> |
172 |
/* via $package at $path line $line */ |
173 |
$css |
174 |
</style> |
175 |
| ); |
176 |
} |
177 |
|
178 |
our $reload_counter = 0; |
179 |
|
180 |
|
181 |
=head2 page |
182 |
|
183 |
$self->page( |
184 |
title => 'page title', |
185 |
head => '<!-- optional head markup -->', |
186 |
body => '<b>Page Body</b>', |
187 |
); |
188 |
|
189 |
=cut |
190 |
|
191 |
our @status; |
192 |
sub status { @status }; |
193 |
|
194 |
our $icon_html; |
195 |
|
196 |
sub page { |
197 |
my $self = shift; |
198 |
my $a = {@_}; |
199 |
|
200 |
warn "## page ",dump($a); |
201 |
|
202 |
$reload_counter++; |
203 |
|
204 |
my $status_line = ''; |
205 |
|
206 |
foreach my $part ( @status ) { |
207 |
foreach my $name ( keys %$part ) { |
208 |
$status_line .= $self->popup( $name, $part->{$name} ); |
209 |
} |
210 |
} |
211 |
|
212 |
my $url = $self->request_url; |
213 |
$url =~ s{\?reload=\d+}{}; |
214 |
|
215 |
my $body = $a->{body}; |
216 |
$body ||= $self->as_markup if $self->can('as_markup'); |
217 |
if ( $self->content_type !~ m{html} ) { |
218 |
warn "# return only $self body ", $self->content_type; |
219 |
return $body |
220 |
} elsif ( ! defined $body ) { |
221 |
warn "# no body"; |
222 |
$body = '<!-- no body -->'; |
223 |
} |
224 |
|
225 |
$status_line .= $self->warnings_html; |
226 |
|
227 |
my ($exit,$description) = ('exit','stop server'); |
228 |
($exit,$description) = ('restart','restart server') |
229 |
if $ENV{FREY_RESTART}; # tune labels on exit link |
230 |
|
231 |
my $right = |
232 |
qq| |
233 |
<span class="right"> |
234 |
<a title="reload $url" href="/reload$url">reload</a> |
235 |
<a title="$description" href="/exit$url">$exit</a> |
236 |
</span> |
237 |
|; |
238 |
|
239 |
my $svk = Frey::SVK->new; |
240 |
my $info = $svk->info; |
241 |
my $revision = $svk->info->{Revision} || ''; |
242 |
$revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)}; |
243 |
|
244 |
$self->add_icon unless $icon_html; |
245 |
|
246 |
my $html = join("\n", |
247 |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
248 |
$self->_head_html, |
249 |
'<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>', |
250 |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
251 |
( $icon_html || '<!-- no icon -->' ), |
252 |
( $a->{head} || '' ), |
253 |
qq| |
254 |
</head><body> |
255 |
$body |
256 |
<div class="frey-status-line"> |
257 |
<a href="/">Frey</a> $Frey::VERSION $revision |
258 |
$status_line |
259 |
$right |
260 |
</div> |
261 |
</body></html> |
262 |
|, |
263 |
); |
264 |
|
265 |
warn "## >>> page ",length($html), " bytes\n" if $self->debug; |
266 |
|
267 |
return $html; |
268 |
} |
269 |
|
270 |
=head2 editor |
271 |
|
272 |
Create HTML editor link with optional line and title |
273 |
|
274 |
my $html = $self->editor( $class, $line, $title ); |
275 |
|
276 |
=cut |
277 |
|
278 |
sub editor { |
279 |
my ( $self, $class, $line, $title ) = @_; |
280 |
confess "need class" unless $class; |
281 |
if ( ! defined $title ) { |
282 |
$title = "edit $class"; |
283 |
$title .= " line $line" if $line; |
284 |
} |
285 |
$line ||= 1; |
286 |
qq|<a target="editor" href="/editor+$class+$line"| . |
287 |
( $title ? qq| title="$title"| : '' ) . |
288 |
qq|>$class</a>|; |
289 |
} |
290 |
|
291 |
=head2 editor_links |
292 |
|
293 |
Create HTML links to editor for perl error message |
294 |
|
295 |
my $html = $self->editor_links( $error ) |
296 |
|
297 |
=cut |
298 |
|
299 |
sub editor_links { |
300 |
my ( $self, $error ) = @_; |
301 |
|
302 |
$error =~ s{at\s+(\S+)\s+line\s+(\d+)} |
303 |
{at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm; |
304 |
|
305 |
$error =~ s{(via (?:package) "?)([\w:]+)("?)} |
306 |
{$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm; |
307 |
|
308 |
return $error; |
309 |
} |
310 |
|
311 |
sub error { |
312 |
my $self = shift; |
313 |
my $error = join(" ", @_); |
314 |
|
315 |
my @backtrace = $self->backtrace; |
316 |
$error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace; |
317 |
|
318 |
warn "ERROR: $error\n"; |
319 |
return |
320 |
qq|<pre class="frey-error">| |
321 |
. $self->editor_links( $error ) . |
322 |
qq|</pre>| |
323 |
; |
324 |
} |
325 |
|
326 |
=head1 Status line |
327 |
|
328 |
=head2 add_status |
329 |
|
330 |
$self->add_status( { name => { some => 'data' } } ); |
331 |
|
332 |
$self->add_status( "append to last status popup" ); |
333 |
|
334 |
=cut |
335 |
|
336 |
sub add_status { |
337 |
my ( $self, $data ) = @_; |
338 |
push @status, { 'X' => [ $self->backtrace ] }; |
339 |
if ( ref($data) ) { |
340 |
push @status, $data; |
341 |
} else { |
342 |
if ( defined $status[ $#status ] ) { |
343 |
$status[ $#status ]->{ '+' } = $data; |
344 |
} else { |
345 |
push @status, { '+' => $data }; |
346 |
} |
347 |
} |
348 |
} |
349 |
|
350 |
=head2 clean_status |
351 |
|
352 |
Called at beginning of each request |
353 |
|
354 |
$self->clean_status; |
355 |
|
356 |
=cut |
357 |
|
358 |
sub clean_status { |
359 |
my ($self) = shift; |
360 |
@head = ( 'static/frey.css' ); |
361 |
@status = ( |
362 |
{ 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup }, |
363 |
{ 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }, |
364 |
{ 'INC' => Frey::INC->new->as_markup }, |
365 |
); |
366 |
$icon_html = ''; |
367 |
} |
368 |
|
369 |
=head2 status_parts |
370 |
|
371 |
Dump all status line parts |
372 |
|
373 |
$self->status_parts |
374 |
|
375 |
=cut |
376 |
|
377 |
sub status_parts { |
378 |
warn "## status parts ", dump( map { keys %$_ } @status ); |
379 |
} |
380 |
|
381 |
=for debug |
382 |
|
383 |
sub DEMOLISH { |
384 |
my ( $self ) = @_; |
385 |
warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status; |
386 |
} |
387 |
|
388 |
=cut |
389 |
|
390 |
=head2 add_icon |
391 |
|
392 |
Frey::Foo->add_icon; # /static/icons/Frey/Foo.png |
393 |
Frey::Foo->add_icon('warning'); # /static/icons/Frey/Foo/warning.png |
394 |
|
395 |
=cut |
396 |
|
397 |
sub icon_path { |
398 |
my ($self,$class,$variant) = @_; |
399 |
my $icon = $class; |
400 |
$icon =~ s{::}{/}g; |
401 |
$icon .= "/$variant" if $variant; |
402 |
my $path = 'static/icons/' . $icon . '.png'; |
403 |
if ( -e $path ) { |
404 |
warn "# $class from $self icon_path $path" if $self->debug; |
405 |
return $path; |
406 |
} else { |
407 |
$self->TODO( "add $path icon for $class" ); |
408 |
return undef; |
409 |
} |
410 |
} |
411 |
|
412 |
sub add_icon { |
413 |
my ($self,$variant) = @_; |
414 |
|
415 |
my $class = ref($self); |
416 |
$class = $self->class if $self->can('class'); |
417 |
my $icon_path = $self->icon_path( $class, $variant ) || return; |
418 |
|
419 |
$icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|; |
420 |
warn "# using icon $icon_path"; |
421 |
|
422 |
=for later |
423 |
|
424 |
# FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work! |
425 |
my $ico_path = $icon_path; |
426 |
$ico_path =~ s{png$}{ico}; |
427 |
if ( ! -e $ico_path ) { |
428 |
system "convert $icon_path $ico_path"; |
429 |
warn "# convert $icon_path $ico_path : $@"; |
430 |
} |
431 |
$icon_html .= qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path">| if -e $ico_path; |
432 |
|
433 |
=cut |
434 |
|
435 |
} |
436 |
|
437 |
my $warn_colors = { |
438 |
'#' => '#444', |
439 |
'##' => '#888', |
440 |
}; |
441 |
|
442 |
my $multiline_markers = { |
443 |
'(' => ')', |
444 |
'{' => '}', |
445 |
'[' => ']', |
446 |
'"' => '"', |
447 |
}; |
448 |
|
449 |
my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']'; |
450 |
warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re"; |
451 |
|
452 |
sub log_path { |
453 |
$Frey::Bootstrap::log_path || warn "no log_path?"; |
454 |
} |
455 |
|
456 |
sub warnings_html { |
457 |
my ($self,$level) = shift; |
458 |
$level ||= $self->debug, |
459 |
my $path = $self->log_path; |
460 |
|
461 |
my $warnings; |
462 |
my $line = 0; |
463 |
my $multiline_end; |
464 |
|
465 |
open(my $log, '<', $path) || die "can't open $path: $!"; |
466 |
while(<$log>) { |
467 |
chomp; |
468 |
$line++; |
469 |
|
470 |
my $style = ''; |
471 |
|
472 |
if ( $multiline_end ) { |
473 |
if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) { |
474 |
# warn "## $line end of $multiline_end in '$_'\n"; |
475 |
undef $multiline_end; |
476 |
} else { |
477 |
# warn "## $line skipped\n"; |
478 |
} |
479 |
} elsif ( m{^(#*)\s+} ) { |
480 |
my $l = $1 ? length($1) : 0; |
481 |
if ( $l > $level ) { |
482 |
undef $multiline_end; |
483 |
$multiline_end = $multiline_markers->{$1} if m{($multiline_re)$}; |
484 |
# warn "## $line start $1 .. $multiline_end level $l > $level for '$_'\n" if $multiline_end; |
485 |
next; |
486 |
} |
487 |
|
488 |
$style = $warn_colors->{$1} |
489 |
? ' style="color:' . $warn_colors->{$1} . '"' |
490 |
: ''; |
491 |
|
492 |
my $msg = $self->html_escape( $_ ); |
493 |
my $spacer = ' '; |
494 |
if ( length($msg) > $self->html_dump_width ) { |
495 |
$msg = substr( $msg, 0, $self->html_dump_width ); |
496 |
$spacer = '…' |
497 |
} |
498 |
$msg =~ s{^\s}{ }g; |
499 |
$warnings .= qq|<tt$style>$msg</tt>$spacer<small><a target="editor" href="/editor+$path+$line">+$line</a></small><br/>|; |
500 |
# FIXME <tt> should be <code> but CSS hates me |
501 |
} |
502 |
} |
503 |
close($log) || die "can't close $path: $!"; |
504 |
|
505 |
return |
506 |
qq|<span class="frey-popup"><a target="editor" href="/editor+$path+$line" title="open $path level $level">warn</a><span>| |
507 |
. $self->editor_links( $warnings ) |
508 |
. qq|</span></span>| |
509 |
; |
510 |
} |
511 |
|
512 |
|
513 |
=head2 backtrace |
514 |
|
515 |
Show backtrace with links to editor |
516 |
|
517 |
my @backtrace = $self->backtrace; |
518 |
|
519 |
=cut |
520 |
|
521 |
sub backtrace { |
522 |
my ($self) = @_; |
523 |
|
524 |
my @backtrace; |
525 |
foreach ( 0 .. 5 ) { |
526 |
my ( |
527 |
$package,$path,$line |
528 |
# subroutine hasargs |
529 |
# wantarray evaltext is_require |
530 |
# hints bitmask hinthash |
531 |
) = caller($_) or last; |
532 |
|
533 |
push @backtrace, |
534 |
qq|via $package at $path line $line|; |
535 |
} |
536 |
warn "# backtrace: ", dump( @backtrace ) if @backtrace; |
537 |
return @backtrace; |
538 |
} |
539 |
|
540 |
1; |