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

  ViewVC Help
Powered by ViewVC 1.1.26