/[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 540 by dpavlin, Wed Nov 26 18:02:38 2008 UTC
# Line 1  Line 1 
1  package Frey::Web;  package Frey::Web;
2  use Moose::Role;  use Moose::Role;
3    
4    with 'Frey::Backtrace';
5    
6    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;
14    use Frey::ClassBrowser;
15    use Frey::INC;
16    
17    use Frey::SVK;
18    
19  has 'head' => (  has 'head' => (
20          is => 'rw',          is => 'rw',
21          isa => 'ArrayRef[Str]',          isa => 'ArrayRef[Str]',
22          default => sub { [ 'static/frey.css' ] },          default => sub { [ 'static/frey.css' ] },
23  );  );
24    
25  =head2 inline_smaller_than  has 'request_url' => (
26            is => 'rw',
27            isa => 'Uri', coerce => 1,
28            default => '/',
29    );
30    
31  Inline JavaScript and CSS smaller than this size into page reducing  has 'title' => (
32  round-trips to server.          is => 'rw',
33            isa => 'Str',
34            lazy => 1,
35            default => sub {
36                    my ($self) = @_;
37                    ref($self);
38            },
39    );
40    
41  =cut  has 'content_type' => (
42            is => 'rw',
43            isa => 'Str',
44            default => 'text/html',
45            documentation => 'Content-type header',
46    );
47    
48    has 'dump_max_bytes' => (
49            is => 'rw',
50            isa => 'Int',
51            default => 4096,
52            documentation => 'Maximum dump size sent to browser before truncation',
53    );
54    
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 48  sub _head_html { Line 103  sub _head_html {
103                          $out .= $self->_inline_path( $path ) ?                          $out .= $self->_inline_path( $path ) ?
104                                  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>| :
105                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
106                    } elsif ( $path =~ m{<.+>}s ) {
107                            $out .= $path;
108                  } else {                  } else {
109                          confess "don't know how to render $path";                          confess "don't know how to render $path";
110                  }                  }
# Line 62  sub _head_html { Line 119  sub _head_html {
119    
120    my $size = $o->add_head( 'path/to/external.css' );    my $size = $o->add_head( 'path/to/external.css' );
121    
122      $o->add_head( '<!-- html content -->' );
123    
124  =cut  =cut
125    
126  sub add_head {  sub add_head {
# Line 69  sub add_head { Line 128  sub add_head {
128          return if ! defined $path || $path eq '';          return if ! defined $path || $path eq '';
129          $path =~ s!^/!!;          $path =~ s!^/!!;
130    
131          if ( -e $path ) {          if ( $path =~ m{<.*>}s ) {
132                    push @{ $self->head }, $path;
133            } elsif ( -e $path ) {
134                  if ( $path =~ m/\.(?:js|css)$/ ) {                  if ( $path =~ m/\.(?:js|css)$/ ) {
135                          push @{ $self->head }, $path;                          push @{ $self->head }, $path;
136                  } else {                  } else {
137                          confess "can't add_head( $path ) it's not js or css";                          confess "can't add_head( $path ) it's not js or css";
138                  }                  }
139                    return -s $path;
140          } else {          } else {
141                  confess "can't find $path: $!";                  confess "can't find $path: $!";
142          }          }
143    
         return -s $path;  
144  }  }
145    
146  our $reload_counter = 0;  our $reload_counter = 0;
# Line 95  our $reload_counter = 0; Line 156  our $reload_counter = 0;
156    
157  =cut  =cut
158    
159  use Frey::Bookmarklet;  our @status;
160  use Frey::ClassBrowser;  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 $html = qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|          my $status_line = '';
188          . $self->_head_html  
189          . '<title>' . ( $a->{title} || ref($self) ) . '</title>'          unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup };
190          . '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'  #       unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup };
191          . ( $a->{head} || '' )          unshift @status, { 'INC' => Frey::INC->new->as_markup };
192          . '</head><body>'  
193          . ( $a->{body} || '<!-- no body -->' )          foreach my $part ( @status ) {
194          . qq|<div class="frey-status-line">                  foreach my $name ( keys %$part ) {
195                  <a href="/">Frey</a> $Frey::VERSION                          $status_line .= $self->popup( $name, $part->{$name} );
196                  <a href="?reload=$reload_counter"><code>| . dump( $ENV{'REQUEST_URI'} ) . qq|</code></a>                  }
197                  <span class="frey-popup">Bookmarklets<span>| . Frey::Bookmarklet->markup . qq|</span></span>          }
198                  <span class="frey-popup">ClassBrowser<span>| . Frey::ClassBrowser->markup . qq|</span></span>  
199                  <span class="frey-popup">ENV<span><code>| . dump( %ENV ) . qq|</code></span></span>          my $url = $self->request_url;
200            $url =~ s{\?reload=\d+}{};
201    
202            my $body = $a->{body};
203            $body ||= $self->as_markup if $self->can('as_markup');
204            if ( $self->content_type !~ m{html} ) {
205                    warn "# return only $self body ", $self->content_type;
206                    return $body
207            } elsif ( ! defined $body ) {
208                    warn "# no body";
209                    $body = '<!-- no body -->';
210            }
211    
212            $status_line .= $self->warnings_html;
213    
214            my      ($exit,$description) = ('exit','stop server');
215                    ($exit,$description) = ('restart','restart server')
216                    if $ENV{FREY_RESTART}; # tune labels on exit link
217    
218            my $right =
219                    qq|
220                            <span class="right">
221                            <a title="reload $url"  href="/reload$url">reload</a>
222                            <a title="$description" href="/exit$url">$exit</a>
223                            </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",
233                    qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
234                    $self->_head_html,
235                    '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',
236                    '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
237                    ( $icon_html || '<!-- no icon -->' ),
238                    ( $a->{head} || '' ),
239                    qq|
240                    </head><body>
241                    $body
242                    <div class="frey-status-line">
243                            <a href="/">Frey</a> $Frey::VERSION $revision
244                            $status_line
245                            $right
246                  </div>                  </div>
247              </body></html>              </body></html>
248          |;                  |,
249            );
250    
251          warn "## >>> page ",length($html), " bytes\n" if $self->debug;          warn "## >>> page ",length($html), " bytes\n" if $self->debug;
252    
253          return $html;          return $html;
254  }  }
255    
256  sub error {  =head2 editor
257    
258    Create HTML editor link with optional line and title
259    
260      my $html = $self->editor( $class, $line, $title );
261    
262    =cut
263    
264    sub editor {
265            my ( $self, $class, $line, $title ) = @_;
266            confess "need class" unless $class;
267            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"| .
273            ( $title ? qq| title="$title"| : '' ) .
274            qq|>$class</a>|;
275    }
276    
277    =head2 editor_links
278    
279    Create HTML links to editor for perl error message
280    
281      my $html = $self->editor_links( $error )
282    
283    =cut
284    
285    sub editor_links {
286          my ( $self, $error ) = @_;          my ( $self, $error ) = @_;
287          warn $error;  
288          $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+)}
289          return qq|<pre class="frey-error">$error</pre>|;                  {at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;
290    
291            $error =~ s{(via package ")([\w:]+)(")}
292                    {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
293    
294            return $error;
295    }
296    
297    sub error {
298            my $self = shift;
299            my $error = join(" ", @_);
300    
301            my @backtrace = $self->backtrace;
302            $error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace;
303    
304            warn "ERROR: $error\n";
305            return
306                    qq|<pre class="frey-error">|
307                    . $self->editor_links( $error ) .
308                    qq|</pre>|
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.385  
changed lines
  Added in v.540

  ViewVC Help
Powered by ViewVC 1.1.26