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

  ViewVC Help
Powered by ViewVC 1.1.26