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

trunk/lib/Frey/Web.pm revision 908 by dpavlin, Fri Jan 2 13:22:13 2009 UTC branches/zimbardo/lib/Frey/Web.pm revision 1196 by dpavlin, Wed Nov 3 21:43:50 2010 UTC
# Line 1  Line 1 
1  package Frey::Web;  package Frey::Web;
2  use Moose::Role;  use Moose::Role;
3    
4  with 'Frey::Session';  with 'Frey::Session', 'Frey::Class::Icon';
5    
 #use Continuity::Widget::DomNode;  
6  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
7  use Carp qw/confess cluck carp/;  use Carp qw/confess cluck carp/;
8  use File::Slurp;  use File::Slurp;
9  use Text::Tabs; # expand, unexpand  use Text::Tabs; # expand, unexpand
10    use Digest::MD5 qw/md5/;
11    
12  use lib 'lib';  use lib 'lib';
13    
# Line 71  has 'html_dump_width' => ( Line 71  has 'html_dump_width' => (
71          default => 250,          default => 250,
72  );  );
73    
74    has 'wrap_in_page' => (
75            documentation => 'wrap full html page with status bar around content',
76            is => 'rw',
77            isa => 'Bool',
78            default => 1,
79    );
80    
81  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
82  my $escape_re  = join '|' => keys %escape;  my $escape_re  = join '|' => keys %escape;
83    
84  sub html_escape {  sub html_escape {
85          my ( $self, $html ) = @_;          my ( $self, $html ) = @_;
86            return '' unless defined $html;
87          $html =~ s/($escape_re)/$escape{$1}/g;          $html =~ s/($escape_re)/$escape{$1}/g;
88          return $html;          return $html;
89  }  }
90    
91    # from Mojo::ByteStream
92    sub url_escape {
93            my ( $self, $url, $pattern ) = @_;
94            $pattern ||= 'A-Za-z0-9\-\.\_\~';
95            $url =~ s/([^$pattern])/sprintf('%%%02X',ord($1))/ge;
96            return $url;
97    }
98    
99  sub html_dump {  sub html_dump {
100          my ( $self, $dump ) = @_;          my ( $self, $dump ) = @_;
101          $dump = dump( $dump ) if ref($dump);          $dump = dump( $dump ) if ref($dump);
# Line 181  sub add_head { Line 197  sub add_head {
197  sub _add_css_js {  sub _add_css_js {
198          my ( $self, $what, $content ) = @_;          my ( $self, $what, $content ) = @_;
199    
200            my $md5 = md5( $content );
201            return if $self->{_add_css_js_seen}->{$what}->{$md5}++;
202    
203          my $tag  = $what eq 'css' ? 'style'    : 'script';          my $tag  = $what eq 'css' ? 'style'    : 'script';
204          my $type = $what eq 'css' ? 'text/css' : 'text/javascript';          my $type = $what eq 'css' ? 'text/css' : 'text/javascript';
205          my $head;          my $head;
206    
207          my ( $package, $path, $line ) = caller(1);          my ( $package, $path, $line ) = caller(1);
208    
209          $content = "/$content" if -e $content;          $content = "/$content" if $content !~ m{[\n\r]} && -e $content;
210          if ( $content =~ $re_html ) {          if ( $content =~ $re_html && $what ne 'js' ) {
211                  $head = qq|                  $head = qq|
212                          $content                          $content
213                          <!-- $type via $package at $path line $line -->                          <!-- $type via $package at $path line $line -->
214                  |;                  |;
215          } elsif ( $content =~ m{^(/|https?://)} ) {          } elsif ( $content =~ m{^(/\w+|https?://)} && $content !~ m{[\n\r]} ) {
216                  if ( $what eq 'js' ) {                  if ( $what eq 'js' ) {
217                          $head = qq|                          $head = qq|
218                                  <$tag type="$type" src="$content">                                  <$tag type="$type" src="$content">
# Line 230  sub add_js { Line 249  sub add_js {
249  our $reload_counter = 0;  our $reload_counter = 0;
250    
251    
252  =head2 page  =head2 html_page
253    
254    $self->page(    $self->html_page(
255          title => 'page title',          title => 'page title',
256          head  => '<!-- optional head markup -->',          head  => '<!-- optional head markup -->',
257          body  => '<b>Page Body</b>',          body  => '<b>Page Body</b>',
# Line 243  our $reload_counter = 0; Line 262  our $reload_counter = 0;
262  our @status;  our @status;
263  sub status { @status };  sub status { @status };
264    
265  our $icon_html;  sub html_page {
   
 sub page {  
266          my $self = shift;          my $self = shift;
267          my $a = {@_};          my $a = {@_};
268    
# Line 268  sub page { Line 285  sub page {
285                  warn "# no body, invoke $self->$run on ", ref($self);                  warn "# no body, invoke $self->$run on ", ref($self);
286                  $body = $self->$run;                  $body = $self->$run;
287          }          }
288          if ( $self->content_type !~ m{html} ) {          if ( $self->content_type !~ m{html} || ! $self->wrap_in_page ) {
289                  warn "# return only $self body ", $self->content_type;                  warn "# return only $self body ", $self->content_type;
290                  return $body                  return $body
291          } elsif ( ! defined $body ) {          } elsif ( ! defined $body ) {
# Line 284  sub page { Line 301  sub page {
301    
302          my $right =          my $right =
303                  qq|                  qq|
304                          <span class="right">                          <span class="frey-status-right">
305                          <a title="reload $url"  href="/reload$url">reload</a>                          <a title="reload $url"  href="/reload$url">reload</a>
306                          <a title="$description" href="/exit$url" target="exit">$exit</a>                          <a title="$description" href="/exit$url" target="exit">$exit</a>
307                          </span>                          </span>
# Line 295  sub page { Line 312  sub page {
312          my $revision = $svk->info->{Revision} || '';          my $revision = $svk->info->{Revision} || '';
313          $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};          $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};
314    
315          $self->add_icon unless $icon_html;          $status_line = $ENV{FREY_DEV} ? qq|
316                    <div class="frey-status-line">
317                            <a href="/">Frey</a> $Frey::VERSION $revision
318                            $status_line
319                            $right
320                    </div>
321            | : '';
322    
323            $self->add_icon;
324    
325            my $title = undef
326                    || $a->{title}
327                    || $self->title
328                    || ref($self)
329                    ;
330    
331    #       $title =~ s{(\w)\w+::}{$1:}g; # XXX compress names of classes
332    
333            $self->add_css(qq|
334                    body {
335                            padding-bottom: 3em; /* don't overlap status line */
336                    }
337            |);
338    
339          my $html = join("\n",          my $html = join("\n",
340                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
341                  $self->_head_html,                  $self->_head_html,
342                  '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',                  qq|<title>$title</title>|,
343                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
344                  ( $icon_html || '<!-- no icon -->' ),                  ( $self->icon_html ),
345                  ( $a->{head} || '' ),                  ( $a->{head} || '' ),
346                  qq|                  qq|
347                  </head><body>                  </head><body>
348                  $body                  $body
349                  <div class="frey-status-line">                  $status_line
                         <a href="/">Frey</a> $Frey::VERSION $revision  
                         $status_line  
                         $right  
                 </div>  
350              </body></html>              </body></html>
351                  |,                  |,
352          );          );
# Line 400  sub error { Line 435  sub error {
435          my $self = shift;          my $self = shift;
436          my $error = join(" ", @_);          my $error = join(" ", @_);
437    
438            if ( $error =~ m{<error>(.+)</error>}s ) {
439                    return qq|<div class="frey-error frey-fatal">$1</div>|;
440            }
441    
442          my $fatal = '';          my $fatal = '';
443          my $backtrace = '';          my $backtrace = '';
444    
# Line 431  sub error { Line 470  sub error {
470    
471  sub add_status {  sub add_status {
472          my ( $self, $data ) = @_;          my ( $self, $data ) = @_;
473          push @status, { 'X' => [ $self->backtrace ] };          die "no data" unless $data;
474          if ( ref($data) ) {          if ( ref $data  ) {
475                  push @status, $data;                  push @status, $data;
476          } else {          } else {
477                  if ( defined $status[ $#status ] ) {                  if ( defined $status[ $#status ] ) {
# Line 447  sub add_status { Line 486  sub add_status {
486    
487  Called at beginning of each request  Called at beginning of each request
488    
489    $self->clean_status;    $self->setup_request;
490    
491  =cut  =cut
492    
493  sub clean_status {  sub setup_request {
494          my ($self) = shift;          my ($self) = shift;
495            warn "## clean_status";
496          @head = ( 'static/frey.css' );          @head = ( 'static/frey.css' );
497          @status = (          @status = (
498                  { 'ClassBrowser' => Frey::Class::Browser->new( usage_sort => 1, usage_on_top => 0 )->as_markup },                  { 'ClassBrowser' => Frey::Class::Browser->new( usage_sort => 1, usage_on_top => 0 )->as_markup },
499                  { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },                  { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },
500                  { 'INC' => Frey::INC->new->as_markup },                  { 'INC' => Frey::INC->new->as_markup },
501          );          );
         $icon_html = '';  
502  }  }
503    
504  =head2 status_parts  =head2 status_parts
# Line 483  sub DEMOLISH { Line 522  sub DEMOLISH {
522    
523  =cut  =cut
524    
 =head2 add_icon  
   
   Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png  
   Frey::Foo->add_icon('warning'); # /static/icons/Frey/Foo/warning.png  
   
 =cut  
   
 sub icon_path {  
         my ($self,$class,$variant) = @_;  
   
         sub icon_exists {  
                 my $class = shift;  
                 $class =~ s{::}{/}g;  
                 $class .= "/$variant" if $variant;  
                 my $icon_path = 'static/icons/' . $class . '.png';  
                 return $icon_path if -e $icon_path;  
                 return;  
         }  
   
         my $path = icon_exists( $class );  
         if ( ! $path ) {  
                 my $super_class = $class;  
                 while ( $super_class =~ s{::[^:]+$}{} && ! $path ) {  
                         $path = icon_exists( $super_class ) unless $super_class eq 'Frey'; # don't default on Frey icon  
                 }  
         }  
   
         if ( ! $path ) {  
                 $self->TODO( "add icon for $class" . ( $variant ? " variant $variant" : '' ) );  
                 return undef;  
         }  
   
         warn "# $class from $self icon_path $path" if $self->debug;  
         return $path;  
 }  
   
 sub add_icon {  
         my ($self,$variant) = @_;  
   
         my $class = $self->class if $self->can('class');  
         #$class ||= $self->title;  
         $class ||= ref($self);  
         my $icon_path = $self->icon_path( $class, $variant ) || return;  
   
         $icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|;  
         warn "# using icon $icon_path";  
   
 =for later  
   
         # FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work!  
         my $ico_path = $icon_path;  
         $ico_path =~ s{png$}{ico};  
         if ( ! -e $ico_path ) {  
                 system "convert $icon_path $ico_path";  
                 warn "# convert $icon_path $ico_path : $@";  
         }  
         $icon_html .= qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path">| if -e $ico_path;  
   
 =cut  
   
 }  
   
525  my $warn_colors = {  my $warn_colors = {
526          '#'  => '#444',          '#'  => '#444',
527          '##' => '#888',          '##' => '#888',
# Line 565  warn "## multiline markers ", dump( $mul Line 542  warn "## multiline markers ", dump( $mul
542  =cut  =cut
543    
544  sub log_path {  sub log_path {
545          $Frey::Bootstrap::log_path || die "no log_path?";          $Frey::Bootstrap::log_path || die "no log_path did you load Frey::Bootstrap?";
546  }  }
547    
548  our $pwd = `pwd`;  our $pwd = `pwd`;
# Line 745  sub strip { Line 722  sub strip {
722          return $t;          return $t;
723  }  }
724    
725    no Moose::Role;
726    
727  1;  1;

Legend:
Removed from v.908  
changed lines
  Added in v.1196

  ViewVC Help
Powered by ViewVC 1.1.26