/[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 142 by dpavlin, Wed Jul 16 14:14:18 2008 UTC revision 465 by dpavlin, Wed Nov 19 19:11:52 2008 UTC
# Line 1  Line 1 
1  package Frey::Web;  package Frey::Web;
2  use Moose::Role;  use Moose::Role;
3  use MooseX::AttributeHelpers;  
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;
12    
13    use Frey::Bookmarklet;
14    use Frey::ClassBrowser;
15    
16  has 'javascript' => (  has 'head' => (
         metaclass => 'Collection::Array',  
17          is => 'rw',          is => 'rw',
18          isa => 'ArrayRef[Str]',          isa => 'ArrayRef[Str]',
19          default => sub { [] },          default => sub { [ 'static/frey.css' ] },
         provides => {  
                 'push' => 'add_javascript',  
         },  
20  );  );
21    
22  has 'css' => (  has 'status' => (
         metaclass => 'Collection::Array',  
23          is => 'rw',          is => 'rw',
24          isa => 'ArrayRef[Str]',          isa => 'ArrayRef[HashRef[Str]]',
25          default => sub { [ 'static/app.css' ] },          lazy => 1,
26          provides => {          default => sub { [
27                  'push' => 'add_css',                  { '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
55    
56    Inline JavaScript and CSS smaller than this size into page reducing
57    round-trips to server.
58    
59    =cut
60    
61    has 'inline_smaller_than' => (
62            is => 'rw',
63            isa => 'Int',
64            default => 10240,
65    );
66    
67  sub dom2html {  sub dom2html {
68  #       warn "## dom2html ",dump( @_ );  #       warn "## dom2html ",dump( @_ );
69          return Continuity::Widget::DomNode->create( @_ )->to_string;          return Continuity::Widget::DomNode->create( @_ )->to_string;
70  }  }
71    
72  sub _unroll_markup {  sub _inline_path {
73  #       warn "## _unroll_markup ",dump( @_ );          my ( $self, $path ) = @_;
74          my ( $markup, $array ) = @_;          -s $path < $self->inline_smaller_than;
75    }
76    
77    sub _head_html {
78            my $self = shift;
79          my $out = '';          my $out = '';
80          foreach my $path ( @$array ) {          foreach my $path ( @{ $self->head } ) {
81                  $path =~ s!^/!!;                  $path =~ s!^/!!;
82                  confess "can't find $path" unless -e $path;                  if ( $path =~ m/\.js$/ ) {
83                  $out .= sprintf( $markup, $path );                          $out .= $self->_inline_path( $path ) ?
84                                    qq|<!-- $path --><script type="text/javascript">\n| . read_file($path) . qq|\n</script>| :
85                                    qq|<script type="text/javascript" src="/$path"></script>|;
86                    } elsif ( $path =~ m/\.css$/ ) {
87                            $out .= $self->_inline_path( $path ) ?
88                                    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">|;
90                    } elsif ( $path =~ m{<.+>}s ) {
91                            $out .= $path;
92                    } else {
93                            confess "don't know how to render $path";
94                    }
95                    $out .= "\n";
96          }          }
97          return $out;          return $out;
98  }  }
99    
100    =head2 add_head
101    
102      $o->add_head( 'path/to/external.js' );
103    
104      my $size = $o->add_head( 'path/to/external.css' );
105    
106      $o->add_head( '<!-- html content -->' );
107    
108    =cut
109    
110    sub add_head {
111            my ( $self, $path ) = @_;
112            return if ! defined $path || $path eq '';
113            $path =~ s!^/!!;
114    
115            if ( $path =~ m{<.*>}s ) {
116                    push @{ $self->head }, $path;
117            } elsif ( -e $path ) {
118                    if ( $path =~ m/\.(?:js|css)$/ ) {
119                            push @{ $self->head }, $path;
120                    } else {
121                            confess "can't add_head( $path ) it's not js or css";
122                    }
123                    return -s $path;
124            } else {
125                    confess "can't find $path: $!";
126            }
127    
128    }
129    
130  our $reload_counter = 0;  our $reload_counter = 0;
131    
132    
133    =head2 page
134    
135      $self->page(
136            title => 'page title',
137            head  => '<!-- optional head markup -->',
138            body  => '<b>Page Body</b>',
139      );
140    
141    =cut
142    
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          . _unroll_markup( qq|<script type="text/javascript" src="/%s"></script>|, $self->javascript )          foreach my $part ( @{ $self->status } ) {
151          . _unroll_markup( qq|<link type="text/css" rel="stylesheet" href="/%s" media="screen">|, $self->css )                  if ( ref($part) ne 'HASH' ) {
152          . '<title>' . ( $a->{title} || ref($self) ) . '</title>'                          warn "part not hash ",dump( $part ) ;
153          . ( $a->{head} || '' )                          #$self->status( $part );
154          . '</head><body>'                          next;
155          . ( $a->{body} || '<!-- no body -->' )                  }
156          . qq|<div class="status-line"> <a href="/">Frey</a> $Frey::VERSION <a href="?reload=$reload_counter">reload</a> </div>|                  foreach my $name ( keys %$part ) {
157          . '</body></html>'                          my $content = $part->{$name};
158          ;                          if ( ref($content) ) {
159                                    $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            my $html = join("\n",
184                    qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
185                    $self->_head_html,
186                    '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',
187                    '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
188                    ( $a->{head} || '' ),
189                    qq|
190                    </head><body>
191                    $body
192                    <div class="frey-status-line">
193                            <a href="/">Frey</a> $Frey::VERSION
194                            <a href="?reload=$reload_counter"><code>$url</code></a>
195                            $status_line
196                    </div>
197                </body></html>
198                    |,
199            );
200    
201          warn "## >>> page ",length($html), " bytes\n" if $self->debug;          warn "## >>> page ",length($html), " bytes\n" if $self->debug;
202    
203          return $html;          return $html;
204  }  }
205    
206    sub error {
207            my $self = shift;
208            my $error = join(" ", @_);
209    
210            my @backtrace = $self->backtrace;
211            $error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace;
212    
213            warn "ERROR: $error\n";
214            $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
215                    {at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;
216            $error =~ s{(via package ")([\w:]+)(")}
217                    {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
218    
219            return qq|<pre class="frey-error">$error</pre>|;
220    }
221    
222  1;  1;

Legend:
Removed from v.142  
changed lines
  Added in v.465

  ViewVC Help
Powered by ViewVC 1.1.26