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; |
70 |
default => 250, |
default => 250, |
71 |
); |
); |
72 |
|
|
73 |
|
has 'wrap_in_page' => ( |
74 |
|
documentation => 'wrap full html page with status bar around content', |
75 |
|
is => 'rw', |
76 |
|
isa => 'Bool', |
77 |
|
default => 1, |
78 |
|
); |
79 |
|
|
80 |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
81 |
my $escape_re = join '|' => keys %escape; |
my $escape_re = join '|' => keys %escape; |
82 |
|
|
83 |
sub html_escape { |
sub html_escape { |
84 |
my ( $self, $html ) = @_; |
my ( $self, $html ) = @_; |
85 |
|
return '' unless defined $html; |
86 |
$html =~ s/($escape_re)/$escape{$1}/g; |
$html =~ s/($escape_re)/$escape{$1}/g; |
87 |
return $html; |
return $html; |
88 |
} |
} |
89 |
|
|
90 |
|
# from Mojo::ByteStream |
91 |
|
sub url_escape { |
92 |
|
my ( $self, $url, $pattern ) = @_; |
93 |
|
$pattern ||= 'A-Za-z0-9\-\.\_\~'; |
94 |
|
$url =~ s/([^$pattern])/sprintf('%%%02X',ord($1))/ge; |
95 |
|
return $url; |
96 |
|
} |
97 |
|
|
98 |
sub html_dump { |
sub html_dump { |
99 |
my ( $self, $dump ) = @_; |
my ( $self, $dump ) = @_; |
100 |
$dump = dump( $dump ) if ref($dump); |
$dump = dump( $dump ) if ref($dump); |
109 |
sub popup { my $self = shift; $self->popup_dropdown('popup', @_); } |
sub popup { my $self = shift; $self->popup_dropdown('popup', @_); } |
110 |
sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); } |
sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); } |
111 |
|
|
112 |
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 |
113 |
|
|
114 |
sub popup_dropdown { |
sub popup_dropdown { |
115 |
my ( $self, $type, $name, $content, $full ) = @_; |
my ( $self, $type, $name, $content, $full ) = @_; |
131 |
} |
} |
132 |
} |
} |
133 |
|
|
134 |
sub _inline_path_size { |
sub _inline { |
135 |
my ( $self, $path ) = @_; |
my ( $self, $path ) = @_; |
136 |
-s $path < $self->inline_smaller_than && -s $path; |
return unless defined $path; |
137 |
|
warn "# _inline $path"; |
138 |
|
-e $path && -s $path < $self->inline_smaller_than && -s $path; |
139 |
} |
} |
140 |
|
|
141 |
sub _head_html { |
sub _head_html { |
143 |
my $out = ''; |
my $out = ''; |
144 |
foreach my $path ( @head ) { |
foreach my $path ( @head ) { |
145 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
|
my $size = $self->_inline_path_size( $path ); |
|
146 |
if ( $path =~ m/\.js$/ ) { |
if ( $path =~ m/\.js$/ ) { |
147 |
$out .= $size ? |
my $size; |
148 |
|
$out .= $size = _inline( $path ) ? |
149 |
qq|<script type="text/javascript">\n/* inline $path $size bytes */\n\n| . read_file($path) . qq|\n</script>| : |
qq|<script type="text/javascript">\n/* inline $path $size bytes */\n\n| . read_file($path) . qq|\n</script>| : |
150 |
qq|<script type="text/javascript" src="/$path"></script>|; |
qq|<script type="text/javascript" src="/$path"></script>|; |
151 |
} elsif ( $path =~ m/\.css$/ ) { |
} elsif ( $path =~ m/\.css$/ ) { |
152 |
$out .= $size ? |
my $size; |
153 |
|
$out .= $size = _inline( $path ) ? |
154 |
qq|<style type="text/css">\n/* inline $path $size bytes */\n\n| . read_file( $path ) . qq|\n</style>| : |
qq|<style type="text/css">\n/* inline $path $size bytes */\n\n| . read_file( $path ) . qq|\n</style>| : |
155 |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
156 |
} elsif ( $path =~ m{<.+>}s ) { |
} elsif ( $path =~ m{<.+>}s ) { |
202 |
|
|
203 |
my ( $package, $path, $line ) = caller(1); |
my ( $package, $path, $line ) = caller(1); |
204 |
|
|
205 |
if ( $content =~ m{\.(js|css)} ) { |
$content = "/$content" if $content !~ m{[\n\r]} && -e $content; |
206 |
$content = "/$content" if -e $content; |
if ( $content =~ $re_html && $what ne 'js' ) { |
207 |
|
$head = qq| |
208 |
|
$content |
209 |
|
<!-- $type via $package at $path line $line --> |
210 |
|
|; |
211 |
|
} elsif ( $content =~ m{^(/\w+|https?://)} && $content !~ m{[\n\r]} ) { |
212 |
if ( $what eq 'js' ) { |
if ( $what eq 'js' ) { |
213 |
$head = qq| |
$head = qq| |
214 |
<$tag type="$type" src="$content"> |
<$tag type="$type" src="$content"> |
215 |
/* via $package at $path line $line */ |
/* $what via $package at $path line $line */ |
216 |
</$tag> |
</$tag> |
217 |
|; |
|; |
218 |
} else { |
} else { |
219 |
$head = qq| |
$head = qq| |
220 |
<link rel="stylesheet" type="$type" href="$content"> |
<link rel="stylesheet" type="$type" href="$content"> |
221 |
<!-- via $package at $path line $line --> |
<!-- $what via $package at $path line $line --> |
222 |
|; |
|; |
223 |
} |
} |
224 |
} else { |
} else { |
245 |
our $reload_counter = 0; |
our $reload_counter = 0; |
246 |
|
|
247 |
|
|
248 |
=head2 page |
=head2 html_page |
249 |
|
|
250 |
$self->page( |
$self->html_page( |
251 |
title => 'page title', |
title => 'page title', |
252 |
head => '<!-- optional head markup -->', |
head => '<!-- optional head markup -->', |
253 |
body => '<b>Page Body</b>', |
body => '<b>Page Body</b>', |
260 |
|
|
261 |
our $icon_html; |
our $icon_html; |
262 |
|
|
263 |
sub page { |
sub html_page { |
264 |
my $self = shift; |
my $self = shift; |
265 |
my $a = {@_}; |
my $a = {@_}; |
266 |
|
|
|
warn "## page ",dump($a); |
|
|
|
|
267 |
$reload_counter++; |
$reload_counter++; |
268 |
|
|
269 |
my $status_line = ''; |
my $status_line = ''; |
283 |
warn "# no body, invoke $self->$run on ", ref($self); |
warn "# no body, invoke $self->$run on ", ref($self); |
284 |
$body = $self->$run; |
$body = $self->$run; |
285 |
} |
} |
286 |
if ( $self->content_type !~ m{html} ) { |
if ( $self->content_type !~ m{html} || ! $self->wrap_in_page ) { |
287 |
warn "# return only $self body ", $self->content_type; |
warn "# return only $self body ", $self->content_type; |
288 |
return $body |
return $body |
289 |
} elsif ( ! defined $body ) { |
} elsif ( ! defined $body ) { |
299 |
|
|
300 |
my $right = |
my $right = |
301 |
qq| |
qq| |
302 |
<span class="right"> |
<span class="frey-status-right"> |
303 |
<a title="reload $url" href="/reload$url">reload</a> |
<a title="reload $url" href="/reload$url">reload</a> |
304 |
<a title="$description" href="/exit$url" target="exit">$exit</a> |
<a title="$description" href="/exit$url" target="exit">$exit</a> |
305 |
</span> |
</span> |
312 |
|
|
313 |
$self->add_icon unless $icon_html; |
$self->add_icon unless $icon_html; |
314 |
|
|
315 |
|
my $title = undef |
316 |
|
|| $a->{title} |
317 |
|
|| $self->title |
318 |
|
|| ref($self) |
319 |
|
; |
320 |
|
|
321 |
|
# $title =~ s{(\w)\w+::}{$1:}g; # XXX compress names of classes |
322 |
|
|
323 |
|
$self->add_css(qq| |
324 |
|
body { |
325 |
|
padding-bottom: 3em; /* don't overlap status line */ |
326 |
|
} |
327 |
|
|); |
328 |
|
|
329 |
my $html = join("\n", |
my $html = join("\n", |
330 |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
331 |
$self->_head_html, |
$self->_head_html, |
332 |
'<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>', |
qq|<title>$title</title>|, |
333 |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
334 |
( $icon_html || '<!-- no icon -->' ), |
( $icon_html || '<!-- no icon -->' ), |
335 |
( $a->{head} || '' ), |
( $a->{head} || '' ), |
460 |
|
|
461 |
sub add_status { |
sub add_status { |
462 |
my ( $self, $data ) = @_; |
my ( $self, $data ) = @_; |
463 |
push @status, { 'X' => [ $self->backtrace ] }; |
die "no data" unless $data; |
464 |
if ( ref($data) ) { |
if ( ref $data ) { |
465 |
push @status, $data; |
push @status, $data; |
466 |
} else { |
} else { |
467 |
if ( defined $status[ $#status ] ) { |
if ( defined $status[ $#status ] ) { |