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