/[Frey]/branches/zimbardo/lib/Frey/Web.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /branches/zimbardo/lib/Frey/Web.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 482 - (hide annotations)
Thu Nov 20 15:23:13 2008 UTC (15 years, 4 months ago) by dpavlin
Original Path: trunk/lib/Frey/Web.pm
File size: 6333 byte(s)
color warn output in popup according to number of prefixed hashes (#) 
1 dpavlin 100 package Frey::Web;
2     use Moose::Role;
3    
4 dpavlin 465 with 'Frey::Backtrace';
5    
6 dpavlin 388 use Frey::Types;
7    
8 dpavlin 100 use Continuity::Widget::DomNode;
9     use Data::Dump qw/dump/;
10 dpavlin 389 use Carp qw/confess/;
11 dpavlin 161 use File::Slurp;
12 dpavlin 100
13 dpavlin 410 use Frey::Bookmarklet;
14     use Frey::ClassBrowser;
15    
16 dpavlin 156 has 'head' => (
17 dpavlin 121 is => 'rw',
18     isa => 'ArrayRef[Str]',
19 dpavlin 160 default => sub { [ 'static/frey.css' ] },
20 dpavlin 121 );
21    
22 dpavlin 388 has 'status' => (
23     is => 'rw',
24     isa => 'ArrayRef[HashRef[Str]]',
25     lazy => 1,
26     default => sub { [
27 dpavlin 473 { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup },
28 dpavlin 455 { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },
29 dpavlin 388 ] },
30     );
31    
32 dpavlin 392 has 'request_url' => (
33 dpavlin 388 is => 'rw',
34     isa => 'Uri', coerce => 1,
35     default => '/',
36     );
37    
38 dpavlin 418 has 'title' => (
39     is => 'rw',
40     isa => 'Str',
41     lazy => 1,
42     default => sub {
43     my ($self) = @_;
44     ref($self);
45     },
46     );
47    
48 dpavlin 448 has 'content_type' => (
49     is => 'rw',
50     isa => 'Str',
51     default => 'text/html',
52 dpavlin 476 documentation => 'Content-type header',
53 dpavlin 448 );
54    
55 dpavlin 476 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 dpavlin 206 =head2 inline_smaller_than
63    
64     Inline JavaScript and CSS smaller than this size into page reducing
65     round-trips to server.
66    
67     =cut
68    
69 dpavlin 161 has 'inline_smaller_than' => (
70     is => 'rw',
71     isa => 'Int',
72     default => 10240,
73     );
74    
75 dpavlin 100 sub dom2html {
76 dpavlin 106 # warn "## dom2html ",dump( @_ );
77 dpavlin 100 return Continuity::Widget::DomNode->create( @_ )->to_string;
78     }
79    
80 dpavlin 161 sub _inline_path {
81     my ( $self, $path ) = @_;
82     -s $path < $self->inline_smaller_than;
83     }
84    
85 dpavlin 156 sub _head_html {
86     my $self = shift;
87 dpavlin 121 my $out = '';
88 dpavlin 156 foreach my $path ( @{ $self->head } ) {
89 dpavlin 121 $path =~ s!^/!!;
90 dpavlin 156 if ( $path =~ m/\.js$/ ) {
91 dpavlin 161 $out .= $self->_inline_path( $path ) ?
92 dpavlin 163 qq|<!-- $path --><script type="text/javascript">\n| . read_file($path) . qq|\n</script>| :
93 dpavlin 161 qq|<script type="text/javascript" src="/$path"></script>|;
94 dpavlin 156 } elsif ( $path =~ m/\.css$/ ) {
95 dpavlin 161 $out .= $self->_inline_path( $path ) ?
96 dpavlin 163 qq|<!-- $path --><style type="text/css">\n| . read_file( $path ) . qq|\n</style>| :
97 dpavlin 161 qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
98 dpavlin 446 } elsif ( $path =~ m{<.+>}s ) {
99 dpavlin 444 $out .= $path;
100 dpavlin 156 } else {
101     confess "don't know how to render $path";
102     }
103 dpavlin 163 $out .= "\n";
104 dpavlin 121 }
105     return $out;
106     }
107 dpavlin 100
108 dpavlin 154 =head2 add_head
109    
110     $o->add_head( 'path/to/external.js' );
111    
112     my $size = $o->add_head( 'path/to/external.css' );
113    
114 dpavlin 445 $o->add_head( '<!-- html content -->' );
115    
116 dpavlin 154 =cut
117    
118     sub add_head {
119     my ( $self, $path ) = @_;
120     return if ! defined $path || $path eq '';
121     $path =~ s!^/!!;
122    
123 dpavlin 446 if ( $path =~ m{<.*>}s ) {
124 dpavlin 444 push @{ $self->head }, $path;
125     } elsif ( -e $path ) {
126 dpavlin 156 if ( $path =~ m/\.(?:js|css)$/ ) {
127     push @{ $self->head }, $path;
128 dpavlin 154 } else {
129     confess "can't add_head( $path ) it's not js or css";
130     }
131 dpavlin 444 return -s $path;
132 dpavlin 154 } else {
133     confess "can't find $path: $!";
134     }
135    
136     }
137    
138 dpavlin 142 our $reload_counter = 0;
139    
140 dpavlin 183
141     =head2 page
142    
143     $self->page(
144     title => 'page title',
145     head => '<!-- optional head markup -->',
146     body => '<b>Page Body</b>',
147     );
148    
149     =cut
150    
151 dpavlin 121 sub page {
152 dpavlin 100 my $self = shift;
153 dpavlin 121 my $a = {@_};
154 dpavlin 100
155 dpavlin 142 $reload_counter++;
156    
157 dpavlin 388 my $status_line = '';
158     foreach my $part ( @{ $self->status } ) {
159 dpavlin 422 if ( ref($part) ne 'HASH' ) {
160     warn "part not hash ",dump( $part ) ;
161     #$self->status( $part );
162     next;
163     }
164 dpavlin 388 foreach my $name ( keys %$part ) {
165 dpavlin 392 my $content = $part->{$name};
166     if ( ref($content) ) {
167 dpavlin 397 $content = '<code>' . dump($content) . '</code>';
168     my $l = length($content);
169 dpavlin 476 $content = qq|<span>$l bytes</span>| if $l > $self->dump_max_bytes;
170 dpavlin 392 } else {
171     $content = qq|<span>$content</span>|;
172     }
173     warn "### part [$name] = ", length( $content ), " bytes" if $self->debug;
174 dpavlin 397 $status_line .= qq|<span class="frey-popup">$name $content</span>\n|;
175 dpavlin 388 }
176     }
177    
178 dpavlin 439 my $url = $self->request_url;
179     $url =~ s{\?reload=\d+}{};
180    
181 dpavlin 460 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 dpavlin 448
191 dpavlin 482 my $warn_colors = {
192     '#' => '#444',
193     '##' => '#888',
194     };
195    
196 dpavlin 468 $status_line
197 dpavlin 482 .= qq|<span class="frey-popup">warn<span>|
198     . $self->editor_links(
199     join("", map {
200     warn "# $_";
201     my $style = '';
202     $style = $warn_colors->{$1}
203     ? ' style="color:' . $warn_colors->{$1} . '"'
204     : ''
205     if m{^(#+)};
206     qq|<tt$style>$_</tt><br/>|; # XXX <tt> should be <code> but CSS hates me
207     } $self->warnings )
208     )
209     . qq|</span></span>|
210 dpavlin 468 if $self->warnings;
211    
212 dpavlin 477 my ($exit,$description) = ('exit','stop server');
213     ($exit,$description) = ('restart','restart server')
214     if $ENV{FREY_RESTART}; # tune labels on exit link
215    
216 dpavlin 473 my $right =
217     qq|
218     <span class="right">
219 dpavlin 477 <a title="reload" href="/reload$url"><code>$url</code></a>
220     <a title="$description" href="/exit$url">$exit</a>
221 dpavlin 473 </span>
222     |;
223    
224 dpavlin 388 my $html = join("\n",
225     qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
226     $self->_head_html,
227 dpavlin 418 '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',
228 dpavlin 388 '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
229     ( $a->{head} || '' ),
230     qq|
231 dpavlin 448 </head><body>
232     $body
233 dpavlin 388 <div class="frey-status-line">
234     <a href="/">Frey</a> $Frey::VERSION
235     $status_line
236 dpavlin 473 $right
237 dpavlin 210 </div>
238     </body></html>
239 dpavlin 388 |,
240     );
241 dpavlin 100
242 dpavlin 121 warn "## >>> page ",length($html), " bytes\n" if $self->debug;
243 dpavlin 100
244 dpavlin 121 return $html;
245 dpavlin 100 }
246    
247 dpavlin 480 =head2 editor
248    
249     Create HTML editor link with optional line and title
250    
251     my $html = $self->editor( $class, $line, $title );
252    
253     =cut
254    
255     sub editor {
256     my ( $self, $class, $line, $title ) = @_;
257     confess "need class" unless $class;
258     $line ||= 1;
259     qq|<a target="editor" href="/editor+$class+$line"| .
260     ( $title ? qq| title="$title"| : '' ) .
261     qq|>$class</a>|;
262     }
263    
264     =head2 editor_links
265    
266     Create HTML links to editor for perl error message
267    
268     my $html = $self->editor_links( $error )
269    
270     =cut
271    
272 dpavlin 468 sub editor_links {
273     my ( $self, $error ) = @_;
274    
275     $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
276     {at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;
277    
278     $error =~ s{(via package ")([\w:]+)(")}
279     {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
280    
281     return $error;
282     }
283    
284 dpavlin 350 sub error {
285 dpavlin 397 my $self = shift;
286     my $error = join(" ", @_);
287 dpavlin 460
288 dpavlin 465 my @backtrace = $self->backtrace;
289     $error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace;
290 dpavlin 460
291     warn "ERROR: $error\n";
292 dpavlin 468 return
293     qq|<pre class="frey-error">|
294     . $self->editor_links( $error ) .
295     qq|</pre>|
296     ;
297 dpavlin 350 }
298    
299 dpavlin 100 1;

  ViewVC Help
Powered by ViewVC 1.1.26