/[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 538 by dpavlin, Wed Nov 26 17:36:02 2008 UTC revision 596 by dpavlin, Fri Nov 28 17:20:05 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 cluck/;  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  use Frey::SVK;  use Frey::SVK;
18    
19  has 'head' => (  our @head;
20          is => 'rw',  sub head { @head }
         isa => 'ArrayRef[Str]',  
         default => sub { [ 'static/frey.css' ] },  
 );  
21    
22  has 'request_url' => (  has 'request_url' => (
23          is => 'rw',          is => 'rw',
# Line 47  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    
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',          documentation => 'inline JavaScript and CSS to reduce round-trips',
57  );  );
58    
59  has 'chop_warning' => (  has 'html_dump_width' => (
60            documentation => 'crop longer lines in dumps',
61          is => 'rw',          is => 'rw',
62          isa => 'Int',          isa => 'Int',
63          default => 80,  #       required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them
64          documentation => 'Crop lines longer',          default => 128,
65  );  );
66    
67  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
# Line 74  sub html_escape { Line 74  sub html_escape {
74  }  }
75    
76  sub html_dump {  sub html_dump {
77          my $self = shift;          my ( $self, $dump ) = @_;
78          $self->html_escape( dump( @_ ) );          $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 dom2html {  sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }
88  #       warn "## dom2html ",dump( @_ );  sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }
89          return Continuity::Widget::DomNode->create( @_ )->to_string;  
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; # FIXME
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 91  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 126  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 141  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 159  sub status { @status }; Line 196  sub status { @status };
196    
197  our $icon_html;  our $icon_html;
198    
 sub popup {  
         my ( $self, $name, $content, $full ) = @_;  
   
         if ( ref($content) ) {  
                 $content = '<code>' . dump($content) . '</code>';  
                 my $l = length($content);  
                 $content = qq|<span>$l bytes</span>| if ! $full && $l > $self->dump_max_bytes;  
         } else {  
                 $content = qq|<span>$content</span>|;  
         }  
   
         warn "## popup [$name] = ", length( $content ), " bytes" if $self->debug;  
         return qq|<span class="frey-popup">$name $content</span>\n|;  
 }  
   
199  sub page {  sub page {
200          my $self = shift;          my $self = shift;
201          my $a = {@_};          my $a = {@_};
# Line 184  sub page { Line 206  sub page {
206    
207          my $status_line = '';          my $status_line = '';
208    
         unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup };  
 #       unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup };  
   
209          foreach my $part ( @status ) {          foreach my $part ( @status ) {
210                  foreach my $name ( keys %$part ) {                  foreach my $name ( keys %$part ) {
211                          $status_line .= $self->popup( $name, $part->{$name} );                          $status_line .= $self->popup( $name, $part->{$name} );
# Line 197  sub page { Line 216  sub page {
216          $url =~ s{\?reload=\d+}{};          $url =~ s{\?reload=\d+}{};
217    
218          my $body = $a->{body};          my $body = $a->{body};
219          $body ||= $self->as_markup if $self->can('as_markup');          if ( ! $body ) {
220                    my $run = $a->{run} || 'as_markup';
221                    warn "# no body, invoke $self->$run";
222                    $body = $self->$run;
223            }
224          if ( $self->content_type !~ m{html} ) {          if ( $self->content_type !~ m{html} ) {
225                  warn "# return only $self body ", $self->content_type;                  warn "# return only $self body ", $self->content_type;
226                  return $body                  return $body
# Line 208  sub page { Line 231  sub page {
231    
232          $status_line .= $self->warnings_html;          $status_line .= $self->warnings_html;
233    
         my $inc_html;  
         {  
                 my $inc;  
                 map {  
                         s{.pm$}{};  
                         my $class = $_;  
                         s[/][}->{]g;  
                         $class =~ s[/][::]g;  
                         eval '$inc->{' . $_ . '} = $class';  
                 } sort keys %INC;  
                 $inc_html = dump( $inc );  
                 $inc_html =~ s{\s+=>\s+\d+}{}gs;  
                 $inc_html =~ s{(['"]?)(\w+)\1\s+=>\s+(['"]?)([\w:]*\2)\3}{<a target="$4" href="/$4" title="$4">$2</a>}gs;  
                 $inc_html =~ s{\s+=>\s+}{ }gs;  
                 $inc_html =~ s{,}{}gs;  
         }  
   
         $status_line .= $self->popup( INC => "<small>$inc_html</small>" );  
   
234          my      ($exit,$description) = ('exit','stop server');          my      ($exit,$description) = ('exit','stop server');
235                  ($exit,$description) = ('restart','restart server')                  ($exit,$description) = ('restart','restart server')
236                  if $ENV{FREY_RESTART}; # tune labels on exit link                  if $ENV{FREY_RESTART}; # tune labels on exit link
# Line 239  sub page { Line 243  sub page {
243                          </span>                          </span>
244                  |;                  |;
245    
246          my $info = Frey::SVK->info;          my $svk = Frey::SVK->new;
247          my $revision = Frey::SVK->info->{Revision} || '';          my $info = $svk->info;
248            my $revision = $svk->info->{Revision} || '';
249          $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};          $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};
250    
251          $self->add_icon unless $icon_html;          $self->add_icon unless $icon_html;
# Line 301  Create HTML links to editor for perl err Line 306  Create HTML links to editor for perl err
306  sub editor_links {  sub editor_links {
307          my ( $self, $error ) = @_;          my ( $self, $error ) = @_;
308    
309    #       $error =~ s[(bless\({\s+.+?\s+},\s+)("[^"]+")(\) at)][<span class="frey-dropdown">$1<code>$2</code>$3</span>]gs; # FIXME insert bless hiding back
310    
311          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
312                  {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;
313    
314          $error =~ s{(via package ")([\w:]+)(")}          $error =~ s{(via (?:package) "?)([\w:]+)("?)}
315                  {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;                  {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
316    
317          return $error;          return $error;
# Line 325  sub error { Line 332  sub error {
332                  ;                  ;
333  }  }
334    
335    =head1 Status line
336    
337    =head2 add_status
338    
339      $self->add_status( { name => { some => 'data' } } );
340    
341      $self->add_status( "append to last status popup" );
342    
343    =cut
344    
345  sub add_status {  sub add_status {
346          my ( $self, $data ) = @_;          my ( $self, $data ) = @_;
347          push @status, $data;          push @status, { 'X' => [ $self->backtrace ] };
348            if ( ref($data) ) {
349                    push @status, $data;
350            } else {
351                    if ( defined $status[ $#status ] ) {
352                            $status[ $#status ]->{ '+' } = $data;
353                    } else {
354                            push @status, { '+' => $data };
355                    }
356            }
357  }  }
358    
359    =head2 clean_status
360    
361    Called at beginning of each request
362    
363      $self->clean_status;
364    
365    =cut
366    
367  sub clean_status {  sub clean_status {
368          @status = ();          my ($self) = shift;
369            @head = ( 'static/frey.css' );
370            @status = (
371                    { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup },
372                    { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },
373                    { 'INC' => Frey::INC->new->as_markup },
374            );
375          $icon_html = '';          $icon_html = '';
376  }  }
377    
378    =head2 status_parts
379    
380    Dump all status line parts
381    
382      $self->status_parts
383    
384    =cut
385    
386  sub status_parts {  sub status_parts {
387          warn "## status parts ", dump( map { keys %$_ } @status );          warn "## status parts ", dump( map { keys %$_ } @status );
388  }  }
389    
390    =for debug
391    
392  sub DEMOLISH {  sub DEMOLISH {
393          my ( $self ) = @_;          my ( $self ) = @_;
394          warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status;          warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status;
395  }  }
396    
397    =cut
398    
399  =head2 add_icon  =head2 add_icon
400    
401    Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png    Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png
# Line 358  sub icon_path { Line 410  sub icon_path {
410          $icon .= "/$variant" if $variant;          $icon .= "/$variant" if $variant;
411          my $path = 'static/icons/' . $icon . '.png';          my $path = 'static/icons/' . $icon . '.png';
412          if ( -e $path ) {          if ( -e $path ) {
413                  warn "# $class from $self icon_path $path";                  warn "# $class from $self icon_path $path" if $self->debug;
414                  return $path;                  return $path;
415          } else {          } else {
416                  warn "TODO: add $path icon for $class";                  $self->TODO( "add $path icon for $class" );
417                  return undef;                  return undef;
418          }          }
419  }  }
# Line 415  sub warnings_html { Line 467  sub warnings_html {
467          $level ||= $self->debug,          $level ||= $self->debug,
468          my $path = $self->log_path;          my $path = $self->log_path;
469    
470          my $warnings;          my $max = 50;
471            my $pos = 0;
472            my @warnings = ( '' x $max ); # XXX circualar buffer for 50 lines
473          my $line = 0;          my $line = 0;
474          my $multiline_end;          my $multiline_end;
475    
# Line 426  sub warnings_html { Line 480  sub warnings_html {
480    
481                  my $style = '';                  my $style = '';
482    
483    =for filter
484    
485                  if ( $multiline_end ) {                  if ( $multiline_end ) {
486                          if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) {                          if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) {
487  #                               warn "## $line end of $multiline_end in '$_'\n";  #                               warn "## $line end of $multiline_end in '$_'\n";
# Line 442  sub warnings_html { Line 498  sub warnings_html {
498                                  next;                                  next;
499                          }                          }
500    
501    =cut
502                    if ( m{^(#*)\s+} ) { # FIXME
503    
504                          $style = $warn_colors->{$1}                          $style = $warn_colors->{$1}
505                                  ? ' style="color:' . $warn_colors->{$1} . '"'                                  ? ' style="color:' . $warn_colors->{$1} . '"'
506                                  : '';                                  : '';
507    
508                          my $msg = $self->html_escape( $_ );                          my $msg = $self->html_escape( $_ );
509                          my $spacer = ' ';                          my $spacer = ' ';
510                          if ( length($msg) > $self->chop_warning ) {                          if ( length($msg) > $self->html_dump_width ) {
511                                  $msg = substr( $msg, 0, $self->chop_warning );                                  $msg = substr( $msg, 0, $self->html_dump_width );
512                                  $spacer = '&hellip;'                                  $spacer = '&hellip;'
513                          }                          }
514                          $msg =~ s{^\s}{&nbsp;}g;                          $warnings[ $pos++ % $max ]
515                          $warnings .= qq|<tt$style>$msg</tt>$spacer<small><a target="editor" href="/editor+$path+$line">+$line</a></small><br/>|;                                  = $msg
516    #                               = ( $style ? qq|<span$style>$msg</span>| : $msg )
517                                    . $spacer
518                                    . qq|<a target="editor" href="/editor+$path+$line" style="float: right;">+$line</a><br/>|;
519                          # FIXME <tt> should be <code> but CSS hates me                          # FIXME <tt> should be <code> but CSS hates me
520                  }                  }
521          }          }
522          close($log) || die "can't close $path: $!";          close($log) || die "can't close $path: $!";
523    
524            my $size = -s $path;
525    
526            my $warnings = join('',
527                    map { $warnings[ ( $pos + $_ ) % $max ] || '' } 0 .. $max
528            );
529    
530            my $s = length($warnings);
531    
532          return          return
533                    qq|<span class="frey-popup"><a target="editor" href="/editor+$path+$line" title="open $path level $level">warn</a><span>|                  # need to wrap into span so we can have links in warnings
534                  . $self->editor_links( $warnings )                    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>|
535                  . qq|</span></span>|                  . $warnings
536    #               . $self->editor_links( $warnings )
537                    . qq|</code></span></a>|
538                  ;                  ;
539  }  }
540    
541    
542    =head2 backtrace
543    
544    Show backtrace with links to editor
545    
546      my @backtrace = $self->backtrace;
547    
548    =cut
549    
550    sub backtrace {
551            my ($self) = @_;
552    
553            my @backtrace;
554            foreach ( 0 .. 5 ) {
555                    my (
556                            $package,$path,$line
557                            # subroutine hasargs
558                            # wantarray evaltext is_require
559                            # hints bitmask hinthash
560                    ) = caller($_) or last;
561    
562                    push @backtrace,
563                            qq|via $package at $path line $line|;
564            }
565            #warn "# backtrace: ", dump( @backtrace ) if @backtrace;
566            return @backtrace;
567    }
568    
569  1;  1;

Legend:
Removed from v.538  
changed lines
  Added in v.596

  ViewVC Help
Powered by ViewVC 1.1.26