/[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 524 - (hide annotations)
Wed Nov 26 00:45:52 2008 UTC (15 years, 5 months ago) by dpavlin
Original Path: trunk/lib/Frey/Web.pm
File size: 7562 byte(s)
first icon implementation with automatic converter which depends on ImageMagick's convert
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 518 use Carp qw/confess cluck/;
11 dpavlin 161 use File::Slurp;
12 dpavlin 100
13 dpavlin 410 use Frey::Bookmarklet;
14     use Frey::ClassBrowser;
15 dpavlin 505 use Frey::SVK;
16 dpavlin 410
17 dpavlin 156 has 'head' => (
18 dpavlin 121 is => 'rw',
19     isa => 'ArrayRef[Str]',
20 dpavlin 160 default => sub { [ 'static/frey.css' ] },
21 dpavlin 121 );
22    
23 dpavlin 392 has 'request_url' => (
24 dpavlin 388 is => 'rw',
25     isa => 'Uri', coerce => 1,
26     default => '/',
27     );
28    
29 dpavlin 418 has 'title' => (
30     is => 'rw',
31     isa => 'Str',
32     lazy => 1,
33     default => sub {
34     my ($self) = @_;
35     ref($self);
36     },
37     );
38    
39 dpavlin 448 has 'content_type' => (
40     is => 'rw',
41     isa => 'Str',
42     default => 'text/html',
43 dpavlin 476 documentation => 'Content-type header',
44 dpavlin 448 );
45    
46 dpavlin 476 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 dpavlin 206 =head2 inline_smaller_than
54    
55     Inline JavaScript and CSS smaller than this size into page reducing
56     round-trips to server.
57    
58     =cut
59    
60 dpavlin 161 has 'inline_smaller_than' => (
61     is => 'rw',
62     isa => 'Int',
63     default => 10240,
64     );
65    
66 dpavlin 100 sub dom2html {
67 dpavlin 106 # warn "## dom2html ",dump( @_ );
68 dpavlin 100 return Continuity::Widget::DomNode->create( @_ )->to_string;
69     }
70    
71 dpavlin 161 sub _inline_path {
72     my ( $self, $path ) = @_;
73     -s $path < $self->inline_smaller_than;
74     }
75    
76 dpavlin 156 sub _head_html {
77     my $self = shift;
78 dpavlin 121 my $out = '';
79 dpavlin 156 foreach my $path ( @{ $self->head } ) {
80 dpavlin 121 $path =~ s!^/!!;
81 dpavlin 156 if ( $path =~ m/\.js$/ ) {
82 dpavlin 161 $out .= $self->_inline_path( $path ) ?
83 dpavlin 163 qq|<!-- $path --><script type="text/javascript">\n| . read_file($path) . qq|\n</script>| :
84 dpavlin 161 qq|<script type="text/javascript" src="/$path"></script>|;
85 dpavlin 156 } elsif ( $path =~ m/\.css$/ ) {
86 dpavlin 161 $out .= $self->_inline_path( $path ) ?
87 dpavlin 163 qq|<!-- $path --><style type="text/css">\n| . read_file( $path ) . qq|\n</style>| :
88 dpavlin 161 qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
89 dpavlin 446 } elsif ( $path =~ m{<.+>}s ) {
90 dpavlin 444 $out .= $path;
91 dpavlin 156 } else {
92     confess "don't know how to render $path";
93     }
94 dpavlin 163 $out .= "\n";
95 dpavlin 121 }
96     return $out;
97     }
98 dpavlin 100
99 dpavlin 154 =head2 add_head
100    
101     $o->add_head( 'path/to/external.js' );
102    
103     my $size = $o->add_head( 'path/to/external.css' );
104    
105 dpavlin 445 $o->add_head( '<!-- html content -->' );
106    
107 dpavlin 154 =cut
108    
109     sub add_head {
110     my ( $self, $path ) = @_;
111     return if ! defined $path || $path eq '';
112     $path =~ s!^/!!;
113    
114 dpavlin 446 if ( $path =~ m{<.*>}s ) {
115 dpavlin 444 push @{ $self->head }, $path;
116     } elsif ( -e $path ) {
117 dpavlin 156 if ( $path =~ m/\.(?:js|css)$/ ) {
118     push @{ $self->head }, $path;
119 dpavlin 154 } else {
120     confess "can't add_head( $path ) it's not js or css";
121     }
122 dpavlin 444 return -s $path;
123 dpavlin 154 } else {
124     confess "can't find $path: $!";
125     }
126    
127     }
128    
129 dpavlin 142 our $reload_counter = 0;
130    
131 dpavlin 183
132     =head2 page
133    
134     $self->page(
135     title => 'page title',
136     head => '<!-- optional head markup -->',
137     body => '<b>Page Body</b>',
138     );
139    
140     =cut
141    
142 dpavlin 519 our @status;
143     sub status { @status };
144    
145 dpavlin 121 sub page {
146 dpavlin 100 my $self = shift;
147 dpavlin 121 my $a = {@_};
148 dpavlin 100
149 dpavlin 519 warn "## page ",dump($a);
150    
151 dpavlin 142 $reload_counter++;
152    
153 dpavlin 388 my $status_line = '';
154 dpavlin 519
155     unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup };
156     unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup };
157    
158     foreach my $part ( @status ) {
159 dpavlin 388 foreach my $name ( keys %$part ) {
160 dpavlin 392 my $content = $part->{$name};
161     if ( ref($content) ) {
162 dpavlin 397 $content = '<code>' . dump($content) . '</code>';
163     my $l = length($content);
164 dpavlin 476 $content = qq|<span>$l bytes</span>| if $l > $self->dump_max_bytes;
165 dpavlin 392 } else {
166     $content = qq|<span>$content</span>|;
167     }
168     warn "### part [$name] = ", length( $content ), " bytes" if $self->debug;
169 dpavlin 397 $status_line .= qq|<span class="frey-popup">$name $content</span>\n|;
170 dpavlin 388 }
171     }
172    
173 dpavlin 439 my $url = $self->request_url;
174     $url =~ s{\?reload=\d+}{};
175    
176 dpavlin 460 my $body = $a->{body};
177     $body ||= $self->as_markup if $self->can('as_markup');
178     if ( $self->content_type !~ m{html} ) {
179     warn "# return only $self body ", $self->content_type;
180     return $body
181     } elsif ( ! defined $body ) {
182     warn "# no body";
183     $body = '<!-- no body -->';
184     }
185 dpavlin 448
186 dpavlin 482 my $warn_colors = {
187     '#' => '#444',
188     '##' => '#888',
189     };
190    
191 dpavlin 468 $status_line
192 dpavlin 482 .= qq|<span class="frey-popup">warn<span>|
193     . $self->editor_links(
194     join("", map {
195     warn "# $_";
196     my $style = '';
197     $style = $warn_colors->{$1}
198     ? ' style="color:' . $warn_colors->{$1} . '"'
199     : ''
200     if m{^(#+)};
201     qq|<tt$style>$_</tt><br/>|; # XXX <tt> should be <code> but CSS hates me
202     } $self->warnings )
203     )
204     . qq|</span></span>|
205 dpavlin 468 if $self->warnings;
206    
207 dpavlin 477 my ($exit,$description) = ('exit','stop server');
208     ($exit,$description) = ('restart','restart server')
209     if $ENV{FREY_RESTART}; # tune labels on exit link
210    
211 dpavlin 473 my $right =
212     qq|
213     <span class="right">
214 dpavlin 519 <a title="reload $url" href="/reload$url">reload</a>
215 dpavlin 477 <a title="$description" href="/exit$url">$exit</a>
216 dpavlin 473 </span>
217     |;
218    
219 dpavlin 516 my $info = Frey::SVK->info;
220 dpavlin 505 my $revision = Frey::SVK->info->{Revision} || '';
221 dpavlin 516 $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};
222 dpavlin 505
223 dpavlin 524 $self->add_icon;
224    
225 dpavlin 388 my $html = join("\n",
226     qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
227     $self->_head_html,
228 dpavlin 418 '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',
229 dpavlin 388 '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
230     ( $a->{head} || '' ),
231     qq|
232 dpavlin 448 </head><body>
233     $body
234 dpavlin 388 <div class="frey-status-line">
235 dpavlin 505 <a href="/">Frey</a> $Frey::VERSION $revision
236 dpavlin 388 $status_line
237 dpavlin 473 $right
238 dpavlin 210 </div>
239     </body></html>
240 dpavlin 388 |,
241     );
242 dpavlin 100
243 dpavlin 121 warn "## >>> page ",length($html), " bytes\n" if $self->debug;
244 dpavlin 100
245 dpavlin 121 return $html;
246 dpavlin 100 }
247    
248 dpavlin 480 =head2 editor
249    
250     Create HTML editor link with optional line and title
251    
252     my $html = $self->editor( $class, $line, $title );
253    
254     =cut
255    
256     sub editor {
257     my ( $self, $class, $line, $title ) = @_;
258     confess "need class" unless $class;
259 dpavlin 519 if ( ! defined $title ) {
260     $title = "edit $class";
261     $title .= " line $line" if $line;
262     }
263     $line ||= 1;
264 dpavlin 480 qq|<a target="editor" href="/editor+$class+$line"| .
265     ( $title ? qq| title="$title"| : '' ) .
266     qq|>$class</a>|;
267     }
268    
269     =head2 editor_links
270    
271     Create HTML links to editor for perl error message
272    
273     my $html = $self->editor_links( $error )
274    
275     =cut
276    
277 dpavlin 468 sub editor_links {
278     my ( $self, $error ) = @_;
279    
280     $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
281     {at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;
282    
283     $error =~ s{(via package ")([\w:]+)(")}
284     {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
285    
286     return $error;
287     }
288    
289 dpavlin 350 sub error {
290 dpavlin 397 my $self = shift;
291     my $error = join(" ", @_);
292 dpavlin 460
293 dpavlin 465 my @backtrace = $self->backtrace;
294     $error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace;
295 dpavlin 460
296     warn "ERROR: $error\n";
297 dpavlin 468 return
298     qq|<pre class="frey-error">|
299     . $self->editor_links( $error ) .
300     qq|</pre>|
301     ;
302 dpavlin 350 }
303    
304 dpavlin 507 sub add_status {
305     my ( $self, $data ) = @_;
306 dpavlin 519 push @status, $data;
307 dpavlin 507 }
308    
309 dpavlin 519 sub clean_status {
310     @status = ();
311     }
312    
313     sub status_parts {
314     warn "## status parts ", dump( map { keys %$_ } @status );
315     }
316    
317 dpavlin 518 sub DEMOLISH {
318     my ( $self ) = @_;
319 dpavlin 519 cluck "## DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status;
320 dpavlin 518 }
321    
322 dpavlin 524 sub add_icon {
323     my $self = shift;
324     my $icon = ref($self);
325     $icon = $self->class if $self->can('class');
326     $icon =~ s{::}{/}g;
327    
328     my $icon_path = "static/icons/$icon.png";
329    
330     if ( -e $icon_path ) {
331     $self->add_head( qq|<link rel="icon" type="image/png" href="/$icon_path" />| );
332     warn "# using icon $icon_path";
333    
334     # FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work!
335     my $ico_path = $icon_path;
336     $ico_path =~ s{png$}{ico};
337     if ( ! -e $ico_path ) {
338     system "convert $icon_path $ico_path";
339     warn "# convert $icon_path $ico_path : $@";
340     }
341     $self->add_head( qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path" />| ) if -e $ico_path;
342    
343     } else {
344     warn "can't find $icon_path";
345     }
346     }
347    
348 dpavlin 100 1;

  ViewVC Help
Powered by ViewVC 1.1.26