1 |
package Frey::Web; |
package Frey::Web; |
2 |
use Moose::Role; |
use Moose::Role; |
3 |
|
|
4 |
#with 'Frey::Escape'; |
with 'Frey::Backtrace'; |
5 |
|
|
6 |
use Frey::Types; |
use Frey::Types; |
7 |
|
|
8 |
use Continuity::Widget::DomNode; |
use Continuity::Widget::DomNode; |
9 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
10 |
use Carp qw/confess/; |
use Carp qw/confess cluck/; |
11 |
use File::Slurp; |
use File::Slurp; |
12 |
|
|
13 |
use Frey::Bookmarklet; |
use Frey::Bookmarklet; |
14 |
use Frey::ClassBrowser; |
use Frey::ClassBrowser; |
15 |
|
use Frey::SVK; |
16 |
|
|
17 |
has 'head' => ( |
has 'head' => ( |
18 |
is => 'rw', |
is => 'rw', |
20 |
default => sub { [ 'static/frey.css' ] }, |
default => sub { [ 'static/frey.css' ] }, |
21 |
); |
); |
22 |
|
|
|
has 'status' => ( |
|
|
is => 'rw', |
|
|
isa => 'ArrayRef[HashRef[Str]]', |
|
|
lazy => 1, |
|
|
default => sub { [ |
|
|
{ 'Bookmarklets' => Frey::Bookmarklet->new->markup }, |
|
|
{ 'ClassBrowser' => Frey::ClassBrowser->new->markup }, |
|
|
] }, |
|
|
); |
|
|
|
|
23 |
has 'request_url' => ( |
has 'request_url' => ( |
24 |
is => 'rw', |
is => 'rw', |
25 |
isa => 'Uri', coerce => 1, |
isa => 'Uri', coerce => 1, |
36 |
}, |
}, |
37 |
); |
); |
38 |
|
|
39 |
|
has 'content_type' => ( |
40 |
|
is => 'rw', |
41 |
|
isa => 'Str', |
42 |
|
default => 'text/html', |
43 |
|
documentation => 'Content-type header', |
44 |
|
); |
45 |
|
|
46 |
|
has 'dump_max_bytes' => ( |
47 |
|
is => 'rw', |
48 |
|
isa => 'Int', |
49 |
|
default => 4096, |
50 |
|
documentation => 'Maximum dump size sent to browser before truncation', |
51 |
|
); |
52 |
|
|
53 |
=head2 inline_smaller_than |
=head2 inline_smaller_than |
54 |
|
|
55 |
Inline JavaScript and CSS smaller than this size into page reducing |
Inline JavaScript and CSS smaller than this size into page reducing |
86 |
$out .= $self->_inline_path( $path ) ? |
$out .= $self->_inline_path( $path ) ? |
87 |
qq|<!-- $path --><style type="text/css">\n| . read_file( $path ) . qq|\n</style>| : |
qq|<!-- $path --><style type="text/css">\n| . read_file( $path ) . qq|\n</style>| : |
88 |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
89 |
|
} elsif ( $path =~ m{<.+>}s ) { |
90 |
|
$out .= $path; |
91 |
} else { |
} else { |
92 |
confess "don't know how to render $path"; |
confess "don't know how to render $path"; |
93 |
} |
} |
102 |
|
|
103 |
my $size = $o->add_head( 'path/to/external.css' ); |
my $size = $o->add_head( 'path/to/external.css' ); |
104 |
|
|
105 |
|
$o->add_head( '<!-- html content -->' ); |
106 |
|
|
107 |
=cut |
=cut |
108 |
|
|
109 |
sub add_head { |
sub add_head { |
111 |
return if ! defined $path || $path eq ''; |
return if ! defined $path || $path eq ''; |
112 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
113 |
|
|
114 |
if ( -e $path ) { |
if ( $path =~ m{<.*>}s ) { |
115 |
|
push @{ $self->head }, $path; |
116 |
|
} elsif ( -e $path ) { |
117 |
if ( $path =~ m/\.(?:js|css)$/ ) { |
if ( $path =~ m/\.(?:js|css)$/ ) { |
118 |
push @{ $self->head }, $path; |
push @{ $self->head }, $path; |
119 |
} else { |
} else { |
120 |
confess "can't add_head( $path ) it's not js or css"; |
confess "can't add_head( $path ) it's not js or css"; |
121 |
} |
} |
122 |
|
return -s $path; |
123 |
} else { |
} else { |
124 |
confess "can't find $path: $!"; |
confess "can't find $path: $!"; |
125 |
} |
} |
126 |
|
|
|
return -s $path; |
|
127 |
} |
} |
128 |
|
|
129 |
our $reload_counter = 0; |
our $reload_counter = 0; |
139 |
|
|
140 |
=cut |
=cut |
141 |
|
|
142 |
|
our @status; |
143 |
|
sub status { @status }; |
144 |
|
|
145 |
|
our $icon_html; |
146 |
|
|
147 |
sub page { |
sub page { |
148 |
my $self = shift; |
my $self = shift; |
149 |
my $a = {@_}; |
my $a = {@_}; |
150 |
|
|
151 |
|
warn "## page ",dump($a); |
152 |
|
|
153 |
$reload_counter++; |
$reload_counter++; |
154 |
|
|
155 |
my $status_line = ''; |
my $status_line = ''; |
156 |
foreach my $part ( @{ $self->status } ) { |
|
157 |
confess "part not hash ",dump( $part ) unless ref($part) eq 'HASH'; |
unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup }; |
158 |
|
unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }; |
159 |
|
|
160 |
|
foreach my $part ( @status ) { |
161 |
foreach my $name ( keys %$part ) { |
foreach my $name ( keys %$part ) { |
162 |
my $content = $part->{$name}; |
my $content = $part->{$name}; |
163 |
if ( ref($content) ) { |
if ( ref($content) ) { |
164 |
$content = '<code>' . dump($content) . '</code>'; |
$content = '<code>' . dump($content) . '</code>'; |
165 |
my $l = length($content); |
my $l = length($content); |
166 |
$content = qq|<span>$l bytes</span>| if $l > 1024; |
$content = qq|<span>$l bytes</span>| if $l > $self->dump_max_bytes; |
167 |
} else { |
} else { |
168 |
$content = qq|<span>$content</span>|; |
$content = qq|<span>$content</span>|; |
169 |
} |
} |
172 |
} |
} |
173 |
} |
} |
174 |
|
|
175 |
|
my $url = $self->request_url; |
176 |
|
$url =~ s{\?reload=\d+}{}; |
177 |
|
|
178 |
|
my $body = $a->{body}; |
179 |
|
$body ||= $self->as_markup if $self->can('as_markup'); |
180 |
|
if ( $self->content_type !~ m{html} ) { |
181 |
|
warn "# return only $self body ", $self->content_type; |
182 |
|
return $body |
183 |
|
} elsif ( ! defined $body ) { |
184 |
|
warn "# no body"; |
185 |
|
$body = '<!-- no body -->'; |
186 |
|
} |
187 |
|
|
188 |
|
my $warn_colors = { |
189 |
|
'#' => '#444', |
190 |
|
'##' => '#888', |
191 |
|
}; |
192 |
|
|
193 |
|
$status_line |
194 |
|
.= qq|<span class="frey-popup">warn<span>| |
195 |
|
. $self->editor_links( |
196 |
|
join("", map { |
197 |
|
warn "# $_"; |
198 |
|
my $style = ''; |
199 |
|
$style = $warn_colors->{$1} |
200 |
|
? ' style="color:' . $warn_colors->{$1} . '"' |
201 |
|
: '' |
202 |
|
if m{^(#+)}; |
203 |
|
qq|<tt$style>$_</tt><br/>|; # XXX <tt> should be <code> but CSS hates me |
204 |
|
} $self->warnings ) |
205 |
|
) |
206 |
|
. qq|</span></span>| |
207 |
|
if $self->warnings; |
208 |
|
|
209 |
|
my ($exit,$description) = ('exit','stop server'); |
210 |
|
($exit,$description) = ('restart','restart server') |
211 |
|
if $ENV{FREY_RESTART}; # tune labels on exit link |
212 |
|
|
213 |
|
my $right = |
214 |
|
qq| |
215 |
|
<span class="right"> |
216 |
|
<a title="reload $url" href="/reload$url">reload</a> |
217 |
|
<a title="$description" href="/exit$url">$exit</a> |
218 |
|
</span> |
219 |
|
|; |
220 |
|
|
221 |
|
my $info = Frey::SVK->info; |
222 |
|
my $revision = Frey::SVK->info->{Revision} || ''; |
223 |
|
$revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)}; |
224 |
|
|
225 |
|
$self->add_icon unless $icon_html; |
226 |
|
|
227 |
my $html = join("\n", |
my $html = join("\n", |
228 |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
229 |
$self->_head_html, |
$self->_head_html, |
230 |
'<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>', |
'<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>', |
231 |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
232 |
|
( $icon_html || '<!-- no icon -->' ), |
233 |
( $a->{head} || '' ), |
( $a->{head} || '' ), |
|
'</head><body>', |
|
|
( $a->{body} || $self->markup || '<!-- no body -->' ), |
|
234 |
qq| |
qq| |
235 |
|
</head><body> |
236 |
|
$body |
237 |
<div class="frey-status-line"> |
<div class="frey-status-line"> |
238 |
<a href="/">Frey</a> $Frey::VERSION |
<a href="/">Frey</a> $Frey::VERSION $revision |
|
<a href="?reload=$reload_counter"><code>| . $self->request_url . qq|</code></a> |
|
239 |
$status_line |
$status_line |
240 |
|
$right |
241 |
</div> |
</div> |
242 |
</body></html> |
</body></html> |
243 |
|, |
|, |
248 |
return $html; |
return $html; |
249 |
} |
} |
250 |
|
|
251 |
|
=head2 editor |
252 |
|
|
253 |
|
Create HTML editor link with optional line and title |
254 |
|
|
255 |
|
my $html = $self->editor( $class, $line, $title ); |
256 |
|
|
257 |
|
=cut |
258 |
|
|
259 |
|
sub editor { |
260 |
|
my ( $self, $class, $line, $title ) = @_; |
261 |
|
confess "need class" unless $class; |
262 |
|
if ( ! defined $title ) { |
263 |
|
$title = "edit $class"; |
264 |
|
$title .= " line $line" if $line; |
265 |
|
} |
266 |
|
$line ||= 1; |
267 |
|
qq|<a target="editor" href="/editor+$class+$line"| . |
268 |
|
( $title ? qq| title="$title"| : '' ) . |
269 |
|
qq|>$class</a>|; |
270 |
|
} |
271 |
|
|
272 |
|
=head2 editor_links |
273 |
|
|
274 |
|
Create HTML links to editor for perl error message |
275 |
|
|
276 |
|
my $html = $self->editor_links( $error ) |
277 |
|
|
278 |
|
=cut |
279 |
|
|
280 |
|
sub editor_links { |
281 |
|
my ( $self, $error ) = @_; |
282 |
|
|
283 |
|
$error =~ s{at\s+(\S+)\s+line\s+(\d+)} |
284 |
|
{at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm; |
285 |
|
|
286 |
|
$error =~ s{(via package ")([\w:]+)(")} |
287 |
|
{$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm; |
288 |
|
|
289 |
|
return $error; |
290 |
|
} |
291 |
|
|
292 |
sub error { |
sub error { |
293 |
my $self = shift; |
my $self = shift; |
294 |
my $error = join(" ", @_); |
my $error = join(" ", @_); |
295 |
my ($package, $filename, $line) = caller; |
|
296 |
$error .= " at $filename line $line" if $error !~ m{ at }; |
my @backtrace = $self->backtrace; |
297 |
warn "WARN: $error\n"; |
$error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace; |
298 |
$error =~ s{at\s+(\S+)\s+line\s+(\d+)}{at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm; |
|
299 |
$error =~ s{(via package ")([\w:]+)(")}{$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm; |
warn "ERROR: $error\n"; |
300 |
return qq|<pre class="frey-error">$error</pre>|; |
return |
301 |
|
qq|<pre class="frey-error">| |
302 |
|
. $self->editor_links( $error ) . |
303 |
|
qq|</pre>| |
304 |
|
; |
305 |
|
} |
306 |
|
|
307 |
|
sub add_status { |
308 |
|
my ( $self, $data ) = @_; |
309 |
|
push @status, $data; |
310 |
|
} |
311 |
|
|
312 |
|
sub clean_status { |
313 |
|
@status = (); |
314 |
|
$icon_html = ''; |
315 |
|
} |
316 |
|
|
317 |
|
sub status_parts { |
318 |
|
warn "## status parts ", dump( map { keys %$_ } @status ); |
319 |
|
} |
320 |
|
|
321 |
|
sub DEMOLISH { |
322 |
|
my ( $self ) = @_; |
323 |
|
cluck "## DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status; |
324 |
|
} |
325 |
|
|
326 |
|
=head2 add_icon |
327 |
|
|
328 |
|
Frey::Foo->add_icon; # /static/icons/Frey/Foo.png |
329 |
|
Frey::Foo->add_icon('warning'); # /static/icons/Frey/Foo/warning.png |
330 |
|
|
331 |
|
=cut |
332 |
|
|
333 |
|
sub add_icon { |
334 |
|
my ($self,$name) = @_; |
335 |
|
my $icon = ref($self); |
336 |
|
$icon = $self->class if $self->can('class'); |
337 |
|
$icon =~ s{::}{/}g; |
338 |
|
$icon .= "/$name" if $name; |
339 |
|
|
340 |
|
my $icon_path = "static/icons/$icon.png"; |
341 |
|
|
342 |
|
if ( -e $icon_path ) { |
343 |
|
$icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|; |
344 |
|
warn "# using icon $icon_path"; |
345 |
|
|
346 |
|
=for later |
347 |
|
|
348 |
|
# FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work! |
349 |
|
my $ico_path = $icon_path; |
350 |
|
$ico_path =~ s{png$}{ico}; |
351 |
|
if ( ! -e $ico_path ) { |
352 |
|
system "convert $icon_path $ico_path"; |
353 |
|
warn "# convert $icon_path $ico_path : $@"; |
354 |
|
} |
355 |
|
$icon_html .= qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path">| if -e $ico_path; |
356 |
|
|
357 |
|
=cut |
358 |
|
|
359 |
|
} else { |
360 |
|
warn "TODO add $icon_path icon"; |
361 |
|
} |
362 |
} |
} |
363 |
|
|
364 |
1; |
1; |