80 |
return $html; |
return $html; |
81 |
} |
} |
82 |
|
|
83 |
|
# from Mojo::ByteStream |
84 |
|
sub url_escape { |
85 |
|
my ( $self, $url, $pattern ) = @_; |
86 |
|
$pattern ||= 'A-Za-z0-9\-\.\_\~'; |
87 |
|
$url =~ s/([^$pattern])/sprintf('%%%02X',ord($1))/ge; |
88 |
|
return $url; |
89 |
|
} |
90 |
|
|
91 |
sub html_dump { |
sub html_dump { |
92 |
my ( $self, $dump ) = @_; |
my ( $self, $dump ) = @_; |
93 |
$dump = dump( $dump ) if ref($dump); |
$dump = dump( $dump ) if ref($dump); |
102 |
sub popup { my $self = shift; $self->popup_dropdown('popup', @_); } |
sub popup { my $self = shift; $self->popup_dropdown('popup', @_); } |
103 |
sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); } |
sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); } |
104 |
|
|
105 |
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 |
106 |
|
|
107 |
sub popup_dropdown { |
sub popup_dropdown { |
108 |
my ( $self, $type, $name, $content, $full ) = @_; |
my ( $self, $type, $name, $content, $full ) = @_; |
124 |
} |
} |
125 |
} |
} |
126 |
|
|
127 |
sub _inline_path { |
sub _inline { |
128 |
my ( $self, $path ) = @_; |
my ( $self, $path ) = @_; |
129 |
-s $path < $self->inline_smaller_than; |
return unless defined $path; |
130 |
|
warn "# _inline $path"; |
131 |
|
-e $path && -s $path < $self->inline_smaller_than && -s $path; |
132 |
} |
} |
133 |
|
|
134 |
sub _head_html { |
sub _head_html { |
137 |
foreach my $path ( @head ) { |
foreach my $path ( @head ) { |
138 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
139 |
if ( $path =~ m/\.js$/ ) { |
if ( $path =~ m/\.js$/ ) { |
140 |
$out .= $self->_inline_path( $path ) ? |
my $size; |
141 |
qq|<script type="text/javascript">\n/* inline $path */\n\n| . read_file($path) . qq|\n</script>| : |
$out .= $size = _inline( $path ) ? |
142 |
|
qq|<script type="text/javascript">\n/* inline $path $size bytes */\n\n| . read_file($path) . qq|\n</script>| : |
143 |
qq|<script type="text/javascript" src="/$path"></script>|; |
qq|<script type="text/javascript" src="/$path"></script>|; |
144 |
} elsif ( $path =~ m/\.css$/ ) { |
} elsif ( $path =~ m/\.css$/ ) { |
145 |
$out .= $self->_inline_path( $path ) ? |
my $size; |
146 |
qq|<style type="text/css">\n/* inline $path */\n\n| . read_file( $path ) . qq|\n</style>| : |
$out .= $size = _inline( $path ) ? |
147 |
|
qq|<style type="text/css">\n/* inline $path $size bytes */\n\n| . read_file( $path ) . qq|\n</style>| : |
148 |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
149 |
} elsif ( $path =~ m{<.+>}s ) { |
} elsif ( $path =~ m{<.+>}s ) { |
150 |
$out .= $path; |
$out .= $path; |
186 |
|
|
187 |
} |
} |
188 |
|
|
189 |
|
sub _add_css_js { |
190 |
|
my ( $self, $what, $content ) = @_; |
191 |
|
|
192 |
|
my $tag = $what eq 'css' ? 'style' : 'script'; |
193 |
|
my $type = $what eq 'css' ? 'text/css' : 'text/javascript'; |
194 |
|
my $head; |
195 |
|
|
196 |
|
my ( $package, $path, $line ) = caller(1); |
197 |
|
|
198 |
|
$content = "/$content" if $content !~ m{[\n\r]} && -e $content; |
199 |
|
if ( $content =~ $re_html ) { |
200 |
|
$head = qq| |
201 |
|
$content |
202 |
|
<!-- $type via $package at $path line $line --> |
203 |
|
|; |
204 |
|
} elsif ( $content =~ m{^(/|https?://)} ) { |
205 |
|
if ( $what eq 'js' ) { |
206 |
|
$head = qq| |
207 |
|
<$tag type="$type" src="$content"> |
208 |
|
/* $what via $package at $path line $line */ |
209 |
|
</$tag> |
210 |
|
|; |
211 |
|
} else { |
212 |
|
$head = qq| |
213 |
|
<link rel="stylesheet" type="$type" href="$content"> |
214 |
|
<!-- $what via $package at $path line $line --> |
215 |
|
|; |
216 |
|
} |
217 |
|
} else { |
218 |
|
$head = qq| |
219 |
|
<$tag type="$type"> |
220 |
|
/* via $package at $path line $line */ |
221 |
|
$content |
222 |
|
</$tag> |
223 |
|
|; |
224 |
|
}; |
225 |
|
$self->add_head( $head ); |
226 |
|
} |
227 |
|
|
228 |
sub add_css { |
sub add_css { |
229 |
my ($self,$css) = @_; |
my ($self,$css) = @_; |
230 |
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> |
|
|
| ); |
|
231 |
} |
} |
232 |
|
|
233 |
sub add_js { |
sub add_js { |
234 |
my ($self,$js) = @_; |
my ($self,$js) = @_; |
235 |
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> |
|
|
| ); |
|
|
}; |
|
236 |
} |
} |
237 |
|
|
238 |
our $reload_counter = 0; |
our $reload_counter = 0; |
257 |
my $self = shift; |
my $self = shift; |
258 |
my $a = {@_}; |
my $a = {@_}; |
259 |
|
|
|
warn "## page ",dump($a); |
|
|
|
|
260 |
$reload_counter++; |
$reload_counter++; |
261 |
|
|
262 |
my $status_line = ''; |
my $status_line = ''; |
305 |
|
|
306 |
$self->add_icon unless $icon_html; |
$self->add_icon unless $icon_html; |
307 |
|
|
308 |
|
my $title = undef |
309 |
|
|| $a->{title} |
310 |
|
|| $self->title |
311 |
|
|| ref($self) |
312 |
|
; |
313 |
|
|
314 |
|
# $title =~ s{(\w)\w+::}{$1:}g; # XXX compress names of classes |
315 |
|
|
316 |
my $html = join("\n", |
my $html = join("\n", |
317 |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
318 |
$self->_head_html, |
$self->_head_html, |
319 |
'<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>', |
qq|<title>$title</title>|, |
320 |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
321 |
( $icon_html || '<!-- no icon -->' ), |
( $icon_html || '<!-- no icon -->' ), |
322 |
( $a->{head} || '' ), |
( $a->{head} || '' ), |
398 |
return $error; |
return $error; |
399 |
} |
} |
400 |
|
|
401 |
|
sub html_self { |
402 |
|
my $self = shift; |
403 |
|
my $html = $self; |
404 |
|
$html =~ s{([\w:]+)=}{<a target="$1" href="/$1" title="introspect $1">$1</a>=}gsm; |
405 |
|
return $html; |
406 |
|
} |
407 |
|
|
408 |
=head2 error |
=head2 error |
409 |
|
|
410 |
This method will return error to browser and backtrace unless |
This method will return error to browser and backtrace unless |
417 |
my $error = join(" ", @_); |
my $error = join(" ", @_); |
418 |
|
|
419 |
my $fatal = ''; |
my $fatal = ''; |
420 |
|
my $backtrace = ''; |
421 |
|
|
422 |
if ( $error !~ m{\n$} ) { |
if ( $error !~ m{\n$} ) { |
423 |
if ( my @backtrace = $self->backtrace ) { |
if ( my @backtrace = $self->backtrace ) { |
424 |
$error .= "\n\t" . join( "\n\t", @backtrace ); |
$backtrace = |
425 |
|
"\n" . $self->html_self . "->error backtrace\n\t" |
426 |
|
. $self->html_links( join( "\n\t", @backtrace ) ) |
427 |
|
; |
428 |
$fatal = qq| frey-fatal|; |
$fatal = qq| frey-fatal|; |
429 |
} |
} |
430 |
} |
} |
431 |
|
|
432 |
warn "ERROR: $error\n"; |
warn "ERROR: $error\n"; |
433 |
$self->add_icon('error'); |
$self->add_icon('error'); |
434 |
return |
$error = $self->html_links( $error ); |
435 |
qq|<pre class="frey-error$fatal">| |
return qq|<pre class="frey-error$fatal">$error $backtrace</pre>| ; |
|
. $self->html_links( $error ) . |
|
|
qq|</pre>| |
|
|
; |
|
436 |
} |
} |
437 |
|
|
438 |
=head1 Status line |
=head1 Status line |
447 |
|
|
448 |
sub add_status { |
sub add_status { |
449 |
my ( $self, $data ) = @_; |
my ( $self, $data ) = @_; |
450 |
push @status, { 'X' => [ $self->backtrace ] }; |
die "no data" unless $data; |
451 |
if ( ref($data) ) { |
if ( ref $data ) { |
452 |
push @status, $data; |
push @status, $data; |
453 |
} else { |
} else { |
454 |
if ( defined $status[ $#status ] ) { |
if ( defined $status[ $#status ] ) { |
711 |
my ($self) = @_; |
my ($self) = @_; |
712 |
|
|
713 |
my @backtrace; |
my @backtrace; |
714 |
foreach ( 0 .. 5 ) { |
foreach ( 1 .. 5 ) { # 0 = backtrace |
715 |
my ( |
my ( |
716 |
$package,$path,$line |
$package,$path,$line |
717 |
# subroutine hasargs |
# subroutine hasargs |