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

  ViewVC Help
Powered by ViewVC 1.1.26