1 |
package Frey::Web; |
package Frey::Web; |
2 |
use Moose::Role; |
use Moose::Role; |
3 |
|
|
4 |
with 'Frey::Session'; |
with 'Frey::Session', 'Frey::Class::Icon'; |
5 |
|
|
|
use Frey::Types; |
|
|
|
|
|
#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 |
10 |
|
use Digest::MD5 qw/md5/; |
11 |
|
|
12 |
|
use lib 'lib'; |
13 |
|
|
14 |
|
use Frey::Types; |
15 |
|
|
16 |
use Frey::Bookmarklet; |
use Frey::Bookmarklet; |
17 |
use Frey::ClassBrowser; |
use Frey::Class::Browser; |
18 |
use Frey::INC; |
use Frey::INC; |
19 |
|
|
20 |
use Frey::SVK; |
use Frey::SVK; |
21 |
|
|
|
use Text::Tabs; # expand, unexpand |
|
|
|
|
22 |
our @head; |
our @head; |
23 |
sub head { @head } |
sub head { @head } |
24 |
|
|
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 ); |
242 |
$self->add_head( qq| |
} |
243 |
<style type="text/css"> |
|
244 |
/* via $package at $path line $line */ |
sub add_js { |
245 |
$css |
my ($self,$js) = @_; |
246 |
</style> |
$self->_add_css_js( 'js', $js ); |
|
| ); |
|
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>', |
262 |
our @status; |
our @status; |
263 |
sub status { @status }; |
sub status { @status }; |
264 |
|
|
265 |
our $icon_html; |
sub html_page { |
|
|
|
|
sub page { |
|
266 |
my $self = shift; |
my $self = shift; |
267 |
my $a = {@_}; |
my $a = {@_}; |
268 |
|
|
|
warn "## page ",dump($a); |
|
|
|
|
269 |
$reload_counter++; |
$reload_counter++; |
270 |
|
|
271 |
my $status_line = ''; |
my $status_line = ''; |
283 |
if ( ! $body ) { |
if ( ! $body ) { |
284 |
my $run = $a->{run} || 'as_markup'; |
my $run = $a->{run} || 'as_markup'; |
285 |
warn "# no body, invoke $self->$run on ", ref($self); |
warn "# no body, invoke $self->$run on ", ref($self); |
286 |
eval { |
$body = $self->$run; |
|
$body = $self->$run; |
|
|
}; |
|
|
$body = $self->error( $@, '' ) if $@; |
|
287 |
} |
} |
288 |
if ( $self->content_type !~ m{html} ) { |
if ( $self->content_type !~ m{html} || ! $self->wrap_in_page ) { |
289 |
warn "# return only $self body ", $self->content_type; |
warn "# return only $self body ", $self->content_type; |
290 |
return $body |
return $body |
291 |
} elsif ( ! defined $body ) { |
} elsif ( ! defined $body ) { |
301 |
|
|
302 |
my $right = |
my $right = |
303 |
qq| |
qq| |
304 |
<span class="right"> |
<span class="frey-status-right"> |
305 |
<a title="reload $url" href="/reload$url">reload</a> |
<a title="reload $url" href="/reload$url">reload</a> |
306 |
<a title="$description" href="/exit$url" target="exit">$exit</a> |
<a title="$description" href="/exit$url" target="exit">$exit</a> |
307 |
</span> |
</span> |
312 |
my $revision = $svk->info->{Revision} || ''; |
my $revision = $svk->info->{Revision} || ''; |
313 |
$revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)}; |
$revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)}; |
314 |
|
|
315 |
$self->add_icon unless $icon_html; |
$self->add_icon; |
316 |
|
|
317 |
|
my $title = undef |
318 |
|
|| $a->{title} |
319 |
|
|| $self->title |
320 |
|
|| ref($self) |
321 |
|
; |
322 |
|
|
323 |
|
# $title =~ s{(\w)\w+::}{$1:}g; # XXX compress names of classes |
324 |
|
|
325 |
|
$self->add_css(qq| |
326 |
|
body { |
327 |
|
padding-bottom: 3em; /* don't overlap status line */ |
328 |
|
} |
329 |
|
|); |
330 |
|
|
331 |
my $html = join("\n", |
my $html = join("\n", |
332 |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
333 |
$self->_head_html, |
$self->_head_html, |
334 |
'<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>', |
qq|<title>$title</title>|, |
335 |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
336 |
( $icon_html || '<!-- no icon -->' ), |
( $self->icon_html ), |
337 |
( $a->{head} || '' ), |
( $a->{head} || '' ), |
338 |
qq| |
qq| |
339 |
</head><body> |
</head><body> |
413 |
return $error; |
return $error; |
414 |
} |
} |
415 |
|
|
416 |
|
sub html_self { |
417 |
|
my $self = shift; |
418 |
|
my $html = $self; |
419 |
|
$html =~ s{([\w:]+)=}{<a target="$1" href="/$1" title="introspect $1">$1</a>=}gsm; |
420 |
|
return $html; |
421 |
|
} |
422 |
|
|
423 |
=head2 error |
=head2 error |
424 |
|
|
425 |
This method will return error to browser and backtrace unless |
This method will return error to browser and backtrace unless |
432 |
my $error = join(" ", @_); |
my $error = join(" ", @_); |
433 |
|
|
434 |
my $fatal = ''; |
my $fatal = ''; |
435 |
|
my $backtrace = ''; |
436 |
|
|
437 |
if ( $error !~ m{\n$} ) { |
if ( $error !~ m{\n$} ) { |
438 |
if ( my @backtrace = $self->backtrace ) { |
if ( my @backtrace = $self->backtrace ) { |
439 |
$error .= "\n\t" . join( "\n\t", @backtrace ); |
$backtrace = |
440 |
|
"\n" . $self->html_self . "->error backtrace\n\t" |
441 |
|
. $self->html_links( join( "\n\t", @backtrace ) ) |
442 |
|
; |
443 |
$fatal = qq| frey-fatal|; |
$fatal = qq| frey-fatal|; |
444 |
} |
} |
445 |
} |
} |
446 |
|
|
447 |
warn "ERROR: $error\n"; |
warn "ERROR: $error\n"; |
448 |
return |
$self->add_icon('error'); |
449 |
qq|<pre class="frey-error$fatal">| |
$error = $self->html_links( $error ); |
450 |
. $self->html_links( $error ) . |
return qq|<pre class="frey-error$fatal">$error $backtrace</pre>| ; |
|
qq|</pre>| |
|
|
; |
|
451 |
} |
} |
452 |
|
|
453 |
=head1 Status line |
=head1 Status line |
462 |
|
|
463 |
sub add_status { |
sub add_status { |
464 |
my ( $self, $data ) = @_; |
my ( $self, $data ) = @_; |
465 |
push @status, { 'X' => [ $self->backtrace ] }; |
die "no data" unless $data; |
466 |
if ( ref($data) ) { |
if ( ref $data ) { |
467 |
push @status, $data; |
push @status, $data; |
468 |
} else { |
} else { |
469 |
if ( defined $status[ $#status ] ) { |
if ( defined $status[ $#status ] ) { |
484 |
|
|
485 |
sub clean_status { |
sub clean_status { |
486 |
my ($self) = shift; |
my ($self) = shift; |
487 |
|
warn "## clean_status"; |
488 |
@head = ( 'static/frey.css' ); |
@head = ( 'static/frey.css' ); |
489 |
@status = ( |
@status = ( |
490 |
{ 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup }, |
{ 'ClassBrowser' => Frey::Class::Browser->new( usage_sort => 1, usage_on_top => 0 )->as_markup }, |
491 |
{ 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }, |
{ 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }, |
492 |
{ 'INC' => Frey::INC->new->as_markup }, |
{ 'INC' => Frey::INC->new->as_markup }, |
493 |
); |
); |
|
$icon_html = ''; |
|
494 |
} |
} |
495 |
|
|
496 |
=head2 status_parts |
=head2 status_parts |
514 |
|
|
515 |
=cut |
=cut |
516 |
|
|
|
=head2 add_icon |
|
|
|
|
|
Frey::Foo->add_icon; # /static/icons/Frey/Foo.png |
|
|
Frey::Foo->add_icon('warning'); # /static/icons/Frey/Foo/warning.png |
|
|
|
|
|
=cut |
|
|
|
|
|
sub icon_path { |
|
|
my ($self,$class,$variant) = @_; |
|
|
my $icon = $class; |
|
|
$icon =~ s{::}{/}g; |
|
|
$icon .= "/$variant" if $variant; |
|
|
my $path = 'static/icons/' . $icon . '.png'; |
|
|
if ( -e $path ) { |
|
|
warn "# $class from $self icon_path $path" if $self->debug; |
|
|
return $path; |
|
|
} else { |
|
|
$self->TODO( "add $path icon for $class" ); |
|
|
return undef; |
|
|
} |
|
|
} |
|
|
|
|
|
sub add_icon { |
|
|
my ($self,$variant) = @_; |
|
|
|
|
|
my $class = ref($self); |
|
|
$class = $self->class if $self->can('class'); |
|
|
my $icon_path = $self->icon_path( $class, $variant ) || return; |
|
|
|
|
|
$icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|; |
|
|
warn "# using icon $icon_path"; |
|
|
|
|
|
=for later |
|
|
|
|
|
# FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work! |
|
|
my $ico_path = $icon_path; |
|
|
$ico_path =~ s{png$}{ico}; |
|
|
if ( ! -e $ico_path ) { |
|
|
system "convert $icon_path $ico_path"; |
|
|
warn "# convert $icon_path $ico_path : $@"; |
|
|
} |
|
|
$icon_html .= qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path">| if -e $ico_path; |
|
|
|
|
|
=cut |
|
|
|
|
|
} |
|
|
|
|
517 |
my $warn_colors = { |
my $warn_colors = { |
518 |
'#' => '#444', |
'#' => '#444', |
519 |
'##' => '#888', |
'##' => '#888', |
664 |
my ($self) = @_; |
my ($self) = @_; |
665 |
|
|
666 |
my @backtrace; |
my @backtrace; |
667 |
foreach ( 0 .. 5 ) { |
foreach ( 1 .. 5 ) { # 0 = backtrace |
668 |
my ( |
my ( |
669 |
$package,$path,$line |
$package,$path,$line |
670 |
# subroutine hasargs |
# subroutine hasargs |
679 |
return @backtrace; |
return @backtrace; |
680 |
} |
} |
681 |
|
|
682 |
|
=head2 checkbox |
683 |
|
|
684 |
|
Generate checkbox html markup from some attribute |
685 |
|
|
686 |
|
my $html = $self->checkbox('attribute_name', $value); |
687 |
|
|
688 |
|
=cut |
689 |
|
|
690 |
|
sub checkbox { |
691 |
|
my ($self,$name,$value) = @_; |
692 |
|
my $checked = ''; |
693 |
|
my $all_checkboxes = eval { $self->$name }; |
694 |
|
warn "ERROR tried to get checkbox value for '$name' which is unknown: $@" if $@; |
695 |
|
$all_checkboxes = [ $all_checkboxes ] unless ref($all_checkboxes) eq 'ARRAY'; # sigh, too chatty |
696 |
|
$checked = ' checked' if grep { defined $_ && $_ eq $value } @$all_checkboxes; |
697 |
|
warn "# checkbox $name $value $checked\t", $self->dump( $self->$name ); |
698 |
|
qq|<input name="$name" value="$value" type="checkbox"$checked>|; |
699 |
|
} |
700 |
|
|
701 |
|
=head2 strip |
702 |
|
|
703 |
|
Strip whitespace around content |
704 |
|
|
705 |
|
my $stripped = strip(' no more whitespace around this '); |
706 |
|
|
707 |
|
=cut |
708 |
|
|
709 |
|
sub strip { |
710 |
|
my $t = shift; |
711 |
|
$t =~ s{^\s+}{}gs; |
712 |
|
$t =~ s{>\s+<}{><}gs; |
713 |
|
$t =~ s{\s+$}{}gs; |
714 |
|
return $t; |
715 |
|
} |
716 |
|
|
717 |
|
no Moose::Role; |
718 |
|
|
719 |
1; |
1; |