/[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 210 by dpavlin, Fri Oct 31 16:28:58 2008 UTC revision 457 by dpavlin, Wed Nov 19 16:53:13 2008 UTC
# Line 1  Line 1 
1  package Frey::Web;  package Frey::Web;
2  use Moose::Role;  use Moose::Role;
3    
4    use Frey::Types;
5    
6  use Continuity::Widget::DomNode;  use Continuity::Widget::DomNode;
7  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
8  use Carp qw/confess/;  use Carp qw/confess/;
9  use File::Slurp;  use File::Slurp;
10    
11    use Frey::Bookmarklet;
12    use Frey::ClassBrowser;
13    
14  has 'head' => (  has 'head' => (
15          is => 'rw',          is => 'rw',
16          isa => 'ArrayRef[Str]',          isa => 'ArrayRef[Str]',
17          default => sub { [ 'static/frey.css' ] },          default => sub { [ 'static/frey.css' ] },
18  );  );
19    
20    has 'status' => (
21            is => 'rw',
22            isa => 'ArrayRef[HashRef[Str]]',
23            lazy => 1,
24            default => sub { [
25                    { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },
26                    { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup },
27            ] },
28    );
29    
30    has 'request_url' => (
31            is => 'rw',
32            isa => 'Uri', coerce => 1,
33            default => '/',
34    );
35    
36    has 'title' => (
37            is => 'rw',
38            isa => 'Str',
39            lazy => 1,
40            default => sub {
41                    my ($self) = @_;
42                    ref($self);
43            },
44    );
45    
46    has 'content_type' => (
47            is => 'rw',
48            isa => 'Str',
49            default => 'text/html',
50    );
51    
52  =head2 inline_smaller_than  =head2 inline_smaller_than
53    
54  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 85  sub _head_html {
85                          $out .= $self->_inline_path( $path ) ?                          $out .= $self->_inline_path( $path ) ?
86                                  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>| :
87                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
88                    } elsif ( $path =~ m{<.+>}s ) {
89                            $out .= $path;
90                  } else {                  } else {
91                          confess "don't know how to render $path";                          confess "don't know how to render $path";
92                  }                  }
# Line 62  sub _head_html { Line 101  sub _head_html {
101    
102    my $size = $o->add_head( 'path/to/external.css' );    my $size = $o->add_head( 'path/to/external.css' );
103    
104      $o->add_head( '<!-- html content -->' );
105    
106  =cut  =cut
107    
108  sub add_head {  sub add_head {
# Line 69  sub add_head { Line 110  sub add_head {
110          return if ! defined $path || $path eq '';          return if ! defined $path || $path eq '';
111          $path =~ s!^/!!;          $path =~ s!^/!!;
112    
113          if ( -e $path ) {          if ( $path =~ m{<.*>}s ) {
114                    push @{ $self->head }, $path;
115            } elsif ( -e $path ) {
116                  if ( $path =~ m/\.(?:js|css)$/ ) {                  if ( $path =~ m/\.(?:js|css)$/ ) {
117                          push @{ $self->head }, $path;                          push @{ $self->head }, $path;
118                  } else {                  } else {
119                          confess "can't add_head( $path ) it's not js or css";                          confess "can't add_head( $path ) it's not js or css";
120                  }                  }
121                    return -s $path;
122          } else {          } else {
123                  confess "can't find $path: $!";                  confess "can't find $path: $!";
124          }          }
125    
         return -s $path;  
126  }  }
127    
128  our $reload_counter = 0;  our $reload_counter = 0;
# Line 95  our $reload_counter = 0; Line 138  our $reload_counter = 0;
138    
139  =cut  =cut
140    
 use Frey::Bookmarklet;  
   
141  sub page {  sub page {
142          my $self = shift;          my $self = shift;
143          my $a = {@_};          my $a = {@_};
144    
145          $reload_counter++;          $reload_counter++;
146    
147          my $html = qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|          my $status_line = '';
148          . $self->_head_html          foreach my $part ( @{ $self->status } ) {
149          . '<title>' . ( $a->{title} || ref($self) ) . '</title>'                  if ( ref($part) ne 'HASH' ) {
150          . ( $a->{head} || '' )                          warn "part not hash ",dump( $part ) ;
151          . '</head><body>'                          #$self->status( $part );
152          . ( $a->{body} || '<!-- no body -->' )                          next;
153          . qq|<div class="frey-status-line">                  }
154                  <a href="/">Frey</a> $Frey::VERSION                  foreach my $name ( keys %$part ) {
155                  <a href="?reload=$reload_counter">reload</a>                          my $content = $part->{$name};
156                  <span class="frey-popup">Bookmarklets<span>| . Frey::Bookmarklet->markup . qq|</span></span>                          if ( ref($content) ) {
157                  <span class="frey-popup">ClassBrowser<span>| . Frey::ClassBrowser->markup . qq|</span></span>                                  $content = '<code>' . dump($content) . '</code>';
158                                    my $l = length($content);
159                                    $content = qq|<span>$l bytes</span>| if $l > 1024;
160                            } else {
161                                    $content = qq|<span>$content</span>|;
162                            }
163                            warn "### part [$name] = ", length( $content ), " bytes" if $self->debug;
164                            $status_line .= qq|<span class="frey-popup">$name $content</span>\n|;
165                    }
166            }
167    
168            my $url = $self->request_url;
169            $url =~ s{\?reload=\d+}{};
170    
171            my $body = $a->{body} || $self->as_markup;
172            return $body if $self->content_type !~ m{html};
173            $body ||= '<!-- no body -->';
174    
175            my $html = join("\n",
176                    qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
177                    $self->_head_html,
178                    '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',
179                    '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
180                    ( $a->{head} || '' ),
181                    qq|
182                    </head><body>
183                    $body
184                    <div class="frey-status-line">
185                            <a href="/">Frey</a> $Frey::VERSION
186                            <a href="?reload=$reload_counter"><code>$url</code></a>
187                            $status_line
188                  </div>                  </div>
189              </body></html>              </body></html>
190          |;                  |,
191            );
192    
193          warn "## >>> page ",length($html), " bytes\n" if $self->debug;          warn "## >>> page ",length($html), " bytes\n" if $self->debug;
194    
195          return $html;          return $html;
196  }  }
197    
198    sub error {
199            my $self = shift;
200            my $error = join(" ", @_);
201            my ($package, $filename, $line) = caller;
202            $error .= " at $filename line $line" if $error !~ m{ at };
203            warn "WARN: $error\n";
204            $error =~ s{at\s+(\S+)\s+line\s+(\d+)}{at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;
205            $error =~ s{(via package ")([\w:]+)(")}{$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
206            return qq|<pre class="frey-error">$error</pre>|;
207    }
208    
209  1;  1;

Legend:
Removed from v.210  
changed lines
  Added in v.457

  ViewVC Help
Powered by ViewVC 1.1.26