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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 477 by dpavlin, Thu Nov 20 13:57:38 2008 UTC revision 588 by dpavlin, Fri Nov 28 15:07:03 2008 UTC
# Line 1  Line 1 
1  package Frey::Web;  package Frey::Web;
2  use Moose::Role;  use Moose::Role;
3    
4  with 'Frey::Backtrace';  with 'Frey::Session';
5    
6  use Frey::Types;  use Frey::Types;
7    
8  use Continuity::Widget::DomNode;  #use Continuity::Widget::DomNode;
9  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
10  use Carp qw/confess/;  use Carp qw/confess cluck/;
11  use File::Slurp;  use File::Slurp;
12    
13  use Frey::Bookmarklet;  use Frey::Bookmarklet;
14  use Frey::ClassBrowser;  use Frey::ClassBrowser;
15    use Frey::INC;
16    
17  has 'head' => (  use Frey::SVK;
         is => 'rw',  
         isa => 'ArrayRef[Str]',  
         default => sub { [ 'static/frey.css' ] },  
 );  
18    
19  has 'status' => (  our @head;
20          is => 'rw',  sub head { @head }
         isa => 'ArrayRef[HashRef[Str]]',  
         lazy => 1,  
         default => sub { [  
                 { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup },  
                 { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },  
         ] },  
 );  
21    
22  has 'request_url' => (  has 'request_url' => (
23          is => 'rw',          is => 'rw',
# Line 56  has 'dump_max_bytes' => ( Line 46  has 'dump_max_bytes' => (
46          is => 'rw',          is => 'rw',
47          isa => 'Int',          isa => 'Int',
48          default => 4096,          default => 4096,
49          documentation => 'Maximum dump size sent to browser before truncation',          documentation => 'maximum dump size sent to browser before truncation',
50  );  );
51    
 =head2 inline_smaller_than  
   
 Inline JavaScript and CSS smaller than this size into page reducing  
 round-trips to server.  
   
 =cut  
   
