/[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 584 by dpavlin, Fri Nov 28 13:25:17 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;
102    
103            if ( $name =~ m{::} && $name !~ $re_html ) {
104                    return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|;
105            } else {
106                    return qq|<span class="frey-$type">$name $content</span>\n|;
107            }
108  }  }
109    
110  sub _inline_path {  sub _inline_path {
# Line 91  sub _inline_path { Line 115  sub _inline_path {
115  sub _head_html {  sub _head_html {
116          my $self = shift;          my $self = shift;
117          my $out = '';          my $out = '';
118          foreach my $path ( @{ $self->head } ) {          foreach my $path ( @head ) {
119                  $path =~ s!^/!!;                  $path =~ s!^/!!;
120                  if ( $path =~ m/\.js$/ ) {                  if ( $path =~ m/\.js$/ ) {
121                          $out .= $self->_inline_path( $path ) ?                          $out .= $self->_inline_path( $path ) ?
# Line 126  sub add_head { Line 150  sub add_head {
150          return if ! defined $path || $path eq '';          return if ! defined $path || $path eq '';
151          $path =~ s!^/!!;          $path =~ s!^/!!;
152    
153          if ( $path =~ m{<.*>}s ) {          if ( $path =~ $re_html ) {
154                  push @{ $self->head }, $path;                  push @head, $path;
155          } elsif ( -e $path ) {          } elsif ( -e $path ) {
156                  if ( $path =~ m/\.(?:js|css)$/ ) {                  if ( $path =~ m/\.(?:js|css)$/ ) {
157                          push @{ $self->head }, $path;                          push @head, $path;
158                  } else {                  } else {
159                          confess "can't add_head( $path ) it's not js or css";                          confess "can't add_head( $path ) it's not js or css";
160                  }                  }
# Line 141  sub add_head { Line 165  sub add_head {
165    
166  }  }
167    
168    sub add_css {
169            my ($self,$css) = @_;
170            my ( $package, $path, $line ) = caller;
171            $self->add_head( qq|
172            <style type="text/css">
173            /* via $package at $path line $line */
174            $css
175            </style>
176            | );
177    }
178    
179  our $reload_counter = 0;  our $reload_counter = 0;
180    
181    
# Line 159  sub status { @status }; Line 194  sub status { @status };
194    
195  our $icon_html;  our $icon_html;
196    
 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|;  
 }  
   
197  sub page {  sub page {
198          my $self = shift;          my $self = shift;
199          my $a = {@_};          my $a = {@_};
# Line 184  sub page { Line 204  sub page {
204    
205          my $status_line = '';          my $status_line = '';
206    
         unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup };  
 #       unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup };  
   
207          foreach my $part ( @status ) {          foreach my $part ( @status ) {
208                  foreach my $name ( keys %$part ) {                  foreach my $name ( keys %$part ) {
209                          $status_line .= $self->popup( $name, $part->{$name} );                          $status_line .= $self->popup( $name, $part->{$name} );
# Line 208  sub page { Line 225  sub page {
225    
226          $status_line .= $self->warnings_html;          $status_line .= $self->warnings_html;
227    
         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>" );  
   
228          my      ($exit,$description) = ('exit','stop server');          my      ($exit,$description) = ('exit','stop server');
229                  ($exit,$description) = ('restart','restart server')                  ($exit,$description) = ('restart','restart server')
230                  if $ENV{FREY_RESTART}; # tune labels on exit link                  if $ENV{FREY_RESTART}; # tune labels on exit link
# Line 239  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 304  sub editor_links { Line 303  sub editor_links {
303          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
304                  {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;
305    
306          $error =~ s{(via package ")([\w:]+)(")}          $error =~ s{(via (?:package) "?)([\w:]+)("?)}
307                  {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;                  {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
308    
309          return $error;          return $error;
# Line 325  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;          push @status, { 'X' => [ $self->backtrace ] };
340            if ( ref($data) ) {
341                    push @status, $data;
342            } else {
343                    if ( defined $status[ $#status ] ) {
344                            $status[ $#status ]->{ '+' } = $data;
345                    } else {
346                            push @status, { '+' => $data };
347                    }
348            }
349  }  }
350    
351    =head2 clean_status
352    
353    Called at beginning of each request
354    
355      $self->clean_status;
356    
357    =cut
358    
359  sub clean_status {  sub clean_status {
360          @status = ();          my ($self) = shift;
361            @head = ( 'static/frey.css' );
362            @status = (
363                    { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup },
364                    { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },
365                    { 'INC' => Frey::INC->new->as_markup },
366            );
367          $icon_html = '';          $icon_html = '';
368  }  }
369    
370    =head2 status_parts
371    
372    Dump all status line parts
373    
374      $self->status_parts
375    
376    =cut
377    
378  sub status_parts {  sub status_parts {
379          warn "## status parts ", dump( map { keys %$_ } @status );          warn "## status parts ", dump( map { keys %$_ } @status );
380  }  }
381    
382    =for debug
383    
384  sub DEMOLISH {  sub DEMOLISH {
385          my ( $self ) = @_;          my ( $self ) = @_;
386          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;
387  }  }
388    
389    =cut
390    
391  =head2 add_icon  =head2 add_icon
392    
393    Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png    Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png
# Line 358  sub icon_path { Line 402  sub icon_path {
402          $icon .= "/$variant" if $variant;          $icon .= "/$variant" if $variant;
403          my $path = 'static/icons/' . $icon . '.png';          my $path = 'static/icons/' . $icon . '.png';
404          if ( -e $path ) {          if ( -e $path ) {
405                  warn "# $class from $self icon_path $path";                  warn "# $class from $self icon_path $path" if $self->debug;
406                  return $path;                  return $path;
407          } else {          } else {
408                  warn "TODO: add $path icon for $class";                  $self->TODO( "add $path icon for $class" );
409                  return undef;                  return undef;
410          }          }
411  }  }
# Line 448  sub warnings_html { Line 492  sub warnings_html {
492    
493                          my $msg = $self->html_escape( $_ );                          my $msg = $self->html_escape( $_ );
494                          my $spacer = ' ';                          my $spacer = ' ';
495                          if ( length($msg) > $self->chop_warning ) {                          if ( length($msg) > $self->html_dump_width ) {
496                                  $msg = substr( $msg, 0, $self->chop_warning );                                  $msg = substr( $msg, 0, $self->html_dump_width );
497                                  $spacer = '&hellip;'                                  $spacer = '&hellip;'
498                          }                          }
499                          $msg =~ s{^\s}{&nbsp;}g;                          $msg =~ s{^\s}{&nbsp;}g;
# Line 466  sub warnings_html { Line 510  sub warnings_html {
510                  ;                  ;
511  }  }
512    
513    
514    =head2 backtrace
515    
516    Show backtrace with links to editor
517    
518      my @backtrace = $self->backtrace;
519    
520    =cut
521    
522    sub backtrace {
523            my ($self) = @_;
524    
525            my @backtrace;
526            foreach ( 0 .. 5 ) {
527                    my (
528                            $package,$path,$line
529                            # subroutine hasargs
530                            # wantarray evaltext is_require
531                            # hints bitmask hinthash
532                    ) = caller($_) or last;
533    
534                    push @backtrace,
535                            qq|via $package at $path line $line|;
536            }
537            warn "# backtrace: ", dump( @backtrace ) if @backtrace;
538            return @backtrace;
539    }
540    
541  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26