/[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 206 by dpavlin, Thu Oct 30 22:35:11 2008 UTC revision 468 by dpavlin, Wed Nov 19 19:59:52 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                    { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },
28                    { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->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    );
53    
54  =head2 inline_smaller_than  =head2 inline_smaller_than
55    
56  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 87  sub _head_html {
87                          $out .= $self->_inline_path( $path ) ?                          $out .= $self->_inline_path( $path ) ?
88                                  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>| :
89                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
90                    } elsif ( $path =~ m{<.+>}s ) {
91                            $out .= $path;
92                  } else {                  } else {
93                          confess "don't know how to render $path";                          confess "don't know how to render $path";
94                  }                  }
# Line 62  sub _head_html { Line 103  sub _head_html {
103    
104    my $size = $o->add_head( 'path/to/external.css' );    my $size = $o->add_head( 'path/to/external.css' );
105    
106      $o->add_head( '<!-- html content -->' );
107    
108  =cut  =cut
109    
110  sub add_head {  sub add_head {
# Line 69  sub add_head { Line 112  sub add_head {
112          return if ! defined $path || $path eq '';          return if ! defined $path || $path eq '';
113          $path =~ s!^/!!;          $path =~ s!^/!!;
114    
115          if ( -e $path ) {          if ( $path =~ m{<.*>}s ) {
116                    push @{ $self->head }, $path;
117            } elsif ( -e $path ) {
118                  if ( $path =~ m/\.(?:js|css)$/ ) {                  if ( $path =~ m/\.(?:js|css)$/ ) {
119                          push @{ $self->head }, $path;                          push @{ $self->head }, $path;
120                  } else {                  } else {
121                          confess "can't add_head( $path ) it's not js or css";                          confess "can't add_head( $path ) it's not js or css";
122                  }                  }
123                    return -s $path;
124          } else {          } else {
125                  confess "can't find $path: $!";                  confess "can't find $path: $!";
126          }          }
127    
         return -s $path;  
128  }  }
129    
130  our $reload_counter = 0;  our $reload_counter = 0;
# Line 95  our $reload_counter = 0; Line 140  our $reload_counter = 0;
140    
141  =cut  =cut
142    
 use Frey::Bookmarklet;  
   
143  sub page {  sub page {
144          my $self = shift;          my $self = shift;
145          my $a = {@_};          my $a = {@_};
146    
147          $reload_counter++;          $reload_counter++;
148    
149          my $html = qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|          my $status_line = '';
150          . $self->_head_html          foreach my $part ( @{ $self->status } ) {
151          . '<title>' . ( $a->{title} || ref($self) ) . '</title>'                  if ( ref($part) ne 'HASH' ) {
152          . ( $a->{head} || '' )                          warn "part not hash ",dump( $part ) ;
153          . '</head><body>'                          #$self->status( $part );
154          . ( $a->{body} || '<!-- no body -->' )                          next;
155          . qq|<div class="frey-status-line">                  }
156                  <a href="/">Frey</a> $Frey::VERSION                  foreach my $name ( keys %$part ) {
157                  <a href="?reload=$reload_counter">reload</a>                          my $content = $part->{$name};
158                  <span class="frey-popup">Bookmarklets<span>| . Frey::Bookmarklet->markup . qq|</span>|                          if ( ref($content) ) {
159          . '</body></html>'                                  $content = '<code>' . dump($content) . '</code>';
160          ;                                  my $l = length($content);
161                                    $content = qq|<span>$l bytes</span>| if $l > 1024;
162                            } else {
163                                    $content = qq|<span>$content</span>|;
164                            }
165                            warn "### part [$name] = ", length( $content ), " bytes" if $self->debug;
166                            $status_line .= qq|<span class="frey-popup">$name $content</span>\n|;
167                    }
168            }
169    
170            my $url = $self->request_url;
171            $url =~ s{\?reload=\d+}{};
172    
173            my $body = $a->{body};
174            $body ||= $self->as_markup if $self->can('as_markup');
175            if ( $self->content_type !~ m{html} ) {
176                    warn "# return only $self body ", $self->content_type;
177                    return $body
178            } elsif ( ! defined $body ) {
179                    warn "# no body";
180                    $body = '<!-- no body -->';
181            }
182    
183            $status_line
184                    .= qq|<span class="frey-popup">warn<code>|
185                    . $self->editor_links( join("", $self->warnings ) )
186                    . qq|</code></span>|
187                    if $self->warnings;
188    
189            my $html = join("\n",
190                    qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
191                    $self->_head_html,
192                    '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',
193                    '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
194                    ( $a->{head} || '' ),
195                    qq|
196                    </head><body>
197                    $body
198                    <div class="frey-status-line">
199                            <a href="/">Frey</a> $Frey::VERSION
200                            <a href="?reload=$reload_counter"><code>$url</code></a>
201                            $status_line
202                    </div>
203                </body></html>
204                    |,
205            );
206    
207          warn "## >>> page ",length($html), " bytes\n" if $self->debug;          warn "## >>> page ",length($html), " bytes\n" if $self->debug;
208    
209          return $html;          return $html;
210  }  }
211    
212    sub editor_links {
213            my ( $self, $error ) = @_;
214    
215            $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
216                    {at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;
217    
218            $error =~ s{(via package ")([\w:]+)(")}
219                    {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
220    
221            return $error;
222    }
223    
224    sub error {
225            my $self = shift;
226            my $error = join(" ", @_);
227    
228            my @backtrace = $self->backtrace;
229            $error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace;
230    
231            warn "ERROR: $error\n";
232            return
233                    qq|<pre class="frey-error">|
234                    . $self->editor_links( $error ) .
235                    qq|</pre>|
236                    ;
237    }
238    
239  1;  1;

Legend:
Removed from v.206  
changed lines
  Added in v.468

  ViewVC Help
Powered by ViewVC 1.1.26