1 |
package Frey::Web; |
package Frey::Web; |
2 |
use Moose::Role; |
use Moose::Role; |
3 |
|
|
4 |
|
#with 'Frey::Escape'; |
5 |
|
|
6 |
|
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/; |
11 |
use File::Slurp; |
use File::Slurp; |
12 |
|
|
13 |
|
use Frey::Bookmarklet; |
14 |
|
use Frey::ClassBrowser; |
15 |
|
|
16 |
has 'head' => ( |
has 'head' => ( |
17 |
is => 'rw', |
is => 'rw', |
18 |
isa => 'ArrayRef[Str]', |
isa => 'ArrayRef[Str]', |
19 |
default => sub { [ 'static/frey.css' ] }, |
default => sub { [ 'static/frey.css' ] }, |
20 |
); |
); |
21 |
|
|
22 |
|
has 'status' => ( |
23 |
|
is => 'rw', |
24 |
|
isa => 'ArrayRef[HashRef[Str]]', |
25 |
|
lazy => 1, |
26 |
|
default => sub { [ |
27 |
|
{ 'Bookmarklets' => Frey::Bookmarklet->new->markup }, |
28 |
|
{ 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->markup }, |
29 |
|
] }, |
30 |
|
); |
31 |
|
|
32 |
|
has 'request_url' => ( |
33 |
|
is => 'rw', |
34 |
|
isa => 'Uri', coerce => 1, |
35 |
|
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 |
|
=head2 inline_smaller_than |
49 |
|
|
50 |
|
Inline JavaScript and CSS smaller than this size into page reducing |
51 |
|
round-trips to server. |
52 |
|
|
53 |
|
=cut |
54 |
|
|
55 |
has 'inline_smaller_than' => ( |
has 'inline_smaller_than' => ( |
56 |
is => 'rw', |
is => 'rw', |
57 |
isa => 'Int', |
isa => 'Int', |
75 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
76 |
if ( $path =~ m/\.js$/ ) { |
if ( $path =~ m/\.js$/ ) { |
77 |
$out .= $self->_inline_path( $path ) ? |
$out .= $self->_inline_path( $path ) ? |
78 |
qq|<script type="text/javascript">| . read_file($path) . qq|</script>| : |
qq|<!-- $path --><script type="text/javascript">\n| . read_file($path) . qq|\n</script>| : |
79 |
qq|<script type="text/javascript" src="/$path"></script>|; |
qq|<script type="text/javascript" src="/$path"></script>|; |
80 |
} elsif ( $path =~ m/\.css$/ ) { |
} elsif ( $path =~ m/\.css$/ ) { |
81 |
$out .= $self->_inline_path( $path ) ? |
$out .= $self->_inline_path( $path ) ? |
82 |
qq|<style type="text/css">| . read_file( $path ) . qq|</style>| : |
qq|<!-- $path --><style type="text/css">\n| . read_file( $path ) . qq|\n</style>| : |
83 |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
84 |
|
} elsif ( $path =~ m{<.+>} ) { |
85 |
|
$out .= $path; |
86 |
} else { |
} else { |
87 |
confess "don't know how to render $path"; |
confess "don't know how to render $path"; |
88 |
} |
} |
89 |
|
$out .= "\n"; |
90 |
} |
} |
91 |
return $out; |
return $out; |
92 |
} |
} |
104 |
return if ! defined $path || $path eq ''; |
return if ! defined $path || $path eq ''; |
105 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
106 |
|
|
107 |
if ( -e $path ) { |
if ( $path =~ m{<.*>} ) { |
108 |
|
push @{ $self->head }, $path; |
109 |
|
} elsif ( -e $path ) { |
110 |
if ( $path =~ m/\.(?:js|css)$/ ) { |
if ( $path =~ m/\.(?:js|css)$/ ) { |
111 |
push @{ $self->head }, $path; |
push @{ $self->head }, $path; |
112 |
} else { |
} else { |
113 |
confess "can't add_head( $path ) it's not js or css"; |
confess "can't add_head( $path ) it's not js or css"; |
114 |
} |
} |
115 |
|
return -s $path; |
116 |
} else { |
} else { |
117 |
confess "can't find $path: $!"; |
confess "can't find $path: $!"; |
118 |
} |
} |
119 |
|
|
|
return -s $path; |
|
120 |
} |
} |
121 |
|
|
122 |
our $reload_counter = 0; |
our $reload_counter = 0; |
123 |
|
|
124 |
|
|
125 |
|
=head2 page |
126 |
|
|
127 |
|
$self->page( |
128 |
|
title => 'page title', |
129 |
|
head => '<!-- optional head markup -->', |
130 |
|
body => '<b>Page Body</b>', |
131 |
|
); |
132 |
|
|
133 |
|
=cut |
134 |
|
|
135 |
sub page { |
sub page { |
136 |
my $self = shift; |
my $self = shift; |
137 |
my $a = {@_}; |
my $a = {@_}; |
138 |
|
|
139 |
$reload_counter++; |
$reload_counter++; |
140 |
|
|
141 |
my $html = qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>| |
my $status_line = ''; |
142 |
. $self->_head_html |
foreach my $part ( @{ $self->status } ) { |
143 |
. '<title>' . ( $a->{title} || ref($self) ) . '</title>' |
if ( ref($part) ne 'HASH' ) { |
144 |
. ( $a->{head} || '' ) |
warn "part not hash ",dump( $part ) ; |
145 |
. '</head><body>' |
#$self->status( $part ); |
146 |
. ( $a->{body} || '<!-- no body -->' ) |
next; |
147 |
. qq|<div class="frey-status-line"> <a href="/">Frey</a> $Frey::VERSION <a href="?reload=$reload_counter">reload</a> </div>| |
} |
148 |
. '</body></html>' |
foreach my $name ( keys %$part ) { |
149 |
; |
my $content = $part->{$name}; |
150 |
|
if ( ref($content) ) { |
151 |
|
$content = '<code>' . dump($content) . '</code>'; |
152 |
|
my $l = length($content); |
153 |
|
$content = qq|<span>$l bytes</span>| if $l > 1024; |
154 |
|
} else { |
155 |
|
$content = qq|<span>$content</span>|; |
156 |
|
} |
157 |
|
warn "### part [$name] = ", length( $content ), " bytes" if $self->debug; |
158 |
|
$status_line .= qq|<span class="frey-popup">$name $content</span>\n|; |
159 |
|
} |
160 |
|
} |
161 |
|
|
162 |
|
my $url = $self->request_url; |
163 |
|
$url =~ s{\?reload=\d+}{}; |
164 |
|
|
165 |
|
my $html = join("\n", |
166 |
|
qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|, |
167 |
|
$self->_head_html, |
168 |
|
'<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>', |
169 |
|
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', |
170 |
|
( $a->{head} || '' ), |
171 |
|
'</head><body>', |
172 |
|
( $a->{body} || $self->markup || '<!-- no body -->' ), |
173 |
|
qq| |
174 |
|
<div class="frey-status-line"> |
175 |
|
<a href="/">Frey</a> $Frey::VERSION |
176 |
|
<a href="?reload=$reload_counter"><code>$url</code></a> |
177 |
|
$status_line |
178 |
|
</div> |
179 |
|
</body></html> |
180 |
|
|, |
181 |
|
); |
182 |
|
|
183 |
warn "## >>> page ",length($html), " bytes\n" if $self->debug; |
warn "## >>> page ",length($html), " bytes\n" if $self->debug; |
184 |
|
|
185 |
return $html; |
return $html; |
186 |
} |
} |
187 |
|
|
188 |
|
sub error { |
189 |
|
my $self = shift; |
190 |
|
my $error = join(" ", @_); |
191 |
|
my ($package, $filename, $line) = caller; |
192 |
|
$error .= " at $filename line $line" if $error !~ m{ at }; |
193 |
|
warn "WARN: $error\n"; |
194 |
|
$error =~ s{at\s+(\S+)\s+line\s+(\d+)}{at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm; |
195 |
|
$error =~ s{(via package ")([\w:]+)(")}{$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm; |
196 |
|
return qq|<pre class="frey-error">$error</pre>|; |
197 |
|
} |
198 |
|
|
199 |
1; |
1; |