/[Frey]/trunk/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 /trunk/lib/Frey/Web.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 527 by dpavlin, Wed Nov 26 02:35:59 2008 UTC revision 540 by dpavlin, Wed Nov 26 18:02:38 2008 UTC
# Line 12  use File::Slurp; Line 12  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' => (  has 'head' => (
# Line 50  has 'dump_max_bytes' => ( Line 52  has 'dump_max_bytes' => (
52          documentation => 'Maximum dump size sent to browser before truncation',          documentation => 'Maximum dump size sent to browser before truncation',
53  );  );
54    
 =head2 inline_smaller_than  
   
 Inline JavaScript and CSS smaller than this size into page reducing  
 round-trips to server.  
   
 =cut  
   
55  has 'inline_smaller_than' => (  has 'inline_smaller_than' => (
56          is => 'rw',          is => 'rw',
57          isa => 'Int',          isa => 'Int',
58          default => 10240,          default => 10240,
59            documentation => 'Inline JavaScript and CSS to reduce round-trips',
60    );
61    
62    has 'chop_warning' => (
63            is => 'rw',
64            isa => 'Int',
65            default => 80,
66            documentation => 'Crop lines longer',
67  );  );
68    
69    my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
70    my $escape_re  = join '|' => keys %escape;
71    
72    sub html_escape {
73            my ( $self, $html ) = @_;
74            $html =~ s/($escape_re)/$escape{$1}/g;
75            return $html;
76    }
77    
78    sub html_dump {
79            my $self = shift;
80            $self->html_escape( dump( @_ ) );
81    }
82    
83  sub dom2html {  sub dom2html {
84  #       warn "## dom2html ",dump( @_ );  #       warn "## dom2html ",dump( @_ );
85          return Continuity::Widget::DomNode->create( @_ )->to_string;          return Continuity::Widget::DomNode->create( @_ )->to_string;
# Line 144  sub status { @status }; Line 161  sub status { @status };
161    
162  our $icon_html;  our $icon_html;
163    
164    sub popup {
165            my ( $self, $name, $content, $full ) = @_;
166    
167            if ( ref($content) ) {
168                    $content = '<code>' . dump($content) . '</code>';
169                    my $l = length($content);
170                    $content = qq|<span>$l bytes</span>| if ! $full && $l > $self->dump_max_bytes;
171            } else {
172                    $content = qq|<span>$content</span>|;
173            }
174    
175            warn "## popup [$name] = ", length( $content ), " bytes" if $self->debug;
176            return qq|<span class="frey-popup">$name $content</span>\n|;
177    }
178    
179  sub page {  sub page {
180          my $self = shift;          my $self = shift;
181          my $a = {@_};          my $a = {@_};
# Line 155  sub page { Line 187  sub page {
187          my $status_line = '';          my $status_line = '';
188    
189          unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup };          unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup };
190          unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup };  #       unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup };
191            unshift @status, { 'INC' => Frey::INC->new->as_markup };
192    
193          foreach my $part ( @status ) {          foreach my $part ( @status ) {
194                  foreach my $name ( keys %$part ) {                  foreach my $name ( keys %$part ) {
195                          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|;  
196                  }                  }
197          }          }
198    
# Line 185  sub page { Line 209  sub page {
209                  $body = '<!-- no body -->';                  $body = '<!-- no body -->';
210          }          }
211    
212          my $warn_colors = {          $status_line .= $self->warnings_html;
                 '#'  => '#444',  
                 '##' => '#888',  
         };  
   
         $status_line  
                 .= qq|<span class="frey-popup">warn<span>|  
                 . $self->editor_links(  
                         join("", map {  
                                 warn "# $_";  
                                 my $style = '';  
                                 $style = $warn_colors->{$1}  
                                         ? ' style="color:' . $warn_colors->{$1} . '"'  
                                         : ''  
                                         if m{^(#+)};  
                                 qq|<tt$style>$_</tt><br/>|; # XXX <tt> should be <code> but CSS hates me  
                         } $self->warnings )  
                 )  
                 . qq|</span></span>|  
                 if $self->warnings;  
213    
214          my      ($exit,$description) = ('exit','stop server');          my      ($exit,$description) = ('exit','stop server');
215                  ($exit,$description) = ('restart','restart server')                  ($exit,$description) = ('restart','restart server')
# Line 320  sub status_parts { Line 325  sub status_parts {
325    
326  sub DEMOLISH {  sub DEMOLISH {
327          my ( $self ) = @_;          my ( $self ) = @_;
328          cluck "## DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status;          warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status;
329  }  }
330    
331  =head2 add_icon  =head2 add_icon
# Line 330  sub DEMOLISH { Line 335  sub DEMOLISH {
335    
336  =cut  =cut
337    
338  sub add_icon {  sub icon_path {
339          my ($self,$name) = @_;          my ($self,$class,$variant) = @_;
340          my $icon = ref($self);          my $icon = $class;
         $icon = $self->class if $self->can('class');  
341          $icon =~ s{::}{/}g;          $icon =~ s{::}{/}g;
342          $icon .= "/$name" if $name;          $icon .= "/$variant" if $variant;
343            my $path = 'static/icons/' . $icon . '.png';
344            if ( -e $path ) {
345                    warn "# $class from $self icon_path $path";
346                    return $path;
347            } else {
348                    warn "TODO: add $path icon for $class";
349                    return undef;
350            }
351    }
352    
353          my $icon_path = "static/icons/$icon.png";  sub add_icon {
354            my ($self,$variant) = @_;
355    
356            my $class = ref($self);
357            $class = $self->class if $self->can('class');
358            my $icon_path = $self->icon_path( $class, $variant ) || return;
359    
360          if ( -e $icon_path ) {          $icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|;
361                  $icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|;          warn "# using icon $icon_path";
                 warn "# using icon $icon_path";  
362    
363  =for later  =for later
364    
365                  # FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work!          # FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work!
366                  my $ico_path = $icon_path;          my $ico_path = $icon_path;
367                  $ico_path =~ s{png$}{ico};          $ico_path =~ s{png$}{ico};
368                  if ( ! -e $ico_path ) {          if ( ! -e $ico_path ) {
369                          system "convert $icon_path $ico_path";                  system "convert $icon_path $ico_path";
370                          warn "# convert $icon_path $ico_path : $@";                  warn "# convert $icon_path $ico_path : $@";
371                  }          }
372                  $icon_html .= qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path">| if -e $ico_path;          $icon_html .= qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path">| if -e $ico_path;
373    
374  =cut  =cut
375    
376          } else {  }
377                  warn "TODO add $icon_path icon";  
378    my $warn_colors = {
379            '#'  => '#444',
380            '##' => '#888',
381    };
382    
383    my $multiline_markers = {
384            '(' => ')',
385            '{' => '}',
386            '[' => ']',
387            '"' => '"',
388    };
389    
390    my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']';
391    warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re";
392    
393    sub log_path {
394            $Frey::Bootstrap::log_path || warn "no log_path?";
395    }
396    
397    sub warnings_html {
398            my ($self,$level) = shift;
399            $level ||= $self->debug,
400            my $path = $self->log_path;
401    
402            my $warnings;
403            my $line = 0;
404            my $multiline_end;
405    
406            open(my $log, '<', $path) || die "can't open $path: $!";
407            while(<$log>) {
408                    chomp;
409                    $line++;
410    
411                    my $style = '';
412    
413                    if ( $multiline_end ) {
414                            if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) {
415    #                               warn "## $line end of $multiline_end in '$_'\n";
416                                    undef $multiline_end;
417                            } else {
418    #                               warn "## $line skipped\n";
419                            }
420                    } elsif ( m{^(#*)\s+} ) {
421                            my $l = $1 ? length($1) : 0;
422                            if ( $l > $level ) {
423                                    undef $multiline_end;
424                                    $multiline_end = $multiline_markers->{$1} if m{($multiline_re)$};
425    #                               warn "## $line start $1 .. $multiline_end level $l > $level for '$_'\n" if $multiline_end;
426                                    next;
427                            }
428    
429                            $style = $warn_colors->{$1}
430                                    ? ' style="color:' . $warn_colors->{$1} . '"'
431                                    : '';
432    
433                            my $msg = $self->html_escape( $_ );
434                            my $spacer = ' ';
435                            if ( length($msg) > $self->chop_warning ) {
436                                    $msg = substr( $msg, 0, $self->chop_warning );
437                                    $spacer = '&hellip;'
438                            }
439                            $msg =~ s{^\s}{&nbsp;}g;
440                            $warnings .= qq|<tt$style>$msg</tt>$spacer<small><a target="editor" href="/editor+$path+$line">+$line</a></small><br/>|;
441                            # FIXME <tt> should be <code> but CSS hates me
442                    }
443          }          }
444            close($log) || die "can't close $path: $!";
445    
446            return
447                      qq|<span class="frey-popup"><a target="editor" href="/editor+$path+$line" title="open $path level $level">warn</a><span>|
448                    . $self->editor_links( $warnings )
449                    . qq|</span></span>|
450                    ;
451  }  }
452    
453  1;  1;

Legend:
Removed from v.527  
changed lines
  Added in v.540

  ViewVC Help
Powered by ViewVC 1.1.26