/[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 725 by dpavlin, Fri Dec 5 17:33:00 2008 UTC revision 859 by dpavlin, Tue Dec 16 14:26:23 2008 UTC
# Line 3  use Moose::Role; Line 3  use Moose::Role;
3    
4  with 'Frey::Session';  with 'Frey::Session';
5    
 use Frey::Types;  
   
6  #use Continuity::Widget::DomNode;  #use Continuity::Widget::DomNode;
7  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
8  use Carp qw/confess cluck carp/;  use Carp qw/confess cluck carp/;
9  use File::Slurp;  use File::Slurp;
10    use Text::Tabs; # expand, unexpand
11    
12    use lib 'lib';
13    
14    use Frey::Types;
15    
16  use Frey::Bookmarklet;  use Frey::Bookmarklet;
17  use Frey::ClassBrowser;  use Frey::Class::Browser;
18  use Frey::INC;  use Frey::INC;
19    
20  use Frey::SVK;  use Frey::SVK;
21    
 use Text::Tabs; # expand, unexpand  
   
22  our @head;  our @head;
23  sub head { @head }  sub head { @head }
24    
# Line 93  sub html_dump { Line 94  sub html_dump {
94  sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }  sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }
95  sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }  sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }
96    
97  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
98    
99  sub popup_dropdown {  sub popup_dropdown {
100          my ( $self, $type, $name, $content, $full ) = @_;          my ( $self, $type, $name, $content, $full ) = @_;
# Line 115  sub popup_dropdown { Line 116  sub popup_dropdown {
116          }          }
117  }  }
118    
119  sub _inline_path {  sub _inline_path_size {
120          my ( $self, $path ) = @_;          my ( $self, $path ) = @_;
121          -s $path < $self->inline_smaller_than;          -s $path < $self->inline_smaller_than && -s $path;
122  }  }
123    
124  sub _head_html {  sub _head_html {
# Line 125  sub _head_html { Line 126  sub _head_html {
126          my $out = '';          my $out = '';
127          foreach my $path ( @head ) {          foreach my $path ( @head ) {
128                  $path =~ s!^/!!;                  $path =~ s!^/!!;
129                    my $size = $self->_inline_path_size( $path );
130                  if ( $path =~ m/\.js$/ ) {                  if ( $path =~ m/\.js$/ ) {
131                          $out .= $self->_inline_path( $path ) ?                          $out .= $size ?
132                                  qq|<script type="text/javascript">\n/* inline $path */\n\n| . read_file($path) . qq|\n</script>| :                                  qq|<script type="text/javascript">\n/* inline $path $size bytes */\n\n| . read_file($path) . qq|\n</script>| :
133                                  qq|<script type="text/javascript" src="/$path"></script>|;                                  qq|<script type="text/javascript" src="/$path"></script>|;
134                  } elsif ( $path =~ m/\.css$/ ) {                  } elsif ( $path =~ m/\.css$/ ) {
135                          $out .= $self->_inline_path( $path ) ?                          $out .= $size ?
136                                  qq|<style type="text/css">\n/* inline $path */\n\n| . read_file( $path ) . qq|\n</style>| :                                  qq|<style type="text/css">\n/* inline $path $size bytes */\n\n| . read_file( $path ) . qq|\n</style>| :
137                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
138                  } elsif ( $path =~ m{<.+>}s ) {                  } elsif ( $path =~ m{<.+>}s ) {
139                          $out .= $path;                          $out .= $path;
# Line 173  sub add_head { Line 175  sub add_head {
175    
176  }  }
177    
178    sub _add_css_js {
179            my ( $self, $what, $content ) = @_;
180    
181            my $tag  = $what eq 'css' ? 'style'    : 'script';
182            my $type = $what eq 'css' ? 'text/css' : 'text/javascript';
183            my $head;
184    
185            my ( $package, $path, $line ) = caller(1);
186    
187            if ( $content =~ m{\.(js|css)} ) {
188                    $content = "/$content" if -e $content;
189                    if ( $content =~ $re_html ) {
190                            $head = qq|
191                                    $content
192                                    <!-- $type via $package at $path line $line -->
193                            |;
194                    } elsif ( $what eq 'js' ) {
195                            $head = qq|
196                                    <$tag type="$type" src="$content">
197                                    /* $what via $package at $path line $line */
198                                    </$tag>
199                            |;
200                    } else {
201                            $head = qq|
202                                    <link rel="stylesheet" type="$type" href="$content">
203                                    <!-- $what via $package at $path line $line -->
204                            |;
205                    }
206            } else {
207                    $head = qq|
208                            <$tag type="$type">
209                            /* via $package at $path line $line */
210                            $content
211                            </$tag>
212                    |;
213            };
214            $self->add_head( $head );
215    }
216    
217  sub add_css {  sub add_css {
218          my ($self,$css) = @_;          my ($self,$css) = @_;
219          my ( $package, $path, $line ) = caller;          $self->_add_css_js( 'css', $css );
220          $self->add_head( qq|  }
221          <style type="text/css">  
222          /* via $package at $path line $line */  sub add_js {
223          $css          my ($self,$js) = @_;
224          </style>          $self->_add_css_js( 'js', $js );
         | );  
225  }  }
226    
227  our $reload_counter = 0;  our $reload_counter = 0;
# Line 225  sub page { Line 265  sub page {
265          if ( ! $body ) {          if ( ! $body ) {
266                  my $run = $a->{run} || 'as_markup';                  my $run = $a->{run} || 'as_markup';
267                  warn "# no body, invoke $self->$run on ", ref($self);                  warn "# no body, invoke $self->$run on ", ref($self);
268                  eval {                  $body = $self->$run;
                         $body = $self->$run;  
                 };  
                 $body = $self->error( $@, '' ) if $@;  
269          }          }
270          if ( $self->content_type !~ m{html} ) {          if ( $self->content_type !~ m{html} ) {
271                  warn "# return only $self body ", $self->content_type;                  warn "# return only $self body ", $self->content_type;
# Line 344  sub html_links { Line 381  sub html_links {
381          return $error;          return $error;
382  }  }
383    
384    sub html_self {
385            my $self = shift;
386            my $html = $self;
387            $html =~ s{([\w:]+)=}{<a target="$1" href="/$1" title="introspect $1">$1</a>=}gsm;
388            return $html;
389    }
390    
391  =head2 error  =head2 error
392    
393  This method will return error to browser and backtrace unless  This method will return error to browser and backtrace unless
# Line 356  sub error { Line 400  sub error {
400          my $error = join(" ", @_);          my $error = join(" ", @_);
401    
402          my $fatal = '';          my $fatal = '';
403            my $backtrace = '';
404    
405          if ( $error !~ m{\n$} ) {          if ( $error !~ m{\n$} ) {
406                  if ( my @backtrace = $self->backtrace ) {                  if ( my @backtrace = $self->backtrace ) {
407                          $error .= "\n\t" . join( "\n\t", @backtrace );                          $backtrace =
408                                      "\n" . $self->html_self . "->error backtrace\n\t"
409                                    . $self->html_links( join( "\n\t", @backtrace ) )
410                                    ;
411                          $fatal = qq| frey-fatal|;                          $fatal = qq| frey-fatal|;
412                  }                  }
413          }          }
414    
415          warn "ERROR: $error\n";          warn "ERROR: $error\n";
416          return          $self->add_icon('error');
417                  qq|<pre class="frey-error$fatal">|          $error = $self->html_links( $error );
418                  . $self->html_links( $error ) .          return qq|<pre class="frey-error$fatal">$error $backtrace</pre>| ;
                 qq|</pre>|  
                 ;  
419  }  }
420    
421  =head1 Status line  =head1 Status line
# Line 408  sub clean_status { Line 454  sub clean_status {
454          my ($self) = shift;          my ($self) = shift;
455          @head = ( 'static/frey.css' );          @head = ( 'static/frey.css' );
456          @status = (          @status = (
457                  { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup },                  { 'ClassBrowser' => Frey::Class::Browser->new( usage_sort => 1, usage_on_top => 0 )->as_markup },
458                  { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },                  { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },
459                  { 'INC' => Frey::INC->new->as_markup },                  { 'INC' => Frey::INC->new->as_markup },
460          );          );
# Line 445  sub DEMOLISH { Line 491  sub DEMOLISH {
491    
492  sub icon_path {  sub icon_path {
493          my ($self,$class,$variant) = @_;          my ($self,$class,$variant) = @_;
494          my $icon = $class;  
495          $icon ||= $self->title;          sub icon_exists {
496          $icon =~ s{::}{/}g;                  my $class = shift;
497          $icon .= "/$variant" if $variant;                  $class =~ s{::}{/}g;
498          my $path = 'static/icons/' . $icon . '.png';                  $class .= "/$variant" if $variant;
499          if ( -e $path ) {                  my $icon_path = 'static/icons/' . $class . '.png';
500                  warn "# $class from $self icon_path $path" if $self->debug;                  return $icon_path if -e $icon_path;
501                  return $path;                  return;
502          } else {          }
503                  $self->TODO( "add $path icon for $class" );  
504            my $path = icon_exists( $class );
505            if ( ! $path ) {
506                    my $super_class = $class;
507                    while ( $super_class =~ s{::[^:]+$}{} && ! $path ) {
508                            $path = icon_exists( $super_class ) unless $super_class eq 'Frey'; # don't default on Frey icon
509                    }
510            }
511    
512            if ( ! $path ) {
513                    $self->TODO( "add icon for $class" . ( $variant ? " variant $variant" : '' ) );
514                  return undef;                  return undef;
515          }          }
516    
517            warn "# $class from $self icon_path $path" if $self->debug;
518            return $path;
519  }  }
520    
521  sub add_icon {  sub add_icon {
522          my ($self,$variant) = @_;          my ($self,$variant) = @_;
523    
524          my $class = ref($self);          my $class = $self->class if $self->can('class');
525          $class = $self->class if $self->can('class');          #$class ||= $self->title;
526            $class ||= ref($self);
527          my $icon_path = $self->icon_path( $class, $variant ) || return;          my $icon_path = $self->icon_path( $class, $variant ) || return;
528    
529          $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 634  sub backtrace { Line 694  sub backtrace {
694          my ($self) = @_;          my ($self) = @_;
695    
696          my @backtrace;          my @backtrace;
697          foreach ( 0 .. 5 ) {          foreach ( 1 .. 5 ) { # 0 = backtrace
698                  my (                  my (
699                          $package,$path,$line                          $package,$path,$line
700                          # subroutine hasargs                          # subroutine hasargs
# Line 668  sub checkbox { Line 728  sub checkbox {
728          qq|<input name="$name" value="$value" type="checkbox"$checked>|;          qq|<input name="$name" value="$value" type="checkbox"$checked>|;
729  }  }
730    
731    =head2 strip
732    
733    Strip whitespace around content
734    
735      my $stripped = strip('  no more whitespace around this   ');
736    
737    =cut
738    
739    sub strip {
740            my $t = shift;
741            $t =~ s{^\s+}{}gs;
742            $t =~ s{>\s+<}{><}gs;
743            $t =~ s{\s+$}{}gs;
744            return $t;
745    }
746    
747  1;  1;

Legend:
Removed from v.725  
changed lines
  Added in v.859

  ViewVC Help
Powered by ViewVC 1.1.26