/[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 482 by dpavlin, Thu Nov 20 15:23:13 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/;
11  use File::Slurp;  use File::Slurp;
12    
13    use Frey::Bookmarklet;
14    use Frey::ClassBrowser;
15    
16  has 'head' => (  has 'head' => (
17          is => 'rw',          is => 'rw',
18          isa => 'ArrayRef[Str]',          isa => 'ArrayRef[Str]',
19          default => sub { [ 'static/frey.css' ] },          default => sub { [ 'static/frey.css' ] },
20  );  );
21    
22    has 'status' => (
23            is => 'rw',
24            isa => 'ArrayRef[HashRef[Str]]',
25            lazy => 1,
26            default => sub { [
27                    { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup },
28                    { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },
29            ] },
30    );
31    
32    has 'request_url' => (
33            is => 'rw',
34            isa => 'Uri', coerce => 1,
35            default => '/',
36    );
37    
38    has 'title' => (
39            is => 'rw',
40            isa => 'Str',
41            lazy => 1,
42            default => sub {
43                    my ($self) = @_;
44                    ref($self);
45            },
46    );
47    
48    has 'content_type' => (
49            is => 'rw',
50            isa => 'Str',
51            default => 'text/html',
52            documentation => 'Content-type header',
53    );
54    
55    has 'dump_max_bytes' => (
56            is => 'rw',
57            isa => 'Int',
58            default => 4096,
59            documentation => 'Maximum dump size sent to browser before truncation',
60    );
61    
62  =head2 inline_smaller_than  =head2 inline_smaller_than
63    
64  Inline JavaScript and CSS smaller than this size into page reducing  Inline JavaScript and CSS smaller than this size into page reducing
# Line 48  sub _head_html { Line 95  sub _head_html {
95                          $out .= $self->_inline_path( $path ) ?                          $out .= $self->_inline_path( $path ) ?
96                                  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>| :
97                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
98                    } elsif ( $path =~ m{<.+>}s ) {
99                            $out .= $path;
100                  } else {                  } else {
101                          confess "don't know how to render $path";                          confess "don't know how to render $path";
102                  }                  }
# Line 62  sub _head_html { Line 111  sub _head_html {
111    
112    my $size = $o->add_head( 'path/to/external.css' );    my $size = $o->add_head( 'path/to/external.css' );
113    
114      $o->add_head( '<!-- html content -->' );
115    
116  =cut  =cut
117    
118  sub add_head {  sub add_head {
# Line 69  sub add_head { Line 120  sub add_head {
120          return if ! defined $path || $path eq '';          return if ! defined $path || $path eq '';
121          $path =~ s!^/!!;          $path =~ s!^/!!;
122    
123          if ( -e $path ) {          if ( $path =~ m{<.*>}s ) {
124                    push @{ $self->head }, $path;
125            } elsif ( -e $path ) {
126                  if ( $path =~ m/\.(?:js|css)$/ ) {                  if ( $path =~ m/\.(?:js|css)$/ ) {
127                          push @{ $self->head }, $path;                          push @{ $self->head }, $path;
128                  } else {                  } else {
129                          confess "can't add_head( $path ) it's not js or css";                          confess "can't add_head( $path ) it's not js or css";
130                  }                  }
131                    return -s $path;
132          } else {          } else {
133                  confess "can't find $path: $!";                  confess "can't find $path: $!";
134          }          }
135    
         return -s $path;  
136  }  }
137    
138  our $reload_counter = 0;  our $reload_counter = 0;
# Line 95  our $reload_counter = 0; Line 148  our $reload_counter = 0;
148    
149  =cut  =cut
150    
 use Frey::Bookmarklet;  
 use Frey::ClassBrowser;  
   
