/[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 535 by dpavlin, Wed Nov 26 16:17:17 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::SVK;
16    
17  has 'head' => (  has 'head' => (
18          is => 'rw',          is => 'rw',
19          isa => 'ArrayRef[Str]',          isa => 'ArrayRef[Str]',
20          default => sub { [ 'static/frey.css' ] },          default => sub { [ 'static/frey.css' ] },
21  );  );
22    
23  =head2 inline_smaller_than  has 'request_url' => (
24            is => 'rw',
25            isa => 'Uri', coerce => 1,
26            default => '/',
27    );
28    
29  Inline JavaScript and CSS smaller than this size into page reducing  has 'title' => (
30  round-trips to server.          is => 'rw',
31            isa => 'Str',
32            lazy => 1,
33            default => sub {
34                    my ($self) = @_;
35                    ref($self);
36            },
37    );
38    
39  =cut  has 'content_type' => (
40            is => 'rw',
41            isa => 'Str',
42            default => 'text/html',
43            documentation => 'Content-type header',
44    );
45    
46    has 'dump_max_bytes' => (
47            is => 'rw',
48            isa => 'Int',
49            default => 4096,
50            documentation => 'Maximum dump size sent to browser before truncation',
51    );
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 'chop_warning' => (
61            is => 'rw',
62            isa => 'Int',
63            default => 80,
64            documentation => 'Crop lines longer',
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 = shift;
78            $self->html_escape( dump( @_ ) );
79    }
80    
81  sub dom2html {  sub dom2html {
82  #       warn "## dom2html ",dump( @_ );  #       warn "## dom2html ",dump( @_ );
83          return Continuity::Widget::DomNode->create( @_ )->to_string;          return Continuity::Widget::DomNode->create( @_ )->to_string;
# Line 48  sub _head_html { Line 101  sub _head_html {
101                          $out .= $self->_inline_path( $path ) ?                          $out .= $self->_inline_path( $path ) ?
102                                  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>| :
103                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
104                    } elsif ( $path =~ m{<.+>}s ) {
105                            $out .= $path;
106                  } else {                  } else {
107                          confess "don't know how to render $path";                          confess "don't know how to render $path";
108                  }                  }
# Line 62  sub _head_html { Line 117  sub _head_html {
117    
118    my $size = $o->add_head( 'path/to/external.css' );    my $size = $o->add_head( 'path/to/external.css' );
119    
120      $o->add_head( '<!-- html content -->' );
121    
122  =cut  =cut
123    
124  sub add_head {  sub add_head {
# Line 69  sub add_head { Line 126  sub add_head {
126          return if ! defined $path || $path eq '';          return if ! defined $path || $path eq '';
127          $path =~ s!^/!!;          $path =~ s!^/!!;
128    
129          if ( -e $path ) {          if ( $path =~ m{<.*>}s ) {
130                    push @{ $self->head }, $path;
131            } elsif ( -e $path ) {
132                  if ( $path =~ m/\.(?:js|css)$/ ) {                  if ( $path =~ m/\.(?:js|css)$/ ) {
133                          push @{ $self->head }, $path;                          push @{ $self->head }, $path;
134                  } else {                  } else {
135                          confess "can't add_head( $path ) it's not js or css";                          confess "can't add_head( $path ) it's not js or css";
136                  }                  }
137                    return -s $path;
138          } else {          } else {
139                  confess "can't find $path: $!";                  confess "can't find $path: $!";
140          }          }
141    
         return -s $path;  
142  }  }
143    
144  our $reload_counter = 0;  our $reload_counter = 0;
# Line 95  our $reload_counter = 0; Line 154  our $reload_counter = 0;
154    
155  =cut  =cut
156    
157  use Frey::Bookmarklet;  our @status;
158  use Frey::ClassBrowser;  sub status { @status };
159    
160    our $icon_html;
161    
162  sub page {  sub page {
163          my $self = shift;          my $self = shift;
164          my $a = {@_};          my $a = {@_};
165    
166            warn "## page ",dump($a);
167    
168          $reload_counter++;          $reload_counter++;
169    
170          my $html = qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|          my $status_line = '';
171          . $self->_head_html  
172          . '<title>' . ( $a->{title} || ref($self) ) . '</title>'          unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup };
173          . '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'          unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup };
174          . ( $a->{head} || '' )  
175          . '</head><body>'          foreach my $part ( @status ) {
176          . ( $a->{body} || '<!-- no body -->' )                  foreach my $name ( keys %$part ) {
177          . qq|<div class="frey-status-line">                          my $content = $part->{$name};
178                  <a href="/">Frey</a> $Frey::VERSION                          if ( ref($content) ) {
179                  <a href="?reload=$reload_counter"><code>| . dump( $ENV{'REQUEST_URI'} ) . qq|</code></a>                                  $content = '<code>' . dump($content) . '</code>';
180                  <span class="frey-popup">Bookmarklets<span>| . Frey::Bookmarklet->markup . qq|</span></span>                                  my $l = length($content);
181                  <span class="frey-popup">ClassBrowser<span>| . Frey::ClassBrowser->markup . qq|</span></span>                                  $content = qq|<span>$l bytes</span>| if $l > $self->dump_max_bytes;
182                  <span class="frey-popup">ENV<span><code>| . dump( %ENV ) . qq|</code></span></span>                          } else {
183                                    $content = qq|<span>$content</span>|;
184                            }
185                            warn "### part [$name] = ", length( $content ), " bytes" if $self->debug;
186                            $status_line .= qq|<span class="frey-popup">$name $content</span>\n|;
187                    }
188            }
189    
190            my $url = $self->request_url;
191            $url =~ s{\?reload=\d+}{};
192    
193            my $body = $a->{body};
194            $body ||= $self->as_markup if $self->can('as_markup');
195            if ( $self->content_type !~ m{html} ) {
196                    warn "# return only $self body ", $self->content_type;
197                    return $body
198            } elsif ( ! defined $body ) {
199                    warn "# no body";
200                    $body = '<!-- no body -->';
201            }
202    
203            $status_line .= $self->warnings_html;
204    
205            my      ($exit,$description) = ('exit','stop server');
206                    ($exit,$description) = ('restart','restart server')
207                    if $ENV{FREY_RESTART}; # tune labels on exit link
208    
209            my $right =
210                    qq|
211                            <span class="right">
212                            <a title="reload $url"  href="/reload$url">reload</a>
213                            <a title="$description" href="/exit$url">$exit</a>
214                            </span>
215                    |;
216    
217            my $info = Frey::SVK->info;
218            my $revision = Frey::SVK->info->{Revision} || '';
219            $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};
220    
221            $self->add_icon unless $icon_html;
222    
223            my $html = join("\n",
224                    qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
225                    $self->_head_html,
226                    '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',
227                    '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
228                    ( $icon_html || '<!-- no icon -->' ),
229                    ( $a->{head} || '' ),
230                    qq|
231                    </head><body>
232                    $body
233                    <div class="frey-status-line">
234                            <a href="/">Frey</a> $Frey::VERSION $revision
235                            $status_line
236                            $right
237                  </div>                  </div>
238              </body></html>              </body></html>
239          |;                  |,
240            );
241    
242          warn "## >>> page ",length($html), " bytes\n" if $self->debug;          warn "## >>> page ",length($html), " bytes\n" if $self->debug;
243    
244          return $html;          return $html;
245  }  }
246    
247  sub error {  =head2 editor
248    
249    Create HTML editor link with optional line and title
250    
251      my $html = $self->editor( $class, $line, $title );
252    
253    =cut
254    
255    sub editor {
256            my ( $self, $class, $line, $title ) = @_;
257            confess "need class" unless $class;
258            if ( ! defined $title ) {
259                    $title  = "edit $class";
260                    $title .= " line $line" if $line;
261            }
262            $line  ||= 1;
263            qq|<a target="editor" href="/editor+$class+$line"| .
264            ( $title ? qq| title="$title"| : '' ) .
265            qq|>$class</a>|;
266    }
267    
268    =head2 editor_links
269    
270    Create HTML links to editor for perl error message
271    
272      my $html = $self->editor_links( $error )
273    
274    =cut
275    
276    sub editor_links {
277          my ( $self, $error ) = @_;          my ( $self, $error ) = @_;
278          warn $error;  
279          $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+)}
280          return qq|<pre class="frey-error">$error</pre>|;                  {at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;
281    
282            $error =~ s{(via package ")([\w:]+)(")}
283                    {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
284    
285            return $error;
286    }
287    
288    sub error {
289            my $self = shift;
290            my $error = join(" ", @_);
291    
292            my @backtrace = $self->backtrace;
293            $error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace;
294    
295            warn "ERROR: $error\n";
296            return
297                    qq|<pre class="frey-error">|
298                    . $self->editor_links( $error ) .
299                    qq|</pre>|
300                    ;
301    }
302    
303    sub add_status {
304            my ( $self, $data ) = @_;
305            push @status, $data;
306    }
307    
308    sub clean_status {
309            @status = ();
310            $icon_html = '';
311    }
312    
313    sub status_parts {
314            warn "## status parts ", dump( map { keys %$_ } @status );
315    }
316    
317    sub DEMOLISH {
318            my ( $self ) = @_;
319            warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status;
320    }
321    
322    =head2 add_icon
323    
324      Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png
325      Frey::Foo->add_icon('warning'); # /static/icons/Frey/Foo/warning.png
326    
327    =cut
328    
329    sub icon_path {
330            my ($self,$class,$variant) = @_;
331            my $icon = $class;
332            $icon =~ s{::}{/}g;
333            $icon .= "/$variant" if $variant;
334            my $path = 'static/icons/' . $icon . '.png';
335            if ( -e $path ) {
336                    warn "# $class from $self icon_path $path";
337                    return $path;
338            } else {
339                    warn "TODO: add $path icon for $class";
340                    return undef;
341            }
342    }
343    
344    sub add_icon {
345            my ($self,$variant) = @_;
346    
347            my $class = ref($self);
348            $class = $self->class if $self->can('class');
349            my $icon_path = $self->icon_path( $class, $variant ) || return;
350    
351            $icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|;
352            warn "# using icon $icon_path";
353    
354    =for later
355    
356            # FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work!
357            my $ico_path = $icon_path;
358            $ico_path =~ s{png$}{ico};
359            if ( ! -e $ico_path ) {
360                    system "convert $icon_path $ico_path";
361                    warn "# convert $icon_path $ico_path : $@";
362            }
363            $icon_html .= qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path">| if -e $ico_path;
364    
365    =cut
366    
367    }
368    
369    my $warn_colors = {
370            '#'  => '#444',
371            '##' => '#888',
372    };
373    
374    my $multiline_markers = {
375            '(' => ')',
376            '{' => '}',
377            '[' => ']',
378            '"' => '"',
379    };
380    
381    my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']';
382    warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re";
383    
384    sub log_path {
385            $Frey::Bootstrap::log_path || warn "no log_path?";
386    }
387    
388    sub warnings_html {
389            my ($self,$level) = shift;
390            $level ||= $self->debug,
391            my $path = $self->log_path;
392    
393            my $warnings;
394            my $line = 0;
395            my $multiline_end;
396    
397            open(my $log, '<', $path) || die "can't open $path: $!";
398            while(<$log>) {
399                    chomp;
400                    $line++;
401    
402                    my $style = '';
403    
404                    if ( $multiline_end ) {
405                            if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) {
406                                    warn "## $line end of $multiline_end in '$_'\n";
407                                    undef $multiline_end;
408                            } else {
409                                    warn "## $line skipped\n";
410                            }
411                    } elsif ( m{^(#*)\s+} ) {
412                            my $l = $1 ? length($1) : 0;
413                            if ( $l > $level ) {
414                                    undef $multiline_end;
415                                    $multiline_end = $multiline_markers->{$1} if m{($multiline_re)$};
416                                    warn "## $line start $1 .. $multiline_end level $l > $level for '$_'\n" if $multiline_end;
417                                    next;
418                            }
419    
420                            $style = $warn_colors->{$1}
421                                    ? ' style="color:' . $warn_colors->{$1} . '"'
422                                    : '';
423    
424                            my $msg = $self->html_escape( $_ );
425                            my $spacer = ' ';
426                            if ( length($msg) > $self->chop_warning ) {
427                                    $msg = substr( $msg, 0, $self->chop_warning );
428                                    $spacer = '&hellip;'
429                            }
430                            $msg =~ s{^\s}{&nbsp;}g;
431                            $warnings .= qq|<tt$style>$msg</tt>$spacer<small><a target="editor" href="/editor+$path+$line">+$line</a></small><br/>|;
432                            # FIXME <tt> should be <code> but CSS hates me
433                    }
434            }
435            close($log) || die "can't close $path: $!";
436    
437            return
438                      qq|<span class="frey-popup"><a target="editor" href="/editor+$path+$line" title="open $path level $level">warn</a><span>|
439                    . $self->editor_links( $warnings )
440                    . qq|</span></span>|
441                    ;
442  }  }
443    
444  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26