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

  ViewVC Help
Powered by ViewVC 1.1.26