/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (show annotations)
Fri Nov 28 00:30:03 2008 UTC (15 years, 4 months ago) by dpavlin
Original Path: trunk/lib/Frey/Web.pm
File size: 11639 byte(s)
first try at refactoring

- remove some of Moose sugar and turn to our which is less code for head
- add_css for easy add of css snippets (tracked with source)
- add_status can now append
- clean_status cleans head
- added some pod
- cleanup backtrace which goes to log
1 package Frey::Web;
2 use Moose::Role;
3
4 with 'Frey::Session';
5
6 use Frey::Types;
7
8 #use Continuity::Widget::DomNode;
9 use Data::Dump qw/dump/;
10 use Carp qw/confess cluck/;
11 use File::Slurp;
12
13 use Frey::Bookmarklet;
14 use Frey::ClassBrowser;
15 use Frey::INC;
16
17 use Frey::SVK;
18
19 our @head = ( 'static/frey.css' );
20
21 has 'request_url' => (
22 is => 'rw',
23 isa => 'Uri', coerce => 1,
24 default => '/',
25 );
26
27 has 'title' => (
28 is => 'rw',
29 isa => 'Str',
30 lazy => 1,
31 default => sub {
32 my ($self) = @_;
33 ref($self);
34 },
35 );
36
37 has 'content_type' => (
38 is => 'rw',
39 isa => 'Str',
40 default => 'text/html',
41 documentation => 'Content-type header',
42 );
43
44 has 'dump_max_bytes' => (
45 is => 'rw',
46 isa => 'Int',
47 default => 4096,
48 documentation => 'maximum dump size sent to browser before truncation',
49 );
50
51 has 'inline_smaller_than' => (
52 is => 'rw',
53 isa => 'Int',
54 default => 10240,
55 documentation => 'inline JavaScript and CSS to reduce round-trips',
56 );
57
58 has 'html_dump_width' => (
59 documentation => 'crop longer lines in dumps',
60 is => 'rw',
61 isa => 'Int',
62 # required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them
63 default => 128,
64 );
65
66 my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
67 my $escape_re = join '|' => keys %escape;
68
69 sub html_escape {
70 my ( $self, $html ) = @_;
71 $html =~ s/($escape_re)/$escape{$1}/g;
72 return $html;
73 }
74
75 sub html_dump {
76 my ( $self, $dump ) = @_;
77 $dump = dump( $dump ) if ref($dump);
78 my $width = $self->html_dump_width;
79 $dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs;
80 $dump = $self->html_escape( $dump );
81 $dump =~ s{\Q...\E}{&hellip;}gs;
82 return "<code>$dump</code>";
83 }
84
85 sub popup { my $self = shift; $self->popup_dropdown('popup', @_); }
86 sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }
87
88 our $re_html = qr{<(?:!--.+?--|(\w+).+?/\1)>}s; # relaxed html check for one semi-valid tag
89
90 sub popup_dropdown {
91 my ( $self, $type, $name, $content, $full ) = @_;
92
93 $content = $self->html_dump($content) if ref $content;
94
95 $content = qq|<span>$content</span>| unless $content =~ m{^\s*<(span|a|code).+?/\1>\s*};
96
97 warn "## $type [$name] = ", length( $content ), " bytes" if $self->debug;
98
99 if ( $name =~ m{::} && $name !~ $re_html ) {
100 return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|;
101 } else {
102 return qq|<span class="frey-$type">$name $content</span>\n|;
103 }
104 }
105
106 sub _inline_path {
107 my ( $self, $path ) = @_;
108 -s $path < $self->inline_smaller_than;
109 }
110
111 sub _head_html {
112 my $self = shift;
113 my $out = '';
114 foreach my $path ( @head ) {
115 $path =~ s!^/!!;
116 if ( $path =~ m/\.js$/ ) {
117 $out .= $self->_inline_path( $path ) ?
118 qq|<!-- $path --><script type="text/javascript">\n| . read_file($path) . qq|\n</script>| :
119 qq|<script type="text/javascript" src="/$path"></script>|;
120 } elsif ( $path =~ m/\.css$/ ) {
121 $out .= $self->_inline_path( $path ) ?
122 qq|<!-- $path --><style type="text/css">\n| . read_file( $path ) . qq|\n</style>| :
123 qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
124 } elsif ( $path =~ m{<.+>}s ) {
125 $out .= $path;
126 } else {
127 confess "don't know how to render $path";
128 }
129 $out .= "\n";
130 }
131 return $out;
132 }
133
134 =head2 add_head
135
136 $o->add_head( 'path/to/external.js' );
137
138 my $size = $o->add_head( 'path/to/external.css' );
139
140 $o->add_head( '<!-- html content -->' );
141
142 =cut
143
144 sub add_head {
145 my ( $self, $path ) = @_;
146 return if ! defined $path || $path eq '';
147 $path =~ s!^/!!;
148
149 if ( $path =~ $re_html ) {
150 push @head, $path;
151 } elsif ( -e $path ) {
152 if ( $path =~ m/\.(?:js|css)$/ ) {
153 push @head, $path;
154 } else {
155 confess "can't add_head( $path ) it's not js or css";
156 }
157 return -s $path;
158 } else {
159 confess "can't find $path: $!";
160 }
161
162 }
163
164 sub add_css {
165 my ($self,$css) = @_;
166 my ( $package, $path, $line ) = caller;
167 $self->add_head( qq|
168 <style type="text/css">
169 /* via package $package at $path line $line */
170 $css
171 </style>
172 | );
173 }
174
175 our $reload_counter = 0;
176
177
178 =head2 page
179
180 $self->page(
181 title => 'page title',
182 head => '<!-- optional head markup -->',
183 body => '<b>Page Body</b>',
184 );
185
186 =cut
187
188 our @status;
189 sub status { @status };
190
191 our $icon_html;
192
193 sub page {
194 my $self = shift;
195 my $a = {@_};
196
197 warn "## page ",dump($a);
198
199 $reload_counter++;
200
201 my $status_line = '';
202
203 unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup };
204 # unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup };
205 unshift @status, { 'INC' => Frey::INC->new->as_markup };
206
207 foreach my $part ( @status ) {
208 foreach my $name ( keys %$part ) {
209 $status_line .= $self->popup( $name, $part->{$name} );
210 }
211 }
212
213 my $url = $self->request_url;
214 $url =~ s{\?reload=\d+}{};
215
216 my $body = $a->{body};
217 $body ||= $self->as_markup if $self->can('as_markup');
218 if ( $self->content_type !~ m{html} ) {
219 warn "# return only $self body ", $self->content_type;
220 return $body
221 } elsif ( ! defined $body ) {
222 warn "# no body";
223 $body = '<!-- no body -->';
224 }
225
226 $status_line .= $self->warnings_html;
227
228 my ($exit,$description) = ('exit','stop server');
229 ($exit,$description) = ('restart','restart server')
230 if $ENV{FREY_RESTART}; # tune labels on exit link
231
232 my $right =
233 qq|
234 <span class="right">
235 <a title="reload $url" href="/reload$url">reload</a>
236 <a title="$description" href="/exit$url">$exit</a>
237 </span>
238 |;
239
240 my $svk = Frey::SVK->new;
241 my $info = $svk->info;
242 my $revision = $svk->info->{Revision} || '';
243 $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};
244
245 $self->add_icon unless $icon_html;
246
247 my $html = join("\n",
248 qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
249 $self->_head_html,
250 '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',
251 '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
252 ( $icon_html || '<!-- no icon -->' ),
253 ( $a->{head} || '' ),
254 qq|
255 </head><body>
256 $body
257 <div class="frey-status-line">
258 <a href="/">Frey</a> $Frey::VERSION $revision
259 $status_line
260 $right
261 </div>
262 </body></html>
263 |,
264 );
265
266 warn "## >>> page ",length($html), " bytes\n" if $self->debug;
267
268 return $html;
269 }
270
271 =head2 editor
272
273 Create HTML editor link with optional line and title
274
275 my $html = $self->editor( $class, $line, $title );
276
277 =cut
278
279 sub editor {
280 my ( $self, $class, $line, $title ) = @_;
281 confess "need class" unless $class;
282 if ( ! defined $title ) {
283 $title = "edit $class";
284 $title .= " line $line" if $line;
285 }
286 $line ||= 1;
287 qq|<a target="editor" href="/editor+$class+$line"| .
288 ( $title ? qq| title="$title"| : '' ) .
289 qq|>$class</a>|;
290 }
291
292 =head2 editor_links
293
294 Create HTML links to editor for perl error message
295
296 my $html = $self->editor_links( $error )
297
298 =cut
299
300 sub editor_links {
301 my ( $self, $error ) = @_;
302
303 $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
304 {at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;
305
306 $error =~ s{(via package ")([\w:]+)(")}
307 {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
308
309 return $error;
310 }
311
312 sub error {
313 my $self = shift;
314 my $error = join(" ", @_);
315
316 my @backtrace = $self->backtrace;
317 $error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace;
318
319 warn "ERROR: $error\n";
320 return
321 qq|<pre class="frey-error">|
322 . $self->editor_links( $error ) .
323 qq|</pre>|
324 ;
325 }
326
327 =head1 Status line
328
329 =head2 add_status
330
331 $self->add_status( name => { some => 'data' } );
332
333 $self->add_status( "append to last status popup" );
334
335 =cut
336
337 sub add_status {
338 my ( $self, $data ) = @_;
339 if ( ref($data) ) {
340 push @status, $data;
341 } else {
342 $status[ $#status ]->{added} = $data;
343 }
344 }
345
346 =head2 clean_status
347
348 Called at beginning of each request
349
350 $self->clean_status;
351
352 =cut
353
354 sub clean_status {
355 my ($self) = shift;
356 @head = ();
357 @status = ();
358 $icon_html = '';
359 }
360
361 =head2 status_parts
362
363 Dump all status line parts
364
365 $self->status_parts
366
367 =cut
368
369 sub status_parts {
370 warn "## status parts ", dump( map { keys %$_ } @status );
371 }
372
373 =for debug
374
375 sub DEMOLISH {
376 my ( $self ) = @_;
377 warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status;
378 }
379
380 =cut
381
382 =head2 add_icon
383
384 Frey::Foo->add_icon; # /static/icons/Frey/Foo.png
385 Frey::Foo->add_icon('warning'); # /static/icons/Frey/Foo/warning.png
386
387 =cut
388
389 sub icon_path {
390 my ($self,$class,$variant) = @_;
391 my $icon = $class;
392 $icon =~ s{::}{/}g;
393 $icon .= "/$variant" if $variant;
394 my $path = 'static/icons/' . $icon . '.png';
395 if ( -e $path ) {
396 warn "# $class from $self icon_path $path" if $self->debug;
397 return $path;
398 } else {
399 $self->TODO( "add $path icon for $class" );
400 return undef;
401 }
402 }
403
404 sub add_icon {
405 my ($self,$variant) = @_;
406
407 my $class = ref($self);
408 $class = $self->class if $self->can('class');
409 my $icon_path = $self->icon_path( $class, $variant ) || return;
410
411 $icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|;
412 warn "# using icon $icon_path";
413
414 =for later
415
416 # FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work!
417 my $ico_path = $icon_path;
418 $ico_path =~ s{png$}{ico};
419 if ( ! -e $ico_path ) {
420 system "convert $icon_path $ico_path";
421 warn "# convert $icon_path $ico_path : $@";
422 }
423 $icon_html .= qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path">| if -e $ico_path;
424
425 =cut
426
427 }
428
429 my $warn_colors = {
430 '#' => '#444',
431 '##' => '#888',
432 };
433
434 my $multiline_markers = {
435 '(' => ')',
436 '{' => '}',
437 '[' => ']',
438 '"' => '"',
439 };
440
441 my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']';
442 warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re";
443
444 sub log_path {
445 $Frey::Bootstrap::log_path || warn "no log_path?";
446 }
447
448 sub warnings_html {
449 my ($self,$level) = shift;
450 $level ||= $self->debug,
451 my $path = $self->log_path;
452
453 my $warnings;
454 my $line = 0;
455 my $multiline_end;
456
457 open(my $log, '<', $path) || die "can't open $path: $!";
458 while(<$log>) {
459 chomp;
460 $line++;
461
462 my $style = '';
463
464 if ( $multiline_end ) {
465 if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) {
466 # warn "## $line end of $multiline_end in '$_'\n";
467 undef $multiline_end;
468 } else {
469 # warn "## $line skipped\n";
470 }
471 } elsif ( m{^(#*)\s+} ) {
472 my $l = $1 ? length($1) : 0;
473 if ( $l > $level ) {
474 undef $multiline_end;
475 $multiline_end = $multiline_markers->{$1} if m{($multiline_re)$};
476 # warn "## $line start $1 .. $multiline_end level $l > $level for '$_'\n" if $multiline_end;
477 next;
478 }
479
480 $style = $warn_colors->{$1}
481 ? ' style="color:' . $warn_colors->{$1} . '"'
482 : '';
483
484 my $msg = $self->html_escape( $_ );
485 my $spacer = ' ';
486 if ( length($msg) > $self->html_dump_width ) {
487 $msg = substr( $msg, 0, $self->html_dump_width );
488 $spacer = '&hellip;'
489 }
490 $msg =~ s{^\s}{&nbsp;}g;
491 $warnings .= qq|<tt$style>$msg</tt>$spacer<small><a target="editor" href="/editor+$path+$line">+$line</a></small><br/>|;
492 # FIXME <tt> should be <code> but CSS hates me
493 }
494 }
495 close($log) || die "can't close $path: $!";
496
497 return
498 qq|<span class="frey-popup"><a target="editor" href="/editor+$path+$line" title="open $path level $level">warn</a><span>|
499 . $self->editor_links( $warnings )
500 . qq|</span></span>|
501 ;
502 }
503
504
505 =head2 backtrace
506
507 Show backtrace with links to editor
508
509 my @backtrace = $self->backtrace;
510
511 =cut
512
513 sub backtrace {
514 my ($self) = @_;
515
516 my @backtrace;
517 foreach ( 0 .. 5 ) {
518 my (
519 $package,$path,$line
520 # subroutine hasargs
521 # wantarray evaltext is_require
522 # hints bitmask hinthash
523 ) = caller($_) or last;
524
525 push @backtrace,
526 qq|via package $package at $path line $line|;
527 }
528 warn "# backtrace: ", dump( @backtrace ) if @backtrace;
529 return @backtrace;
530 }
531
532 1;

  ViewVC Help
Powered by ViewVC 1.1.26