/[Frey]/trunk/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

Diff of /trunk/lib/Frey/Web.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 161 by dpavlin, Thu Jul 17 19:33:51 2008 UTC revision 418 by dpavlin, Tue Nov 18 16:54:10 2008 UTC
# Line 1  Line 1 
1  package Frey::Web;  package Frey::Web;
2  use Moose::Role;  use Moose::Role;
3    
4    #with 'Frey::Escape';
5    
6    use Frey::Types;
7    
8  use Continuity::Widget::DomNode;  use Continuity::Widget::DomNode;
9  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
10  use Carp qw/confess/;  use Carp qw/confess/;
11  use File::Slurp;  use File::Slurp;
12    
13    use Frey::Bookmarklet;
14    use Frey::ClassBrowser;
15    
16  has 'head' => (  has 'head' => (
17          is => 'rw',          is => 'rw',
18          isa => 'ArrayRef[Str]',          isa => 'ArrayRef[Str]',
19          default => sub { [ 'static/frey.css' ] },          default => sub { [ 'static/frey.css' ] },
20  );  );
21    
22    has 'status' => (
23            is => 'rw',
24            isa => 'ArrayRef[HashRef[Str]]',
25            lazy => 1,
26            default => sub { [
27                    { 'Bookmarklets' => Frey::Bookmarklet->new->markup },
28                    { 'ClassBrowser' => Frey::ClassBrowser->new->markup },
29            ] },
30    );
31    
32    has 'request_url' => (
33            is => 'rw',
34            isa => 'Uri', coerce => 1,
35            default => '/',
36    );
37    
38    has 'title' => (
39            is => 'rw',
40            isa => 'Str',
41            lazy => 1,
42            default => sub {
43                    my ($self) = @_;
44                    ref($self);
45            },
46    );
47    
48    =head2 inline_smaller_than
49    
50    Inline JavaScript and CSS smaller than this size into page reducing
51    round-trips to server.
52    
53    =cut
54    
55  has 'inline_smaller_than' => (  has 'inline_smaller_than' => (
56          is => 'rw',          is => 'rw',
57          isa => 'Int',          isa => 'Int',
# Line 35  sub _head_html { Line 75  sub _head_html {
75                  $path =~ s!^/!!;                  $path =~ s!^/!!;
76                  if ( $path =~ m/\.js$/ ) {                  if ( $path =~ m/\.js$/ ) {
77                          $out .= $self->_inline_path( $path ) ?                          $out .= $self->_inline_path( $path ) ?
78                                  qq|<script type="text/javascript">| . read_file($path) . qq|</script>| :                                  qq|<!-- $path --><script type="text/javascript">\n| . read_file($path) . qq|\n</script>| :
79                                  qq|<script type="text/javascript" src="/$path"></script>|;                                  qq|<script type="text/javascript" src="/$path"></script>|;
80                  } elsif ( $path =~ m/\.css$/ ) {                  } elsif ( $path =~ m/\.css$/ ) {
81                          $out .= $self->_inline_path( $path ) ?                          $out .= $self->_inline_path( $path ) ?
82                                  qq|<style type="text/css">| . read_file( $path ) . qq|</style>| :                                  qq|<!-- $path --><style type="text/css">\n| . read_file( $path ) . qq|\n</style>| :
83                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
84                  } else {                  } else {
85                          confess "don't know how to render $path";                          confess "don't know how to render $path";
86                  }                  }
87                    $out .= "\n";
88          }          }
89          return $out;          return $out;
90  }  }
# Line 76  sub add_head { Line 117  sub add_head {
117    
118  our $reload_counter = 0;  our $reload_counter = 0;
119    
120    
121    =head2 page
122    
123      $self->page(
124            title => 'page title',
125            head  => '<!-- optional head markup -->',
126            body  => '<b>Page Body</b>',
127      );
128    
129    =cut
130    
131  sub page {  sub page {
132          my $self = shift;          my $self = shift;
133          my $a = {@_};          my $a = {@_};
134    
135          $reload_counter++;          $reload_counter++;
136    
137          my $html = qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|          my $status_line = '';
138          . $self->_head_html          foreach my $part ( @{ $self->status } ) {
139          . '<title>' . ( $a->{title} || ref($self) ) . '</title>'                  confess "part not hash ",dump( $part ) unless ref($part) eq 'HASH';
140          . ( $a->{head} || '' )                  foreach my $name ( keys %$part ) {
141          . '</head><body>'                          my $content = $part->{$name};
142          . ( $a->{body} || '<!-- no body -->' )                          if ( ref($content) ) {
143          . qq|<div class="frey-status-line"> <a href="/">Frey</a> $Frey::VERSION <a href="?reload=$reload_counter">reload</a> </div>|                                  $content = '<code>' . dump($content) . '</code>';
144          . '</body></html>'                                  my $l = length($content);
145          ;                                  $content = qq|<span>$l bytes</span>| if $l > 1024;
146                            } else {
147                                    $content = qq|<span>$content</span>|;
148                            }
149                            warn "### part [$name] = ", length( $content ), " bytes" if $self->debug;
150                            $status_line .= qq|<span class="frey-popup">$name $content</span>\n|;
151                    }
152            }
153    
154            my $html = join("\n",
155                    qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
156                    $self->_head_html,
157                    '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',
158                    '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
159                    ( $a->{head} || '' ),
160                    '</head><body>',
161                    ( $a->{body} || $self->markup || '<!-- no body -->' ),
162                    qq|
163                    <div class="frey-status-line">
164                            <a href="/">Frey</a> $Frey::VERSION
165                            <a href="?reload=$reload_counter"><code>| . $self->request_url . qq|</code></a>
166                            $status_line
167                    </div>
168                </body></html>
169                    |,
170            );
171    
172          warn "## >>> page ",length($html), " bytes\n" if $self->debug;          warn "## >>> page ",length($html), " bytes\n" if $self->debug;
173    
174          return $html;          return $html;
175  }  }
176    
177    sub error {
178            my $self = shift;
179            my $error = join(" ", @_);
180            my ($package, $filename, $line) = caller;
181            $error .= " at $filename line $line" if $error !~ m{ at };
182            warn "WARN: $error\n";
183            $error =~ s{at\s+(\S+)\s+line\s+(\d+)}{at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;
184            $error =~ s{(via package ")([\w:]+)(")}{$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
185            return qq|<pre class="frey-error">$error</pre>|;
186    }
187    
188  1;  1;

Legend:
Removed from v.161  
changed lines
  Added in v.418

  ViewVC Help
Powered by ViewVC 1.1.26