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

Legend:
Removed from v.385  
changed lines
  Added in v.584

  ViewVC Help
Powered by ViewVC 1.1.26