/[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 532 by dpavlin, Wed Nov 26 07:57:12 2008 UTC revision 577 by dpavlin, Fri Nov 28 00:30: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 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 = ( 'static/frey.css' );
         is => 'rw',  
         isa => 'ArrayRef[Str]',  
         default => sub { [ 'static/frey.css' ] },  
 );  
20    
21  has 'request_url' => (  has 'request_url' => (
22          is => 'rw',          is => 'rw',
# Line 47  has 'dump_max_bytes' => ( Line 45  has 'dump_max_bytes' => (
45          is => 'rw',          is => 'rw',
46          isa => 'Int',          isa => 'Int',
47          default => 4096,          default => 4096,
48          documentation => 'Maximum dump size sent to browser before truncation',          documentation => 'maximum dump size sent to browser before truncation',
49  );  );
50    
 =head2 inline_smaller_than  
   
 Inline JavaScript and CSS smaller than this size into page reducing  
 round-trips to server.  
   
 =cut  
   
51  has 'inline_smaller_than' => (  has 'inline_smaller_than' => (
52          is => 'rw',          is => 'rw',
53          isa => 'Int',          isa => 'Int',
54          default => 10240,          default => 10240,
55            documentation => 'inline JavaScript and CSS to reduce round-trips',
56    );
57    
58    has 'html_dump_width' => (
59            documentation => 'crop longer lines in dumps',
60            is => 'rw',
61            isa => 'Int',
62    #       required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them
63            default => 128,
64  );  );
65    
66  sub dom2html {  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
67  #       warn "## dom2html ",dump( @_ );  my $escape_re  = join '|' => keys %escape;
68          return Continuity::Widget::DomNode->create( @_ )->to_string;  
69    sub html_escape {
70            my ( $self, $html ) = @_;
71            $html =~ s/($escape_re)/$escape{$1}/g;
72            return $html;
73    }
74    
75    sub html_dump {
76            my ( $self, $dump ) = @_;
77            $dump = dump( $dump ) if ref($dump);
78            my $width = $self->html_dump_width;
79            $dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs;
80            $dump = $self->html_escape( $dump );
81            $dump =~ s{\Q...\E}{&hellip;}gs;
82            return "<code>$dump</code>";
83    }
84    
85    sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }
86    sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }
87    
88    our $re_html = qr{<(?:!--.+?--|(\w+).+?/\1)>}s; # relaxed html check for one semi-valid tag
89    
90    sub popup_dropdown {
91            my ( $self, $type, $name, $content, $full ) = @_;
92    
93            $content = $self->html_dump($content) if ref $content;
94    
95            $content = qq|<span>$content</span>| unless $content =~ m{^\s*<(span|a|code).+?/\1>\s*};
96    
97            warn "## $type [$name] = ", length( $content ), " bytes" if $self->debug;
98    
99            if ( $name =~ m{::} && $name !~ $re_html ) {
100                    return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|;
101            } else {
102                    return qq|<span class="frey-$type">$name $content</span>\n|;
103            }
104  }  }
105    
106  sub _inline_path {  sub _inline_path {
# Line 76  sub _inline_path { Line 111  sub _inline_path {
111  sub _head_html {  sub _head_html {
112          my $self = shift;          my $self = shift;
113          my $out = '';          my $out = '';
114          foreach my $path ( @{ $self->head } ) {          foreach my $path ( @head ) {
115                  $path =~ s!^/!!;                  $path =~ s!^/!!;
116                  if ( $path =~ m/\.js$/ ) {                  if ( $path =~ m/\.js$/ ) {
117                          $out .= $self->_inline_path( $path ) ?                          $out .= $self->_inline_path( $path ) ?
# Line 111  sub add_head { Line 146  sub add_head {
146          return if ! defined $path || $path eq '';          return if ! defined $path || $path eq '';
147          $path =~ s!^/!!;          $path =~ s!^/!!;
148    
149          if ( $path =~ m{<.*>}s ) {          if ( $path =~ $re_html ) {
150                  push @{ $self->head }, $path;                  push @head, $path;
151          } elsif ( -e $path ) {          } elsif ( -e $path ) {
152                  if ( $path =~ m/\.(?:js|css)$/ ) {                  if ( $path =~ m/\.(?:js|css)$/ ) {
153                          push @{ $self->head }, $path;                          push @head, $path;
154                  } else {                  } else {
155                          confess "can't add_head( $path ) it's not js or css";                          confess "can't add_head( $path ) it's not js or css";
156                  }                  }
# Line 126  sub add_head { Line 161  sub add_head {
161    
162  }  }
163    
164    sub add_css {
165            my ($self,$css) = @_;
166            my ( $package, $path, $line ) = caller;
167            $self->add_head( qq|
168            <style type="text/css">
169            /* via package $package at $path line $line */
170            $css
171            </style>
172            | );
173    }
174    
175  our $reload_counter = 0;  our $reload_counter = 0;
176    
177    
# Line 155  sub page { Line 201  sub page {
201          my $status_line = '';          my $status_line = '';
202    
203          unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup };          unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup };
204          unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup };  #       unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup };
205            unshift @status, { 'INC' => Frey::INC->new->as_markup };
206    
207          foreach my $part ( @status ) {          foreach my $part ( @status ) {
208                  foreach my $name ( keys %$part ) {                  foreach my $name ( keys %$part ) {
209                          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|;  
210                  }                  }
211          }          }
212    
# Line 199  sub page { Line 237  sub page {
237                          </span>                          </span>
238                  |;                  |;
239    
240          my $info = Frey::SVK->info;          my $svk = Frey::SVK->new;
241          my $revision = Frey::SVK->info->{Revision} || '';          my $info = $svk->info;
242            my $revision = $svk->info->{Revision} || '';
243          $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};          $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};
244    
245          $self->add_icon unless $icon_html;          $self->add_icon unless $icon_html;
# Line 285  sub error { Line 324  sub error {
324                  ;                  ;
325  }  }
326    
327    =head1 Status line
328    
329    =head2 add_status
330    
331      $self->add_status( name => { some => 'data' } );
332    
333      $self->add_status( "append to last status popup" );
334    
335    =cut
336    
337  sub add_status {  sub add_status {
338          my ( $self, $data ) = @_;          my ( $self, $data ) = @_;
339          push @status, $data;          if ( ref($data) ) {
340                    push @status, $data;
341            } else {
342                    $status[ $#status ]->{added} = $data;
343            }
344  }  }
345    
346    =head2 clean_status
347    
348    Called at beginning of each request
349    
350      $self->clean_status;
351    
352    =cut
353    
354  sub clean_status {  sub clean_status {
355            my ($self) = shift;
356            @head = ();
357          @status = ();          @status = ();
358          $icon_html = '';          $icon_html = '';
359  }  }
360    
361    =head2 status_parts
362    
363    Dump all status line parts
364    
365      $self->status_parts
366    
367    =cut
368    
369  sub status_parts {  sub status_parts {
370          warn "## status parts ", dump( map { keys %$_ } @status );          warn "## status parts ", dump( map { keys %$_ } @status );
371  }  }
372    
373    =for debug
374    
375  sub DEMOLISH {  sub DEMOLISH {
376          my ( $self ) = @_;          my ( $self ) = @_;
377          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;
378  }  }
379    
380    =cut
381    
382  =head2 add_icon  =head2 add_icon
383    
384    Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png    Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png
# Line 318  sub icon_path { Line 393  sub icon_path {
393          $icon .= "/$variant" if $variant;          $icon .= "/$variant" if $variant;
394          my $path = 'static/icons/' . $icon . '.png';          my $path = 'static/icons/' . $icon . '.png';
395          if ( -e $path ) {          if ( -e $path ) {
396                  warn "# $class from $self icon_path $path";                  warn "# $class from $self icon_path $path" if $self->debug;
397                  return $path;                  return $path;
398          } else {          } else {
399                  warn "TODO: add $path icon for $class";                  $self->TODO( "add $path icon for $class" );
400                  return undef;                  return undef;
401          }          }
402  }  }
# Line 357  my $warn_colors = { Line 432  my $warn_colors = {
432  };  };
433    
434  my $multiline_markers = {  my $multiline_markers = {
435          '(' => '\)',          '(' => ')',
436          '{' => '}',          '{' => '}',
437          '[' => '\]',          '[' => ']',
438          '"' => '"',          '"' => '"',
439  };  };
440    
# Line 372  sub log_path { Line 447  sub log_path {
447    
448  sub warnings_html {  sub warnings_html {
449          my ($self,$level) = shift;          my ($self,$level) = shift;
450          $level ||= $self->debug;          $level ||= $self->debug,
451          my $path = $self->log_path;          my $path = $self->log_path;
452    
453          my $warnings;          my $warnings;
# Line 384  sub warnings_html { Line 459  sub warnings_html {
459                  chomp;                  chomp;
460                  $line++;                  $line++;
461    
                 if ( $multiline_end ) {  
                         undef $multiline_end if m{^$multiline_end};  
                         next;  
                 }  
   
462                  my $style = '';                  my $style = '';
463    
464                  if ( m{^(#*)\s+} ) {                  if ( $multiline_end ) {
465                            if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) {
466    #                               warn "## $line end of $multiline_end in '$_'\n";
467                                    undef $multiline_end;
468                            } else {
469    #                               warn "## $line skipped\n";
470                            }
471                    } elsif ( m{^(#*)\s+} ) {
472                          my $l = $1 ? length($1) : 0;                          my $l = $1 ? length($1) : 0;
473                          $multiline_end = $multiline_markers->{$1} if m{($multiline_re)$};                          if ( $l > $level ) {
474                          next if $l > $level;                                  undef $multiline_end;
475                          warn "## multiline_end: $multiline_end $l > $level for '$_'" if $multiline_end;                                  $multiline_end = $multiline_markers->{$1} if m{($multiline_re)$};
476                          undef $multiline_end;  #                               warn "## $line start $1 .. $multiline_end level $l > $level for '$_'\n" if $multiline_end;
477                                    next;
478                            }
479    
480                          $style = $warn_colors->{$1}                          $style = $warn_colors->{$1}
481                                  ? ' style="color:' . $warn_colors->{$1} . '"'                                  ? ' style="color:' . $warn_colors->{$1} . '"'
482                                  : '';                                  : '';
483    
484                          $warnings .= qq|<tt$style>$_</tt> <small> <a target="editor" href="/editor+$path+$line">+$line</a> </small> <br/>|;                          my $msg = $self->html_escape( $_ );
485                            my $spacer = ' ';
486                            if ( length($msg) > $self->html_dump_width ) {
487                                    $msg = substr( $msg, 0, $self->html_dump_width );
488                                    $spacer = '&hellip;'
489                            }
490                            $msg =~ s{^\s}{&nbsp;}g;
491                            $warnings .= qq|<tt$style>$msg</tt>$spacer<small><a target="editor" href="/editor+$path+$line">+$line</a></small><br/>|;
492                          # FIXME <tt> should be <code> but CSS hates me                          # FIXME <tt> should be <code> but CSS hates me
493                  }                  }
494          }          }
# Line 415  sub warnings_html { Line 501  sub warnings_html {
501                  ;                  ;
502  }  }
503    
504    
505    =head2 backtrace
506    
507    Show backtrace with links to editor
508    
509      my @backtrace = $self->backtrace;
510    
511    =cut
512    
513    sub backtrace {
514            my ($self) = @_;
515    
516            my @backtrace;
517            foreach ( 0 .. 5 ) {
518                    my (
519                            $package,$path,$line
520                            # subroutine hasargs
521                            # wantarray evaltext is_require
522                            # hints bitmask hinthash
523                    ) = caller($_) or last;
524    
525                    push @backtrace,
526                            qq|via package $package at $path line $line|;
527            }
528            warn "# backtrace: ", dump( @backtrace ) if @backtrace;
529            return @backtrace;
530    }
531    
532  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26