/[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 537 by dpavlin, Wed Nov 26 16:34:25 2008 UTC revision 611 by dpavlin, Fri Nov 28 23:34:26 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' => (  use Text::Tabs; # expand, unexpand
20          is => 'rw',  
21          isa => 'ArrayRef[Str]',  our @head;
22          default => sub { [ 'static/frey.css' ] },  sub head { @head }
 );  
23    
24  has 'request_url' => (  has 'request_url' => (
25          is => 'rw',          is => 'rw',
# Line 47  has 'dump_max_bytes' => ( Line 48  has 'dump_max_bytes' => (
48          is => 'rw',          is => 'rw',
49          isa => 'Int',          isa => 'Int',
50          default => 4096,          default => 4096,
51          documentation => 'Maximum dump size sent to browser before truncation',          documentation => 'maximum dump size sent to browser before truncation',
52  );  );
53    
54  has 'inline_smaller_than' => (  has 'inline_smaller_than' => (
55          is => 'rw',          is => 'rw',
56          isa => 'Int',          isa => 'Int',
57          default => 10240,          default => 10240,
58          documentation => 'Inline JavaScript and CSS to reduce round-trips',          documentation => 'inline JavaScript and CSS to reduce round-trips',
59  );  );
60    
61  has 'chop_warning' => (  has 'html_dump_width' => (
62            documentation => 'crop longer lines in dumps',
63          is => 'rw',          is => 'rw',
64          isa => 'Int',          isa => 'Int',
65          default => 80,  #       required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them
66          documentation => 'Crop lines longer',          default => 120,
67  );  );
68    
69  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
# Line 74  sub html_escape { Line 76  sub html_escape {
76  }  }
77    
78  sub html_dump {  sub html_dump {
79          my $self = shift;          my ( $self, $dump ) = @_;
80          $self->html_escape( dump( @_ ) );          $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 dom2html {  sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }
90  #       warn "## dom2html ",dump( @_ );  sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }
91          return Continuity::Widget::DomNode->create( @_ )->to_string;  
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 {  sub _inline_path {
# Line 91  sub _inline_path { Line 119  sub _inline_path {
119  sub _head_html {  sub _head_html {
120          my $self = shift;          my $self = shift;
121          my $out = '';          my $out = '';
122          foreach my $path ( @{ $self->head } ) {          foreach my $path ( @head ) {
123                  $path =~ s!^/!!;                  $path =~ s!^/!!;
124                  if ( $path =~ m/\.js$/ ) {                  if ( $path =~ m/\.js$/ ) {
125                          $out .= $self->_inline_path( $path ) ?                          $out .= $self->_inline_path( $path ) ?
126                                  qq|<!-- $path --><script type="text/javascript">\n| . read_file($path) . qq|\n</script>| :                                  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>|;                                  qq|<script type="text/javascript" src="/$path"></script>|;
128                  } elsif ( $path =~ m/\.css$/ ) {                  } elsif ( $path =~ m/\.css$/ ) {
129                          $out .= $self->_inline_path( $path ) ?                          $out .= $self->_inline_path( $path ) ?
130                                  qq|<!-- $path --><style type="text/css">\n| . read_file( $path ) . qq|\n</style>| :                                  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">|;                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
132                  } elsif ( $path =~ m{<.+>}s ) {                  } elsif ( $path =~ m{<.+>}s ) {
133                          $out .= $path;                          $out .= $path;
# Line 126  sub add_head { Line 154  sub add_head {
154          return if ! defined $path || $path eq '';          return if ! defined $path || $path eq '';
155          $path =~ s!^/!!;          $path =~ s!^/!!;
156    
157          if ( $path =~ m{<.*>}s ) {          if ( $path =~ $re_html ) {
158                  push @{ $self->head }, $path;                  push @head, $path;
159          } elsif ( -e $path ) {          } elsif ( -e $path ) {
160                  if ( $path =~ m/\.(?:js|css)$/ ) {                  if ( $path =~ m/\.(?:js|css)$/ ) {
161                          push @{ $self->head }, $path;                          push @head, $path;
162                  } else {                  } else {
163                          confess "can't add_head( $path ) it's not js or css";                          confess "can't add_head( $path ) it's not js or css";
164                  }                  }
# Line 141  sub add_head { Line 169  sub add_head {
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;  our $reload_counter = 0;
184    
185    
# Line 159  sub status { @status }; Line 198  sub status { @status };
198    
199  our $icon_html;  our $icon_html;
200    
 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|;  
 }  
   
201  sub page {  sub page {
202          my $self = shift;          my $self = shift;
203          my $a = {@_};          my $a = {@_};
# Line 184  sub page { Line 208  sub page {
208    
209          my $status_line = '';          my $status_line = '';
210    
         unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup };  
 #       unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup };  
   
211          foreach my $part ( @status ) {          foreach my $part ( @status ) {
212                  foreach my $name ( keys %$part ) {                  foreach my $name ( keys %$part ) {
213                          $status_line .= $self->popup( $name, $part->{$name} );                          $status_line .= $self->popup( $name, $part->{$name} );
# Line 197  sub page { Line 218  sub page {
218          $url =~ s{\?reload=\d+}{};          $url =~ s{\?reload=\d+}{};
219    
220          my $body = $a->{body};          my $body = $a->{body};
221          $body ||= $self->as_markup if $self->can('as_markup');          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} ) {          if ( $self->content_type !~ m{html} ) {
227                  warn "# return only $self body ", $self->content_type;                  warn "# return only $self body ", $self->content_type;
228                  return $body                  return $body
# Line 208  sub page { Line 233  sub page {
233    
234          $status_line .= $self->warnings_html;          $status_line .= $self->warnings_html;
235    
         $status_line .= $self->popup( INC => { %INC }, 1 );  
   
236          my      ($exit,$description) = ('exit','stop server');          my      ($exit,$description) = ('exit','stop server');
237                  ($exit,$description) = ('restart','restart server')                  ($exit,$description) = ('restart','restart server')
238                  if $ENV{FREY_RESTART}; # tune labels on exit link                  if $ENV{FREY_RESTART}; # tune labels on exit link
# Line 222  sub page { Line 245  sub page {
245                          </span>                          </span>
246                  |;                  |;
247    
248          my $info = Frey::SVK->info;          my $svk = Frey::SVK->new;
249          my $revision = Frey::SVK->info->{Revision} || '';          my $info = $svk->info;
250            my $revision = $svk->info->{Revision} || '';
251          $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};          $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};
252    
253          $self->add_icon unless $icon_html;          $self->add_icon unless $icon_html;
# Line 284  Create HTML links to editor for perl err Line 308  Create HTML links to editor for perl err
308  sub editor_links {  sub editor_links {
309          my ( $self, $error ) = @_;          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+)}          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
314                  {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;
315    
316          $error =~ s{(via package ")([\w:]+)(")}          $error =~ s{(via (?:package) "?)([\w:]+)("?)}
317                  {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;                  {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
318    
319          return $error;          return $error;
# Line 308  sub error { Line 334  sub error {
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 {  sub add_status {
348          my ( $self, $data ) = @_;          my ( $self, $data ) = @_;
349          push @status, $data;          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 {  sub clean_status {
370          @status = ();          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 = '';          $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 {  sub status_parts {
389          warn "## status parts ", dump( map { keys %$_ } @status );          warn "## status parts ", dump( map { keys %$_ } @status );
390  }  }
391    
392    =for debug
393    
394  sub DEMOLISH {  sub DEMOLISH {
395          my ( $self ) = @_;          my ( $self ) = @_;
396          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;
397  }  }
398    
399    =cut
400    
401  =head2 add_icon  =head2 add_icon
402    
403    Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png    Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png
# Line 341  sub icon_path { Line 412  sub icon_path {
412          $icon .= "/$variant" if $variant;          $icon .= "/$variant" if $variant;
413          my $path = 'static/icons/' . $icon . '.png';          my $path = 'static/icons/' . $icon . '.png';
414          if ( -e $path ) {          if ( -e $path ) {
415                  warn "# $class from $self icon_path $path";                  warn "# $class from $self icon_path $path" if $self->debug;
416                  return $path;                  return $path;
417          } else {          } else {
418                  warn "TODO: add $path icon for $class";                  $self->TODO( "add $path icon for $class" );
419                  return undef;                  return undef;
420          }          }
421  }  }
# Line 398  sub warnings_html { Line 469  sub warnings_html {
469          $level ||= $self->debug,          $level ||= $self->debug,
470          my $path = $self->log_path;          my $path = $self->log_path;
471    
472          my $warnings;          my $max = 30;
473            my $pos = 0;
474            my @warnings = ( '' x $max ); # XXX circualar buffer for 50 lines
475          my $line = 0;          my $line = 0;
476          my $multiline_end;          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: $!";          open(my $log, '<', $path) || die "can't open $path: $!";
490          while(<$log>) {          while(<$log>) {
491                  chomp;                  chomp;
# Line 409  sub warnings_html { Line 493  sub warnings_html {
493    
494                  my $style = '';                  my $style = '';
495    
496    =for filter
497    
498                  if ( $multiline_end ) {                  if ( $multiline_end ) {
499                          if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) {                          if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) {
500  #                               warn "## $line end of $multiline_end in '$_'\n";  #                               warn "## $line end of $multiline_end in '$_'\n";
# Line 425  sub warnings_html { Line 511  sub warnings_html {
511                                  next;                                  next;
512                          }                          }
513    
514                          $style = $warn_colors->{$1}  =cut
515                                  ? ' style="color:' . $warn_colors->{$1} . '"'                  if ( m{^(#*)} ) {
516                                  : '';  
517                            my $level = $1;
518                            my $msg = $_;
519    
                         my $msg = $self->html_escape( $_ );  
520                          my $spacer = ' ';                          my $spacer = ' ';
521                          if ( length($msg) > $self->chop_warning ) {                          my $real_msg = expand( $msg );
522                                  $msg = substr( $msg, 0, $self->chop_warning );                          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;'                                  $spacer = '&hellip;'
527                          }                          }
528                          $msg =~ s{^\s}{&nbsp;}g;  
529                          $warnings .= qq|<tt$style>$msg</tt>$spacer<small><a target="editor" href="/editor+$path+$line">+$line</a></small><br/>|;                          $msg = $self->html_escape( $msg );
530                          # FIXME <tt> should be <code> but CSS hates me  
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: $!";          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          return
553                    qq|<span class="frey-popup"><a target="editor" href="/editor+$path+$line" title="open $path level $level">warn</a><span>|                  # 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 )                  . $self->editor_links( $warnings )
556                  . qq|</span></span>|                  . 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;  1;

Legend:
Removed from v.537  
changed lines
  Added in v.611

  ViewVC Help
Powered by ViewVC 1.1.26