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

  ViewVC Help
Powered by ViewVC 1.1.26