/[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 816 by dpavlin, Thu Dec 11 21:36:04 2008 UTC revision 1064 by dpavlin, Mon Apr 27 18:40:41 2009 UTC
# Line 3  use Moose::Role; Line 3  use Moose::Role;
3    
4  with 'Frey::Session';  with 'Frey::Session';
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 94  sub html_dump { Line 110  sub html_dump {
110  sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }  sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }
111  sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }  sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }
112    
113  our $re_html = qr{<(?:!--.+?--|(\w+).+?/\1|[^>]+/?)>}s; # relaxed html check for one semi-valid tag  our $re_html = qr{<(?:!--|(\w+)|[^>]+)/?>}s; # relaxed html check for one semi-valid tag
114    
115  sub popup_dropdown {  sub popup_dropdown {
116          my ( $self, $type, $name, $content, $full ) = @_;          my ( $self, $type, $name, $content, $full ) = @_;
# Line 116  sub popup_dropdown { Line 132  sub popup_dropdown {
132          }          }
133  }  }
134    
135  sub _inline_path {  sub _inline {
136          my ( $self, $path ) = @_;          my ( $self, $path ) = @_;
137          -s $path < $self->inline_smaller_than;          return unless defined $path;
138            warn "# _inline $path";
139            -e $path && -s $path < $self->inline_smaller_than && -s $path;
140  }  }
141    
142  sub _head_html {  sub _head_html {
# Line 127  sub _head_html { Line 145  sub _head_html {
145          foreach my $path ( @head ) {          foreach my $path ( @head ) {
146                  $path =~ s!^/!!;                  $path =~ s!^/!!;
147                  if ( $path =~ m/\.js$/ ) {                  if ( $path =~ m/\.js$/ ) {
148                          $out .= $self->_inline_path( $path ) ?                          my $size;
149                                  qq|<script type="text/javascript">\n/* inline $path */\n\n| . read_file($path) . qq|\n</script>| :                          $out .= $size = _inline( $path ) ?
150                                    qq|<script type="text/javascript">\n/* inline $path $size bytes */\n\n| . read_file($path) . qq|\n</script>| :
151                                  qq|<script type="text/javascript" src="/$path"></script>|;                                  qq|<script type="text/javascript" src="/$path"></script>|;
152                  } elsif ( $path =~ m/\.css$/ ) {                  } elsif ( $path =~ m/\.css$/ ) {
153                          $out .= $self->_inline_path( $path ) ?                          my $size;
154                                  qq|<style type="text/css">\n/* inline $path */\n\n| . read_file( $path ) . qq|\n</style>| :                          $out .= $size = _inline( $path ) ?
155                                    qq|<style type="text/css">\n/* inline $path $size bytes */\n\n| . read_file( $path ) . qq|\n</style>| :
156                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
157                  } elsif ( $path =~ m{<.+>}s ) {                  } elsif ( $path =~ m{<.+>}s ) {
158                          $out .= $path;                          $out .= $path;
# Line 174  sub add_head { Line 194  sub add_head {
194    
195  }  }
196    
197    my $seen;
198    
199    sub _add_css_js {
200            my ( $self, $what, $content ) = @_;
201    
202            my $md5 = md5( $content );
203            return if $seen->{$what}->{$md5}++;
204    
205            my $tag  = $what eq 'css' ? 'style'    : 'script';
206            my $type = $what eq 'css' ? 'text/css' : 'text/javascript';
207            my $head;
208    
209            my ( $package, $path, $line ) = caller(1);
210    
211            $content = "/$content" if $content !~ m{[\n\r]} && -e $content;
212            if ( $content =~ $re_html && $what ne 'js' ) {
213                    $head = qq|
214                            $content
215                            <!-- $type via $package at $path line $line -->
216                    |;
217            } elsif ( $content =~ m{^(/\w+|https?://)} && $content !~ m{[\n\r]} ) {
218                    if ( $what eq 'js' ) {
219                            $head = qq|
220                                    <$tag type="$type" src="$content">
221                                    /* $what via $package at $path line $line */
222                                    </$tag>
223                            |;
224                    } else {
225                            $head = qq|
226                                    <link rel="stylesheet" type="$type" href="$content">
227                                    <!-- $what via $package at $path line $line -->
228                            |;
229                    }
230            } else {
231                    $head = qq|
232                            <$tag type="$type">
233                            /* via $package at $path line $line */
234                            $content
235                            </$tag>
236                    |;
237            };
238            $self->add_head( $head );
239    }
240    
241  sub add_css {  sub add_css {
242          my ($self,$css) = @_;          my ($self,$css) = @_;
243          my ( $package, $path, $line ) = caller;          $self->_add_css_js( 'css', $css );
         $self->add_head( qq|  
         <style type="text/css">  
         /* via $package at $path line $line */  
         $css  
         </style>  
         | );  
244  }  }
245    
246  sub add_js {  sub add_js {
247          my ($self,$js) = @_;          my ($self,$js) = @_;
248          my ( $package, $path, $line ) = caller;          $self->_add_css_js( 'js', $js );
   
         if ( $js =~ m{http.*\.js} ) {  
                 $self->add_head( qq|  
                         <script type="text/javascript" src="$js">  
                         /* via $package at $path line $line */  
                         </script>  
                 |);  
         } else {  
                 $self->add_head( qq|  
                         <script type="text/javascript">  
                         /* via $package at $path line $line */  
                         $js  
                         </script>  
                 | );  
         };  
249  }  }
250    
251  our $reload_counter = 0;  our $reload_counter = 0;
252    
253    
254  =head2 page  =head2 html_page
255    
256    $self->page(    $self->html_page(
257          title => 'page title',          title => 'page title',
258          head  => '<!-- optional head markup -->',          head  => '<!-- optional head markup -->',
259          body  => '<b>Page Body</b>',          body  => '<b>Page Body</b>',
# Line 223  sub status { @status }; Line 266  sub status { @status };
266    
267  our $icon_html;  our $icon_html;
268    
269  sub page {  sub html_page {
270          my $self = shift;          my $self = shift;
271          my $a = {@_};          my $a = {@_};
272    
         warn "## page ",dump($a);  
   
273          $reload_counter++;          $reload_counter++;
274    
275          my $status_line = '';          my $status_line = '';
# Line 246  sub page { Line 287  sub page {
287          if ( ! $body ) {          if ( ! $body ) {
288                  my $run = $a->{run} || 'as_markup';                  my $run = $a->{run} || 'as_markup';
289                  warn "# no body, invoke $self->$run on ", ref($self);                  warn "# no body, invoke $self->$run on ", ref($self);
290                  eval {                  $body = $self->$run;
                         $body = $self->$run;  
                 };  
                 $body = $self->error( $@, '' ) if $@;  
291          }          }
292          if ( $self->content_type !~ m{html} ) {          if ( $self->content_type !~ m{html} || ! $self->wrap_in_page ) {
293                  warn "# return only $self body ", $self->content_type;                  warn "# return only $self body ", $self->content_type;
294                  return $body                  return $body
295          } elsif ( ! defined $body ) {          } elsif ( ! defined $body ) {
# Line 267  sub page { Line 305  sub page {
305    
306          my $right =          my $right =
307                  qq|                  qq|
308                          <span class="right">                          <span class="frey-status-right">
309                          <a title="reload $url"  href="/reload$url">reload</a>                          <a title="reload $url"  href="/reload$url">reload</a>
310                          <a title="$description" href="/exit$url" target="exit">$exit</a>                          <a title="$description" href="/exit$url" target="exit">$exit</a>
311                          </span>                          </span>
# Line 280  sub page { Line 318  sub page {
318    
319          $self->add_icon unless $icon_html;          $self->add_icon unless $icon_html;
320    
321            my $title = undef
322                    || $a->{title}
323                    || $self->title
324                    || ref($self)
325                    ;
326    
327    #       $title =~ s{(\w)\w+::}{$1:}g; # XXX compress names of classes
328    
329            $self->add_css(qq|
330                    body {
331                            padding-bottom: 3em; /* don't overlap status line */
332                    }
333            |);
334    
335          my $html = join("\n",          my $html = join("\n",
336                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
337                  $self->_head_html,                  $self->_head_html,
338                  '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',                  qq|<title>$title</title>|,
339                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
340                  ( $icon_html || '<!-- no icon -->' ),                  ( $icon_html || '<!-- no icon -->' ),
341                  ( $a->{head} || '' ),                  ( $a->{head} || '' ),
# Line 365  sub html_links { Line 417  sub html_links {
417          return $error;          return $error;
418  }  }
419    
420    sub html_self {
421            my $self = shift;
422            my $html = $self;
423            $html =~ s{([\w:]+)=}{<a target="$1" href="/$1" title="introspect $1">$1</a>=}gsm;
424            return $html;
425    }
426    
427  =head2 error  =head2 error
428    
429  This method will return error to browser and backtrace unless  This method will return error to browser and backtrace unless
# Line 377  sub error { Line 436  sub error {
436          my $error = join(" ", @_);          my $error = join(" ", @_);
437    
438          my $fatal = '';          my $fatal = '';
439            my $backtrace = '';
440    
441          if ( $error !~ m{\n$} ) {          if ( $error !~ m{\n$} ) {
442                  if ( my @backtrace = $self->backtrace ) {                  if ( my @backtrace = $self->backtrace ) {
443                          $error .= "\n\t" . join( "\n\t", @backtrace );                          $backtrace =
444                                      "\n" . $self->html_self . "->error backtrace\n\t"
445                                    . $self->html_links( join( "\n\t", @backtrace ) )
446                                    ;
447                          $fatal = qq| frey-fatal|;                          $fatal = qq| frey-fatal|;
448                  }                  }
449          }          }
450    
451          warn "ERROR: $error\n";          warn "ERROR: $error\n";
452          return          $self->add_icon('error');
453                  qq|<pre class="frey-error$fatal">|          $error = $self->html_links( $error );
454                  . $self->html_links( $error ) .          return qq|<pre class="frey-error$fatal">$error $backtrace</pre>| ;
                 qq|</pre>|  
                 ;  
455  }  }
456    
457  =head1 Status line  =head1 Status line
# Line 405  sub error { Line 466  sub error {
466    
467  sub add_status {  sub add_status {
468          my ( $self, $data ) = @_;          my ( $self, $data ) = @_;
469          push @status, { 'X' => [ $self->backtrace ] };          die "no data" unless $data;
470          if ( ref($data) ) {          if ( ref $data  ) {
471                  push @status, $data;                  push @status, $data;
472          } else {          } else {
473                  if ( defined $status[ $#status ] ) {                  if ( defined $status[ $#status ] ) {
# Line 466  sub DEMOLISH { Line 527  sub DEMOLISH {
527    
528  sub icon_path {  sub icon_path {
529          my ($self,$class,$variant) = @_;          my ($self,$class,$variant) = @_;
 #       $class ||= $self->title;  
530    
531          sub icon_exists {          sub icon_exists {
532                  my $class = shift;                  my $class = shift;
# Line 478  sub icon_path { Line 538  sub icon_path {
538          }          }
539    
540          my $path = icon_exists( $class );          my $path = icon_exists( $class );
541            if ( ! $path ) {
542          while ( $class =~ s{::[^:]+$}{} && ! $path ) {                  my $super_class = $class;
543                  $path = icon_exists( $class ) unless $class eq 'Frey'; # don't default on Frey icon                  while ( $super_class =~ s{::[^:]+$}{} && ! $path ) {
544                            $path = icon_exists( $super_class ) unless $super_class eq 'Frey'; # don't default on Frey icon
545                    }
546          }          }
547    
548          if ( -e $path ) {          if ( ! $path ) {
549                  warn "# $class from $self icon_path $path" if $self->debug;                  $self->TODO( "add icon for $class" . ( $variant ? " variant $variant" : '' ) );
                 return $path;  
         } else {  
                 $self->TODO( "add $path icon for $class $variant" );  
550                  return undef;                  return undef;
551          }          }
552    
553            warn "# $class from $self icon_path $path" if $self->debug;
554            return $path;
555  }  }
556    
557  sub add_icon {  sub add_icon {
558          my ($self,$variant) = @_;          my ($self,$variant) = @_;
559    
560          my $class = ref($self);          my $class = $self->class if $self->can('class');
561          $class = $self->class if $self->can('class');          #$class ||= $self->title;
562            $class ||= ref($self);
563          my $icon_path = $self->icon_path( $class, $variant ) || return;          my $icon_path = $self->icon_path( $class, $variant ) || return;
564    
565          $icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|;          $icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|;
# Line 667  sub backtrace { Line 730  sub backtrace {
730          my ($self) = @_;          my ($self) = @_;
731    
732          my @backtrace;          my @backtrace;
733          foreach ( 0 .. 5 ) {          foreach ( 1 .. 5 ) { # 0 = backtrace
734                  my (                  my (
735                          $package,$path,$line                          $package,$path,$line
736                          # subroutine hasargs                          # subroutine hasargs

Legend:
Removed from v.816  
changed lines
  Added in v.1064

  ViewVC Help
Powered by ViewVC 1.1.26