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 |
|
|
24 |
isa => 'ArrayRef[HashRef[Str]]', |
isa => 'ArrayRef[HashRef[Str]]', |
25 |
lazy => 1, |
lazy => 1, |
26 |
default => sub { [ |
default => sub { [ |
27 |
{ 'Bookmarklets' => Frey::Bookmarklet->new->markup }, |
{ 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup }, |
28 |
{ 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->markup }, |
{ 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }, |
29 |
] }, |
] }, |
30 |
); |
); |
31 |
|
|
45 |
}, |
}, |
46 |
); |
); |
47 |
|
|
48 |
|
has 'content_type' => ( |
49 |
|
is => 'rw', |
50 |
|
isa => 'Str', |
51 |
|
default => 'text/html', |
52 |
|
documentation => 'Content-type header', |
53 |
|
); |
54 |
|
|
55 |
|
has 'dump_max_bytes' => ( |
56 |
|
is => 'rw', |
57 |
|
isa => 'Int', |
58 |
|
default => 4096, |
59 |
|
documentation => 'Maximum dump size sent to browser before truncation', |
60 |
|
); |
61 |
|
|
62 |
=head2 inline_smaller_than |
=head2 inline_smaller_than |
63 |
|
|
64 |
Inline JavaScript and CSS smaller than this size into page reducing |
Inline JavaScript and CSS smaller than this size into page reducing |
95 |
$out .= $self->_inline_path( $path ) ? |
$out .= $self->_inline_path( $path ) ? |
96 |
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>| : |
97 |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
98 |
} elsif ( $path =~ m{<.+>} ) { |
} elsif ( $path =~ m{<.+>}s ) { |
99 |
$out .= $path; |
$out .= $path; |
100 |
} else { |
} else { |
101 |
confess "don't know how to render $path"; |
confess "don't know how to render $path"; |
111 |
|
|
112 |
my $size = $o->add_head( 'path/to/external.css' ); |
my $size = $o->add_head( 'path/to/external.css' ); |
113 |
|
|
114 |
|
$o->add_head( '<!-- html content -->' ); |
115 |
|
|
116 |
=cut |
=cut |
117 |
|
|
118 |
sub add_head { |
sub add_head { |
120 |
return if ! defined $path || $path eq ''; |
return if ! defined $path || $path eq ''; |
121 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
122 |
|
|
123 |
if ( $path =~ m{<.*>} ) { |
if ( $path =~ m{<.*>}s ) { |
124 |
push @{ $self->head }, $path; |
push @{ $self->head }, $path; |
125 |
} elsif ( -e $path ) { |
} elsif ( -e $path ) { |
126 |
if ( $path =~ m/\.(?:js|css)$/ ) { |
if ( $path =~ m/\.(?:js|css)$/ ) { |
166 |
if ( ref($content) ) { |
if ( ref($content) ) { |
167 |
$content = '<code>' . dump($content) . '</code>'; |
$content = '<code>' . dump($content) . '</code>'; |
168 |
my $l = length($content); |
my $l = length($content); |
169 |
$content = qq|<span>$l bytes</span>| if $l > 1024; |
$content = qq|<span>$l bytes</span>| if $l > $self->dump_max_bytes; |
170 |
} else { |
} else { |
171 |
$content = qq|<span>$content</span>|; |
$content = qq|<span>$content</span>|; |
172 |
} |
} |
178 |
my $url = $self->request_url; |
my $url = $self->request_url; |
179 |
$url =~ s{\?reload=\d+}{}; |
$url =~ s{\?reload=\d+}{}; |
180 |
|
|
181 |
|
my $body = $a->{body}; |
182 |
|
$body ||= $self->as_markup if $self->can('as_markup'); |
183 |
|
if ( $self->content_type !~ m{html} ) { |
184 |
|
warn "# return only $self body ", $self->content_type; |
185 |
|
return $body |
186 |
|
} elsif ( ! defined $body ) { |
187 |
|
warn "# no body"; |
188 |
|
$body = '<!-- no body -->'; |
189 |
|
} |
190 |
|
|
191 |
|
$status_line |
192 |
|
.= qq|<span class="frey-popup">warn<code>| |
193 |
|
. $self->editor_links( join("", $self->warnings ) ) |
194 |
|
. qq|</code></span>| |
195 |
|
if $self->warnings; |
196 |
|
|
197 |
|
my $right = |
198 |
|
qq| |
199 |
|
<span class="right"> |
200 |
|
<a href="?reload=$reload_counter"><code>$url</code></a> |
201 |
|
</span> |
202 |
|
|; |
203 |
|
|
204 |
my $html = join("\n", |
my $html = join("\n", |
205 |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
206 |
$self->_head_html, |
$self->_head_html, |
207 |
'<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>', |
'<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>', |
208 |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
209 |
( $a->{head} || '' ), |
( $a->{head} || '' ), |
|
'</head><body>', |
|
|
( $a->{body} || $self->markup || '<!-- no body -->' ), |
|
210 |
qq| |
qq| |
211 |
|
</head><body> |
212 |
|
$body |
213 |
<div class="frey-status-line"> |
<div class="frey-status-line"> |
214 |
<a href="/">Frey</a> $Frey::VERSION |
<a href="/">Frey</a> $Frey::VERSION |
|
<a href="?reload=$reload_counter"><code>$url</code></a> |
|
215 |
$status_line |
$status_line |
216 |
|
$right |
217 |
</div> |
</div> |
218 |
</body></html> |
</body></html> |
219 |
|, |
|, |
224 |
return $html; |
return $html; |
225 |
} |
} |
226 |
|
|
227 |
|
sub editor_links { |
228 |
|
my ( $self, $error ) = @_; |
229 |
|
|
230 |
|
$error =~ s{at\s+(\S+)\s+line\s+(\d+)} |
231 |
|
{at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm; |
232 |
|
|
233 |
|
$error =~ s{(via package ")([\w:]+)(")} |
234 |
|
{$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm; |
235 |
|
|
236 |
|
return $error; |
237 |
|
} |
238 |
|
|
239 |
sub error { |
sub error { |
240 |
my $self = shift; |
my $self = shift; |
241 |
my $error = join(" ", @_); |
my $error = join(" ", @_); |
242 |
my ($package, $filename, $line) = caller; |
|
243 |
$error .= " at $filename line $line" if $error !~ m{ at }; |
my @backtrace = $self->backtrace; |
244 |
warn "WARN: $error\n"; |
$error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace; |
245 |
$error =~ s{at\s+(\S+)\s+line\s+(\d+)}{at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm; |
|
246 |
$error =~ s{(via package ")([\w:]+)(")}{$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm; |
warn "ERROR: $error\n"; |
247 |
return qq|<pre class="frey-error">$error</pre>|; |
return |
248 |
|
qq|<pre class="frey-error">| |
249 |
|
. $self->editor_links( $error ) . |
250 |
|
qq|</pre>| |
251 |
|
; |
252 |
} |
} |
253 |
|
|
254 |
1; |
1; |