/[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

Annotation of /branches/zimbardo/lib/Frey/Web.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 422 - (hide annotations)
Tue Nov 18 17:41:41 2008 UTC (15 years, 4 months ago) by dpavlin
Original Path: trunk/lib/Frey/Web.pm
File size: 4255 byte(s)
better reporting of mungled status parts
1 dpavlin 100 package Frey::Web;
2     use Moose::Role;
3    
4 dpavlin 397 #with 'Frey::Escape';
5    
6 dpavlin 388 use Frey::Types;
7    
8 dpavlin 100 use Continuity::Widget::DomNode;
9     use Data::Dump qw/dump/;
10 dpavlin 389 use Carp qw/confess/;
11 dpavlin 161 use File::Slurp;
12 dpavlin 100
13 dpavlin 410 use Frey::Bookmarklet;
14     use Frey::ClassBrowser;
15    
16 dpavlin 156 has 'head' => (
17 dpavlin 121 is => 'rw',
18     isa => 'ArrayRef[Str]',
19 dpavlin 160 default => sub { [ 'static/frey.css' ] },
20 dpavlin 121 );
21    
22 dpavlin 388 has 'status' => (
23     is => 'rw',
24     isa => 'ArrayRef[HashRef[Str]]',
25     lazy => 1,
26     default => sub { [
27 dpavlin 410 { 'Bookmarklets' => Frey::Bookmarklet->new->markup },
28     { 'ClassBrowser' => Frey::ClassBrowser->new->markup },
29 dpavlin 388 ] },
30     );
31    
32 dpavlin 392 has 'request_url' => (
33 dpavlin 388 is => 'rw',
34     isa => 'Uri', coerce => 1,
35     default => '/',
36     );
37    
38 dpavlin 418 has 'title' => (
39     is => 'rw',
40     isa => 'Str',
41     lazy => 1,
42     default => sub {
43     my ($self) = @_;
44     ref($self);
45     },
46     );
47    
48 dpavlin 206 =head2 inline_smaller_than
49    
50     Inline JavaScript and CSS smaller than this size into page reducing
51     round-trips to server.
52    
53     =cut
54    
55 dpavlin 161 has 'inline_smaller_than' => (
56     is => 'rw',
57     isa => 'Int',
58     default => 10240,
59     );
60    
61 dpavlin 100 sub dom2html {
62 dpavlin 106 # warn "## dom2html ",dump( @_ );
63 dpavlin 100 return Continuity::Widget::DomNode->create( @_ )->to_string;
64     }
65    
66 dpavlin 161 sub _inline_path {
67     my ( $self, $path ) = @_;
68     -s $path < $self->inline_smaller_than;
69     }
70    
71 dpavlin 156 sub _head_html {
72     my $self = shift;
73 dpavlin 121 my $out = '';
74 dpavlin 156 foreach my $path ( @{ $self->head } ) {
75 dpavlin 121 $path =~ s!^/!!;
76 dpavlin 156 if ( $path =~ m/\.js$/ ) {
77 dpavlin 161 $out .= $self->_inline_path( $path ) ?
78 dpavlin 163 qq|<!-- $path --><script type="text/javascript">\n| . read_file($path) . qq|\n</script>| :
79 dpavlin 161 qq|<script type="text/javascript" src="/$path"></script>|;
80 dpavlin 156 } elsif ( $path =~ m/\.css$/ ) {
81 dpavlin 161 $out .= $self->_inline_path( $path ) ?
82 dpavlin 163 qq|<!-- $path --><style type="text/css">\n| . read_file( $path ) . qq|\n</style>| :
83 dpavlin 161 qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
84 dpavlin 156 } else {
85     confess "don't know how to render $path";
86     }
87 dpavlin 163 $out .= "\n";
88 dpavlin 121 }
89     return $out;
90     }
91 dpavlin 100
92 dpavlin 154 =head2 add_head
93    
94     $o->add_head( 'path/to/external.js' );
95    
96     my $size = $o->add_head( 'path/to/external.css' );
97    
98     =cut
99    
100     sub add_head {
101     my ( $self, $path ) = @_;
102     return if ! defined $path || $path eq '';
103     $path =~ s!^/!!;
104    
105     if ( -e $path ) {
106 dpavlin 156 if ( $path =~ m/\.(?:js|css)$/ ) {
107     push @{ $self->head }, $path;
108 dpavlin 154 } else {
109     confess "can't add_head( $path ) it's not js or css";
110     }
111     } else {
112     confess "can't find $path: $!";
113     }
114    
115     return -s $path;
116     }
117    
118 dpavlin 142 our $reload_counter = 0;
119    
120 dpavlin 183
121     =head2 page
122    
123     $self->page(
124     title => 'page title',
125     head => '<!-- optional head markup -->',
126     body => '<b>Page Body</b>',
127     );
128    
129     =cut
130    
131 dpavlin 121 sub page {
132 dpavlin 100 my $self = shift;
133 dpavlin 121 my $a = {@_};
134 dpavlin 100
135 dpavlin 142 $reload_counter++;
136    
137 dpavlin 388 my $status_line = '';
138     foreach my $part ( @{ $self->status } ) {
139 dpavlin 422 if ( ref($part) ne 'HASH' ) {
140     warn "part not hash ",dump( $part ) ;
141     #$self->status( $part );
142     next;
143     }
144 dpavlin 388 foreach my $name ( keys %$part ) {
145 dpavlin 392 my $content = $part->{$name};
146     if ( ref($content) ) {
147 dpavlin 397 $content = '<code>' . dump($content) . '</code>';
148     my $l = length($content);
149     $content = qq|<span>$l bytes</span>| if $l > 1024;
150 dpavlin 392 } else {
151     $content = qq|<span>$content</span>|;
152     }
153     warn "### part [$name] = ", length( $content ), " bytes" if $self->debug;
154 dpavlin 397 $status_line .= qq|<span class="frey-popup">$name $content</span>\n|;
155 dpavlin 388 }
156     }
157    
158     my $html = join("\n",
159     qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
160     $self->_head_html,
161 dpavlin 418 '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',
162 dpavlin 388 '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
163     ( $a->{head} || '' ),
164     '</head><body>',
165 dpavlin 418 ( $a->{body} || $self->markup || '<!-- no body -->' ),
166 dpavlin 388 qq|
167     <div class="frey-status-line">
168     <a href="/">Frey</a> $Frey::VERSION
169 dpavlin 392 <a href="?reload=$reload_counter"><code>| . $self->request_url . qq|</code></a>
170 dpavlin 388 $status_line
171 dpavlin 210 </div>
172     </body></html>
173 dpavlin 388 |,
174     );
175 dpavlin 100
176 dpavlin 121 warn "## >>> page ",length($html), " bytes\n" if $self->debug;
177 dpavlin 100
178 dpavlin 121 return $html;
179 dpavlin 100 }
180    
181 dpavlin 350 sub error {
182 dpavlin 397 my $self = shift;
183     my $error = join(" ", @_);
184 dpavlin 389 my ($package, $filename, $line) = caller;
185 dpavlin 398 $error .= " at $filename line $line" if $error !~ m{ at };
186 dpavlin 389 warn "WARN: $error\n";
187 dpavlin 407 $error =~ s{at\s+(\S+)\s+line\s+(\d+)}{at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;
188 dpavlin 417 $error =~ s{(via package ")([\w:]+)(")}{$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
189 dpavlin 350 return qq|<pre class="frey-error">$error</pre>|;
190     }
191    
192 dpavlin 100 1;

  ViewVC Help
Powered by ViewVC 1.1.26