2 |
use Moose::Role; |
use Moose::Role; |
3 |
|
|
4 |
with 'Frey::Session'; |
with 'Frey::Session'; |
5 |
|
with 'Frey::Class::Icon'; |
6 |
|
|
|
use Frey::Types; |
|
|
|
|
|
#use Continuity::Widget::DomNode; |
|
7 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
8 |
use Carp qw/confess cluck carp/; |
use Carp qw/confess cluck carp/; |
9 |
use File::Slurp; |
use File::Slurp; |
10 |
|
use Text::Tabs; # expand, unexpand |
11 |
|
use Digest::MD5 qw/md5/; |
12 |
|
|
13 |
|
use lib 'lib'; |
14 |
|
|
15 |
|
use Frey::Types; |
16 |
|
|
17 |
use Frey::Bookmarklet; |
use Frey::Bookmarklet; |
18 |
use Frey::ClassBrowser; |
use Frey::Class::Browser; |
19 |
use Frey::INC; |
use Frey::INC; |
20 |
|
|
21 |
use Frey::SVK; |
use Frey::SVK; |
22 |
|
|
|
use Text::Tabs; # expand, unexpand |
|
|
|
|
23 |
our @head; |
our @head; |
24 |
sub head { @head } |
sub head { @head } |
25 |
|
|
72 |
default => 250, |
default => 250, |
73 |
); |
); |
74 |
|
|
75 |
|
has 'wrap_in_page' => ( |
76 |
|
documentation => 'wrap full html page with status bar around content', |
77 |
|
is => 'rw', |
78 |
|
isa => 'Bool', |
79 |
|
default => 1, |
80 |
|
); |
81 |
|
|
82 |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
83 |
my $escape_re = join '|' => keys %escape; |
my $escape_re = join '|' => keys %escape; |
84 |
|
|
85 |
sub html_escape { |
sub html_escape { |
86 |
my ( $self, $html ) = @_; |
my ( $self, $html ) = @_; |
87 |
|
return '' unless defined $html; |
88 |
$html =~ s/($escape_re)/$escape{$1}/g; |
$html =~ s/($escape_re)/$escape{$1}/g; |
89 |
return $html; |
return $html; |
90 |
} |
} |
91 |
|
|
92 |
|
# from Mojo::ByteStream |
93 |
|
sub url_escape { |
94 |
|
my ( $self, $url, $pattern ) = @_; |
95 |
|
$pattern ||= 'A-Za-z0-9\-\.\_\~'; |
96 |
|
$url =~ s/([^$pattern])/sprintf('%%%02X',ord($1))/ge; |
97 |
|
return $url; |
98 |
|
} |
99 |
|
|
100 |
sub html_dump { |
sub html_dump { |
101 |
my ( $self, $dump ) = @_; |
my ( $self, $dump ) = @_; |
102 |
$dump = dump( $dump ) if ref($dump); |
$dump = dump( $dump ) if ref($dump); |
111 |
sub popup { my $self = shift; $self->popup_dropdown('popup', @_); } |
sub popup { my $self = shift; $self->popup_dropdown('popup', @_); } |
112 |
sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); } |
sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); } |
113 |
|
|
114 |
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 |
115 |
|
|
116 |
sub popup_dropdown { |
sub popup_dropdown { |
117 |
my ( $self, $type, $name, $content, $full ) = @_; |
my ( $self, $type, $name, $content, $full ) = @_; |
133 |
} |
} |
134 |
} |
} |
135 |
|
|
136 |
sub _inline_path { |
sub _inline { |
137 |
my ( $self, $path ) = @_; |
my ( $self, $path ) = @_; |
138 |
-s $path < $self->inline_smaller_than; |
return unless defined $path; |
139 |
|
warn "# _inline $path"; |
140 |
|
-e $path && -s $path < $self->inline_smaller_than && -s $path; |
141 |
} |
} |
142 |
|
|
143 |
sub _head_html { |
sub _head_html { |
146 |
foreach my $path ( @head ) { |
foreach my $path ( @head ) { |
147 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
148 |
if ( $path =~ m/\.js$/ ) { |
if ( $path =~ m/\.js$/ ) { |
149 |
$out .= $self->_inline_path( $path ) ? |
my $size; |
150 |
qq|<script type="text/javascript">\n/* inline $path */\n\n| . read_file($path) . qq|\n</script>| : |
$out .= $size = _inline( $path ) ? |
151 |
|
qq|<script type="text/javascript">\n/* inline $path $size bytes */\n\n| . read_file($path) . qq|\n</script>| : |
152 |
qq|<script type="text/javascript" src="/$path"></script>|; |
qq|<script type="text/javascript" src="/$path"></script>|; |
153 |
} elsif ( $path =~ m/\.css$/ ) { |
} elsif ( $path =~ m/\.css$/ ) { |
154 |
$out .= $self->_inline_path( $path ) ? |
my $size; |
155 |
qq|<style type="text/css">\n/* inline $path */\n\n| . read_file( $path ) . qq|\n</style>| : |
$out .= $size = _inline( $path ) ? |
156 |
|
qq|<style type="text/css">\n/* inline $path $size bytes */\n\n| . read_file( $path ) . qq|\n</style>| : |
157 |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
158 |
} elsif ( $path =~ m{<.+>}s ) { |
} elsif ( $path =~ m{<.+>}s ) { |
159 |
$out .= $path; |
$out .= $path; |
195 |
|
|
196 |
} |
} |
197 |
|
|
198 |
|
sub _add_css_js { |
199 |
|
my ( $self, $what, $content ) = @_; |
200 |
|
|
201 |
|
my $md5 = md5( $content ); |
202 |
|
return if $self->{_add_css_js_seen}->{$what}->{$md5}++; |
203 |
|
|
204 |
|
my $tag = $what eq 'css' ? 'style' : 'script'; |
205 |
|
my $type = $what eq 'css' ? 'text/css' : 'text/javascript'; |
206 |
|
my $head; |
207 |
|
|
208 |
|
my ( $package, $path, $line ) = caller(1); |
209 |
|
|
210 |
|
$content = "/$content" if $content !~ m{[\n\r]} && -e $content; |
211 |
|
if ( $content =~ $re_html && $what ne 'js' ) { |
212 |
|
$head = qq| |
213 |
|
$content |
214 |
|
<!-- $type via $package at $path line $line --> |
215 |
|
|; |
216 |
|
} elsif ( $content =~ m{^(/\w+|https?://)} && $content !~ m{[\n\r]} ) { |
217 |
|
if ( $what eq 'js' ) { |
218 |
|
$head = qq| |
219 |
|
<$tag type="$type" src="$content"> |
220 |
|
/* $what via $package at $path line $line */ |
221 |
|
</$tag> |
222 |
|
|; |
223 |
|
} else { |
224 |
|
$head = qq| |
225 |
|
<link rel="stylesheet" type="$type" href="$content"> |
226 |
|
<!-- $what via $package at $path line $line --> |
227 |
|
|; |
228 |
|
} |
229 |
|
} else { |
230 |
|
$head = qq| |
231 |
|
<$tag type="$type"> |
232 |
|
/* via $package at $path line $line */ |
233 |
|
$content |
234 |
|
</$tag> |
235 |
|
|; |
236 |
|
}; |
237 |
|
$self->add_head( $head ); |
238 |
|
} |
239 |
|
|
240 |
sub add_css { |
sub add_css { |
241 |
my ($self,$css) = @_; |
my ($self,$css) = @_; |
242 |
my ( $package, $path, $line ) = caller; |
$self->_add_css_js( 'css', $css ); |
243 |
$self->add_head( qq| |
} |
244 |
<style type="text/css"> |
|
245 |
/* via $package at $path line $line */ |
sub add_js { |
246 |
$css |
my ($self,$js) = @_; |
247 |
</style> |
$self->_add_css_js( 'js', $js ); |
|
| ); |
|
248 |
} |
} |
249 |
|
|
250 |
our $reload_counter = 0; |
our $reload_counter = 0; |
251 |
|
|
252 |
|
|
253 |
=head2 page |
=head2 html_page |
254 |
|
|
255 |
$self->page( |
$self->html_page( |
256 |
title => 'page title', |
title => 'page title', |
257 |
head => '<!-- optional head markup -->', |
head => '<!-- optional head markup -->', |
258 |
body => '<b>Page Body</b>', |
body => '<b>Page Body</b>', |
263 |
our @status; |
our @status; |
264 |
sub status { @status }; |
sub status { @status }; |
265 |
|
|
266 |
our $icon_html; |
sub html_page { |
|
|
|
|
sub page { |
|
267 |
my $self = shift; |
my $self = shift; |
268 |
my $a = {@_}; |
my $a = {@_}; |
269 |
|
|
|
warn "## page ",dump($a); |
|
|
|
|
270 |
$reload_counter++; |
$reload_counter++; |
271 |
|
|
272 |
my $status_line = ''; |
my $status_line = ''; |
284 |
if ( ! $body ) { |
if ( ! $body ) { |
285 |
my $run = $a->{run} || 'as_markup'; |
my $run = $a->{run} || 'as_markup'; |
286 |
warn "# no body, invoke $self->$run on ", ref($self); |
warn "# no body, invoke $self->$run on ", ref($self); |
287 |
eval { |
$body = $self->$run; |
|
$body = $self->$run; |
|
|
}; |
|
|
$body = $self->error( $@, '' ) if $@; |
|
288 |
} |
} |
289 |
if ( $self->content_type !~ m{html} ) { |
if ( $self->content_type !~ m{html} || ! $self->wrap_in_page ) { |
290 |
warn "# return only $self body ", $self->content_type; |
warn "# return only $self body ", $self->content_type; |
291 |
return $body |
return $body |
292 |
} elsif ( ! defined $body ) { |
} elsif ( ! defined $body ) { |
302 |
|
|
303 |
my $right = |
my $right = |
304 |
qq| |
qq| |
305 |
<span class="right"> |
<span class="frey-status-right"> |
306 |
<a title="reload $url" href="/reload$url">reload</a> |
<a title="reload $url" href="/reload$url">reload</a> |
307 |
<a title="$description" href="/exit$url" target="exit">$exit</a> |
<a title="$description" href="/exit$url" target="exit">$exit</a> |
308 |
</span> |
</span> |
313 |
my $revision = $svk->info->{Revision} || ''; |
my $revision = $svk->info->{Revision} || ''; |
314 |
$revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)}; |
$revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)}; |
315 |
|
|
316 |
$self->add_icon unless $icon_html; |
$self->add_icon; |
317 |
|
|
318 |
|
my $title = undef |
319 |
|
|| $a->{title} |
320 |
|
|| $self->title |
321 |
|
|| ref($self) |
322 |
|
; |
323 |
|
|
324 |
|
# $title =~ s{(\w)\w+::}{$1:}g; # XXX compress names of classes |
325 |
|
|
326 |
|
$self->add_css(qq| |
327 |
|
body { |
328 |
|
padding-bottom: 3em; /* don't overlap status line */ |
329 |
|
} |
330 |
|
|); |
331 |
|
|
332 |
my $html = join("\n", |
my $html = join("\n", |
333 |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
334 |
$self->_head_html, |
$self->_head_html, |
335 |
'<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>', |
qq|<title>$title</title>|, |
336 |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
337 |
( $icon_html || '<!-- no icon -->' ), |
( $self->icon_html ), |
338 |
( $a->{head} || '' ), |
( $a->{head} || '' ), |
339 |
qq| |
qq| |
340 |
</head><body> |
</head><body> |
414 |
return $error; |
return $error; |
415 |
} |
} |
416 |
|
|
417 |
|
sub html_self { |
418 |
|
my $self = shift; |
419 |
|
my $html = $self; |
420 |
|
$html =~ s{([\w:]+)=}{<a target="$1" href="/$1" title="introspect $1">$1</a>=}gsm; |
421 |
|
return $html; |
422 |
|
} |
423 |
|
|
424 |
=head2 error |
=head2 error |
425 |
|
|
426 |
This method will return error to browser and backtrace unless |
This method will return error to browser and backtrace unless |
433 |
my $error = join(" ", @_); |
my $error = join(" ", @_); |
434 |
|
|
435 |
my $fatal = ''; |
my $fatal = ''; |
436 |
|
my $backtrace = ''; |
437 |
|
|
438 |
if ( $error !~ m{\n$} ) { |
if ( $error !~ m{\n$} ) { |
439 |
if ( my @backtrace = $self->backtrace ) { |
if ( my @backtrace = $self->backtrace ) { |
440 |
$error .= "\n\t" . join( "\n\t", @backtrace ); |
$backtrace = |
441 |
$fatal = qq| class="fatal"|; |
"\n" . $self->html_self . "->error backtrace\n\t" |
442 |
|
. $self->html_links( join( "\n\t", @backtrace ) ) |
443 |
|
; |
444 |
|
$fatal = qq| frey-fatal|; |
445 |
} |
} |
446 |
} |
} |
447 |
|
|
448 |
warn "ERROR: $error\n"; |
warn "ERROR: $error\n"; |
449 |
return |
$self->add_icon('error'); |
450 |
qq|<pre class="frey-error$fatal">| |
$error = $self->html_links( $error ); |
451 |
. $self->html_links( $error ) . |
return qq|<pre class="frey-error$fatal">$error $backtrace</pre>| ; |
|
qq|</pre>| |
|
|
; |
|
452 |
} |
} |
453 |
|
|
454 |
=head1 Status line |
=head1 Status line |
463 |
|
|
464 |
sub add_status { |
sub add_status { |
465 |
my ( $self, $data ) = @_; |
my ( $self, $data ) = @_; |
466 |
push @status, { 'X' => [ $self->backtrace ] }; |
die "no data" unless $data; |
467 |
if ( ref($data) ) { |
if ( ref $data ) { |
468 |
push @status, $data; |
push @status, $data; |
469 |
} else { |
} else { |
470 |
if ( defined $status[ $#status ] ) { |
if ( defined $status[ $#status ] ) { |
485 |
|
|
486 |
sub clean_status { |
sub clean_status { |
487 |
my ($self) = shift; |
my ($self) = shift; |
488 |
|
warn "## clean_status"; |
489 |
@head = ( 'static/frey.css' ); |
@head = ( 'static/frey.css' ); |
490 |
@status = ( |
@status = ( |
491 |
{ 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup }, |
{ 'ClassBrowser' => Frey::Class::Browser->new( usage_sort => 1, usage_on_top => 0 )->as_markup }, |
492 |
{ 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }, |
{ 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }, |
493 |
{ 'INC' => Frey::INC->new->as_markup }, |
{ 'INC' => Frey::INC->new->as_markup }, |
494 |
); |
); |
|
$icon_html = ''; |
|
495 |
} |
} |
496 |
|
|
497 |
=head2 status_parts |
=head2 status_parts |
515 |
|
|
516 |
=cut |
=cut |
517 |
|
|
|
=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 |
|
|
|
|
|
} |
|
|
|
|
518 |
my $warn_colors = { |
my $warn_colors = { |
519 |
'#' => '#444', |
'#' => '#444', |
520 |
'##' => '#888', |
'##' => '#888', |
665 |
my ($self) = @_; |
my ($self) = @_; |
666 |
|
|
667 |
my @backtrace; |
my @backtrace; |
668 |
foreach ( 0 .. 5 ) { |
foreach ( 1 .. 5 ) { # 0 = backtrace |
669 |
my ( |
my ( |
670 |
$package,$path,$line |
$package,$path,$line |
671 |
# subroutine hasargs |
# subroutine hasargs |
680 |
return @backtrace; |
return @backtrace; |
681 |
} |
} |
682 |
|
|
683 |
|
=head2 checkbox |
684 |
|
|
685 |
|
Generate checkbox html markup from some attribute |
686 |
|
|
687 |
|
my $html = $self->checkbox('attribute_name', $value); |
688 |
|
|
689 |
|
=cut |
690 |
|
|
691 |
|
sub checkbox { |
692 |
|
my ($self,$name,$value) = @_; |
693 |
|
my $checked = ''; |
694 |
|
my $all_checkboxes = eval { $self->$name }; |
695 |
|
warn "ERROR tried to get checkbox value for '$name' which is unknown: $@" if $@; |
696 |
|
$all_checkboxes = [ $all_checkboxes ] unless ref($all_checkboxes) eq 'ARRAY'; # sigh, too chatty |
697 |
|
$checked = ' checked' if grep { defined $_ && $_ eq $value } @$all_checkboxes; |
698 |
|
warn "# checkbox $name $value $checked\t", $self->dump( $self->$name ); |
699 |
|
qq|<input name="$name" value="$value" type="checkbox"$checked>|; |
700 |
|
} |
701 |
|
|
702 |
|
=head2 strip |
703 |
|
|
704 |
|
Strip whitespace around content |
705 |
|
|
706 |
|
my $stripped = strip(' no more whitespace around this '); |
707 |
|
|
708 |
|
=cut |
709 |
|
|
710 |
|
sub strip { |
711 |
|
my $t = shift; |
712 |
|
$t =~ s{^\s+}{}gs; |
713 |
|
$t =~ s{>\s+<}{><}gs; |
714 |
|
$t =~ s{\s+$}{}gs; |
715 |
|
return $t; |
716 |
|
} |
717 |
|
|
718 |
1; |
1; |