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