52  has 'inline_smaller_than' => (  has 'inline_smaller_than' => (
53          is => 'rw',          is => 'rw',
54          isa => 'Int',          isa => 'Int',
55          default => 10240,          default => 10240,
56            documentation => 'inline JavaScript and CSS to reduce round-trips',
57    );
58    
59    has 'html_dump_width' => (
60            documentation => 'crop longer lines in dumps',
61            is => 'rw',
62            isa => 'Int',
63    #       required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them
64            default => 128,
65  );  );
66    
67  sub dom2html {  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
68  #       warn "## dom2html ",dump( @_ );  my $escape_re  = join '|' => keys %escape;
69          return Continuity::Widget::DomNode->create( @_ )->to_string;  
70    sub html_escape {
71            my ( $self, $html ) = @_;
72            $html =~ s/($escape_re)/$escape{$1}/g;
73            return $html;
74    }
75    
76    sub html_dump {
77            my ( $self, $dump ) = @_;
78            $dump = dump( $dump ) if ref($dump);
79            my $width = $self->html_dump_width;
80            $dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs;
81            $dump = $self->html_escape( $dump );
82            $dump =~ s{\Q...\E}{&hellip;}gs;
83    #       $dump =~ $self->editor_links( $dump ); # FIXME include this
84            return "<code>$dump</code>";
85    }
86    
87    sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }
88    sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }
89    
90    our $re_html = qr{<(?:!--.+?--|(\w+).+?/\1)>}s; # relaxed html check for one semi-valid tag
91    
92    sub popup_dropdown {
93            my ( $self, $type, $name, $content, $full ) = @_;
94    
95            $content = $self->html_dump($content) if ref $content;
96    
97            $content = qq|<span>$content</span>| unless $content =~ m{^\s*<(span|a|code).+?/\1>\s*};
98    
99            $content =~ s{<span>(<code>[^<]+</code>)</span>}{$1} && $self->TODO("code wrapped in span");
100    
101            warn "## $type [$name] = ", length( $content ), " bytes" if $self->debug;
102    
103            if ( $name =~ m{::} && $name !~ $re_html ) {
104                    return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|;
105            } elsif ( $name =~ s{^\s*(<a)\s+}{$1 class="frey-$type"} ) {
106                    return qq|$name $content\n|;
107            } else {
108                    return qq|<span class="frey-$type">$name $content</span>\n|;
109            }
110  }  }
111    
112  sub _inline_path {  sub _inline_path {
# Line 85  sub _inline_path { Line 117  sub _inline_path {
117  sub _head_html {  sub _head_html {
118          my $self = shift;          my $self = shift;
119          my $out = '';          my $out = '';
120          foreach my $path ( @{ $self->head } ) {          foreach my $path ( @head ) {
121                  $path =~ s!^/!!;                  $path =~ s!^/!!;
122                  if ( $path =~ m/\.js$/ ) {                  if ( $path =~ m/\.js$/ ) {
123                          $out .= $self->_inline_path( $path ) ?                          $out .= $self->_inline_path( $path ) ?
# Line 120  sub add_head { Line 152  sub add_head {
152          return if ! defined $path || $path eq '';          return if ! defined $path || $path eq '';
153          $path =~ s!^/!!;          $path =~ s!^/!!;
154    
155          if ( $path =~ m{<.*>}s ) {          if ( $path =~ $re_html ) {
156                  push @{ $self->head }, $path;                  push @head, $path;
157          } elsif ( -e $path ) {          } elsif ( -e $path ) {
158                  if ( $path =~ m/\.(?:js|css)$/ ) {                  if ( $path =~ m/\.(?:js|css)$/ ) {
159                          push @{ $self->head }, $path;                          push @head, $path;
160                  } else {                  } else {
161                          confess "can't add_head( $path ) it's not js or css";                          confess "can't add_head( $path ) it's not js or css";
162                  }                  }
# Line 135  sub add_head { Line 167  sub add_head {
167    
168  }  }
169    
170    sub add_css {
171            my ($self,$css) = @_;
172            my ( $package, $path, $line ) = caller;
173            $self->add_head( qq|
174            <style type="text/css">
175            /* via $package at $path line $line */
176            $css
177            </style>
178            | );
179    }
180    
181  our $reload_counter = 0;  our $reload_counter = 0;
182    
183    
# Line 148  our $reload_counter = 0; Line 191  our $reload_counter = 0;
191    
192  =cut  =cut
193    
194    our @status;
195    sub status { @status };
196    
197    our $icon_html;
198    
199  sub page {  sub page {
200          my $self = shift;          my $self = shift;
201          my $a = {@_};          my $a = {@_};
202    
203            warn "## page ",dump($a);
204    
205          $reload_counter++;          $reload_counter++;
206    
207          my $status_line = '';          my $status_line = '';
208          foreach my $part ( @{ $self->status } ) {  
209                  if ( ref($part) ne 'HASH' ) {          foreach my $part ( @status ) {
                         warn "part not hash ",dump( $part ) ;  
                         #$self->status( $part );  
                         next;  
                 }  
210                  foreach my $name ( keys %$part ) {                  foreach my $name ( keys %$part ) {
211                          my $content = $part->{$name};                          $status_line .= $self->popup( $name, $part->{$name} );
                         if ( ref($content) ) {  
                                 $content = '<code>' . dump($content) . '</code>';  
                                 my $l = length($content);  
                                 $content = qq|<span>$l bytes</span>| if $l > $self->dump_max_bytes;  
                         } else {  
                                 $content = qq|<span>$content</span>|;  
                         }  
                         warn "### part [$name] = ", length( $content ), " bytes" if $self->debug;  
                         $status_line .= qq|<span class="frey-popup">$name $content</span>\n|;  
212                  }                  }
213          }          }
214    
# Line 188  sub page { Line 225  sub page {
225                  $body = '<!-- no body -->';                  $body = '<!-- no body -->';
226          }          }
227    
228          $status_line          $status_line .= $self->warnings_html;
                 .= qq|<span class="frey-popup">warn<code>|  
                 . $self->editor_links( join("", $self->warnings ) )  
                 . qq|</code></span>|  
                 if $self->warnings;  
229    
230          my      ($exit,$description) = ('exit','stop server');          my      ($exit,$description) = ('exit','stop server');
231                  ($exit,$description) = ('restart','restart server')                  ($exit,$description) = ('restart','restart server')
# Line 201  sub page { Line 234  sub page {
234          my $right =          my $right =
235                  qq|                  qq|
236                          <span class="right">                          <span class="right">
237                          <a title="reload"  href="/reload$url"><code>$url</code></a>                          <a title="reload $url"  href="/reload$url">reload</a>
238                          <a title="$description" href="/exit$url">$exit</a>                          <a title="$description" href="/exit$url">$exit</a>
239                          </span>                          </span>
240                  |;                  |;
241    
242            my $svk = Frey::SVK->new;
243            my $info = $svk->info;
244            my $revision = $svk->info->{Revision} || '';
245            $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};
246    
247            $self->add_icon unless $icon_html;
248    
249          my $html = join("\n",          my $html = join("\n",
250                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
251                  $self->_head_html,                  $self->_head_html,
252                  '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',                  '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',
253                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
254                    ( $icon_html || '<!-- no icon -->' ),
255                  ( $a->{head} || '' ),                  ( $a->{head} || '' ),
256                  qq|                  qq|
257                  </head><body>                  </head><body>
258                  $body                  $body
259                  <div class="frey-status-line">                  <div class="frey-status-line">
260                          <a href="/">Frey</a> $Frey::VERSION                          <a href="/">Frey</a> $Frey::VERSION $revision
261                          $status_line                          $status_line
262                          $right                          $right
263                  </div>                  </div>
# Line 229  sub page { Line 270  sub page {
270          return $html;          return $html;
271  }  }
272    
273    =head2 editor
274    
275    Create HTML editor link with optional line and title
276    
277      my $html = $self->editor( $class, $line, $title );
278    
279    =cut
280    
281    sub editor {
282            my ( $self, $class, $line, $title ) = @_;
283            confess "need class" unless $class;
284            if ( ! defined $title ) {
285                    $title  = "edit $class";
286                    $title .= " line $line" if $line;
287            }
288            $line  ||= 1;
289            qq|<a target="editor" href="/editor+$class+$line"| .
290            ( $title ? qq| title="$title"| : '' ) .
291            qq|>$class</a>|;
292    }
293    
294    =head2 editor_links
295    
296    Create HTML links to editor for perl error message
297    
298      my $html = $self->editor_links( $error )
299    
300    =cut
301    
302  sub editor_links {  sub editor_links {
303          my ( $self, $error ) = @_;          my ( $self, $error ) = @_;
304    
305          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
306                  {at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;                  {at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;
307    
308          $error =~ s{(via package ")([\w:]+)(")}          $error =~ s{(via (?:package) "?)([\w:]+)("?)}
309                  {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;                  {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
310    
311          return $error;          return $error;
# Line 256  sub error { Line 326  sub error {
326                  ;                  ;
327  }  }
328    
329    =head1 Status line
330    
331    =head2 add_status
332    
333      $self->add_status( { name => { some => 'data' } } );
334    
335      $self->add_status( "append to last status popup" );
336    
337    =cut
338    
339    sub add_status {
340            my ( $self, $data ) = @_;
341            push @status, { 'X' => [ $self->backtrace ] };
342            if ( ref($data) ) {
343                    push @status, $data;
344            } else {
345                    if ( defined $status[ $#status ] ) {
346                            $status[ $#status ]->{ '+' } = $data;
347                    } else {
348                            push @status, { '+' => $data };
349                    }
350            }
351    }
352    
353    =head2 clean_status
354    
355    Called at beginning of each request
356    
357      $self->clean_status;
358    
359    =cut
360    
361    sub clean_status {
362            my ($self) = shift;
363            @head = ( 'static/frey.css' );
364            @status = (
365                    { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup },
366                    { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },
367                    { 'INC' => Frey::INC->new->as_markup },
368            );
369            $icon_html = '';
370    }
371    
372    =head2 status_parts
373    
374    Dump all status line parts
375    
376      $self->status_parts
377    
378    =cut
379    
380    sub status_parts {
381            warn "## status parts ", dump( map { keys %$_ } @status );
382    }
383    
384    =for debug
385    
386    sub DEMOLISH {
387            my ( $self ) = @_;
388            warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status;
389    }
390    
391    =cut
392    
393    =head2 add_icon
394    
395      Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png
396      Frey::Foo->add_icon('warning'); # /static/icons/Frey/Foo/warning.png
397    
398    =cut
399    
400    sub icon_path {
401            my ($self,$class,$variant) = @_;
402            my $icon = $class;
403            $icon =~ s{::}{/}g;
404            $icon .= "/$variant" if $variant;
405            my $path = 'static/icons/' . $icon . '.png';
406            if ( -e $path ) {
407                    warn "# $class from $self icon_path $path" if $self->debug;
408                    return $path;
409            } else {
410                    $self->TODO( "add $path icon for $class" );
411                    return undef;
412            }
413    }
414    
415    sub add_icon {
416            my ($self,$variant) = @_;
417    
418            my $class = ref($self);
419            $class = $self->class if $self->can('class');
420            my $icon_path = $self->icon_path( $class, $variant ) || return;
421    
422            $icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|;
423            warn "# using icon $icon_path";
424    
425    =for later
426    
427            # FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work!
428            my $ico_path = $icon_path;
429            $ico_path =~ s{png$}{ico};
430            if ( ! -e $ico_path ) {
431                    system "convert $icon_path $ico_path";
432                    warn "# convert $icon_path $ico_path : $@";
433            }
434            $icon_html .= qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path">| if -e $ico_path;
435    
436    =cut
437    
438    }
439    
440    my $warn_colors = {
441            '#'  => '#444',
442            '##' => '#888',
443    };
444    
445    my $multiline_markers = {
446            '(' => ')',
447            '{' => '}',
448            '[' => ']',
449            '"' => '"',
450    };
451    
452    my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']';
453    warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re";
454    
455    sub log_path {
456            $Frey::Bootstrap::log_path || warn "no log_path?";
457    }
458    
459    sub warnings_html {
460            my ($self,$level) = shift;
461            $level ||= $self->debug,
462            my $path = $self->log_path;
463    
464            my $max = 50;
465            my $pos = 0;
466            my @warnings = ( '' x $max ); # XXX circualar buffer for 50 lines
467            my $line = 0;
468            my $multiline_end;
469    
470            open(my $log, '<', $path) || die "can't open $path: $!";
471            while(<$log>) {
472                    chomp;
473                    $line++;
474    
475                    my $style = '';
476    
477    =for filter
478    
479                    if ( $multiline_end ) {
480                            if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) {
481    #                               warn "## $line end of $multiline_end in '$_'\n";
482                                    undef $multiline_end;
483                            } else {
484    #                               warn "## $line skipped\n";
485                            }
486                    } elsif ( m{^(#*)\s+} ) {
487                            my $l = $1 ? length($1) : 0;
488                            if ( $l > $level ) {
489                                    undef $multiline_end;
490                                    $multiline_end = $multiline_markers->{$1} if m{($multiline_re)$};
491    #                               warn "## $line start $1 .. $multiline_end level $l > $level for '$_'\n" if $multiline_end;
492                                    next;
493                            }
494    
495    =cut
496                    if ( m{^(#*)\s+} ) { # FIXME
497    
498                            $style = $warn_colors->{$1}
499                                    ? ' style="color:' . $warn_colors->{$1} . '"'
500                                    : '';
501    
502                            my $msg = $self->html_escape( $_ );
503                            my $spacer = ' ';
504                            if ( length($msg) > $self->html_dump_width ) {
505                                    $msg = substr( $msg, 0, $self->html_dump_width );
506                                    $spacer = '&hellip;'
507                            }
508                            $warnings[ $pos++ % $max ]
509                                    = $msg
510    #                               = ( $style ? qq|<span$style>$msg</span>| : $msg )
511                                    . $spacer
512                                    . qq|<a target="editor" href="/editor+$path+$line" style="float: right;">+$line</a><br/>|;
513                            # FIXME <tt> should be <code> but CSS hates me
514                    }
515            }
516            close($log) || die "can't close $path: $!";
517    
518            my $size = -s $path;
519    
520            my $warnings = join('',
521                    map { $warnings[ ( $pos + $_ ) % $max ] || '' } 0 .. $max
522            );
523    
524            my $s = length($warnings);
525    
526            return
527                    # need to wrap into span so we can have links in warnings
528                      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>|
529                    . $warnings
530    #               . $self->editor_links( $warnings )
531                    . qq|</code></span></a>|
532                    ;
533    }
534    
535    
536    =head2 backtrace
537    
538    Show backtrace with links to editor
539    
540      my @backtrace = $self->backtrace;
541    
542    =cut
543    
544    sub backtrace {
545            my ($self) = @_;
546    
547            my @backtrace;
548            foreach ( 0 .. 5 ) {
549                    my (
550                            $package,$path,$line
551                            # subroutine hasargs
552                            # wantarray evaltext is_require
553                            # hints bitmask hinthash
554                    ) = caller($_) or last;
555    
556                    push @backtrace,
557                            qq|via $package at $path line $line|;
558            }
559            warn "# backtrace: ", dump( @backtrace ) if @backtrace;
560            return @backtrace;
561    }
562    
563  1;  1;

Legend:
Removed from v.477  
changed lines
  Added in v.588

  ViewVC Help
Powered by ViewVC 1.1.26