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