/[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 460 by dpavlin, Wed Nov 19 17:57:48 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::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/;  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  has 'head' => (  use Frey::SVK;
         is => 'rw',  
         isa => 'ArrayRef[Str]',  
         default => sub { [ 'static/frey.css' ] },  
 );  
18    
19  has 'status' => (  our @head;
         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 },  
         ] },  
 );  
20    
21  has 'request_url' => (  has 'request_url' => (
22          is => 'rw',          is => 'rw',
# Line 47  has 'content_type' => ( Line 38  has 'content_type' => (
38          is => 'rw',          is => 'rw',
39          isa => 'Str',          isa => 'Str',
40          default => 'text/html',          default => 'text/html',
41            documentation => 'Content-type header',
42  );  );
43    
44  =head2 inline_smaller_than  has 'dump_max_bytes' => (
45            is => 'rw',
46  Inline JavaScript and CSS smaller than this size into page reducing          isa => 'Int',
47  round-trips to server.          default => 4096,
48            documentation => 'maximum dump size sent to browser before truncation',
49  =cut  );
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',
56    );
57    
58    has 'html_dump_width' => (
59            documentation => 'crop longer lines in dumps',
60            is => 'rw',
61            isa => 'Int',
62    #       required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them
63            default => 128,
64  );  );
65    
66  sub dom2html {  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
67  #       warn "## dom2html ",dump( @_ );  my $escape_re  = join '|' => keys %escape;
68          return Continuity::Widget::DomNode->create( @_ )->to_string;  
69    sub html_escape {
70            my ( $self, $html ) = @_;
71            $html =~ s/($escape_re)/$escape{$1}/g;
72            return $html;
73    }
74    
75    sub html_dump {
76            my ( $self, $dump ) = @_;
77            $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 popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }
87    sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }
88    
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 75  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 110  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 125  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 138  our $reload_counter = 0; Line 188  our $reload_counter = 0;
188    
189  =cut  =cut
190    
191    our @status;
192    sub status { @status };
193    
194    our $icon_html;
195    
196  sub page {  sub page {
197          my $self = shift;          my $self = shift;
198          my $a = {@_};          my $a = {@_};
199    
200            warn "## page ",dump($a);
201    
202          $reload_counter++;          $reload_counter++;
203    
204          my $status_line = '';          my $status_line = '';
205          foreach my $part ( @{ $self->status } ) {  
206                  if ( ref($part) ne 'HASH' ) {          foreach my $part ( @status ) {
                         warn "part not hash ",dump( $part ) ;  
                         #$self->status( $part );  
                         next;  
                 }  
207                  foreach my $name ( keys %$part ) {                  foreach my $name ( keys %$part ) {
208                          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|;  
209                  }                  }
210          }          }
211    
# Line 178  sub page { Line 222  sub page {
222                  $body = '<!-- no body -->';                  $body = '<!-- no body -->';
223          }          }
224    
225            $status_line .= $self->warnings_html;
226    
227            my      ($exit,$description) = ('exit','stop server');
228                    ($exit,$description) = ('restart','restart server')
229                    if $ENV{FREY_RESTART}; # tune labels on exit link
230    
231            my $right =
232                    qq|
233                            <span class="right">
234                            <a title="reload $url"  href="/reload$url">reload</a>
235                            <a title="$description" href="/exit$url">$exit</a>
236                            </span>
237                    |;
238    
239            my $svk = Frey::SVK->new;
240            my $info = $svk->info;
241            my $revision = $svk->info->{Revision} || '';
242            $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};
243    
244            $self->add_icon unless $icon_html;
245    
246          my $html = join("\n",          my $html = join("\n",
247                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
248                  $self->_head_html,                  $self->_head_html,
249                  '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',                  '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',
250                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
251                    ( $icon_html || '<!-- no icon -->' ),
252                  ( $a->{head} || '' ),                  ( $a->{head} || '' ),
253                  qq|                  qq|
254                  </head><body>                  </head><body>
255                  $body                  $body
256                  <div class="frey-status-line">                  <div class="frey-status-line">
257                          <a href="/">Frey</a> $Frey::VERSION                          <a href="/">Frey</a> $Frey::VERSION $revision
                         <a href="?reload=$reload_counter"><code>$url</code></a>  
258                          $status_line                          $status_line
259                            $right
260                  </div>                  </div>
261              </body></html>              </body></html>
262                  |,                  |,
# Line 201  sub page { Line 267  sub page {
267          return $html;          return $html;
268  }  }
269    
270    =head2 editor
271    
272    Create HTML editor link with optional line and title
273    
274      my $html = $self->editor( $class, $line, $title );
275    
276    =cut
277    
278    sub editor {
279            my ( $self, $class, $line, $title ) = @_;
280            confess "need class" unless $class;
281            if ( ! defined $title ) {
282                    $title  = "edit $class";
283                    $title .= " line $line" if $line;
284            }
285            $line  ||= 1;
286            qq|<a target="editor" href="/editor+$class+$line"| .
287            ( $title ? qq| title="$title"| : '' ) .
288            qq|>$class</a>|;
289    }
290    
291    =head2 editor_links
292    
293    Create HTML links to editor for perl error message
294    
295      my $html = $self->editor_links( $error )
296    
297    =cut
298    
299    sub editor_links {
300            my ( $self, $error ) = @_;
301    
302            $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
303                    {at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;
304    
305            $error =~ s{(via (?:package) "?)([\w:]+)("?)}
306                    {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
307    
308            return $error;
309    }
310    
311  sub error {  sub error {
312          my $self = shift;          my $self = shift;
313          my $error = join(" ", @_);          my $error = join(" ", @_);
314    
315            my @backtrace = $self->backtrace;
316            $error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace;
317    
318            warn "ERROR: $error\n";
319            return
320                    qq|<pre class="frey-error">|
321                    . $self->editor_links( $error ) .
322                    qq|</pre>|
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 {
337            my ( $self, $data ) = @_;
338            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 {
359            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 = '';
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 {
378            warn "## status parts ", dump( map { keys %$_ } @status );
379    }
380    
381    =for debug
382    
383    sub DEMOLISH {
384            my ( $self ) = @_;
385            warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status;
386    }
387    
388    =cut
389    
390    =head2 add_icon
391    
392      Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png
393      Frey::Foo->add_icon('warning'); # /static/icons/Frey/Foo/warning.png
394    
395    =cut
396    
397    sub icon_path {
398            my ($self,$class,$variant) = @_;
399            my $icon = $class;
400            $icon =~ s{::}{/}g;
401            $icon .= "/$variant" if $variant;
402            my $path = 'static/icons/' . $icon . '.png';
403            if ( -e $path ) {
404                    warn "# $class from $self icon_path $path" if $self->debug;
405                    return $path;
406            } else {
407                    $self->TODO( "add $path icon for $class" );
408                    return undef;
409            }
410    }
411    
412    sub add_icon {
413            my ($self,$variant) = @_;
414    
415            my $class = ref($self);
416            $class = $self->class if $self->can('class');
417            my $icon_path = $self->icon_path( $class, $variant ) || return;
418    
419            $icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|;
420            warn "# using icon $icon_path";
421    
422    =for later
423    
424            # FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work!
425            my $ico_path = $icon_path;
426            $ico_path =~ s{png$}{ico};
427            if ( ! -e $ico_path ) {
428                    system "convert $icon_path $ico_path";
429                    warn "# convert $icon_path $ico_path : $@";
430            }
431            $icon_html .= qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path">| if -e $ico_path;
432    
433    =cut
434    
435    }
436    
437    my $warn_colors = {
438            '#'  => '#444',
439            '##' => '#888',
440    };
441    
442    my $multiline_markers = {
443            '(' => ')',
444            '{' => '}',
445            '[' => ']',
446            '"' => '"',
447    };
448    
449    my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']';
450    warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re";
451    
452    sub log_path {
453            $Frey::Bootstrap::log_path || warn "no log_path?";
454    }
455    
456    sub warnings_html {
457            my ($self,$level) = shift;
458            $level ||= $self->debug,
459            my $path = $self->log_path;
460    
461            my $warnings;
462            my $line = 0;
463            my $multiline_end;
464    
465            open(my $log, '<', $path) || die "can't open $path: $!";
466            while(<$log>) {
467                    chomp;
468                    $line++;
469    
470                    my $style = '';
471    
472                    if ( $multiline_end ) {
473                            if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) {
474    #                               warn "## $line end of $multiline_end in '$_'\n";
475                                    undef $multiline_end;
476                            } else {
477    #                               warn "## $line skipped\n";
478                            }
479                    } elsif ( m{^(#*)\s+} ) {
480                            my $l = $1 ? length($1) : 0;
481                            if ( $l > $level ) {
482                                    undef $multiline_end;
483                                    $multiline_end = $multiline_markers->{$1} if m{($multiline_re)$};
484    #                               warn "## $line start $1 .. $multiline_end level $l > $level for '$_'\n" if $multiline_end;
485                                    next;
486                            }
487    
488                            $style = $warn_colors->{$1}
489                                    ? ' style="color:' . $warn_colors->{$1} . '"'
490                                    : '';
491    
492                            my $msg = $self->html_escape( $_ );
493                            my $spacer = ' ';
494                            if ( length($msg) > $self->html_dump_width ) {
495                                    $msg = substr( $msg, 0, $self->html_dump_width );
496                                    $spacer = '&hellip;'
497                            }
498                            $msg =~ s{^\s}{&nbsp;}g;
499                            $warnings .= qq|<tt$style>$msg</tt>$spacer<small><a target="editor" href="/editor+$path+$line">+$line</a></small><br/>|;
500                            # FIXME <tt> should be <code> but CSS hates me
501                    }
502            }
503            close($log) || die "can't close $path: $!";
504    
505            return
506                      qq|<span class="frey-popup"><a target="editor" href="/editor+$path+$line" title="open $path level $level">warn</a><span>|
507                    . $self->editor_links( $warnings )
508                    . qq|</span></span>|
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;          my @backtrace;
525          foreach ( 0 .. 5 ) {          foreach ( 0 .. 5 ) {
526                  my @caller = caller($_) or last;                  my (
527                  my @description = ( qw/                          $package,$path,$line
528                          package filename line                          # subroutine hasargs
529                          subroutine hasargs                          # wantarray evaltext is_require
530                          wantarray evaltext is_require                          # hints bitmask hinthash
531                          hints bitmask hinthash                  ) = caller($_) or last;
                 /);  
                 push @backtrace, join(' ',  
                         map {  
                                 $description[$_] . ': ' . dump $caller[$_]  
                         } ( 0 .. $#caller )  
                 );  
         }  
         if ( @backtrace ) {  
                 warn "# append backtrace: ", dump( @backtrace );  
                 $error .= "\n\t" . join( "\n\t", @backtrace );  
         }  
532    
533          warn "ERROR: $error\n";                  push @backtrace,
534          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}{at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;                          qq|via $package at $path line $line|;
535          $error =~ s{(via package ")([\w:]+)(")}{$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;          }
536          return qq|<pre class="frey-error">$error</pre>|;          warn "# backtrace: ", dump( @backtrace ) if @backtrace;
537            return @backtrace;
538  }  }
539    
540  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26