1 |
package Frey::Web; |
package Frey::Web; |
2 |
use Moose::Role; |
use Moose::Role; |
3 |
|
|
4 |
use Continuity::Widget::DomNode; |
with 'Frey::Session'; |
5 |
|
|
6 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
7 |
use Carp qw/confess/; |
use Carp qw/confess cluck carp/; |
8 |
use File::Slurp; |
use File::Slurp; |
9 |
|
use Text::Tabs; # expand, unexpand |
10 |
|
use Digest::MD5 qw/md5/; |
11 |
|
|
12 |
|
use lib 'lib'; |
13 |
|
|
14 |
|
use Frey::Types; |
15 |
|
|
16 |
|
use Frey::Bookmarklet; |
17 |
|
use Frey::Class::Browser; |
18 |
|
use Frey::INC; |
19 |
|
|
20 |
|
use Frey::SVK; |
21 |
|
|
22 |
|
our @head; |
23 |
|
sub head { @head } |
24 |
|
|
25 |
has 'head' => ( |
has 'request_url' => ( |
26 |
is => 'rw', |
is => 'rw', |
27 |
isa => 'ArrayRef[Str]', |
isa => 'Uri', coerce => 1, |
28 |
default => sub { [ 'static/frey.css' ] }, |
required => 1, |
29 |
|
default => sub { |
30 |
|
carp "undefined request_url"; |
31 |
|
'/'; |
32 |
|
}, |
33 |
); |
); |
34 |
|
|
35 |
=head2 inline_smaller_than |
has 'title' => ( |
36 |
|
is => 'rw', |
37 |
|
isa => 'Str', |
38 |
|
lazy => 1, |
39 |
|
default => sub { |
40 |
|
my ($self) = @_; |
41 |
|
ref($self); |
42 |
|
}, |
43 |
|
); |
44 |
|
|
45 |
Inline JavaScript and CSS smaller than this size into page reducing |
has 'content_type' => ( |
46 |
round-trips to server. |
is => 'rw', |
47 |
|
isa => 'Str', |
48 |
|
default => 'text/html', |
49 |
|
documentation => 'Content-type header', |
50 |
|
); |
51 |
|
|
52 |
=cut |
has 'dump_max_bytes' => ( |
53 |
|
is => 'rw', |
54 |
|
isa => 'Int', |
55 |
|
default => 4096, |
56 |
|
documentation => 'maximum dump size sent to browser before truncation', |
57 |
|
); |
58 |
|
|
59 |
has 'inline_smaller_than' => ( |
has 'inline_smaller_than' => ( |
60 |
is => 'rw', |
is => 'rw', |
61 |
isa => 'Int', |
isa => 'Int', |
62 |
default => 10240, |
default => 10240, |
63 |
|
documentation => 'inline JavaScript and CSS to reduce round-trips', |
64 |
); |
); |
65 |
|
|
66 |
sub dom2html { |
has 'html_dump_width' => ( |
67 |
# warn "## dom2html ",dump( @_ ); |
documentation => 'crop longer lines in dumps', |
68 |
return Continuity::Widget::DomNode->create( @_ )->to_string; |
is => 'rw', |
69 |
|
isa => 'Int', |
70 |
|
# required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them |
71 |
|
default => 250, |
72 |
|
); |
73 |
|
|
74 |
|
has 'wrap_in_page' => ( |
75 |
|
documentation => 'wrap full html page with status bar around content', |
76 |
|
is => 'rw', |
77 |
|
isa => 'Bool', |
78 |
|
default => 1, |
79 |
|
); |
80 |
|
|
81 |
|
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
82 |
|
my $escape_re = join '|' => keys %escape; |
83 |
|
|
84 |
|
sub html_escape { |
85 |
|
my ( $self, $html ) = @_; |
86 |
|
return '' unless defined $html; |
87 |
|
$html =~ s/($escape_re)/$escape{$1}/g; |
88 |
|
return $html; |
89 |
|
} |
90 |
|
|
91 |
|
# from Mojo::ByteStream |
92 |
|
sub url_escape { |
93 |
|
my ( $self, $url, $pattern ) = @_; |
94 |
|
$pattern ||= 'A-Za-z0-9\-\.\_\~'; |
95 |
|
$url =~ s/([^$pattern])/sprintf('%%%02X',ord($1))/ge; |
96 |
|
return $url; |
97 |
} |
} |
98 |
|
|
99 |
sub _inline_path { |
sub html_dump { |
100 |
|
my ( $self, $dump ) = @_; |
101 |
|
$dump = dump( $dump ) if ref($dump); |
102 |
|
my $width = $self->html_dump_width; |
103 |
|
$dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs; |
104 |
|
$dump = $self->html_escape( $dump ); |
105 |
|
$dump =~ s{\Q...\E}{…}gs; |
106 |
|
# $dump =~ $self->html_links( $dump ); # FIXME include this |
107 |
|
return "<code>$dump</code>"; |
108 |
|
} |
109 |
|
|
110 |
|
sub popup { my $self = shift; $self->popup_dropdown('popup', @_); } |
111 |
|
sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); } |
112 |
|
|
113 |
|
our $re_html = qr{<(?:!--|(\w+)|[^>]+)/?>}s; # relaxed html check for one semi-valid tag |
114 |
|
|
115 |
|
sub popup_dropdown { |
116 |
|
my ( $self, $type, $name, $content, $full ) = @_; |
117 |
|
|
118 |
|
$content = $self->html_dump($content) if ref $content; |
119 |
|
|
120 |
|
$content = qq|<span>$content</span>| unless $content =~ m{^\s*<(span|a|code).+?/\1>\s*}; |
121 |
|
|
122 |
|
$content =~ s{<span>(<code>[^<]+</code>)</span>}{$1} && $self->TODO("code wrapped in span"); |
123 |
|
|
124 |
|
warn "## $type [$name] = ", length( $content ), " bytes"; # if $self->debug; # FIXME |
125 |
|
|
126 |
|
if ( $name =~ m{::} && $name !~ $re_html ) { |
127 |
|
return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|; |
128 |
|
} elsif ( $name =~ s{^\s*(<a)\s+}{$1 class="frey-$type"} ) { |
129 |
|
return qq|$name $content\n|; |
130 |
|
} else { |
131 |
|
return qq|<span class="frey-$type">$name $content</span>\n|; |
132 |
|
} |
133 |
|
} |
134 |
|
|
135 |
|
sub _inline { |
136 |
my ( $self, $path ) = @_; |
my ( $self, $path ) = @_; |
137 |
-s $path < $self->inline_smaller_than; |
return unless defined $path; |
138 |
|
warn "# _inline $path"; |
139 |
|
-e $path && -s $path < $self->inline_smaller_than && -s $path; |
140 |
} |
} |
141 |
|
|
142 |
sub _head_html { |
sub _head_html { |
143 |
my $self = shift; |
my $self = shift; |
144 |
my $out = ''; |
my $out = ''; |
145 |
foreach my $path ( @{ $self->head } ) { |
foreach my $path ( @head ) { |
146 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
147 |
if ( $path =~ m/\.js$/ ) { |
if ( $path =~ m/\.js$/ ) { |
148 |
$out .= $self->_inline_path( $path ) ? |
my $size; |
149 |
qq|<!-- $path --><script type="text/javascript">\n| . read_file($path) . qq|\n</script>| : |
$out .= $size = _inline( $path ) ? |
150 |
|
qq|<script type="text/javascript">\n/* inline $path $size bytes */\n\n| . read_file($path) . qq|\n</script>| : |
151 |
qq|<script type="text/javascript" src="/$path"></script>|; |
qq|<script type="text/javascript" src="/$path"></script>|; |
152 |
} elsif ( $path =~ m/\.css$/ ) { |
} elsif ( $path =~ m/\.css$/ ) { |
153 |
$out .= $self->_inline_path( $path ) ? |
my $size; |
154 |
qq|<!-- $path --><style type="text/css">\n| . read_file( $path ) . qq|\n</style>| : |
$out .= $size = _inline( $path ) ? |
155 |
|
qq|<style type="text/css">\n/* inline $path $size bytes */\n\n| . read_file( $path ) . qq|\n</style>| : |
156 |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
157 |
|
} elsif ( $path =~ m{<.+>}s ) { |
158 |
|
$out .= $path; |
159 |
} else { |
} else { |
160 |
confess "don't know how to render $path"; |
confess "don't know how to render $path"; |
161 |
} |
} |
170 |
|
|
171 |
my $size = $o->add_head( 'path/to/external.css' ); |
my $size = $o->add_head( 'path/to/external.css' ); |
172 |
|
|
173 |
|
$o->add_head( '<!-- html content -->' ); |
174 |
|
|
175 |
=cut |
=cut |
176 |
|
|
177 |
sub add_head { |
sub add_head { |
179 |
return if ! defined $path || $path eq ''; |
return if ! defined $path || $path eq ''; |
180 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
181 |
|
|
182 |
if ( -e $path ) { |
if ( $path =~ $re_html ) { |
183 |
|
push @head, $path; |
184 |
|
} elsif ( -e $path ) { |
185 |
if ( $path =~ m/\.(?:js|css)$/ ) { |
if ( $path =~ m/\.(?:js|css)$/ ) { |
186 |
push @{ $self->head }, $path; |
push @head, $path; |
187 |
} else { |
} else { |
188 |
confess "can't add_head( $path ) it's not js or css"; |
confess "can't add_head( $path ) it's not js or css"; |
189 |
} |
} |
190 |
|
return -s $path; |
191 |
} else { |
} else { |
192 |
confess "can't find $path: $!"; |
confess "can't find $path: $!"; |
193 |
} |
} |
194 |
|
|
195 |
return -s $path; |
} |
196 |
|
|
197 |
|
my $seen; |
198 |
|
|
199 |
|
sub _add_css_js { |
200 |
|
my ( $self, $what, $content ) = @_; |
201 |
|
|
202 |
|
my $md5 = md5( $content ); |
203 |
|
return if $seen->{$what}->{$md5}++; |
204 |
|
|
205 |
|
my $tag = $what eq 'css' ? 'style' : 'script'; |
206 |
|
my $type = $what eq 'css' ? 'text/css' : 'text/javascript'; |
207 |
|
my $head; |
208 |
|
|
209 |
|
my ( $package, $path, $line ) = caller(1); |
210 |
|
|
211 |
|
$content = "/$content" if $content !~ m{[\n\r]} && -e $content; |
212 |
|
if ( $content =~ $re_html && $what ne 'js' ) { |
213 |
|
$head = qq| |
214 |
|
$content |
215 |
|
<!-- $type via $package at $path line $line --> |
216 |
|
|; |
217 |
|
} elsif ( $content =~ m{^(/\w+|https?://)} && $content !~ m{[\n\r]} ) { |
218 |
|
if ( $what eq 'js' ) { |
219 |
|
$head = qq| |
220 |
|
<$tag type="$type" src="$content"> |
221 |
|
/* $what via $package at $path line $line */ |
222 |
|
</$tag> |
223 |
|
|; |
224 |
|
} else { |
225 |
|
$head = qq| |
226 |
|
<link rel="stylesheet" type="$type" href="$content"> |
227 |
|
<!-- $what via $package at $path line $line --> |
228 |
|
|; |
229 |
|
} |
230 |
|
} else { |
231 |
|
$head = qq| |
232 |
|
<$tag type="$type"> |
233 |
|
/* via $package at $path line $line */ |
234 |
|
$content |
235 |
|
</$tag> |
236 |
|
|; |
237 |
|
}; |
238 |
|
$self->add_head( $head ); |
239 |
|
} |
240 |
|
|
241 |
|
sub add_css { |
242 |
|
my ($self,$css) = @_; |
243 |
|
$self->_add_css_js( 'css', $css ); |
244 |
|
} |
245 |
|
|
246 |
|
sub add_js { |
247 |
|
my ($self,$js) = @_; |
248 |
|
$self->_add_css_js( 'js', $js ); |
249 |
} |
} |
250 |
|
|
251 |
our $reload_counter = 0; |
our $reload_counter = 0; |
252 |
|
|
253 |
|
|
254 |
=head2 page |
=head2 html_page |
255 |
|
|
256 |
$self->page( |
$self->html_page( |
257 |
title => 'page title', |
title => 'page title', |
258 |
head => '<!-- optional head markup -->', |
head => '<!-- optional head markup -->', |
259 |
body => '<b>Page Body</b>', |
body => '<b>Page Body</b>', |
261 |
|
|
262 |
=cut |
=cut |
263 |
|
|
264 |
use Frey::Bookmarklet; |
our @status; |
265 |
use Frey::ClassBrowser; |
sub status { @status }; |
266 |
|
|
267 |
sub page { |
our $icon_html; |
268 |
|
|
269 |
|
sub html_page { |
270 |
my $self = shift; |
my $self = shift; |
271 |
my $a = {@_}; |
my $a = {@_}; |
272 |
|
|
273 |
$reload_counter++; |
$reload_counter++; |
274 |
|
|
275 |
my $html = qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>| |
my $status_line = ''; |
276 |
. $self->_head_html |
|
277 |
. '<title>' . ( $a->{title} || ref($self) ) . '</title>' |
foreach my $part ( @status ) { |
278 |
. ( $a->{head} || '' ) |
foreach my $name ( keys %$part ) { |
279 |
. '</head><body>' |
$status_line .= $self->popup( $name, $part->{$name} ); |
280 |
. ( $a->{body} || '<!-- no body -->' ) |
} |
281 |
. qq|<div class="frey-status-line"> |
} |
282 |
<a href="/">Frey</a> $Frey::VERSION |
|
283 |
<a href="?reload=$reload_counter"><code>| . dump( $ENV{'REQUEST_URI'} ) . qq|</code></a> |
my $url = $self->request_url; |
284 |
<span class="frey-popup">Bookmarklets<span>| . Frey::Bookmarklet->markup . qq|</span></span> |
$url =~ s{\?reload=\d+}{}; |
285 |
<span class="frey-popup">ClassBrowser<span>| . Frey::ClassBrowser->markup . qq|</span></span> |
|
286 |
<span class="frey-popup">ENV<span><code>| . dump( %ENV ) . qq|</code></span></span> |
my $body = $a->{body}; |
287 |
|
if ( ! $body ) { |
288 |
|
my $run = $a->{run} || 'as_markup'; |
289 |
|
warn "# no body, invoke $self->$run on ", ref($self); |
290 |
|
$body = $self->$run; |
291 |
|
} |
292 |
|
if ( $self->content_type !~ m{html} || ! $self->wrap_in_page ) { |
293 |
|
warn "# return only $self body ", $self->content_type; |
294 |
|
return $body |
295 |
|
} elsif ( ! defined $body ) { |
296 |
|
warn "# no body"; |
297 |
|
$body = '<!-- no body -->'; |
298 |
|
} |
299 |
|
|
300 |
|
$status_line .= $self->warnings_html; |
301 |
|
|
302 |
|
my ($exit,$description) = ('exit','stop server'); |
303 |
|
($exit,$description) = ('restart','restart server') |
304 |
|
if $ENV{FREY_RESTART}; # tune labels on exit link |
305 |
|
|
306 |
|
my $right = |
307 |
|
qq| |
308 |
|
<span class="frey-status-right"> |
309 |
|
<a title="reload $url" href="/reload$url">reload</a> |
310 |
|
<a title="$description" href="/exit$url" target="exit">$exit</a> |
311 |
|
</span> |
312 |
|
|; |
313 |
|
|
314 |
|
my $svk = Frey::SVK->new; |
315 |
|
my $info = $svk->info; |
316 |
|
my $revision = $svk->info->{Revision} || ''; |
317 |
|
$revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)}; |
318 |
|
|
319 |
|
$self->add_icon unless $icon_html; |
320 |
|
|
321 |
|
my $title = undef |
322 |
|
|| $a->{title} |
323 |
|
|| $self->title |
324 |
|
|| ref($self) |
325 |
|
; |
326 |
|
|
327 |
|
# $title =~ s{(\w)\w+::}{$1:}g; # XXX compress names of classes |
328 |
|
|
329 |
|
$self->add_css(qq| |
330 |
|
body { |
331 |
|
padding-bottom: 3em; /* don't overlap status line */ |
332 |
|
} |
333 |
|
|); |
334 |
|
|
335 |
|
my $html = join("\n", |
336 |
|
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
337 |
|
$self->_head_html, |
338 |
|
qq|<title>$title</title>|, |
339 |
|
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
340 |
|
( $icon_html || '<!-- no icon -->' ), |
341 |
|
( $a->{head} || '' ), |
342 |
|
qq| |
343 |
|
</head><body> |
344 |
|
$body |
345 |
|
<div class="frey-status-line"> |
346 |
|
<a href="/">Frey</a> $Frey::VERSION $revision |
347 |
|
$status_line |
348 |
|
$right |
349 |
</div> |
</div> |
350 |
</body></html> |
</body></html> |
351 |
|; |
|, |
352 |
|
); |
353 |
|
|
354 |
warn "## >>> page ",length($html), " bytes\n" if $self->debug; |
warn "## >>> page ",length($html), " bytes\n" if $self->debug; |
355 |
|
|
356 |
return $html; |
return $html; |
357 |
} |
} |
358 |
|
|
359 |
sub error { |
=head2 editor |
360 |
|
|
361 |
|
Create HTML editor link with optional line and title |
362 |
|
|
363 |
|
my $html = $self->editor( $class, $line, $title ); |
364 |
|
|
365 |
|
=cut |
366 |
|
|
367 |
|
sub editor { |
368 |
|
my ( $self, $class, $line, $title ) = @_; |
369 |
|
confess "need class" unless $class; |
370 |
|
if ( ! defined $title ) { |
371 |
|
$title = "edit $class"; |
372 |
|
$title .= " line $line" if $line; |
373 |
|
} |
374 |
|
$line ||= 1; |
375 |
|
qq|<a target="editor" href="/editor+$class+$line"| . |
376 |
|
( $title ? qq| title="$title"| : '' ) . |
377 |
|
qq|>$class</a>|; |
378 |
|
} |
379 |
|
|
380 |
|
=head2 html_links |
381 |
|
|
382 |
|
Create HTML links to editor for perl error message |
383 |
|
|
384 |
|
my $html = $self->html_links( $error ) |
385 |
|
|
386 |
|
=cut |
387 |
|
|
388 |
|
sub html_links { |
389 |
my ( $self, $error ) = @_; |
my ( $self, $error ) = @_; |
390 |
warn $error; |
|
391 |
$error =~ s{\s+(\S+)\s+line\s+(\d+)}{ <a href="/editor$1+$2" target="editor">$1</a> line $2}gsm; |
$error = $self->strip_full_path( $error ); |
392 |
return qq|<pre class="frey-error">$error</pre>|; |
|
393 |
|
# $error =~ s[(bless\({\s+.+?\s+},\s+)("[^"]+")(\) at)][<span class="frey-dropdown">$1<code>$2</code>$3</span>]gs; # FIXME insert bless hiding back |
394 |
|
|
395 |
|
# perl's backtrace |
396 |
|
$error =~ s{at\s+(\S+)\s+line\s+(\d+)} |
397 |
|
{at <a target="editor" href="/editor+$1+$2" title="vi $1 +$2">$1</a> line $2}gsm; |
398 |
|
|
399 |
|
$error =~ s{(via (?:package)\s+"?)([\w:]+)("?)} |
400 |
|
{$1<a target="$2" href="/$2" title="introspect $2">$2</a>$3}gsm |
401 |
|
|| # or anything that looks like "Class::Name" |
402 |
|
$error =~ s{"(\w+(?:::\w+)+)"} |
403 |
|
{"<a target="$1" href="/$1" title="introspect $1">$1</a>"}gsm; |
404 |
|
|
405 |
|
# method error messages |
406 |
|
# FIXME replace with link to Frey::Introspect data |
407 |
|
$error =~ s{(method ")(\w+)(" via)} |
408 |
|
{$1<a target="$2" href="/Frey::Shell::Grep/as_markup?pattern=$2" title="grep $2">$2</a>$3}gsm; |
409 |
|
|
410 |
|
# link paths to editor |
411 |
|
$error =~ s{((?:lib|t)/[\S]+)\s+(\d+\s+bytes)} |
412 |
|
{<a target="editor" href="/editor+$1+1" title="vi $1 [$2]">$1</a>}gsm; |
413 |
|
|
414 |
|
$error =~ s{(class ")([\w:]+)(")} |
415 |
|
{$1<a target="$2" href="/$2" title="introspect $2">$2</a>$3}gsm; |
416 |
|
|
417 |
|
return $error; |
418 |
|
} |
419 |
|
|
420 |
|
sub html_self { |
421 |
|
my $self = shift; |
422 |
|
my $html = $self; |
423 |
|
$html =~ s{([\w:]+)=}{<a target="$1" href="/$1" title="introspect $1">$1</a>=}gsm; |
424 |
|
return $html; |
425 |
|
} |
426 |
|
|
427 |
|
=head2 error |
428 |
|
|
429 |
|
This method will return error to browser and backtrace unless |
430 |
|
error message ends with LF C<\n> just like L<warn> |
431 |
|
|
432 |
|
=cut |
433 |
|
|
434 |
|
sub error { |
435 |
|
my $self = shift; |
436 |
|
my $error = join(" ", @_); |
437 |
|
|
438 |
|
my $fatal = ''; |
439 |
|
my $backtrace = ''; |
440 |
|
|
441 |
|
if ( $error !~ m{\n$} ) { |
442 |
|
if ( my @backtrace = $self->backtrace ) { |
443 |
|
$backtrace = |
444 |
|
"\n" . $self->html_self . "->error backtrace\n\t" |
445 |
|
. $self->html_links( join( "\n\t", @backtrace ) ) |
446 |
|
; |
447 |
|
$fatal = qq| frey-fatal|; |
448 |
|
} |
449 |
|
} |
450 |
|
|
451 |
|
warn "ERROR: $error\n"; |
452 |
|
$self->add_icon('error'); |
453 |
|
$error = $self->html_links( $error ); |
454 |
|
return qq|<pre class="frey-error$fatal">$error $backtrace</pre>| ; |
455 |
|
} |
456 |
|
|
457 |
|
=head1 Status line |
458 |
|
|
459 |
|
=head2 add_status |
460 |
|
|
461 |
|
$self->add_status( { name => { some => 'data' } } ); |
462 |
|
|
463 |
|
$self->add_status( "append to last status popup" ); |
464 |
|
|
465 |
|
=cut |
466 |
|
|
467 |
|
sub add_status { |
468 |
|
my ( $self, $data ) = @_; |
469 |
|
die "no data" unless $data; |
470 |
|
if ( ref $data ) { |
471 |
|
push @status, $data; |
472 |
|
} else { |
473 |
|
if ( defined $status[ $#status ] ) { |
474 |
|
$status[ $#status ]->{ '+' } = $data; |
475 |
|
} else { |
476 |
|
push @status, { '+' => $data }; |
477 |
|
} |
478 |
|
} |
479 |
|
} |
480 |
|
|
481 |
|
=head2 clean_status |
482 |
|
|
483 |
|
Called at beginning of each request |
484 |
|
|
485 |
|
$self->clean_status; |
486 |
|
|
487 |
|
=cut |
488 |
|
|
489 |
|
sub clean_status { |
490 |
|
my ($self) = shift; |
491 |
|
@head = ( 'static/frey.css' ); |
492 |
|
@status = ( |
493 |
|
{ 'ClassBrowser' => Frey::Class::Browser->new( usage_sort => 1, usage_on_top => 0 )->as_markup }, |
494 |
|
{ 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }, |
495 |
|
{ 'INC' => Frey::INC->new->as_markup }, |
496 |
|
); |
497 |
|
$icon_html = ''; |
498 |
|
} |
499 |
|
|
500 |
|
=head2 status_parts |
501 |
|
|
502 |
|
Dump all status line parts |
503 |
|
|
504 |
|
$self->status_parts |
505 |
|
|
506 |
|
=cut |
507 |
|
|
508 |
|
sub status_parts { |
509 |
|
warn "## status parts ", dump( map { keys %$_ } @status ); |
510 |
|
} |
511 |
|
|
512 |
|
=for debug |
513 |
|
|
514 |
|
sub DEMOLISH { |
515 |
|
my ( $self ) = @_; |
516 |
|
warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status; |
517 |
|
} |
518 |
|
|
519 |
|
=cut |
520 |
|
|
521 |
|
=head2 add_icon |
522 |
|
|
523 |
|
Frey::Foo->add_icon; # /static/icons/Frey/Foo.png |
524 |
|
Frey::Foo->add_icon('warning'); # /static/icons/Frey/Foo/warning.png |
525 |
|
|
526 |
|
=cut |
527 |
|
|
528 |
|
sub icon_path { |
529 |
|
my ($self,$class,$variant) = @_; |
530 |
|
|
531 |
|
sub icon_exists { |
532 |
|
my $class = shift; |
533 |
|
$class =~ s{::}{/}g; |
534 |
|
$class .= "/$variant" if $variant; |
535 |
|
my $icon_path = 'static/icons/' . $class . '.png'; |
536 |
|
return $icon_path if -e $icon_path; |
537 |
|
return; |
538 |
|
} |
539 |
|
|
540 |
|
my $path = icon_exists( $class ); |
541 |
|
if ( ! $path ) { |
542 |
|
my $super_class = $class; |
543 |
|
while ( $super_class =~ s{::[^:]+$}{} && ! $path ) { |
544 |
|
$path = icon_exists( $super_class ) unless $super_class eq 'Frey'; # don't default on Frey icon |
545 |
|
} |
546 |
|
} |
547 |
|
|
548 |
|
if ( ! $path ) { |
549 |
|
$self->TODO( "add icon for $class" . ( $variant ? " variant $variant" : '' ) ); |
550 |
|
return undef; |
551 |
|
} |
552 |
|
|
553 |
|
warn "# $class from $self icon_path $path" if $self->debug; |
554 |
|
return $path; |
555 |
|
} |
556 |
|
|
557 |
|
sub add_icon { |
558 |
|
my ($self,$variant) = @_; |
559 |
|
|
560 |
|
my $class = $self->class if $self->can('class'); |
561 |
|
#$class ||= $self->title; |
562 |
|
$class ||= ref($self); |
563 |
|
my $icon_path = $self->icon_path( $class, $variant ) || return; |
564 |
|
|
565 |
|
$icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|; |
566 |
|
warn "# using icon $icon_path"; |
567 |
|
|
568 |
|
=for later |
569 |
|
|
570 |
|
# FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work! |
571 |
|
my $ico_path = $icon_path; |
572 |
|
$ico_path =~ s{png$}{ico}; |
573 |
|
if ( ! -e $ico_path ) { |
574 |
|
system "convert $icon_path $ico_path"; |
575 |
|
warn "# convert $icon_path $ico_path : $@"; |
576 |
|
} |
577 |
|
$icon_html .= qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path">| if -e $ico_path; |
578 |
|
|
579 |
|
=cut |
580 |
|
|
581 |
|
} |
582 |
|
|
583 |
|
my $warn_colors = { |
584 |
|
'#' => '#444', |
585 |
|
'##' => '#888', |
586 |
|
}; |
587 |
|
|
588 |
|
my $multiline_markers = { |
589 |
|
'(' => ')', |
590 |
|
'{' => '}', |
591 |
|
'[' => ']', |
592 |
|
'"' => '"', |
593 |
|
}; |
594 |
|
|
595 |
|
=for later |
596 |
|
|
597 |
|
my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']'; |
598 |
|
warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re"; |
599 |
|
|
600 |
|
=cut |
601 |
|
|
602 |
|
sub log_path { |
603 |
|
$Frey::Bootstrap::log_path || die "no log_path?"; |
604 |
|
} |
605 |
|
|
606 |
|
our $pwd = `pwd`; |
607 |
|
chomp $pwd; |
608 |
|
|
609 |
|
sub strip_full_path { |
610 |
|
my ($self, $msg) = @_; |
611 |
|
# Mojo seems to expand warn messages to full path which is annoying |
612 |
|
$msg =~ s{/[^/]+/\.\./}{/}gs; |
613 |
|
$msg =~ s{$pwd/*}{}gs; |
614 |
|
return $msg; |
615 |
|
} |
616 |
|
|
617 |
|
our $last_log_pos = 0; |
618 |
|
our $last_log_line = 0; |
619 |
|
|
620 |
|
sub warnings_html { |
621 |
|
my ($self,$level) = shift; |
622 |
|
$level ||= $self->debug, |
623 |
|
my $path = $self->log_path; |
624 |
|
|
625 |
|
my $max = 30; |
626 |
|
my $pos = 0; |
627 |
|
my @warnings = ( '' x $max ); # XXX circualar buffer for 50 lines |
628 |
|
my $line = $last_log_line; |
629 |
|
my $multiline_end; |
630 |
|
|
631 |
|
# XXX do we really want to do this every time? |
632 |
|
my $css = qq|/* short css classes for levels */\n|; |
633 |
|
my $level_to_class; |
634 |
|
foreach ( keys %$warn_colors ) { |
635 |
|
my $l = length($_); |
636 |
|
my $class = 'l' . $l; |
637 |
|
$css .= qq|.$class { color: $warn_colors->{$_} }\n|; |
638 |
|
$level_to_class->{ $_ } = $class; |
639 |
|
} |
640 |
|
$self->add_css( $css ); |
641 |
|
|
642 |
|
open(my $log, '<', $path) || die "can't open $path: $!"; |
643 |
|
seek($log, $last_log_pos, 0) || warn "can't seek: $!"; |
644 |
|
while(<$log>) { |
645 |
|
chomp; |
646 |
|
$line++; |
647 |
|
|
648 |
|
next if m{^\s+(Mojo|Class::MOP|Moose)::}; |
649 |
|
|
650 |
|
my $style = ''; |
651 |
|
|
652 |
|
=for filter |
653 |
|
|
654 |
|
if ( $multiline_end ) { |
655 |
|
if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) { |
656 |
|
# warn "## $line end of $multiline_end in '$_'\n"; |
657 |
|
undef $multiline_end; |
658 |
|
} else { |
659 |
|
# warn "## $line skipped\n"; |
660 |
|
} |
661 |
|
} elsif ( m{^(#*)\s+} ) { |
662 |
|
my $l = $1 ? length($1) : 0; |
663 |
|
if ( $l > $level ) { |
664 |
|
undef $multiline_end; |
665 |
|
$multiline_end = $multiline_markers->{$1} if m{($multiline_re)$}; |
666 |
|
# warn "## $line start $1 .. $multiline_end level $l > $level for '$_'\n" if $multiline_end; |
667 |
|
next; |
668 |
|
} |
669 |
|
|
670 |
|
=cut |
671 |
|
if ( m{^(#*)} ) { |
672 |
|
|
673 |
|
my $level = $1; |
674 |
|
my $msg = $self->strip_full_path( $_ ); |
675 |
|
|
676 |
|
my $spacer = ' '; |
677 |
|
my $real_msg = expand( $msg ); |
678 |
|
if ( length($real_msg) > $self->html_dump_width ) { |
679 |
|
|
680 |
|
$real_msg = substr( $msg, 0, $self->html_dump_width ); |
681 |
|
$msg = unexpand( $real_msg ); |
682 |
|
$spacer = '…' |
683 |
|
} |
684 |
|
|
685 |
|
$msg = $self->html_escape( $msg ); |
686 |
|
|
687 |
|
if ( my $class = $level_to_class->{ $level } ) { |
688 |
|
$msg = qq|<span class="$class">$msg</span>|; |
689 |
|
} |
690 |
|
|
691 |
|
#$msg .= $spacer . qq|<a target="editor" href="/editor+$path+$line" style="float: right;">$line</a>\n|; |
692 |
|
$msg = qq|<a target="editor" href="/editor+$path+$line" style="float: right;">$line</a>$msg| |
693 |
|
. ( $spacer ? $spacer : '' ) |
694 |
|
. "\n"; # XXX <pre> needs this |
695 |
|
|
696 |
|
$warnings[ $pos++ % $max ] = $msg; |
697 |
|
} |
698 |
|
} |
699 |
|
$last_log_pos = tell($log); |
700 |
|
$last_log_line = $line; |
701 |
|
warn "log has $line lines tell position $last_log_pos"; |
702 |
|
close($log) || die "can't close $path: $!"; |
703 |
|
|
704 |
|
my $size = -s $path; |
705 |
|
|
706 |
|
my $warnings = join("", |
707 |
|
map { $warnings[ ( $pos + $_ ) % $max ] || '' } 0 .. ( $max - 1 ) |
708 |
|
); |
709 |
|
|
710 |
|
my $s = length($warnings); |
711 |
|
|
712 |
|
return |
713 |
|
# need to wrap editor link into span so we can have links in warnings |
714 |
|
qq|<span class="frey-popup"><a target="editor" href="/editor+$path+$line" title="$path \| $size -> $s bytes \| $line -> $pos lines \| level $level">warn</a><code>| |
715 |
|
. $self->html_links( $warnings ) |
716 |
|
. qq|</code></span>| |
717 |
|
; |
718 |
|
} |
719 |
|
|
720 |
|
|
721 |
|
=head2 backtrace |
722 |
|
|
723 |
|
Show backtrace with links to editor |
724 |
|
|
725 |
|
my @backtrace = $self->backtrace; |
726 |
|
|
727 |
|
=cut |
728 |
|
|
729 |
|
sub backtrace { |
730 |
|
my ($self) = @_; |
731 |
|
|
732 |
|
my @backtrace; |
733 |
|
foreach ( 1 .. 5 ) { # 0 = backtrace |
734 |
|
my ( |
735 |
|
$package,$path,$line |
736 |
|
# subroutine hasargs |
737 |
|
# wantarray evaltext is_require |
738 |
|
# hints bitmask hinthash |
739 |
|
) = caller($_) or last; |
740 |
|
|
741 |
|
push @backtrace, |
742 |
|
qq|via "$package" at $path line $line|; |
743 |
|
} |
744 |
|
#warn "# backtrace: ", dump( @backtrace ) if @backtrace; |
745 |
|
return @backtrace; |
746 |
|
} |
747 |
|
|
748 |
|
=head2 checkbox |
749 |
|
|
750 |
|
Generate checkbox html markup from some attribute |
751 |
|
|
752 |
|
my $html = $self->checkbox('attribute_name', $value); |
753 |
|
|
754 |
|
=cut |
755 |
|
|
756 |
|
sub checkbox { |
757 |
|
my ($self,$name,$value) = @_; |
758 |
|
my $checked = ''; |
759 |
|
my $all_checkboxes = eval { $self->$name }; |
760 |
|
warn "ERROR tried to get checkbox value for '$name' which is unknown: $@" if $@; |
761 |
|
$all_checkboxes = [ $all_checkboxes ] unless ref($all_checkboxes) eq 'ARRAY'; # sigh, too chatty |
762 |
|
$checked = ' checked' if grep { defined $_ && $_ eq $value } @$all_checkboxes; |
763 |
|
warn "# checkbox $name $value $checked\t", $self->dump( $self->$name ); |
764 |
|
qq|<input name="$name" value="$value" type="checkbox"$checked>|; |
765 |
|
} |
766 |
|
|
767 |
|
=head2 strip |
768 |
|
|
769 |
|
Strip whitespace around content |
770 |
|
|
771 |
|
my $stripped = strip(' no more whitespace around this '); |
772 |
|
|
773 |
|
=cut |
774 |
|
|
775 |
|
sub strip { |
776 |
|
my $t = shift; |
777 |
|
$t =~ s{^\s+}{}gs; |
778 |
|
$t =~ s{>\s+<}{><}gs; |
779 |
|
$t =~ s{\s+$}{}gs; |
780 |
|
return $t; |
781 |
} |
} |
782 |
|
|
783 |
1; |
1; |