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

  ViewVC Help
Powered by ViewVC 1.1.26