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::SVK; |
16 |
|
|
17 |
has 'head' => ( |
has 'head' => ( |
18 |
is => 'rw', |
is => 'rw', |
20 |
default => sub { [ 'static/frey.css' ] }, |
default => sub { [ 'static/frey.css' ] }, |
21 |
); |
); |
22 |
|
|
|
has 'status' => ( |
|
|
is => 'rw', |
|
|
isa => 'ArrayRef[HashRef[Str]]', |
|
|
lazy => 1, |
|
|
default => sub { [ |
|
|
{ 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }, |
|
|
{ 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup }, |
|
|
] }, |
|
|
); |
|
|
|
|
23 |
has 'request_url' => ( |
has 'request_url' => ( |
24 |
is => 'rw', |
is => 'rw', |
25 |
isa => 'Uri', coerce => 1, |
isa => 'Uri', coerce => 1, |
40 |
is => 'rw', |
is => 'rw', |
41 |
isa => 'Str', |
isa => 'Str', |
42 |
default => 'text/html', |
default => 'text/html', |
43 |
|
documentation => 'Content-type header', |
44 |
); |
); |
45 |
|
|
46 |
=head2 inline_smaller_than |
has 'dump_max_bytes' => ( |
47 |
|
is => 'rw', |
48 |
Inline JavaScript and CSS smaller than this size into page reducing |
isa => 'Int', |
49 |
round-trips to server. |
default => 4096, |
50 |
|
documentation => 'Maximum dump size sent to browser before truncation', |
51 |
=cut |
); |
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 'chop_warning' => ( |
61 |
|
is => 'rw', |
62 |
|
isa => 'Int', |
63 |
|
default => 80, |
64 |
|
documentation => 'Crop lines longer', |
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 = shift; |
78 |
|
$self->html_escape( dump( @_ ) ); |
79 |
|
} |
80 |
|
|
81 |
sub dom2html { |
sub dom2html { |
82 |
# warn "## dom2html ",dump( @_ ); |
# warn "## dom2html ",dump( @_ ); |
83 |
return Continuity::Widget::DomNode->create( @_ )->to_string; |
return Continuity::Widget::DomNode->create( @_ )->to_string; |
154 |
|
|
155 |
=cut |
=cut |
156 |
|
|
157 |
|
our @status; |
158 |
|
sub status { @status }; |
159 |
|
|
160 |
|
our $icon_html; |
161 |
|
|
162 |
|
sub popup { |
163 |
|
my ( $self, $name, $content, $full ) = @_; |
164 |
|
|
165 |
|
if ( ref($content) ) { |
166 |
|
$content = '<code>' . dump($content) . '</code>'; |
167 |
|
my $l = length($content); |
168 |
|
$content = qq|<span>$l bytes</span>| if ! $full && $l > $self->dump_max_bytes; |
169 |
|
} else { |
170 |
|
$content = qq|<span>$content</span>|; |
171 |
|
} |
172 |
|
|
173 |
|
warn "## popup [$name] = ", length( $content ), " bytes" if $self->debug; |
174 |
|
return qq|<span class="frey-popup">$name $content</span>\n|; |
175 |
|
} |
176 |
|
|
177 |
sub page { |
sub page { |
178 |
my $self = shift; |
my $self = shift; |
179 |
my $a = {@_}; |
my $a = {@_}; |
180 |
|
|
181 |
|
warn "## page ",dump($a); |
182 |
|
|
183 |
$reload_counter++; |
$reload_counter++; |
184 |
|
|
185 |
my $status_line = ''; |
my $status_line = ''; |
186 |
foreach my $part ( @{ $self->status } ) { |
|
187 |
if ( ref($part) ne 'HASH' ) { |
unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup }; |
188 |
warn "part not hash ",dump( $part ) ; |
# unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }; |
189 |
#$self->status( $part ); |
|
190 |
next; |
foreach my $part ( @status ) { |
|
} |
|
191 |
foreach my $name ( keys %$part ) { |
foreach my $name ( keys %$part ) { |
192 |
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|; |
|
193 |
} |
} |
194 |
} |
} |
195 |
|
|
206 |
$body = '<!-- no body -->'; |
$body = '<!-- no body -->'; |
207 |
} |
} |
208 |
|
|
209 |
$status_line |
$status_line .= $self->warnings_html; |
210 |
.= qq|<span class="frey-popup">warn<code>| |
|
211 |
. $self->editor_links( join("", $self->warnings ) ) |
my $inc_html; |
212 |
. qq|</code></span>| |
{ |
213 |
if $self->warnings; |
my $inc; |
214 |
|
map { |
215 |
|
s{.pm$}{}; |
216 |
|
my $class = $_; |
217 |
|
s[/][}->{]g; |
218 |
|
$class =~ s[/][::]g; |
219 |
|
eval '$inc->{' . $_ . '} = $class'; |
220 |
|
} sort keys %INC; |
221 |
|
$inc_html = dump( $inc ); |
222 |
|
$inc_html =~ s{\s+=>\s+\d+}{}gs; |
223 |
|
$inc_html =~ s{(['"]?)(\w+)\1\s+=>\s+(['"]?)([\w:]*\2)\3}{<a target="$4" href="/$4" title="$4">$2</a>}gs; |
224 |
|
$inc_html =~ s{\s+=>\s+}{ }gs; |
225 |
|
$inc_html =~ s{,}{}gs; |
226 |
|
} |
227 |
|
|
228 |
|
$status_line .= $self->popup( INC => "<small>$inc_html</small>" ); |
229 |
|
|
230 |
|
my ($exit,$description) = ('exit','stop server'); |
231 |
|
($exit,$description) = ('restart','restart server') |
232 |
|
if $ENV{FREY_RESTART}; # tune labels on exit link |
233 |
|
|
234 |
|
my $right = |
235 |
|
qq| |
236 |
|
<span class="right"> |
237 |
|
<a title="reload $url" href="/reload$url">reload</a> |
238 |
|
<a title="$description" href="/exit$url">$exit</a> |
239 |
|
</span> |
240 |
|
|; |
241 |
|
|
242 |
|
my $info = Frey::SVK->info; |
243 |
|
my $revision = Frey::SVK->info->{Revision} || ''; |
244 |
|
$revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)}; |
245 |
|
|
246 |
|
$self->add_icon unless $icon_html; |
247 |
|
|
248 |
my $html = join("\n", |
my $html = join("\n", |
249 |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
250 |
$self->_head_html, |
$self->_head_html, |
251 |
'<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>', |
'<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>', |
252 |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
253 |
|
( $icon_html || '<!-- no icon -->' ), |
254 |
( $a->{head} || '' ), |
( $a->{head} || '' ), |
255 |
qq| |
qq| |
256 |
</head><body> |
</head><body> |
257 |
$body |
$body |
258 |
<div class="frey-status-line"> |
<div class="frey-status-line"> |
259 |
<a href="/">Frey</a> $Frey::VERSION |
<a href="/">Frey</a> $Frey::VERSION $revision |
|
<a href="?reload=$reload_counter"><code>$url</code></a> |
|
260 |
$status_line |
$status_line |
261 |
|
$right |
262 |
</div> |
</div> |
263 |
</body></html> |
</body></html> |
264 |
|, |
|, |
269 |
return $html; |
return $html; |
270 |
} |
} |
271 |
|
|
272 |
|
=head2 editor |
273 |
|
|
274 |
|
Create HTML editor link with optional line and title |
275 |
|
|
276 |
|
my $html = $self->editor( $class, $line, $title ); |
277 |
|
|
278 |
|
=cut |
279 |
|
|
280 |
|
sub editor { |
281 |
|
my ( $self, $class, $line, $title ) = @_; |
282 |
|
confess "need class" unless $class; |
283 |
|
if ( ! defined $title ) { |
284 |
|
$title = "edit $class"; |
285 |
|
$title .= " line $line" if $line; |
286 |
|
} |
287 |
|
$line ||= 1; |
288 |
|
qq|<a target="editor" href="/editor+$class+$line"| . |
289 |
|
( $title ? qq| title="$title"| : '' ) . |
290 |
|
qq|>$class</a>|; |
291 |
|
} |
292 |
|
|
293 |
|
=head2 editor_links |
294 |
|
|
295 |
|
Create HTML links to editor for perl error message |
296 |
|
|
297 |
|
my $html = $self->editor_links( $error ) |
298 |
|
|
299 |
|
=cut |
300 |
|
|
301 |
sub editor_links { |
sub editor_links { |
302 |
my ( $self, $error ) = @_; |
my ( $self, $error ) = @_; |
303 |
|
|
325 |
; |
; |
326 |
} |
} |
327 |
|
|
328 |
|
sub add_status { |
329 |
|
my ( $self, $data ) = @_; |
330 |
|
push @status, $data; |
331 |
|
} |
332 |
|
|
333 |
|
sub clean_status { |
334 |
|
@status = (); |
335 |
|
$icon_html = ''; |
336 |
|
} |
337 |
|
|
338 |
|
sub status_parts { |
339 |
|
warn "## status parts ", dump( map { keys %$_ } @status ); |
340 |
|
} |
341 |
|
|
342 |
|
sub DEMOLISH { |
343 |
|
my ( $self ) = @_; |
344 |
|
warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status; |
345 |
|
} |
346 |
|
|
347 |
|
=head2 add_icon |
348 |
|
|
349 |
|
Frey::Foo->add_icon; # /static/icons/Frey/Foo.png |
350 |
|
Frey::Foo->add_icon('warning'); # /static/icons/Frey/Foo/warning.png |
351 |
|
|
352 |
|
=cut |
353 |
|
|
354 |
|
sub icon_path { |
355 |
|
my ($self,$class,$variant) = @_; |
356 |
|
my $icon = $class; |
357 |
|
$icon =~ s{::}{/}g; |
358 |
|
$icon .= "/$variant" if $variant; |
359 |
|
my $path = 'static/icons/' . $icon . '.png'; |
360 |
|
if ( -e $path ) { |
361 |
|
warn "# $class from $self icon_path $path"; |
362 |
|
return $path; |
363 |
|
} else { |
364 |
|
warn "TODO: add $path icon for $class"; |
365 |
|
return undef; |
366 |
|
} |
367 |
|
} |
368 |
|
|
369 |
|
sub add_icon { |
370 |
|
my ($self,$variant) = @_; |
371 |
|
|
372 |
|
my $class = ref($self); |
373 |
|
$class = $self->class if $self->can('class'); |
374 |
|
my $icon_path = $self->icon_path( $class, $variant ) || return; |
375 |
|
|
376 |
|
$icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|; |
377 |
|
warn "# using icon $icon_path"; |
378 |
|
|
379 |
|
=for later |
380 |
|
|
381 |
|
# FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work! |
382 |
|
my $ico_path = $icon_path; |
383 |
|
$ico_path =~ s{png$}{ico}; |
384 |
|
if ( ! -e $ico_path ) { |
385 |
|
system "convert $icon_path $ico_path"; |
386 |
|
warn "# convert $icon_path $ico_path : $@"; |
387 |
|
} |
388 |
|
$icon_html .= qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path">| if -e $ico_path; |
389 |
|
|
390 |
|
=cut |
391 |
|
|
392 |
|
} |
393 |
|
|
394 |
|
my $warn_colors = { |
395 |
|
'#' => '#444', |
396 |
|
'##' => '#888', |
397 |
|
}; |
398 |
|
|
399 |
|
my $multiline_markers = { |
400 |
|
'(' => ')', |
401 |
|
'{' => '}', |
402 |
|
'[' => ']', |
403 |
|
'"' => '"', |
404 |
|
}; |
405 |
|
|
406 |
|
my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']'; |
407 |
|
warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re"; |
408 |
|
|
409 |
|
sub log_path { |
410 |
|
$Frey::Bootstrap::log_path || warn "no log_path?"; |
411 |
|
} |
412 |
|
|
413 |
|
sub warnings_html { |
414 |
|
my ($self,$level) = shift; |
415 |
|
$level ||= $self->debug, |
416 |
|
my $path = $self->log_path; |
417 |
|
|
418 |
|
my $warnings; |
419 |
|
my $line = 0; |
420 |
|
my $multiline_end; |
421 |
|
|
422 |
|
open(my $log, '<', $path) || die "can't open $path: $!"; |
423 |
|
while(<$log>) { |
424 |
|
chomp; |
425 |
|
$line++; |
426 |
|
|
427 |
|
my $style = ''; |
428 |
|
|
429 |
|
if ( $multiline_end ) { |
430 |
|
if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) { |
431 |
|
# warn "## $line end of $multiline_end in '$_'\n"; |
432 |
|
undef $multiline_end; |
433 |
|
} else { |
434 |
|
# warn "## $line skipped\n"; |
435 |
|
} |
436 |
|
} elsif ( m{^(#*)\s+} ) { |
437 |
|
my $l = $1 ? length($1) : 0; |
438 |
|
if ( $l > $level ) { |
439 |
|
undef $multiline_end; |
440 |
|
$multiline_end = $multiline_markers->{$1} if m{($multiline_re)$}; |
441 |
|
# warn "## $line start $1 .. $multiline_end level $l > $level for '$_'\n" if $multiline_end; |
442 |
|
next; |
443 |
|
} |
444 |
|
|
445 |
|
$style = $warn_colors->{$1} |
446 |
|
? ' style="color:' . $warn_colors->{$1} . '"' |
447 |
|
: ''; |
448 |
|
|
449 |
|
my $msg = $self->html_escape( $_ ); |
450 |
|
my $spacer = ' '; |
451 |
|
if ( length($msg) > $self->chop_warning ) { |
452 |
|
$msg = substr( $msg, 0, $self->chop_warning ); |
453 |
|
$spacer = '…' |
454 |
|
} |
455 |
|
$msg =~ s{^\s}{ }g; |
456 |
|
$warnings .= qq|<tt$style>$msg</tt>$spacer<small><a target="editor" href="/editor+$path+$line">+$line</a></small><br/>|; |
457 |
|
# FIXME <tt> should be <code> but CSS hates me |
458 |
|
} |
459 |
|
} |
460 |
|
close($log) || die "can't close $path: $!"; |
461 |
|
|
462 |
|
return |
463 |
|
qq|<span class="frey-popup"><a target="editor" href="/editor+$path+$line" title="open $path level $level">warn</a><span>| |
464 |
|
. $self->editor_links( $warnings ) |
465 |
|
. qq|</span></span>| |
466 |
|
; |
467 |
|
} |
468 |
|
|
469 |
1; |
1; |