/[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 465 by dpavlin, Wed Nov 19 19:11:52 2008 UTC revision 543 by dpavlin, Wed Nov 26 19:43:03 2008 UTC
# Line 1  Line 1 
1  package Frey::Web;  package Frey::Web;
2  use Moose::Role;  use Moose::Role;
3    
 with 'Frey::Backtrace';  
   
4  use Frey::Types;  use Frey::Types;
5    
6  use Continuity::Widget::DomNode;  use Continuity::Widget::DomNode;
7  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
8  use Carp qw/confess/;  use Carp qw/confess cluck/;
9  use File::Slurp;  use File::Slurp;
10    
11  use Frey::Bookmarklet;  use Frey::Bookmarklet;
12  use Frey::ClassBrowser;  use Frey::ClassBrowser;
13    use Frey::INC;
14    
15    use Frey::SVK;
16    
17  has 'head' => (  has 'head' => (
18          is => 'rw',          is => 'rw',
# Line 19  has 'head' => ( Line 20  has 'head' => (
20          default => sub { [ 'static/frey.css' ] },          default => sub { [ 'static/frey.css' ] },
21  );  );
22    
 has 'status' => (  
         is => 'rw',  
         isa => 'ArrayRef[HashRef[Str]]',  
         lazy => 1,  
         default => sub { [  
                 { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },  
                 { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup },  
         ] },  
 );  
   
23  has 'request_url' => (  has 'request_url' => (
24          is => 'rw',          is => 'rw',
25          isa => 'Uri', coerce => 1,          isa => 'Uri', coerce => 1,
# Line 49  has 'content_type' => ( Line 40  has 'content_type' => (
40          is => 'rw',          is => 'rw',
41          isa => 'Str',          isa => 'Str',
42          default => 'text/html',          default => 'text/html',
43            documentation => 'Content-type header',
44  );  );
45    
46  =head2 inline_smaller_than  has 'dump_max_bytes' => (
47            is => 'rw',
48  Inline JavaScript and CSS smaller than this size into page reducing          isa => 'Int',
49  round-trips to server.          default => 4096,
50            documentation => 'maximum dump size sent to browser before truncation',
51  =cut  );
52    
53  has 'inline_smaller_than' => (  has 'inline_smaller_than' => (
54          is => 'rw',          is => 'rw',
55          isa => 'Int',          isa => 'Int',
56          default => 10240,          default => 10240,
57            documentation => 'inline JavaScript and CSS to reduce round-trips',
58    );
59    
60    has 'html_dump_width' => (
61            documentation => 'crop longer lines in dumps',
62            is => 'rw',
63            isa => 'Int',
64    #       required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them
65            default => 128,
66  );  );
67    
68    my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
69    my $escape_re  = join '|' => keys %escape;
70    
71    sub html_escape {
72            my ( $self, $html ) = @_;
73            $html =~ s/($escape_re)/$escape{$1}/g;
74            return $html;
75    }
76    
77    sub html_dump {
78            my ( $self, $dump ) = @_;
79            $dump = dump( $dump ) if ref($dump);
80            my $width = $self->html_dump_width;
81            $dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs;
82            $dump = $self->html_escape( $dump );
83            $dump =~ s{\Q...\E}{&hellip;}gs;
84            return "<code>$dump</code>";
85    }
86    
87  sub dom2html {  sub dom2html {
88  #       warn "## dom2html ",dump( @_ );  #       warn "## dom2html ",dump( @_ );
89          return Continuity::Widget::DomNode->create( @_ )->to_string;          return Continuity::Widget::DomNode->create( @_ )->to_string;
# Line 140  our $reload_counter = 0; Line 160  our $reload_counter = 0;
160    
161  =cut  =cut
162    
163    our @status;
164    sub status { @status };
165    
166    our $icon_html;
167    
168    sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }
169    sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }
170    
171    sub popup_dropdown {
172            my ( $self, $type, $name, $content, $full ) = @_;
173    
174            if ( ref($content) ) {
175                    $content = $self->html_dump($content);
176    #               my $l = length($content);
177    #               $content = qq|<span>$l bytes</span>| if ! $full && $l > $self->dump_max_bytes;
178            } else {
179                    $content = qq|<span>$content</span>|;
180            }
181    
182            warn "## $type [$name] = ", length( $content ), " bytes" if $self->debug;
183    
184            if ( $name =~ m{::} && ! $name =~ m{<(\w+).+?/\1>} ) {
185                    return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|;
186            } else {
187                    return qq|<span class="frey-$type">$name $content</span>\n|;
188            }
189    }
190    
191  sub page {  sub page {
192          my $self = shift;          my $self = shift;
193          my $a = {@_};          my $a = {@_};
194    
195            warn "## page ",dump($a);
196    
197          $reload_counter++;          $reload_counter++;
198    
199          my $status_line = '';          my $status_line = '';
200          foreach my $part ( @{ $self->status } ) {  
201                  if ( ref($part) ne 'HASH' ) {          unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup };
202                          warn "part not hash ",dump( $part ) ;  #       unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup };
203                          #$self->status( $part );          unshift @status, { 'INC' => Frey::INC->new->as_markup };
204                          next;  
205                  }          foreach my $part ( @status ) {
206                  foreach my $name ( keys %$part ) {                  foreach my $name ( keys %$part ) {
207                          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 > 1024;  
                         } 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|;  
208                  }                  }
209          }          }
210    
# Line 180  sub page { Line 221  sub page {
221                  $body = '<!-- no body -->';                  $body = '<!-- no body -->';
222          }          }
223    
224            $status_line .= $self->warnings_html;
225    
226            my      ($exit,$description) = ('exit','stop server');
227                    ($exit,$description) = ('restart','restart server')
228                    if $ENV{FREY_RESTART}; # tune labels on exit link
229    
230            my $right =
231                    qq|
232                            <span class="right">
233                            <a title="reload $url"  href="/reload$url">reload</a>
234                            <a title="$description" href="/exit$url">$exit</a>
235                            </span>
236                    |;
237    
238            my $info = Frey::SVK->info;
239            my $revision = Frey::SVK->info->{Revision} || '';
240            $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};
241    
242            $self->add_icon unless $icon_html;
243    
244          my $html = join("\n",          my $html = join("\n",
245                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
246                  $self->_head_html,                  $self->_head_html,
247                  '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',                  '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',
248                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
249                    ( $icon_html || '<!-- no icon -->' ),
250                  ( $a->{head} || '' ),                  ( $a->{head} || '' ),
251                  qq|                  qq|
252                  </head><body>                  </head><body>
253                  $body                  $body
254                  <div class="frey-status-line">                  <div class="frey-status-line">
255                          <a href="/">Frey</a> $Frey::VERSION                          <a href="/">Frey</a> $Frey::VERSION $revision
                         <a href="?reload=$reload_counter"><code>$url</code></a>  
256                          $status_line                          $status_line
257                            $right
258                  </div>                  </div>
259              </body></html>              </body></html>
260                  |,                  |,
# Line 203  sub page { Line 265  sub page {
265          return $html;          return $html;
266  }  }
267    
268    =head2 editor
269    
270    Create HTML editor link with optional line and title
271    
272      my $html = $self->editor( $class, $line, $title );
273    
274    =cut
275    
276    sub editor {
277            my ( $self, $class, $line, $title ) = @_;
278            confess "need class" unless $class;
279            if ( ! defined $title ) {
280                    $title  = "edit $class";
281                    $title .= " line $line" if $line;
282            }
283            $line  ||= 1;
284            qq|<a target="editor" href="/editor+$class+$line"| .
285            ( $title ? qq| title="$title"| : '' ) .
286            qq|>$class</a>|;
287    }
288    
289    =head2 editor_links
290    
291    Create HTML links to editor for perl error message
292    
293      my $html = $self->editor_links( $error )
294    
295    =cut
296    
297    sub editor_links {
298            my ( $self, $error ) = @_;
299    
300            $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
301                    {at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;
302    
303            $error =~ s{(via package ")([\w:]+)(")}
304                    {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
305    
306            return $error;
307    }
308    
309  sub error {  sub error {
310          my $self = shift;          my $self = shift;
311          my $error = join(" ", @_);          my $error = join(" ", @_);
# Line 211  sub error { Line 314  sub error {
314          $error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace;          $error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace;
315    
316          warn "ERROR: $error\n";          warn "ERROR: $error\n";
317          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}          return
318                  {at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;                  qq|<pre class="frey-error">|
319          $error =~ s{(via package ")([\w:]+)(")}                  . $self->editor_links( $error ) .
320                  {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;                  qq|</pre>|
321                    ;
322    }
323    
324    sub add_status {
325            my ( $self, $data ) = @_;
326            push @status, $data;
327    }
328    
329    sub clean_status {
330            @status = ();
331            $icon_html = '';
332    }
333    
334    sub status_parts {
335            warn "## status parts ", dump( map { keys %$_ } @status );
336    }
337    
338    sub DEMOLISH {
339            my ( $self ) = @_;
340            warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status;
341    }
342    
343    =head2 add_icon
344    
345      Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png
346      Frey::Foo->add_icon('warning'); # /static/icons/Frey/Foo/warning.png
347    
348    =cut
349    
350    sub icon_path {
351            my ($self,$class,$variant) = @_;
352            my $icon = $class;
353            $icon =~ s{::}{/}g;
354            $icon .= "/$variant" if $variant;
355            my $path = 'static/icons/' . $icon . '.png';
356            if ( -e $path ) {
357                    warn "# $class from $self icon_path $path";
358                    return $path;
359            } else {
360                    warn "TODO: add $path icon for $class";
361                    return undef;
362            }
363    }
364    
365    sub add_icon {
366            my ($self,$variant) = @_;
367    
368          return qq|<pre class="frey-error">$error</pre>|;          my $class = ref($self);
369            $class = $self->class if $self->can('class');
370            my $icon_path = $self->icon_path( $class, $variant ) || return;
371    
372            $icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|;
373            warn "# using icon $icon_path";
374    
375    =for later
376    
377            # FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work!
378            my $ico_path = $icon_path;
379            $ico_path =~ s{png$}{ico};
380            if ( ! -e $ico_path ) {
381                    system "convert $icon_path $ico_path";
382                    warn "# convert $icon_path $ico_path : $@";
383            }
384            $icon_html .= qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path">| if -e $ico_path;
385    
386    =cut
387    
388    }
389    
390    my $warn_colors = {
391            '#'  => '#444',
392            '##' => '#888',
393    };
394    
395    my $multiline_markers = {
396            '(' => ')',
397            '{' => '}',
398            '[' => ']',
399            '"' => '"',
400    };
401    
402    my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']';
403    warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re";
404    
405    sub log_path {
406            $Frey::Bootstrap::log_path || warn "no log_path?";
407    }
408    
409    sub warnings_html {
410            my ($self,$level) = shift;
411            $level ||= $self->debug,
412            my $path = $self->log_path;
413    
414            my $warnings;
415            my $line = 0;
416            my $multiline_end;
417    
418            open(my $log, '<', $path) || die "can't open $path: $!";
419            while(<$log>) {
420                    chomp;
421                    $line++;
422    
423                    my $style = '';
424    
425                    if ( $multiline_end ) {
426                            if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) {
427    #                               warn "## $line end of $multiline_end in '$_'\n";
428                                    undef $multiline_end;
429                            } else {
430    #                               warn "## $line skipped\n";
431                            }
432                    } elsif ( m{^(#*)\s+} ) {
433                            my $l = $1 ? length($1) : 0;
434                            if ( $l > $level ) {
435                                    undef $multiline_end;
436                                    $multiline_end = $multiline_markers->{$1} if m{($multiline_re)$};
437    #                               warn "## $line start $1 .. $multiline_end level $l > $level for '$_'\n" if $multiline_end;
438                                    next;
439                            }
440    
441                            $style = $warn_colors->{$1}
442                                    ? ' style="color:' . $warn_colors->{$1} . '"'
443                                    : '';
444    
445                            my $msg = $self->html_escape( $_ );
446                            my $spacer = ' ';
447                            if ( length($msg) > $self->html_dump_width ) {
448                                    $msg = substr( $msg, 0, $self->html_dump_width );
449                                    $spacer = '&hellip;'
450                            }
451                            $msg =~ s{^\s}{&nbsp;}g;
452                            $warnings .= qq|<tt$style>$msg</tt>$spacer<small><a target="editor" href="/editor+$path+$line">+$line</a></small><br/>|;
453                            # FIXME <tt> should be <code> but CSS hates me
454                    }
455            }
456            close($log) || die "can't close $path: $!";
457    
458            return
459                      qq|<span class="frey-popup"><a target="editor" href="/editor+$path+$line" title="open $path level $level">warn</a><span>|
460                    . $self->editor_links( $warnings )
461                    . qq|</span></span>|
462                    ;
463    }
464    
465    
466    =head2 backtrace
467    
468    Show backtrace with links to editor
469    
470      my @backtrace = $self->backtrace;
471    
472    =cut
473    
474    sub backtrace {
475            my ($self) = @_;
476    
477            my @backtrace;
478            foreach ( 0 .. 5 ) {
479                    my (
480                            $package,$path,$line
481                            # subroutine hasargs
482                            # wantarray evaltext is_require
483                            # hints bitmask hinthash
484                    ) = caller($_) or last;
485    
486                    push @backtrace,
487                            qq|via $package from $path <a target="editor" href="/editor+$path+$line">$path</a>|;
488            }
489            warn "# backtrace: ", dump( @backtrace ) if @backtrace;
490            return @backtrace;
491  }  }
492    
493  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26