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

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

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

revision 445 by dpavlin, Wed Nov 19 03:00:46 2008 UTC revision 516 by dpavlin, Tue Nov 25 14:14:21 2008 UTC
# Line 1  Line 1 
1  package Frey::Web;  package Frey::Web;
2  use Moose::Role;  use Moose::Role;
3    
4    with 'Frey::Backtrace';
5    
6  use Frey::Types;  use Frey::Types;
7    
8  use Continuity::Widget::DomNode;  use Continuity::Widget::DomNode;
# Line 10  use File::Slurp; Line 12  use File::Slurp;
12    
13  use Frey::Bookmarklet;  use Frey::Bookmarklet;
14  use Frey::ClassBrowser;  use Frey::ClassBrowser;
15    use Frey::SVK;
16    
17  has 'head' => (  has 'head' => (
18          is => 'rw',          is => 'rw',
# Line 22  has 'status' => ( Line 25  has 'status' => (
25          isa => 'ArrayRef[HashRef[Str]]',          isa => 'ArrayRef[HashRef[Str]]',
26          lazy => 1,          lazy => 1,
27          default => sub { [          default => sub { [
28                  { 'Bookmarklets' => Frey::Bookmarklet->new->markup },                  { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup },
29                  { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->markup },                  { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },
30          ] },          ] },
31  );  );
32    
# Line 43  has 'title' => ( Line 46  has 'title' => (
46          },          },
47  );  );
48    
49    has 'content_type' => (
50            is => 'rw',
51            isa => 'Str',
52            default => 'text/html',
53            documentation => 'Content-type header',
54    );
55    
56    has 'dump_max_bytes' => (
57            is => 'rw',
58            isa => 'Int',
59            default => 4096,
60            documentation => 'Maximum dump size sent to browser before truncation',
61    );
62    
63  =head2 inline_smaller_than  =head2 inline_smaller_than
64    
65  Inline JavaScript and CSS smaller than this size into page reducing  Inline JavaScript and CSS smaller than this size into page reducing
# Line 79  sub _head_html { Line 96  sub _head_html {
96                          $out .= $self->_inline_path( $path ) ?                          $out .= $self->_inline_path( $path ) ?
97                                  qq|<!-- $path --><style type="text/css">\n| . read_file( $path ) . qq|\n</style>| :                                  qq|<!-- $path --><style type="text/css">\n| . read_file( $path ) . qq|\n</style>| :
98                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
99                  } elsif ( $path =~ m{<.+>} ) {                  } elsif ( $path =~ m{<.+>}s ) {
100                          $out .= $path;                          $out .= $path;
101                  } else {                  } else {
102                          confess "don't know how to render $path";                          confess "don't know how to render $path";
# Line 104  sub add_head { Line 121  sub add_head {
121          return if ! defined $path || $path eq '';          return if ! defined $path || $path eq '';
122          $path =~ s!^/!!;          $path =~ s!^/!!;
123    
124          if ( $path =~ m{<.*>} ) {          if ( $path =~ m{<.*>}s ) {
125                  push @{ $self->head }, $path;                  push @{ $self->head }, $path;
126          } elsif ( -e $path ) {          } elsif ( -e $path ) {
127                  if ( $path =~ m/\.(?:js|css)$/ ) {                  if ( $path =~ m/\.(?:js|css)$/ ) {
# Line 150  sub page { Line 167  sub page {
167                          if ( ref($content) ) {                          if ( ref($content) ) {
168                                  $content = '<code>' . dump($content) . '</code>';                                  $content = '<code>' . dump($content) . '</code>';
169                                  my $l = length($content);                                  my $l = length($content);
170                                  $content = qq|<span>$l bytes</span>| if $l > 1024;                                  $content = qq|<span>$l bytes</span>| if $l > $self->dump_max_bytes;
171                          } else {                          } else {
172                                  $content = qq|<span>$content</span>|;                                  $content = qq|<span>$content</span>|;
173                          }                          }
# Line 162  sub page { Line 179  sub page {
179          my $url = $self->request_url;          my $url = $self->request_url;
180          $url =~ s{\?reload=\d+}{};          $url =~ s{\?reload=\d+}{};
181    
182            my $body = $a->{body};
183            $body ||= $self->as_markup if $self->can('as_markup');
184            if ( $self->content_type !~ m{html} ) {
185                    warn "# return only $self body ", $self->content_type;
186                    return $body
187            } elsif ( ! defined $body ) {
188                    warn "# no body";
189                    $body = '<!-- no body -->';
190            }
191    
192            my $warn_colors = {
193                    '#'  => '#444',
194                    '##' => '#888',
195            };
196    
197            $status_line
198                    .= qq|<span class="frey-popup">warn<span>|
199                    . $self->editor_links(
200                            join("", map {
201                                    warn "# $_";
202                                    my $style = '';
203                                    $style = $warn_colors->{$1}
204                                            ? ' style="color:' . $warn_colors->{$1} . '"'
205                                            : ''
206                                            if m{^(#+)};
207                                    qq|<tt$style>$_</tt><br/>|; # XXX <tt> should be <code> but CSS hates me
208                            } $self->warnings )
209                    )
210                    . qq|</span></span>|
211                    if $self->warnings;
212    
213            my      ($exit,$description) = ('exit','stop server');
214                    ($exit,$description) = ('restart','restart server')
215                    if $ENV{FREY_RESTART}; # tune labels on exit link
216    
217            my $right =
218                    qq|
219                            <span class="right">
220                            <a title="reload"  href="/reload$url"><code>$url</code></a>
221                            <a title="$description" href="/exit$url">$exit</a>
222                            </span>
223                    |;
224    
225            my $info = Frey::SVK->info;
226            my $revision = Frey::SVK->info->{Revision} || '';
227            $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};
228    
229          my $html = join("\n",          my $html = join("\n",
230                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
231                  $self->_head_html,                  $self->_head_html,
232                  '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',                  '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',
233                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
234                  ( $a->{head} || '' ),                  ( $a->{head} || '' ),
                 '</head><body>',  
                 ( $a->{body} || $self->markup || '<!-- no body -->' ),  
235                  qq|                  qq|
236                    </head><body>
237                    $body
238                  <div class="frey-status-line">                  <div class="frey-status-line">
239                          <a href="/">Frey</a> $Frey::VERSION                          <a href="/">Frey</a> $Frey::VERSION $revision
                         <a href="?reload=$reload_counter"><code>$url</code></a>  
240                          $status_line                          $status_line
241                            $right
242                  </div>                  </div>
243              </body></html>              </body></html>
244                  |,                  |,
# Line 185  sub page { Line 249  sub page {
249          return $html;          return $html;
250  }  }
251    
252    =head2 editor
253    
254    Create HTML editor link with optional line and title
255    
256      my $html = $self->editor( $class, $line, $title );
257    
258    =cut
259    
260    sub editor {
261            my ( $self, $class, $line, $title ) = @_;
262            confess "need class" unless $class;
263            $line ||= 1;
264            qq|<a target="editor" href="/editor+$class+$line"| .
265            ( $title ? qq| title="$title"| : '' ) .
266            qq|>$class</a>|;
267    }
268    
269    =head2 editor_links
270    
271    Create HTML links to editor for perl error message
272    
273      my $html = $self->editor_links( $error )
274    
275    =cut
276    
277    sub editor_links {
278            my ( $self, $error ) = @_;
279    
280            $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
281                    {at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;
282    
283            $error =~ s{(via package ")([\w:]+)(")}
284                    {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
285    
286            return $error;
287    }
288    
289  sub error {  sub error {
290          my $self = shift;          my $self = shift;
291          my $error = join(" ", @_);          my $error = join(" ", @_);
292          my ($package, $filename, $line) = caller;  
293          $error .= " at $filename line $line" if $error !~ m{ at };          my @backtrace = $self->backtrace;
294          warn "WARN: $error\n";          $error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace;
295          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}{at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;  
296          $error =~ s{(via package ")([\w:]+)(")}{$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;          warn "ERROR: $error\n";
297          return qq|<pre class="frey-error">$error</pre>|;          return
298                    qq|<pre class="frey-error">|
299                    . $self->editor_links( $error ) .
300                    qq|</pre>|
301                    ;
302    }
303    
304    sub add_status {
305            my ( $self, $data ) = @_;
306            push @{ $self->status }, $data;
307            warn "## current status ", $#{ $self->status }, " elements";
308  }  }
309    
310  1;  1;

Legend:
Removed from v.445  
changed lines
  Added in v.516

  ViewVC Help
Powered by ViewVC 1.1.26