/[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 482 by dpavlin, Thu Nov 20 15:23:13 2008 UTC revision 540 by dpavlin, Wed Nov 26 18:02:38 2008 UTC
# Line 7  use Frey::Types; Line 7  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/;  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;
18    
19  has 'head' => (  has 'head' => (
20          is => 'rw',          is => 'rw',
# Line 19  has 'head' => ( Line 22  has 'head' => (
22          default => sub { [ 'static/frey.css' ] },          default => sub { [ 'static/frey.css' ] },
23  );  );
24    
 has 'status' => (  
         is => 'rw',  
         isa => 'ArrayRef[HashRef[Str]]',  
         lazy => 1,  
         default => sub { [  
                 { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup },  
                 { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },  
         ] },  
 );  
   
25  has 'request_url' => (  has 'request_url' => (
26          is => 'rw',          is => 'rw',
27          isa => 'Uri', coerce => 1,          isa => 'Uri', coerce => 1,
# Line 59  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 148  our $reload_counter = 0; Line 156  our $reload_counter = 0;
156    
157  =cut  =cut
158    
159    our @status;
160    sub status { @status };
161    
162    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 = {@_};
182    
183            warn "## page ",dump($a);
184    
185          $reload_counter++;          $reload_counter++;
186    
187          my $status_line = '';          my $status_line = '';
188          foreach my $part ( @{ $self->status } ) {  
189                  if ( ref($part) ne 'HASH' ) {          unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup };
190                          warn "part not hash ",dump( $part ) ;  #       unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup };
191                          #$self->status( $part );          unshift @status, { 'INC' => Frey::INC->new->as_markup };
192                          next;  
193                  }          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 188  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 216  sub page { Line 218  sub page {
218          my $right =          my $right =
219                  qq|                  qq|
220                          <span class="right">                          <span class="right">
221                          <a title="reload"  href="/reload$url"><code>$url</code></a>                          <a title="reload $url"  href="/reload$url">reload</a>
222                          <a title="$description" href="/exit$url">$exit</a>                          <a title="$description" href="/exit$url">$exit</a>
223                          </span>                          </span>
224                  |;                  |;
225    
226            my $info = Frey::SVK->info;
227            my $revision = Frey::SVK->info->{Revision} || '';
228            $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};
229    
230            $self->add_icon unless $icon_html;
231    
232          my $html = join("\n",          my $html = join("\n",
233                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
234                  $self->_head_html,                  $self->_head_html,
235                  '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',                  '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',
236                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
237                    ( $icon_html || '<!-- no icon -->' ),
238                  ( $a->{head} || '' ),                  ( $a->{head} || '' ),
239                  qq|                  qq|
240                  </head><body>                  </head><body>
241                  $body                  $body
242                  <div class="frey-status-line">                  <div class="frey-status-line">
243                          <a href="/">Frey</a> $Frey::VERSION                          <a href="/">Frey</a> $Frey::VERSION $revision
244                          $status_line                          $status_line
245                          $right                          $right
246                  </div>                  </div>
# Line 255  Create HTML editor link with optional li Line 264  Create HTML editor link with optional li
264  sub editor {  sub editor {
265          my ( $self, $class, $line, $title ) = @_;          my ( $self, $class, $line, $title ) = @_;
266          confess "need class" unless $class;          confess "need class" unless $class;
267          $line ||= 1;          if ( ! defined $title ) {
268                    $title  = "edit $class";
269                    $title .= " line $line" if $line;
270            }
271            $line  ||= 1;
272          qq|<a target="editor" href="/editor+$class+$line"| .          qq|<a target="editor" href="/editor+$class+$line"| .
273          ( $title ? qq| title="$title"| : '' ) .          ( $title ? qq| title="$title"| : '' ) .
274          qq|>$class</a>|;          qq|>$class</a>|;
# Line 296  sub error { Line 309  sub error {
309                  ;                  ;
310  }  }
311    
312    sub add_status {
313            my ( $self, $data ) = @_;
314            push @status, $data;
315    }
316    
317    sub clean_status {
318            @status = ();
319            $icon_html = '';
320    }
321    
322    sub status_parts {
323            warn "## status parts ", dump( map { keys %$_ } @status );
324    }
325    
326    sub DEMOLISH {
327            my ( $self ) = @_;
328            warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status;
329    }
330    
331    =head2 add_icon
332    
333      Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png
334      Frey::Foo->add_icon('warning'); # /static/icons/Frey/Foo/warning.png
335    
336    =cut
337    
338    sub icon_path {
339            my ($self,$class,$variant) = @_;
340            my $icon = $class;
341            $icon =~ s{::}{/}g;
342            $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    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            $icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|;
361            warn "# using icon $icon_path";
362    
363    =for later
364    
365            # FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work!
366            my $ico_path = $icon_path;
367            $ico_path =~ s{png$}{ico};
368            if ( ! -e $ico_path ) {
369                    system "convert $icon_path $ico_path";
370                    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;
373    
374    =cut
375    
376    }
377    
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.482  
changed lines
  Added in v.540

  ViewVC Help
Powered by ViewVC 1.1.26