/[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 581 by dpavlin, Fri Nov 28 13:16:47 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;
         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    #       $dump =~ $self->editor_links( $dump ); # FIXME include this
83            return "<code>$dump</code>";
84  }  }
85    
86  sub dom2html {  sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }
87  #       warn "## dom2html ",dump( @_ );  sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }
88          return Continuity::Widget::DomNode->create( @_ )->to_string;  
89    our $re_html = qr{<(?:!--.+?--|(\w+).+?/\1)>}s; # relaxed html check for one semi-valid tag
90    
91    sub popup_dropdown {
92            my ( $self, $type, $name, $content, $full ) = @_;
93    
94            $content = $self->html_dump($content) if ref $content;
95    
96            $content = qq|<span>$content</span>| unless $content =~ m{^\s*<(span|a|code).+?/\1>\s*};
97    
98            $content =~ s{<span>(<code>[^<]+</code>)</span>}{$1} && $self->TODO("code wrapped in span");
99    
100            warn "## $type [$name] = ", length( $content ), " bytes" if $self->debug;
101    
102            if ( $name =~ m{::} && $name !~ $re_html ) {
103                    return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|;
104            } else {
105                    return qq|<span class="frey-$type">$name $content</span>\n|;
106            }
107  }  }
108    
109  sub _inline_path {  sub _inline_path {
# Line 91  sub _inline_path { Line 114  sub _inline_path {
114  sub _head_html {  sub _head_html {
115          my $self = shift;          my $self = shift;
116          my $out = '';          my $out = '';
117          foreach my $path ( @{ $self->head } ) {          foreach my $path ( @head ) {
118                  $path =~ s!^/!!;                  $path =~ s!^/!!;
119                  if ( $path =~ m/\.js$/ ) {                  if ( $path =~ m/\.js$/ ) {
120                          $out .= $self->_inline_path( $path ) ?                          $out .= $self->_inline_path( $path ) ?
# Line 126  sub add_head { Line 149  sub add_head {
149          return if ! defined $path || $path eq '';          return if ! defined $path || $path eq '';
150          $path =~ s!^/!!;          $path =~ s!^/!!;
151    
152          if ( $path =~ m{<.*>}s ) {          if ( $path =~ $re_html ) {
153                  push @{ $self->head }, $path;                  push @head, $path;
154          } elsif ( -e $path ) {          } elsif ( -e $path ) {
155                  if ( $path =~ m/\.(?:js|css)$/ ) {                  if ( $path =~ m/\.(?:js|css)$/ ) {
156                          push @{ $self->head }, $path;                          push @head, $path;
157                  } else {                  } else {
158                          confess "can't add_head( $path ) it's not js or css";                          confess "can't add_head( $path ) it's not js or css";
159                  }                  }
# Line 141  sub add_head { Line 164  sub add_head {
164    
165  }  }
166    
167    sub add_css {
168            my ($self,$css) = @_;
169            my ( $package, $path, $line ) = caller;
170            $self->add_head( qq|
171            <style type="text/css">
172            /* via $package at $path line $line */
173            $css
174            </style>
175            | );
176    }
177    
178  our $reload_counter = 0;  our $reload_counter = 0;
179    
180    
# Line 159  sub status { @status }; Line 193  sub status { @status };
193    
194  our $icon_html;  our $icon_html;
195    
 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|;  
 }  
   
196  sub page {  sub page {
197          my $self = shift;          my $self = shift;
198          my $a = {@_};          my $a = {@_};
# Line 184  sub page { Line 203  sub page {
203    
204          my $status_line = '';          my $status_line = '';
205    
         unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup };  
 #       unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup };  
   
206          foreach my $part ( @status ) {          foreach my $part ( @status ) {
207                  foreach my $name ( keys %$part ) {                  foreach my $name ( keys %$part ) {
208                          $status_line .= $self->popup( $name, $part->{$name} );                          $status_line .= $self->popup( $name, $part->{$name} );
# Line 208  sub page { Line 224  sub page {
224    
225          $status_line .= $self->warnings_html;          $status_line .= $self->warnings_html;
226    
         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>" );  
   
227          my      ($exit,$description) = ('exit','stop server');          my      ($exit,$description) = ('exit','stop server');
228                  ($exit,$description) = ('restart','restart server')                  ($exit,$description) = ('restart','restart server')
229                  if $ENV{FREY_RESTART}; # tune labels on exit link                  if $ENV{FREY_RESTART}; # tune labels on exit link
# Line 239  sub page { Line 236  sub page {
236                          </span>                          </span>
237                  |;                  |;
238    
239          my $info = Frey::SVK->info;          my $svk = Frey::SVK->new;
240          my $revision = Frey::SVK->info->{Revision} || '';          my $info = $svk->info;
241            my $revision = $svk->info->{Revision} || '';
242          $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};          $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};
243    
244          $self->add_icon unless $icon_html;          $self->add_icon unless $icon_html;
# Line 304  sub editor_links { Line 302  sub editor_links {
302          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
303                  {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;
304    
305          $error =~ s{(via package ")([\w:]+)(")}          $error =~ s{(via (?:package) "?)([\w:]+)("?)}
306                  {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;                  {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
307    
308          return $error;          return $error;
# Line 325  sub error { Line 323  sub error {
323                  ;                  ;
324  }  }
325    
326    =head1 Status line
327    
328    =head2 add_status
329    
330      $self->add_status( { name => { some => 'data' } } );
331    
332      $self->add_status( "append to last status popup" );
333    
334    =cut
335    
336  sub add_status {  sub add_status {
337          my ( $self, $data ) = @_;          my ( $self, $data ) = @_;
338          push @status, $data;          push @status, { 'X' => [ $self->backtrace ] };
339            if ( ref($data) ) {
340                    push @status, $data;
341            } else {
342                    if ( defined $status[ $#status ] ) {
343                            $status[ $#status ]->{ '+' } = $data;
344                    } else {
345                            push @status, { '+' => $data };
346                    }
347            }
348  }  }
349    
350    =head2 clean_status
351    
352    Called at beginning of each request
353    
354      $self->clean_status;
355    
356    =cut
357    
358  sub clean_status {  sub clean_status {
359          @status = ();          my ($self) = shift;
360            @head = ( 'static/frey.css' );
361            @status = (
362                    { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup },
363                    { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },
364                    { 'INC' => Frey::INC->new->as_markup },
365            );
366          $icon_html = '';          $icon_html = '';
367  }  }
368    
369    =head2 status_parts
370    
371    Dump all status line parts
372    
373      $self->status_parts
374    
375    =cut
376    
377  sub status_parts {  sub status_parts {
378          warn "## status parts ", dump( map { keys %$_ } @status );          warn "## status parts ", dump( map { keys %$_ } @status );
379  }  }
380    
381    =for debug
382    
383  sub DEMOLISH {  sub DEMOLISH {
384          my ( $self ) = @_;          my ( $self ) = @_;
385          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;
386  }  }
387    
388    =cut
389    
390  =head2 add_icon  =head2 add_icon
391    
392    Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png    Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png
# Line 358  sub icon_path { Line 401  sub icon_path {
401          $icon .= "/$variant" if $variant;          $icon .= "/$variant" if $variant;
402          my $path = 'static/icons/' . $icon . '.png';          my $path = 'static/icons/' . $icon . '.png';
403          if ( -e $path ) {          if ( -e $path ) {
404                  warn "# $class from $self icon_path $path";                  warn "# $class from $self icon_path $path" if $self->debug;
405                  return $path;                  return $path;
406          } else {          } else {
407                  warn "TODO: add $path icon for $class";                  $self->TODO( "add $path icon for $class" );
408                  return undef;                  return undef;
409          }          }
410  }  }
# Line 448  sub warnings_html { Line 491  sub warnings_html {
491    
492                          my $msg = $self->html_escape( $_ );                          my $msg = $self->html_escape( $_ );
493                          my $spacer = ' ';                          my $spacer = ' ';
494                          if ( length($msg) > $self->chop_warning ) {                          if ( length($msg) > $self->html_dump_width ) {
495                                  $msg = substr( $msg, 0, $self->chop_warning );                                  $msg = substr( $msg, 0, $self->html_dump_width );
496                                  $spacer = '&hellip;'                                  $spacer = '&hellip;'
497                          }                          }
498                          $msg =~ s{^\s}{&nbsp;}g;                          $msg =~ s{^\s}{&nbsp;}g;
# Line 466  sub warnings_html { Line 509  sub warnings_html {
509                  ;                  ;
510  }  }
511    
512    
513    =head2 backtrace
514    
515    Show backtrace with links to editor
516    
517      my @backtrace = $self->backtrace;
518    
519    =cut
520    
521    sub backtrace {
522            my ($self) = @_;
523    
524            my @backtrace;
525            foreach ( 0 .. 5 ) {
526                    my (
527                            $package,$path,$line
528                            # subroutine hasargs
529                            # wantarray evaltext is_require
530                            # hints bitmask hinthash
531                    ) = caller($_) or last;
532    
533                    push @backtrace,
534                            qq|via $package at $path line $line|;
535            }
536            warn "# backtrace: ", dump( @backtrace ) if @backtrace;
537            return @backtrace;
538    }
539    
540  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26