/[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 577 by dpavlin, Fri Nov 28 00:30:03 2008 UTC revision 611 by dpavlin, Fri Nov 28 23:34:26 2008 UTC
# Line 16  use Frey::INC; Line 16  use Frey::INC;
16    
17  use Frey::SVK;  use Frey::SVK;
18    
19  our @head = ( 'static/frey.css' );  use Text::Tabs; # expand, unexpand
20    
21    our @head;
22    sub head { @head }
23    
24  has 'request_url' => (  has 'request_url' => (
25          is => 'rw',          is => 'rw',
# Line 60  has 'html_dump_width' => ( Line 63  has 'html_dump_width' => (
63          is => 'rw',          is => 'rw',
64          isa => 'Int',          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  #       required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them
66          default => 128,          default => 120,
67  );  );
68    
69  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
# Line 79  sub html_dump { Line 82  sub html_dump {
82          $dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs;          $dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs;
83          $dump = $self->html_escape( $dump );          $dump = $self->html_escape( $dump );
84          $dump =~ s{\Q...\E}{&hellip;}gs;          $dump =~ s{\Q...\E}{&hellip;}gs;
85    #       $dump =~ $self->editor_links( $dump ); # FIXME include this
86          return "<code>$dump</code>";          return "<code>$dump</code>";
87  }  }
88    
89  sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }  sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }
90  sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }  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  our $re_html = qr{<(?:!--.+?--|(\w+).+?/\1|[^>]+/)>}s; # relaxed html check for one semi-valid tag
93    
94  sub popup_dropdown {  sub popup_dropdown {
95          my ( $self, $type, $name, $content, $full ) = @_;          my ( $self, $type, $name, $content, $full ) = @_;
# Line 94  sub popup_dropdown { Line 98  sub popup_dropdown {
98    
99          $content = qq|<span>$content</span>| unless $content =~ m{^\s*<(span|a|code).+?/\1>\s*};          $content = qq|<span>$content</span>| unless $content =~ m{^\s*<(span|a|code).+?/\1>\s*};
100    
101          warn "## $type [$name] = ", length( $content ), " bytes" if $self->debug;          $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 ) {          if ( $name =~ m{::} && $name !~ $re_html ) {
106                  return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|;                  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 {          } else {
110                  return qq|<span class="frey-$type">$name $content</span>\n|;                  return qq|<span class="frey-$type">$name $content</span>\n|;
111          }          }
# Line 115  sub _head_html { Line 123  sub _head_html {
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 166  sub add_css { Line 174  sub add_css {
174          my ( $package, $path, $line ) = caller;          my ( $package, $path, $line ) = caller;
175          $self->add_head( qq|          $self->add_head( qq|
176          <style type="text/css">          <style type="text/css">
177          /* via package $package at $path line $line */          /* via $package at $path line $line */
178          $css          $css
179          </style>          </style>
180          | );          | );
# Line 200  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 };  
         unshift @status, { 'INC' => Frey::INC->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 214  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 300  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 328  sub error { Line 338  sub error {
338    
339  =head2 add_status  =head2 add_status
340    
341    $self->add_status( name => { some => 'data' } );    $self->add_status( { name => { some => 'data' } } );
342    
343    $self->add_status( "append to last status popup" );    $self->add_status( "append to last status popup" );
344    
# Line 336  sub error { Line 346  sub error {
346    
347  sub add_status {  sub add_status {
348          my ( $self, $data ) = @_;          my ( $self, $data ) = @_;
349            push @status, { 'X' => [ $self->backtrace ] };
350          if ( ref($data) ) {          if ( ref($data) ) {
351                  push @status, $data;                  push @status, $data;
352          } else {          } else {
353                  $status[ $#status ]->{added} = $data;                  if ( defined $status[ $#status ] ) {
354                            $status[ $#status ]->{ '+' } = $data;
355                    } else {
356                            push @status, { '+' => $data };
357                    }
358          }          }
359  }  }
360    
# Line 353  Called at beginning of each request Line 368  Called at beginning of each request
368    
369  sub clean_status {  sub clean_status {
370          my ($self) = shift;          my ($self) = shift;
371          @head = ();          @head = ( 'static/frey.css' );
372          @status = ();          @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    
# Line 450  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 461  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 477  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->html_dump_width ) {                          my $real_msg = expand( $msg );
522                                  $msg = substr( $msg, 0, $self->html_dump_width );                          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    
# Line 523  sub backtrace { Line 579  sub backtrace {
579                  ) = caller($_) or last;                  ) = caller($_) or last;
580    
581                  push @backtrace,                  push @backtrace,
582                          qq|via package $package at $path line $line|;                          qq|via $package at $path line $line|;
583          }          }
584          warn "# backtrace: ", dump( @backtrace ) if @backtrace;          #warn "# backtrace: ", dump( @backtrace ) if @backtrace;
585          return @backtrace;          return @backtrace;
586  }  }
587    

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

  ViewVC Help
Powered by ViewVC 1.1.26