/[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 445 by dpavlin, Wed Nov 19 03:00:46 2008 UTC revision 519 by dpavlin, Tue Nov 25 17:15:18 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;  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::SVK;
16    
17  has 'head' => (  has 'head' => (
18          is => 'rw',          is => 'rw',
# Line 17  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->markup },  
                 { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->markup },  
         ] },  
 );  
   
23  has 'request_url' => (  has 'request_url' => (
24          is => 'rw',          is => 'rw',
25          isa => 'Uri', coerce => 1,          isa => 'Uri', coerce => 1,
# Line 43  has 'title' => ( Line 36  has 'title' => (
36          },          },
37  );  );
38    
39    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  =head2 inline_smaller_than  =head2 inline_smaller_than
54    
55  Inline JavaScript and CSS smaller than this size into page reducing  Inline JavaScript and CSS smaller than this size into page reducing
# Line 79  sub _head_html { Line 86  sub _head_html {
86                          $out .= $self->_inline_path( $path ) ?                          $out .= $self->_inline_path( $path ) ?
87                                  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>| :
88                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
89                  } elsif ( $path =~ m{<.+>} ) {                  } elsif ( $path =~ m{<.+>}s ) {
90                          $out .= $path;                          $out .= $path;
91                  } else {                  } else {
92                          confess "don't know how to render $path";                          confess "don't know how to render $path";
# Line 104  sub add_head { Line 111  sub add_head {
111          return if ! defined $path || $path eq '';          return if ! defined $path || $path eq '';
112          $path =~ s!^/!!;          $path =~ s!^/!!;
113    
114          if ( $path =~ m{<.*>} ) {          if ( $path =~ m{<.*>}s ) {
115                  push @{ $self->head }, $path;                  push @{ $self->head }, $path;
116          } elsif ( -e $path ) {          } elsif ( -e $path ) {
117                  if ( $path =~ m/\.(?:js|css)$/ ) {                  if ( $path =~ m/\.(?:js|css)$/ ) {
# Line 132  our $reload_counter = 0; Line 139  our $reload_counter = 0;
139    
140  =cut  =cut
141    
142    our @status;
143    sub status { @status };
144    
145  sub page {  sub page {
146          my $self = shift;          my $self = shift;
147          my $a = {@_};          my $a = {@_};
148    
149            warn "## page ",dump($a);
150    
151          $reload_counter++;          $reload_counter++;
152    
153          my $status_line = '';          my $status_line = '';
154          foreach my $part ( @{ $self->status } ) {  
155                  if ( ref($part) ne 'HASH' ) {          unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup };
156                          warn "part not hash ",dump( $part ) ;          unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup };
157                          #$self->status( $part );  
158                          next;          foreach my $part ( @status ) {
                 }  
159                  foreach my $name ( keys %$part ) {                  foreach my $name ( keys %$part ) {
160                          my $content = $part->{$name};                          my $content = $part->{$name};
161                          if ( ref($content) ) {                          if ( ref($content) ) {
162                                  $content = '<code>' . dump($content) . '</code>';                                  $content = '<code>' . dump($content) . '</code>';
163                                  my $l = length($content);                                  my $l = length($content);
164                                  $content = qq|<span>$l bytes</span>| if $l > 1024;                                  $content = qq|<span>$l bytes</span>| if $l > $self->dump_max_bytes;
165                          } else {                          } else {
166                                  $content = qq|<span>$content</span>|;                                  $content = qq|<span>$content</span>|;
167                          }                          }
# Line 162  sub page { Line 173  sub page {
173          my $url = $self->request_url;          my $url = $self->request_url;
174          $url =~ s{\?reload=\d+}{};          $url =~ s{\?reload=\d+}{};
175    
176            my $body = $a->{body};
177            $body ||= $self->as_markup if $self->can('as_markup');
178            if ( $self->content_type !~ m{html} ) {
179                    warn "# return only $self body ", $self->content_type;
180                    return $body
181            } elsif ( ! defined $body ) {
182                    warn "# no body";
183                    $body = '<!-- no body -->';
184            }
185    
186            my $warn_colors = {
187                    '#'  => '#444',
188                    '##' => '#888',
189            };
190    
191            $status_line
192                    .= qq|<span class="frey-popup">warn<span>|
193                    . $self->editor_links(
194                            join("", map {
195                                    warn "# $_";
196                                    my $style = '';
197                                    $style = $warn_colors->{$1}
198                                            ? ' style="color:' . $warn_colors->{$1} . '"'
199                                            : ''
200                                            if m{^(#+)};
201                                    qq|<tt$style>$_</tt><br/>|; # XXX <tt> should be <code> but CSS hates me
202                            } $self->warnings )
203                    )
204                    . qq|</span></span>|
205                    if $self->warnings;
206    
207            my      ($exit,$description) = ('exit','stop server');
208                    ($exit,$description) = ('restart','restart server')
209                    if $ENV{FREY_RESTART}; # tune labels on exit link
210    
211            my $right =
212                    qq|
213                            <span class="right">
214                            <a title="reload $url"  href="/reload$url">reload</a>
215                            <a title="$description" href="/exit$url">$exit</a>
216                            </span>
217                    |;
218    
219            my $info = Frey::SVK->info;
220            my $revision = Frey::SVK->info->{Revision} || '';
221            $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};
222    
223          my $html = join("\n",          my $html = join("\n",
224                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
225                  $self->_head_html,                  $self->_head_html,
226                  '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',                  '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',
227                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
228                  ( $a->{head} || '' ),                  ( $a->{head} || '' ),
                 '</head><body>',  
                 ( $a->{body} || $self->markup || '<!-- no body -->' ),  
229                  qq|                  qq|
230                    </head><body>
231                    $body
232                  <div class="frey-status-line">                  <div class="frey-status-line">
233                          <a href="/">Frey</a> $Frey::VERSION                          <a href="/">Frey</a> $Frey::VERSION $revision
                         <a href="?reload=$reload_counter"><code>$url</code></a>  
234                          $status_line                          $status_line
235                            $right
236                  </div>                  </div>
237              </body></html>              </body></html>
238                  |,                  |,
# Line 185  sub page { Line 243  sub page {
243          return $html;          return $html;
244  }  }
245    
246    =head2 editor
247    
248    Create HTML editor link with optional line and title
249    
250      my $html = $self->editor( $class, $line, $title );
251    
252    =cut
253    
254    sub editor {
255            my ( $self, $class, $line, $title ) = @_;
256            confess "need class" unless $class;
257            if ( ! defined $title ) {
258                    $title  = "edit $class";
259                    $title .= " line $line" if $line;
260            }
261            $line  ||= 1;
262            qq|<a target="editor" href="/editor+$class+$line"| .
263            ( $title ? qq| title="$title"| : '' ) .
264            qq|>$class</a>|;
265    }
266    
267    =head2 editor_links
268    
269    Create HTML links to editor for perl error message
270    
271      my $html = $self->editor_links( $error )
272    
273    =cut
274    
275    sub editor_links {
276            my ( $self, $error ) = @_;
277    
278            $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
279                    {at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;
280    
281            $error =~ s{(via package ")([\w:]+)(")}
282                    {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
283    
284            return $error;
285    }
286    
287  sub error {  sub error {
288          my $self = shift;          my $self = shift;
289          my $error = join(" ", @_);          my $error = join(" ", @_);
290          my ($package, $filename, $line) = caller;  
291          $error .= " at $filename line $line" if $error !~ m{ at };          my @backtrace = $self->backtrace;
292          warn "WARN: $error\n";          $error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace;
293          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}{at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;  
294          $error =~ s{(via package ")([\w:]+)(")}{$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;          warn "ERROR: $error\n";
295          return qq|<pre class="frey-error">$error</pre>|;          return
296                    qq|<pre class="frey-error">|
297                    . $self->editor_links( $error ) .
298                    qq|</pre>|
299                    ;
300    }
301    
302    sub add_status {
303            my ( $self, $data ) = @_;
304            push @status, $data;
305    }
306    
307    sub clean_status {
308            @status = ();
309    }
310    
311    sub status_parts {
312            warn "## status parts ", dump( map { keys %$_ } @status );
313    }
314    
315    sub DEMOLISH {
316            my ( $self ) = @_;
317            cluck "## DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status;
318  }  }
319    
320  1;  1;

Legend:
Removed from v.445  
changed lines
  Added in v.519

  ViewVC Help
Powered by ViewVC 1.1.26