4 |
use Continuity::Widget::DomNode; |
use Continuity::Widget::DomNode; |
5 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
6 |
use Carp qw/confess/; |
use Carp qw/confess/; |
7 |
|
use File::Slurp; |
8 |
|
|
9 |
has 'head' => ( |
has 'head' => ( |
10 |
is => 'rw', |
is => 'rw', |
12 |
default => sub { [ 'static/frey.css' ] }, |
default => sub { [ 'static/frey.css' ] }, |
13 |
); |
); |
14 |
|
|
15 |
|
=head2 inline_smaller_than |
16 |
|
|
17 |
|
Inline JavaScript and CSS smaller than this size into page reducing |
18 |
|
round-trips to server. |
19 |
|
|
20 |
|
=cut |
21 |
|
|
22 |
|
has 'inline_smaller_than' => ( |
23 |
|
is => 'rw', |
24 |
|
isa => 'Int', |
25 |
|
default => 10240, |
26 |
|
); |
27 |
|
|
28 |
sub dom2html { |
sub dom2html { |
29 |
# warn "## dom2html ",dump( @_ ); |
# warn "## dom2html ",dump( @_ ); |
30 |
return Continuity::Widget::DomNode->create( @_ )->to_string; |
return Continuity::Widget::DomNode->create( @_ )->to_string; |
31 |
} |
} |
32 |
|
|
33 |
|
sub _inline_path { |
34 |
|
my ( $self, $path ) = @_; |
35 |
|
-s $path < $self->inline_smaller_than; |
36 |
|
} |
37 |
|
|
38 |
sub _head_html { |
sub _head_html { |
39 |
my $self = shift; |
my $self = shift; |
40 |
my $out = ''; |
my $out = ''; |
41 |
foreach my $path ( @{ $self->head } ) { |
foreach my $path ( @{ $self->head } ) { |
42 |
$path =~ s!^/!!; |
$path =~ s!^/!!; |
43 |
if ( $path =~ m/\.js$/ ) { |
if ( $path =~ m/\.js$/ ) { |
44 |
$out .= qq|<script type="text/javascript" src="/$path"></script>|; |
$out .= $self->_inline_path( $path ) ? |
45 |
|
qq|<!-- $path --><script type="text/javascript">\n| . read_file($path) . qq|\n</script>| : |
46 |
|
qq|<script type="text/javascript" src="/$path"></script>|; |
47 |
} elsif ( $path =~ m/\.css$/ ) { |
} elsif ( $path =~ m/\.css$/ ) { |
48 |
$out .= qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
$out .= $self->_inline_path( $path ) ? |
49 |
|
qq|<!-- $path --><style type="text/css">\n| . read_file( $path ) . qq|\n</style>| : |
50 |
|
qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|; |
51 |
} else { |
} else { |
52 |
confess "don't know how to render $path"; |
confess "don't know how to render $path"; |
53 |
} |
} |
54 |
|
$out .= "\n"; |
55 |
} |
} |
56 |
return $out; |
return $out; |
57 |
} |
} |
84 |
|
|
85 |
our $reload_counter = 0; |
our $reload_counter = 0; |
86 |
|
|
87 |
|
|
88 |
|
=head2 page |
89 |
|
|
90 |
|
$self->page( |
91 |
|
title => 'page title', |
92 |
|
head => '<!-- optional head markup -->', |
93 |
|
body => '<b>Page Body</b>', |
94 |
|
); |
95 |
|
|
96 |
|
=cut |
97 |
|
|
98 |
|
use Frey::Bookmarklet; |
99 |
|
|
100 |
sub page { |
sub page { |
101 |
my $self = shift; |
my $self = shift; |
102 |
my $a = {@_}; |
my $a = {@_}; |
109 |
. ( $a->{head} || '' ) |
. ( $a->{head} || '' ) |
110 |
. '</head><body>' |
. '</head><body>' |
111 |
. ( $a->{body} || '<!-- no body -->' ) |
. ( $a->{body} || '<!-- no body -->' ) |
112 |
. qq|<div class="frey-status-line"> <a href="/">Frey</a> $Frey::VERSION <a href="?reload=$reload_counter">reload</a> </div>| |
. qq|<div class="frey-status-line"> |
113 |
|
<a href="/">Frey</a> $Frey::VERSION |
114 |
|
<a href="?reload=$reload_counter">reload</a> |
115 |
|
<span class="frey-popup">Bookmarklets<span>| . Frey::Bookmarklet->markup . qq|</span>| |
116 |
. '</body></html>' |
. '</body></html>' |
117 |
; |
; |
118 |
|
|