3 |
|
|
4 |
with 'Frey::Session'; |
with 'Frey::Session'; |
5 |
|
|
|
#use Continuity::Widget::DomNode; |
|
6 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
7 |
use Carp qw/confess cluck carp/; |
use Carp qw/confess cluck carp/; |
8 |
use File::Slurp; |
use File::Slurp; |
9 |
use Text::Tabs; # expand, unexpand |
use Text::Tabs; # expand, unexpand |
10 |
|
use Digest::MD5 qw/md5/; |
11 |
|
|
12 |
use lib 'lib'; |
use lib 'lib'; |
13 |
|
|
71 |
default => 250, |
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 = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
82 |
my $escape_re = join '|' => keys %escape; |
my $escape_re = join '|' => keys %escape; |
83 |
|
|
84 |
sub html_escape { |
sub html_escape { |
85 |
my ( $self, $html ) = @_; |
my ( $self, $html ) = @_; |
86 |
|
return '' unless defined $html; |
87 |
$html =~ s/($escape_re)/$escape{$1}/g; |
$html =~ s/($escape_re)/$escape{$1}/g; |
88 |
return $html; |
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 html_dump { |
sub html_dump { |
100 |
my ( $self, $dump ) = @_; |
my ( $self, $dump ) = @_; |
101 |
$dump = dump( $dump ) if ref($dump); |
$dump = dump( $dump ) if ref($dump); |
110 |
sub popup { my $self = shift; $self->popup_dropdown('popup', @_); } |
sub popup { my $self = shift; $self->popup_dropdown('popup', @_); } |
111 |
sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); } |
sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); } |
112 |
|
|
113 |
our $re_html = qr{<(?:!--.+?--|(\w+).+?/\1|[^>]+/?)>}s; # relaxed html check for one semi-valid tag |
our $re_html = qr{<(?:!--|(\w+)|[^>]+)/?>}s; # relaxed html check for one semi-valid tag |
114 |
|
|
115 |
sub popup_dropdown { |
sub popup_dropdown { |
116 |
my ( $self, $type, $name, $content, $full ) = @_; |
my ( $self, $type, $name, $content, $full ) = @_; |
132 |
} |
} |
133 |
} |
} |
134 |
|
|
135 |
sub _inline_path { |
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 { |
145 |
foreach my $path ( @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|<script type="text/javascript">\n/* inline $path */\n\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|<style type="text/css">\n/* inline $path */\n\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 ) { |
} elsif ( $path =~ m{<.+>}s ) { |
158 |
$out .= $path; |
$out .= $path; |
194 |
|
|
195 |
} |
} |
196 |
|
|
197 |
|
sub _add_css_js { |
198 |
|
my ( $self, $what, $content ) = @_; |
199 |
|
|
200 |
|
my $md5 = md5( $content ); |
201 |
|
return if $self->{_add_css_js_seen}->{$what}->{$md5}++; |
202 |
|
|
203 |
|
my $tag = $what eq 'css' ? 'style' : 'script'; |
204 |
|
my $type = $what eq 'css' ? 'text/css' : 'text/javascript'; |
205 |
|
my $head; |
206 |
|
|
207 |
|
my ( $package, $path, $line ) = caller(1); |
208 |
|
|
209 |
|
$content = "/$content" if $content !~ m{[\n\r]} && -e $content; |
210 |
|
if ( $content =~ $re_html && $what ne 'js' ) { |
211 |
|
$head = qq| |
212 |
|
$content |
213 |
|
<!-- $type via $package at $path line $line --> |
214 |
|
|; |
215 |
|
} elsif ( $content =~ m{^(/\w+|https?://)} && $content !~ m{[\n\r]} ) { |
216 |
|
if ( $what eq 'js' ) { |
217 |
|
$head = qq| |
218 |
|
<$tag type="$type" src="$content"> |
219 |
|
/* $what via $package at $path line $line */ |
220 |
|
</$tag> |
221 |
|
|; |
222 |
|
} else { |
223 |
|
$head = qq| |
224 |
|
<link rel="stylesheet" type="$type" href="$content"> |
225 |
|
<!-- $what via $package at $path line $line --> |
226 |
|
|; |
227 |
|
} |
228 |
|
} else { |
229 |
|
$head = qq| |
230 |
|
<$tag type="$type"> |
231 |
|
/* via $package at $path line $line */ |
232 |
|
$content |
233 |
|
</$tag> |
234 |
|
|; |
235 |
|
}; |
236 |
|
$self->add_head( $head ); |
237 |
|
} |
238 |
|
|
239 |
sub add_css { |
sub add_css { |
240 |
my ($self,$css) = @_; |
my ($self,$css) = @_; |
241 |
my ( $package, $path, $line ) = caller; |
$self->_add_css_js( 'css', $css ); |
|
$self->add_head( qq| |
|
|
<style type="text/css"> |
|
|
/* via $package at $path line $line */ |
|
|
$css |
|
|
</style> |
|
|
| ); |
|
242 |
} |
} |
243 |
|
|
244 |
sub add_js { |
sub add_js { |
245 |
my ($self,$js) = @_; |
my ($self,$js) = @_; |
246 |
my ( $package, $path, $line ) = caller; |
$self->_add_css_js( 'js', $js ); |
|
|
|
|
if ( $js =~ m{http.*\.js} ) { |
|
|
$self->add_head( qq| |
|
|
<script type="text/javascript" src="$js"> |
|
|
/* via $package at $path line $line */ |
|
|
</script> |
|
|
|); |
|
|
} else { |
|
|
$self->add_head( qq| |
|
|
<script type="text/javascript"> |
|
|
/* via $package at $path line $line */ |
|
|
$js |
|
|
</script> |
|
|
| ); |
|
|
}; |
|
247 |
} |
} |
248 |
|
|
249 |
our $reload_counter = 0; |
our $reload_counter = 0; |
250 |
|
|
251 |
|
|
252 |
=head2 page |
=head2 html_page |
253 |
|
|
254 |
$self->page( |
$self->html_page( |
255 |
title => 'page title', |
title => 'page title', |
256 |
head => '<!-- optional head markup -->', |
head => '<!-- optional head markup -->', |
257 |
body => '<b>Page Body</b>', |
body => '<b>Page Body</b>', |
264 |
|
|
265 |
our $icon_html; |
our $icon_html; |
266 |
|
|
267 |
sub page { |
sub html_page { |
268 |
my $self = shift; |
my $self = shift; |
269 |
my $a = {@_}; |
my $a = {@_}; |
270 |
|
|
|
warn "## page ",dump($a); |
|
|
|
|
271 |
$reload_counter++; |
$reload_counter++; |
272 |
|
|
273 |
my $status_line = ''; |
my $status_line = ''; |
287 |
warn "# no body, invoke $self->$run on ", ref($self); |
warn "# no body, invoke $self->$run on ", ref($self); |
288 |
$body = $self->$run; |
$body = $self->$run; |
289 |
} |
} |
290 |
if ( $self->content_type !~ m{html} ) { |
if ( $self->content_type !~ m{html} || ! $self->wrap_in_page ) { |
291 |
warn "# return only $self body ", $self->content_type; |
warn "# return only $self body ", $self->content_type; |
292 |
return $body |
return $body |
293 |
} elsif ( ! defined $body ) { |
} elsif ( ! defined $body ) { |
303 |
|
|
304 |
my $right = |
my $right = |
305 |
qq| |
qq| |
306 |
<span class="right"> |
<span class="frey-status-right"> |
307 |
<a title="reload $url" href="/reload$url">reload</a> |
<a title="reload $url" href="/reload$url">reload</a> |
308 |
<a title="$description" href="/exit$url" target="exit">$exit</a> |
<a title="$description" href="/exit$url" target="exit">$exit</a> |
309 |
</span> |
</span> |
316 |
|
|
317 |
$self->add_icon unless $icon_html; |
$self->add_icon unless $icon_html; |
318 |
|
|
319 |
|
my $title = undef |
320 |
|
|| $a->{title} |
321 |
|
|| $self->title |
322 |
|
|| ref($self) |
323 |
|
; |
324 |
|
|
325 |
|
# $title =~ s{(\w)\w+::}{$1:}g; # XXX compress names of classes |
326 |
|
|
327 |
|
$self->add_css(qq| |
328 |
|
body { |
329 |
|
padding-bottom: 3em; /* don't overlap status line */ |
330 |
|
} |
331 |
|
|); |
332 |
|
|
333 |
my $html = join("\n", |
my $html = join("\n", |
334 |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
335 |
$self->_head_html, |
$self->_head_html, |
336 |
'<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>', |
qq|<title>$title</title>|, |
337 |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
338 |
( $icon_html || '<!-- no icon -->' ), |
( $icon_html || '<!-- no icon -->' ), |
339 |
( $a->{head} || '' ), |
( $a->{head} || '' ), |
415 |
return $error; |
return $error; |
416 |
} |
} |
417 |
|
|
418 |
|
sub html_self { |
419 |
|
my $self = shift; |
420 |
|
my $html = $self; |
421 |
|
$html =~ s{([\w:]+)=}{<a target="$1" href="/$1" title="introspect $1">$1</a>=}gsm; |
422 |
|
return $html; |
423 |
|
} |
424 |
|
|
425 |
=head2 error |
=head2 error |
426 |
|
|
427 |
This method will return error to browser and backtrace unless |
This method will return error to browser and backtrace unless |
434 |
my $error = join(" ", @_); |
my $error = join(" ", @_); |
435 |
|
|
436 |
my $fatal = ''; |
my $fatal = ''; |
437 |
|
my $backtrace = ''; |
438 |
|
|
439 |
if ( $error !~ m{\n$} ) { |
if ( $error !~ m{\n$} ) { |
440 |
if ( my @backtrace = $self->backtrace ) { |
if ( my @backtrace = $self->backtrace ) { |
441 |
$error .= "\n\t" . join( "\n\t", @backtrace ); |
$backtrace = |
442 |
|
"\n" . $self->html_self . "->error backtrace\n\t" |
443 |
|
. $self->html_links( join( "\n\t", @backtrace ) ) |
444 |
|
; |
445 |
$fatal = qq| frey-fatal|; |
$fatal = qq| frey-fatal|; |
446 |
} |
} |
447 |
} |
} |
448 |
|
|
449 |
warn "ERROR: $error\n"; |
warn "ERROR: $error\n"; |
450 |
$self->add_icon('error'); |
$self->add_icon('error'); |
451 |
return |
$error = $self->html_links( $error ); |
452 |
qq|<pre class="frey-error$fatal">| |
return qq|<pre class="frey-error$fatal">$error $backtrace</pre>| ; |
|
. $self->html_links( $error ) . |
|
|
qq|</pre>| |
|
|
; |
|
453 |
} |
} |
454 |
|
|
455 |
=head1 Status line |
=head1 Status line |
464 |
|
|
465 |
sub add_status { |
sub add_status { |
466 |
my ( $self, $data ) = @_; |
my ( $self, $data ) = @_; |
467 |
push @status, { 'X' => [ $self->backtrace ] }; |
die "no data" unless $data; |
468 |
if ( ref($data) ) { |
if ( ref $data ) { |
469 |
push @status, $data; |
push @status, $data; |
470 |
} else { |
} else { |
471 |
if ( defined $status[ $#status ] ) { |
if ( defined $status[ $#status ] ) { |
728 |
my ($self) = @_; |
my ($self) = @_; |
729 |
|
|
730 |
my @backtrace; |
my @backtrace; |
731 |
foreach ( 0 .. 5 ) { |
foreach ( 1 .. 5 ) { # 0 = backtrace |
732 |
my ( |
my ( |
733 |
$package,$path,$line |
$package,$path,$line |
734 |
# subroutine hasargs |
# subroutine hasargs |