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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 161 - (show 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 package Frey::Web;
2 use Moose::Role;
3
4 use Continuity::Widget::DomNode;
5 use Data::Dump qw/dump/;
6 use Carp qw/confess/;
7 use File::Slurp;
8
9 has 'head' => (
10 is => 'rw',
11 isa => 'ArrayRef[Str]',
12 default => sub { [ 'static/frey.css' ] },
13 );
14
15 has 'inline_smaller_than' => (
16 is => 'rw',
17 isa => 'Int',
18 default => 10240,
19 );
20
21 sub dom2html {
22 # warn "## dom2html ",dump( @_ );
23 return Continuity::Widget::DomNode->create( @_ )->to_string;
24 }
25
26 sub _inline_path {
27 my ( $self, $path ) = @_;
28 -s $path < $self->inline_smaller_than;
29 }
30
31 sub _head_html {
32 my $self = shift;
33 my $out = '';
34 foreach my $path ( @{ $self->head } ) {
35 $path =~ s!^/!!;
36 if ( $path =~ m/\.js$/ ) {
37 $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 } elsif ( $path =~ m/\.css$/ ) {
41 $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 } else {
45 confess "don't know how to render $path";
46 }
47 }
48 return $out;
49 }
50
51 =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 if ( $path =~ m/\.(?:js|css)$/ ) {
66 push @{ $self->head }, $path;
67 } 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 our $reload_counter = 0;
78
79 sub page {
80 my $self = shift;
81 my $a = {@_};
82
83 $reload_counter++;
84
85 my $html = qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|
86 . $self->_head_html
87 . '<title>' . ( $a->{title} || ref($self) ) . '</title>'
88 . ( $a->{head} || '' )
89 . '</head><body>'
90 . ( $a->{body} || '<!-- no body -->' )
91 . qq|<div class="frey-status-line"> <a href="/">Frey</a> $Frey::VERSION <a href="?reload=$reload_counter">reload</a> </div>|
92 . '</body></html>'
93 ;
94
95 warn "## >>> page ",length($html), " bytes\n" if $self->debug;
96
97 return $html;
98 }
99
100 1;

  ViewVC Help
Powered by ViewVC 1.1.26