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->markup }, |
{ 'Bookmarklets' => Frey::Bookmarklet->new->as_markup }, |
29 |
] }, |
] }, |
30 |
); |
); |
31 |
|
|
35 |
default => '/', |
default => '/', |
36 |
); |
); |
37 |
|
|
38 |
|
has 'title' => ( |
39 |
|
is => 'rw', |
40 |
|
isa => 'Str', |
41 |
|
lazy => 1, |
42 |
|
default => sub { |
43 |
|
my ($self) = @_; |
44 |
|
ref($self); |
45 |
|
}, |
46 |
|
); |
47 |
|
|
48 |
|
has 'content_type' => ( |
49 |
|
is => 'rw', |
50 |
|
isa => 'Str', |
51 |
|
default => 'text/html', |
52 |
|
); |
53 |
|
|
54 |
=head2 inline_smaller_than |
=head2 inline_smaller_than |
55 |
|
|
56 |
Inline JavaScript and CSS smaller than this size into page reducing |
Inline JavaScript and CSS smaller than this size into page reducing |
87 |
$out .= $self->_inline_path( $path ) ? |
$out .= $self->_inline_path( $path ) ? |
88 |
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>| : |
89 |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
90 |
|
} elsif ( $path =~ m{<.+>}s ) { |
91 |
|
$out .= $path; |
92 |
} else { |
} else { |
93 |
confess "don't know how to render $path"; |
confess "don't know how to render $path"; |
94 |
} |
} |
103 |
|
|
104 |
my $size = $o->add_head( 'path/to/external.css' ); |
my $size = $o->add_head( 'path/to/external.css' ); |
105 |
|
|
106 |
|
$o->add_head( '<!-- html content -->' ); |
107 |
|
|
108 |
=cut |
=cut |
109 |
|
|
110 |
sub add_head { |
sub add_head { |
112 |
return if ! defined $path || $path eq ''; |
return if ! defined $path || $path eq ''; |
113 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
114 |
|
|
115 |
if ( -e $path ) { |
if ( $path =~ m{<.*>}s ) { |
116 |
|
push @{ $self->head }, $path; |
117 |
|
} elsif ( -e $path ) { |
118 |
if ( $path =~ m/\.(?:js|css)$/ ) { |
if ( $path =~ m/\.(?:js|css)$/ ) { |
119 |
push @{ $self->head }, $path; |
push @{ $self->head }, $path; |
120 |
} else { |
} else { |
121 |
confess "can't add_head( $path ) it's not js or css"; |
confess "can't add_head( $path ) it's not js or css"; |
122 |
} |
} |
123 |
|
return -s $path; |
124 |
} else { |
} else { |
125 |
confess "can't find $path: $!"; |
confess "can't find $path: $!"; |
126 |
} |
} |
127 |
|
|
|
return -s $path; |
|
128 |
} |
} |
129 |
|
|
130 |
our $reload_counter = 0; |
our $reload_counter = 0; |
148 |
|
|
149 |
my $status_line = ''; |
my $status_line = ''; |
150 |
foreach my $part ( @{ $self->status } ) { |
foreach my $part ( @{ $self->status } ) { |
151 |
confess "part not hash ",dump( $part ) unless ref($part) eq 'HASH'; |
if ( ref($part) ne 'HASH' ) { |
152 |
|
warn "part not hash ",dump( $part ) ; |
153 |
|
#$self->status( $part ); |
154 |
|
next; |
155 |
|
} |
156 |
foreach my $name ( keys %$part ) { |
foreach my $name ( keys %$part ) { |
157 |
my $content = $part->{$name}; |
my $content = $part->{$name}; |
158 |
if ( ref($content) ) { |
if ( ref($content) ) { |
167 |
} |
} |
168 |
} |
} |
169 |
|
|
170 |
|
my $url = $self->request_url; |
171 |
|
$url =~ s{\?reload=\d+}{}; |
172 |
|
|
173 |
|
my $body = $a->{body}; |
174 |
|
$body ||= $self->as_markup if $self->can('as_markup'); |
175 |
|
if ( $self->content_type !~ m{html} ) { |
176 |
|
warn "# return only $self body ", $self->content_type; |
177 |
|
return $body |
178 |
|
} elsif ( ! defined $body ) { |
179 |
|
warn "# no body"; |
180 |
|
$body = '<!-- no body -->'; |
181 |
|
} |
182 |
|
|
183 |
|
$status_line |
184 |
|
.= qq|<span class="frey-popup">warn<code>| |
185 |
|
. $self->editor_links( join("", $self->warnings ) ) |
186 |
|
. qq|</code></span>| |
187 |
|
if $self->warnings; |
188 |
|
|
189 |
|
my $right = |
190 |
|
qq| |
191 |
|
<span class="right"> |
192 |
|
<a href="?reload=$reload_counter"><code>$url</code></a> |
193 |
|
</span> |
194 |
|
|; |
195 |
|
|
196 |
my $html = join("\n", |
my $html = join("\n", |
197 |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
198 |
$self->_head_html, |
$self->_head_html, |
199 |
'<title>' . ( $a->{title} || ref($self) ) . '</title>', |
'<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>', |
200 |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
201 |
( $a->{head} || '' ), |
( $a->{head} || '' ), |
|
'</head><body>', |
|
|
( $a->{body} || '<!-- no body -->' ), |
|
202 |
qq| |
qq| |
203 |
|
</head><body> |
204 |
|
$body |
205 |
<div class="frey-status-line"> |
<div class="frey-status-line"> |
206 |
<a href="/">Frey</a> $Frey::VERSION |
<a href="/">Frey</a> $Frey::VERSION |
|
<a href="?reload=$reload_counter"><code>| . $self->request_url . qq|</code></a> |
|
207 |
$status_line |
$status_line |
208 |
|
$right |
209 |
</div> |
</div> |
210 |
</body></html> |
</body></html> |
211 |
|, |
|, |
216 |
return $html; |
return $html; |
217 |
} |
} |
218 |
|
|
219 |
|
sub editor_links { |
220 |
|
my ( $self, $error ) = @_; |
221 |
|
|
222 |
|
$error =~ s{at\s+(\S+)\s+line\s+(\d+)} |
223 |
|
{at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm; |
224 |
|
|
225 |
|
$error =~ s{(via package ")([\w:]+)(")} |
226 |
|
{$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm; |
227 |
|
|
228 |
|
return $error; |
229 |
|
} |
230 |
|
|
231 |
sub error { |
sub error { |
232 |
my $self = shift; |
my $self = shift; |
233 |
my $error = join(" ", @_); |
my $error = join(" ", @_); |
234 |
my ($package, $filename, $line) = caller; |
|
235 |
$error .= " at $filename line $line" if $error !~ m{ at }; |
my @backtrace = $self->backtrace; |
236 |
warn "WARN: $error\n"; |
$error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace; |
237 |
$error =~ s{at\s+(\S+)\s+line\s+(\d+)}{at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm; |
|
238 |
return qq|<pre class="frey-error">$error</pre>|; |
warn "ERROR: $error\n"; |
239 |
|
return |
240 |
|
qq|<pre class="frey-error">| |
241 |
|
. $self->editor_links( $error ) . |
242 |
|
qq|</pre>| |
243 |
|
; |
244 |
} |
} |
245 |
|
|
246 |
1; |
1; |