/[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 161 - (hide annotations)
Thu Jul 17 19:33:51 2008 UTC (15 years, 9 months ago) by dpavlin
Original Path: trunk/lib/Frey/Web.pm
File size: 2171 byte(s)
inline css and javascript smaller than some size (by default, 10k)
1 dpavlin 100 package Frey::Web;
2     use Moose::Role;
3    
4     use Continuity::Widget::DomNode;
5     use Data::Dump qw/dump/;
6 dpavlin 121 use Carp qw/confess/;
7 dpavlin 161 use File::Slurp;
8 dpavlin 100
9 dpavlin 156 has 'head' => (
10 dpavlin 121 is => 'rw',
11     isa => 'ArrayRef[Str]',
12 dpavlin 160 default => sub { [ 'static/frey.css' ] },
13 dpavlin 121 );
14    
15 dpavlin 161 has 'inline_smaller_than' => (
16     is => 'rw',
17     isa => 'Int',
18     default => 10240,
19     );
20    
21 dpavlin 100 sub dom2html {
22 dpavlin 106 # warn "## dom2html ",dump( @_ );
23 dpavlin 100 return Continuity::Widget::DomNode->create( @_ )->to_string;
24     }
25    
26 dpavlin 161 sub _inline_path {
27     my ( $self, $path ) = @_;
28     -s $path < $self->inline_smaller_than;
29     }
30    
31 dpavlin 156 sub _head_html {
32     my $self = shift;
33 dpavlin 121 my $out = '';
34 dpavlin 156 foreach my $path ( @{ $self->head } ) {
35 dpavlin 121 $path =~ s!^/!!;
36 dpavlin 156 if ( $path =~ m/\.js$/ ) {
37 dpavlin 161 $out .= $self->_inline_path( $path ) ?
38     qq|<script type="text/javascript">| . read_file($path) . qq|</script>| :
39     qq|<script type="text/javascript" src="/$path"></script>|;
40 dpavlin 156 } elsif ( $path =~ m/\.css$/ ) {
41 dpavlin 161 $out .= $self->_inline_path( $path ) ?
42     qq|<style type="text/css">| . read_file( $path ) . qq|</style>| :
43     qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
44 dpavlin 156 } else {
45     confess "don't know how to render $path";
46     }
47 dpavlin 121 }
48     return $out;
49     }
50 dpavlin 100
51 dpavlin 154 =head2 add_head
52    
53     $o->add_head( 'path/to/external.js' );
54    
55     my $size = $o->add_head( 'path/to/external.css' );
56    
57     =cut
58    
59     sub add_head {
60     my ( $self, $path ) = @_;
61     return if ! defined $path || $path eq '';
62     $path =~ s!^/!!;
63    
64     if ( -e $path ) {
65 dpavlin 156 if ( $path =~ m/\.(?:js|css)$/ ) {
66     push @{ $self->head }, $path;
67 dpavlin 154 } else {
68     confess "can't add_head( $path ) it's not js or css";
69     }
70     } else {
71     confess "can't find $path: $!";
72     }
73    
74     return -s $path;
75     }
76    
77 dpavlin 142 our $reload_counter = 0;
78    
79 dpavlin 121 sub page {
80 dpavlin 100 my $self = shift;
81 dpavlin 121 my $a = {@_};
82 dpavlin 100
83 dpavlin 142 $reload_counter++;
84    
85 dpavlin 121 my $html = qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|
86 dpavlin 156 . $self->_head_html
87 dpavlin 121 . '<title>' . ( $a->{title} || ref($self) ) . '</title>'
88     . ( $a->{head} || '' )
89     . '</head><body>'
90     . ( $a->{body} || '<!-- no body -->' )
91 dpavlin 157 . qq|<div class="frey-status-line"> <a href="/">Frey</a> $Frey::VERSION <a href="?reload=$reload_counter">reload</a> </div>|
92 dpavlin 121 . '</body></html>'
93     ;
94 dpavlin 100
95 dpavlin 121 warn "## >>> page ",length($html), " bytes\n" if $self->debug;
96 dpavlin 100
97 dpavlin 121 return $html;
98 dpavlin 100 }
99    
100     1;

  ViewVC Help
Powered by ViewVC 1.1.26