/[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 543 by dpavlin, Wed Nov 26 19:43:03 2008 UTC revision 588 by dpavlin, Fri Nov 28 15:07: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::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;
# Line 14  use Frey::INC; Line 16  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 81  sub html_dump { Line 80  sub html_dump {
80          $dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs;          $dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs;
81          $dump = $self->html_escape( $dump );          $dump = $self->html_escape( $dump );
82          $dump =~ s{\Q...\E}{…}gs;          $dump =~ s{\Q...\E}{…}gs;
83    #       $dump =~ $self->editor_links( $dump ); # FIXME include this
84          return "<code>$dump</code>";          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            } elsif ( $name =~ s{^\s*(<a)\s+}{$1 class="frey-$type"} ) {
106                    return qq|$name $content\n|;
107            } else {
108                    return qq|<span class="frey-$type">$name $content</span>\n|;
109            }
110  }  }
111    
112  sub _inline_path {  sub _inline_path {
# Line 97  sub _inline_path { Line 117  sub _inline_path {
117  sub _head_html {  sub _head_html {
118          my $self = shift;          my $self = shift;
119          my $out = '';          my $out = '';
120          foreach my $path ( @{ $self->head } ) {          foreach my $path ( @head ) {
121                  $path =~ s!^/!!;                  $path =~ s!^/!!;
122                  if ( $path =~ m/\.js$/ ) {                  if ( $path =~ m/\.js$/ ) {
123                          $out .= $self->_inline_path( $path ) ?                          $out .= $self->_inline_path( $path ) ?
# Line 132  sub add_head { Line 152  sub add_head {
152          return if ! defined $path || $path eq '';          return if ! defined $path || $path eq '';
153          $path =~ s!^/!!;          $path =~ s!^/!!;
154    
155          if ( $path =~ m{<.*>}s ) {          if ( $path =~ $re_html ) {
156                  push @{ $self->head }, $path;                  push @head, $path;
157          } elsif ( -e $path ) {          } elsif ( -e $path ) {
158                  if ( $path =~ m/\.(?:js|css)$/ ) {                  if ( $path =~ m/\.(?:js|css)$/ ) {
159                          push @{ $self->head }, $path;                          push @head, $path;
160                  } else {                  } else {
161                          confess "can't add_head( $path ) it's not js or css";                          confess "can't add_head( $path ) it's not js or css";
162                  }                  }
# Line 147  sub add_head { Line 167  sub add_head {
167    
168  }  }
169    
170    sub add_css {
171            my ($self,$css) = @_;
172            my ( $package, $path, $line ) = caller;
173            $self->add_head( qq|
174            <style type="text/css">
175            /* via $package at $path line $line */
176            $css
177            </style>
178            | );
179    }
180    
181  our $reload_counter = 0;  our $reload_counter = 0;
182    
183    
# Line 165  sub status { @status }; Line 196  sub status { @status };
196    
197  our $icon_html;  our $icon_html;
198    
 sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }  
 sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }  
   
 sub popup_dropdown {  
         my ( $self, $type, $name, $content, $full ) = @_;  
   
         if ( ref($content) ) {  
                 $content = $self->html_dump($content);  
 #               my $l = length($content);  
 #               $content = qq|<span>$l bytes</span>| if ! $full && $l > $self->dump_max_bytes;  
         } else {  
                 $content = qq|<span>$content</span>|;  
         }  
   
         warn "## $type [$name] = ", length( $content ), " bytes" if $self->debug;  
   
         if ( $name =~ m{::} && ! $name =~ m{<(\w+).+?/\1>} ) {  
                 return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|;  
         } else {  
                 return qq|<span class="frey-$type">$name $content</span>\n|;  
         }  
 }  
   
199  sub page {  sub page {
200          my $self = shift;          my $self = shift;
201          my $a = {@_};          my $a = {@_};
# Line 198  sub page { Line 206  sub page {
206    
207          my $status_line = '';          my $status_line = '';
208    
         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 };  
   
209          foreach my $part ( @status ) {          foreach my $part ( @status ) {
210                  foreach my $name ( keys %$part ) {                  foreach my $name ( keys %$part ) {
211                          $status_line .= $self->popup( $name, $part->{$name} );                          $status_line .= $self->popup( $name, $part->{$name} );
# Line 235  sub page { Line 239  sub page {
239                          </span>                          </span>
240                  |;                  |;
241    
242          my $info = Frey::SVK->info;          my $svk = Frey::SVK->new;
243          my $revision = Frey::SVK->info->{Revision} || '';          my $info = $svk->info;
244            my $revision = $svk->info->{Revision} || '';
245          $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};          $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};
246    
247          $self->add_icon unless $icon_html;          $self->add_icon unless $icon_html;
# Line 300  sub editor_links { Line 305  sub editor_links {
305          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
306                  {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;
307    
308          $error =~ s{(via package ")([\w:]+)(")}          $error =~ s{(via (?:package) "?)([\w:]+)("?)}
309                  {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;                  {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
310    
311          return $error;          return $error;
# Line 321  sub error { Line 326  sub error {
326                  ;                  ;
327  }  }
328    
329    =head1 Status line
330    
331    =head2 add_status
332    
333      $self->add_status( { name => { some => 'data' } } );
334    
335      $self->add_status( "append to last status popup" );
336    
337    =cut
338    
339  sub add_status {  sub add_status {
340          my ( $self, $data ) = @_;          my ( $self, $data ) = @_;
341          push @status, $data;          push @status, { 'X' => [ $self->backtrace ] };
342            if ( ref($data) ) {
343                    push @status, $data;
344            } else {
345                    if ( defined $status[ $#status ] ) {
346                            $status[ $#status ]->{ '+' } = $data;
347                    } else {
348                            push @status, { '+' => $data };
349                    }
350            }
351  }  }
352    
353    =head2 clean_status
354    
355    Called at beginning of each request
356    
357      $self->clean_status;
358    
359    =cut
360    
361  sub clean_status {  sub clean_status {
362          @status = ();          my ($self) = shift;
363            @head = ( 'static/frey.css' );
364            @status = (
365                    { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup },
366                    { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },
367                    { 'INC' => Frey::INC->new->as_markup },
368            );
369          $icon_html = '';          $icon_html = '';
370  }  }
371    
372    =head2 status_parts
373    
374    Dump all status line parts
375    
376      $self->status_parts
377    
378    =cut
379    
380  sub status_parts {  sub status_parts {
381          warn "## status parts ", dump( map { keys %$_ } @status );          warn "## status parts ", dump( map { keys %$_ } @status );
382  }  }
383    
384    =for debug
385    
386  sub DEMOLISH {  sub DEMOLISH {
387          my ( $self ) = @_;          my ( $self ) = @_;
388          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;
389  }  }
390    
391    =cut
392    
393  =head2 add_icon  =head2 add_icon
394    
395    Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png    Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png
# Line 354  sub icon_path { Line 404  sub icon_path {
404          $icon .= "/$variant" if $variant;          $icon .= "/$variant" if $variant;
405          my $path = 'static/icons/' . $icon . '.png';          my $path = 'static/icons/' . $icon . '.png';
406          if ( -e $path ) {          if ( -e $path ) {
407                  warn "# $class from $self icon_path $path";                  warn "# $class from $self icon_path $path" if $self->debug;
408                  return $path;                  return $path;
409          } else {          } else {
410                  warn "TODO: add $path icon for $class";                  $self->TODO( "add $path icon for $class" );
411                  return undef;                  return undef;
412          }          }
413  }  }
# Line 411  sub warnings_html { Line 461  sub warnings_html {
461          $level ||= $self->debug,          $level ||= $self->debug,
462          my $path = $self->log_path;          my $path = $self->log_path;
463    
464          my $warnings;          my $max = 50;
465            my $pos = 0;
466            my @warnings = ( '' x $max ); # XXX circualar buffer for 50 lines
467          my $line = 0;          my $line = 0;
468          my $multiline_end;          my $multiline_end;
469    
# Line 422  sub warnings_html { Line 474  sub warnings_html {
474    
475                  my $style = '';                  my $style = '';
476    
477    =for filter
478    
479                  if ( $multiline_end ) {                  if ( $multiline_end ) {
480                          if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) {                          if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) {
481  #                               warn "## $line end of $multiline_end in '$_'\n";  #                               warn "## $line end of $multiline_end in '$_'\n";
# Line 438  sub warnings_html { Line 492  sub warnings_html {
492                                  next;                                  next;
493                          }                          }
494    
495    =cut
496                    if ( m{^(#*)\s+} ) { # FIXME
497    
498                          $style = $warn_colors->{$1}                          $style = $warn_colors->{$1}
499                                  ? ' style="color:' . $warn_colors->{$1} . '"'                                  ? ' style="color:' . $warn_colors->{$1} . '"'
500                                  : '';                                  : '';
# Line 448  sub warnings_html { Line 505  sub warnings_html {
505                                  $msg = substr( $msg, 0, $self->html_dump_width );                                  $msg = substr( $msg, 0, $self->html_dump_width );
506                                  $spacer = '&hellip;'                                  $spacer = '&hellip;'
507                          }                          }
508                          $msg =~ s{^\s}{&nbsp;}g;                          $warnings[ $pos++ % $max ]
509                          $warnings .= qq|<tt$style>$msg</tt>$spacer<small><a target="editor" href="/editor+$path+$line">+$line</a></small><br/>|;                                  = $msg
510    #                               = ( $style ? qq|<span$style>$msg</span>| : $msg )
511                                    . $spacer
512                                    . qq|<a target="editor" href="/editor+$path+$line" style="float: right;">+$line</a><br/>|;
513                          # FIXME <tt> should be <code> but CSS hates me                          # FIXME <tt> should be <code> but CSS hates me
514                  }                  }
515          }          }
516          close($log) || die "can't close $path: $!";          close($log) || die "can't close $path: $!";
517    
518            my $size = -s $path;
519    
520            my $warnings = join('',
521                    map { $warnings[ ( $pos + $_ ) % $max ] || '' } 0 .. $max
522            );
523    
524            my $s = length($warnings);
525    
526          return          return
527                    qq|<span class="frey-popup"><a target="editor" href="/editor+$path+$line" title="open $path level $level">warn</a><span>|                  # need to wrap into span so we can have links in warnings
528                  . $self->editor_links( $warnings )                    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>|
529                  . qq|</span></span>|                  . $warnings
530    #               . $self->editor_links( $warnings )
531                    . qq|</code></span></a>|
532                  ;                  ;
533  }  }
534    
# Line 484  sub backtrace { Line 554  sub backtrace {
554                  ) = caller($_) or last;                  ) = caller($_) or last;
555    
556                  push @backtrace,                  push @backtrace,
557                          qq|via $package from $path <a target="editor" href="/editor+$path+$line">$path</a>|;                          qq|via $package at $path line $line|;
558          }          }
559          warn "# backtrace: ", dump( @backtrace ) if @backtrace;          warn "# backtrace: ", dump( @backtrace ) if @backtrace;
560          return @backtrace;          return @backtrace;

Legend:
Removed from v.543  
changed lines
  Added in v.588

  ViewVC Help
Powered by ViewVC 1.1.26