/[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 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    
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',          documentation => 'inline JavaScript and CSS to reduce round-trips',
56  );  );
57    
58  has 'chop_warning' => (  has 'html_dump_width' => (
59            documentation => 'crop longer lines in dumps',
60          is => 'rw',          is => 'rw',
61          isa => 'Int',          isa => 'Int',
62          default => 80,  #       required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them
63          documentation => 'Crop lines longer',          default => 128,
64  );  );
65    
66  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
# Line 74  sub html_escape { Line 73  sub html_escape {
73  }  }
74    
75  sub html_dump {  sub html_dump {
76          my $self = shift;          my ( $self, $dump ) = @_;
77          $self->html_escape( dump( @_ ) );          $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 dom2html {  sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }
86  #       warn "## dom2html ",dump( @_ );  sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }
87          return Continuity::Widget::DomNode->create( @_ )->to_string;  
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 91  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 126  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 141  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 159  sub status { @status }; Line 190  sub status { @status };
190    
191  our $icon_html;  our $icon_html;
192    
 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|;  
 }  
   
193  sub page {  sub page {
194          my $self = shift;          my $self = shift;
195          my $a = {@_};          my $a = {@_};
# Line 186  sub page { Line 202  sub page {
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 ) {
# 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 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;          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 358  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 448  sub warnings_html { Line 483  sub warnings_html {
483    
484                          my $msg = $self->html_escape( $_ );                          my $msg = $self->html_escape( $_ );
485                          my $spacer = ' ';                          my $spacer = ' ';
486                          if ( length($msg) > $self->chop_warning ) {                          if ( length($msg) > $self->html_dump_width ) {
487                                  $msg = substr( $msg, 0, $self->chop_warning );                                  $msg = substr( $msg, 0, $self->html_dump_width );
488                                  $spacer = '&hellip;'                                  $spacer = '&hellip;'
489                          }                          }
490                          $msg =~ s{^\s}{&nbsp;}g;                          $msg =~ s{^\s}{&nbsp;}g;
# Line 466  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.538  
changed lines
  Added in v.577

  ViewVC Help
Powered by ViewVC 1.1.26