151  sub page {  sub page {
152          my $self = shift;          my $self = shift;
153          my $a = {@_};          my $a = {@_};
154    
155          $reload_counter++;          $reload_counter++;
156    
157          my $html = qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|          my $status_line = '';
158          . $self->_head_html          foreach my $part ( @{ $self->status } ) {
159          . '<title>' . ( $a->{title} || ref($self) ) . '</title>'                  if ( ref($part) ne 'HASH' ) {
160          . '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'                          warn "part not hash ",dump( $part ) ;
161          . ( $a->{head} || '' )                          #$self->status( $part );
162          . '</head><body>'                          next;
163          . ( $a->{body} || '<!-- no body -->' )                  }
164          . qq|<div class="frey-status-line">                  foreach my $name ( keys %$part ) {
165                  <a href="/">Frey</a> $Frey::VERSION                          my $content = $part->{$name};
166                  <a href="?reload=$reload_counter"><code>| . dump( $ENV{'REQUEST_URI'} ) . qq|</code></a>                          if ( ref($content) ) {
167                  <span class="frey-popup">Bookmarklets<span>| . Frey::Bookmarklet->markup . qq|</span></span>                                  $content = '<code>' . dump($content) . '</code>';
168                  <span class="frey-popup">ClassBrowser<span>| . Frey::ClassBrowser->markup . qq|</span></span>                                  my $l = length($content);
169                  <span class="frey-popup">ENV<span><code>| . dump( %ENV ) . qq|</code></span></span>                                  $content = qq|<span>$l bytes</span>| if $l > $self->dump_max_bytes;
170                            } else {
171                                    $content = qq|<span>$content</span>|;
172                            }
173                            warn "### part [$name] = ", length( $content ), " bytes" if $self->debug;
174                            $status_line .= qq|<span class="frey-popup">$name $content</span>\n|;
175                    }
176            }
177    
178            my $url = $self->request_url;
179            $url =~ s{\?reload=\d+}{};
180    
181            my $body = $a->{body};
182            $body ||= $self->as_markup if $self->can('as_markup');
183            if ( $self->content_type !~ m{html} ) {
184                    warn "# return only $self body ", $self->content_type;
185                    return $body
186            } elsif ( ! defined $body ) {
187                    warn "# no body";
188                    $body = '<!-- no body -->';
189            }
190    
191            my $warn_colors = {
192                    '#'  => '#444',
193                    '##' => '#888',
194            };
195    
196            $status_line
197                    .= qq|<span class="frey-popup">warn<span>|
198                    . $self->editor_links(
199                            join("", map {
200                                    warn "# $_";
201                                    my $style = '';
202                                    $style = $warn_colors->{$1}
203                                            ? ' style="color:' . $warn_colors->{$1} . '"'
204                                            : ''
205                                            if m{^(#+)};
206                                    qq|<tt$style>$_</tt><br/>|; # XXX <tt> should be <code> but CSS hates me
207                            } $self->warnings )
208                    )
209                    . qq|</span></span>|
210                    if $self->warnings;
211    
212            my      ($exit,$description) = ('exit','stop server');
213                    ($exit,$description) = ('restart','restart server')
214                    if $ENV{FREY_RESTART}; # tune labels on exit link
215    
216            my $right =
217                    qq|
218                            <span class="right">
219                            <a title="reload"  href="/reload$url"><code>$url</code></a>
220                            <a title="$description" href="/exit$url">$exit</a>
221                            </span>
222                    |;
223    
224            my $html = join("\n",
225                    qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
226                    $self->_head_html,
227                    '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',
228                    '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
229                    ( $a->{head} || '' ),
230                    qq|
231                    </head><body>
232                    $body
233                    <div class="frey-status-line">
234                            <a href="/">Frey</a> $Frey::VERSION
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            $line ||= 1;
259            qq|<a target="editor" href="/editor+$class+$line"| .
260            ( $title ? qq| title="$title"| : '' ) .
261            qq|>$class</a>|;
262    }
263    
264    =head2 editor_links
265    
266    Create HTML links to editor for perl error message
267    
268      my $html = $self->editor_links( $error )
269    
270    =cut
271    
272    sub editor_links {
273          my ( $self, $error ) = @_;          my ( $self, $error ) = @_;
274          warn $error;  
275          $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+)}
276          return qq|<pre class="frey-error">$error</pre>|;                  {at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;
277    
278            $error =~ s{(via package ")([\w:]+)(")}
279                    {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
280    
281            return $error;
282    }
283    
284    sub error {
285            my $self = shift;
286            my $error = join(" ", @_);
287    
288            my @backtrace = $self->backtrace;
289            $error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace;
290    
291            warn "ERROR: $error\n";
292            return
293                    qq|<pre class="frey-error">|
294                    . $self->editor_links( $error ) .
295                    qq|</pre>|
296                    ;
297  }  }
298    
299  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26