/[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 540 by dpavlin, Wed Nov 26 18:02:38 2008 UTC revision 564 by dpavlin, Thu Nov 27 21:31:02 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';  with 'Frey::Session';
5    
6  use Frey::Types;  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 cluck/;  use Carp qw/confess cluck/;
11  use File::Slurp;  use File::Slurp;
# Line 49  has 'dump_max_bytes' => ( Line 49  has 'dump_max_bytes' => (
49          is => 'rw',          is => 'rw',
50          isa => 'Int',          isa => 'Int',
51          default => 4096,          default => 4096,
52          documentation => 'Maximum dump size sent to browser before truncation',          documentation => 'maximum dump size sent to browser before truncation',
53  );  );
54    
55  has 'inline_smaller_than' => (  has 'inline_smaller_than' => (
56          is => 'rw',          is => 'rw',
57          isa => 'Int',          isa => 'Int',
58          default => 10240,          default => 10240,
59          documentation => 'Inline JavaScript and CSS to reduce round-trips',          documentation => 'inline JavaScript and CSS to reduce round-trips',
60  );  );
61    
62  has 'chop_warning' => (  has 'html_dump_width' => (
63            documentation => 'crop longer lines in dumps',
64          is => 'rw',          is => 'rw',
65          isa => 'Int',          isa => 'Int',
66          default => 80,  #       required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them
67          documentation => 'Crop lines longer',          default => 128,
68  );  );
69    
70  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
# Line 76  sub html_escape { Line 77  sub html_escape {
77  }  }
78    
79  sub html_dump {  sub html_dump {
80          my $self = shift;          my ( $self, $dump ) = @_;
81          $self->html_escape( dump( @_ ) );          $dump = dump( $dump ) if ref($dump);
82            my $width = $self->html_dump_width;
83            $dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs;
84            $dump = $self->html_escape( $dump );
85            $dump =~ s{\Q...\E}{&hellip;}gs;
86            return "<code>$dump</code>";
87  }  }
88    
89  sub dom2html {  sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }
90  #       warn "## dom2html ",dump( @_ );  sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }
91          return Continuity::Widget::DomNode->create( @_ )->to_string;  
92    our $re_html = qr{<(?:!--.+?--|(\w+).+?/\1)>}s; # relaxed html check for one semi-valid tag
93    
94    sub popup_dropdown {
95            my ( $self, $type, $name, $content, $full ) = @_;
96    
97            $content = $self->html_dump($content) if ref $content;
98    
99            $content = qq|<span>$content</span>| unless $content =~ m{^\s*<(span|a|code).+?/\1>\s*};
100    
101            warn "## $type [$name] = ", length( $content ), " bytes" if $self->debug;
102    
103            if ( $name =~ m{::} && $name !~ $re_html ) {
104                    return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|;
105            } else {
106                    return qq|<span class="frey-$type">$name $content</span>\n|;
107            }
108  }  }
109    
110  sub _inline_path {  sub _inline_path {
# Line 128  sub add_head { Line 150  sub add_head {
150          return if ! defined $path || $path eq '';          return if ! defined $path || $path eq '';
151          $path =~ s!^/!!;          $path =~ s!^/!!;
152    
153          if ( $path =~ m{<.*>}s ) {          if ( $path =~ $re_html ) {
154                  push @{ $self->head }, $path;                  push @{ $self->head }, $path;
155          } elsif ( -e $path ) {          } elsif ( -e $path ) {
156                  if ( $path =~ m/\.(?:js|css)$/ ) {                  if ( $path =~ m/\.(?:js|css)$/ ) {
# Line 161  sub status { @status }; Line 183  sub status { @status };
183    
184  our $icon_html;  our $icon_html;
185    
 sub popup {  
         my ( $self, $name, $content, $full ) = @_;  
   
         if ( ref($content) ) {  
                 $content = '<code>' . dump($content) . '</code>';  
                 my $l = length($content);  
                 $content = qq|<span>$l bytes</span>| if ! $full && $l > $self->dump_max_bytes;  
         } else {  
                 $content = qq|<span>$content</span>|;  
         }  
   
         warn "## popup [$name] = ", length( $content ), " bytes" if $self->debug;  
         return qq|<span class="frey-popup">$name $content</span>\n|;  
 }  
   
186  sub page {  sub page {
187          my $self = shift;          my $self = shift;
188          my $a = {@_};          my $a = {@_};
# Line 345  sub icon_path { Line 352  sub icon_path {
352                  warn "# $class from $self icon_path $path";                  warn "# $class from $self icon_path $path";
353                  return $path;                  return $path;
354          } else {          } else {
355                  warn "TODO: add $path icon for $class";                  $self->TODO( "add $path icon for $class" );
356                  return undef;                  return undef;
357          }          }
358  }  }
# Line 432  sub warnings_html { Line 439  sub warnings_html {
439    
440                          my $msg = $self->html_escape( $_ );                          my $msg = $self->html_escape( $_ );
441                          my $spacer = ' ';                          my $spacer = ' ';
442                          if ( length($msg) > $self->chop_warning ) {                          if ( length($msg) > $self->html_dump_width ) {
443                                  $msg = substr( $msg, 0, $self->chop_warning );                                  $msg = substr( $msg, 0, $self->html_dump_width );
444                                  $spacer = '&hellip;'                                  $spacer = '&hellip;'
445                          }                          }
446                          $msg =~ s{^\s}{&nbsp;}g;                          $msg =~ s{^\s}{&nbsp;}g;
# Line 450  sub warnings_html { Line 457  sub warnings_html {
457                  ;                  ;
458  }  }
459    
460    
461    =head2 backtrace
462    
463    Show backtrace with links to editor
464    
465      my @backtrace = $self->backtrace;
466    
467    =cut
468    
469    sub backtrace {
470            my ($self) = @_;
471    
472            my @backtrace;
473            foreach ( 0 .. 5 ) {
474                    my (
475                            $package,$path,$line
476                            # subroutine hasargs
477                            # wantarray evaltext is_require
478                            # hints bitmask hinthash
479                    ) = caller($_) or last;
480    
481                    push @backtrace,
482                            qq|via $package from $path <a target="editor" href="/editor+$path+$line">$path</a>|;
483            }
484            warn "# backtrace: ", dump( @backtrace ) if @backtrace;
485            return @backtrace;
486    }
487    
488  1;  1;

Legend:
Removed from v.540  
changed lines
  Added in v.564

  ViewVC Help
Powered by ViewVC 1.1.26