/[Frey]/trunk/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 /trunk/lib/Frey/Web.pm

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

revision 532 by dpavlin, Wed Nov 26 07:57:12 2008 UTC revision 568 by dpavlin, Thu Nov 27 22:09:59 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;
12    
13  use Frey::Bookmarklet;  use Frey::Bookmarklet;
14  use Frey::ClassBrowser;  use Frey::ClassBrowser;
15    use Frey::INC;
16    
17  use Frey::SVK;  use Frey::SVK;
18    
19  has 'head' => (  has 'head' => (
# Line 47  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    
 =head2 inline_smaller_than  
   
 Inline JavaScript and CSS smaller than this size into page reducing  
 round-trips to server.  
   
 =cut  
   
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',
60    );
61    
62    has 'html_dump_width' => (
63            documentation => 'crop longer lines in dumps',
64            is => 'rw',
65            isa => 'Int',
66    #       required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them
67            default => 128,
68  );  );
69    
70  sub dom2html {  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
71  #       warn "## dom2html ",dump( @_ );  my $escape_re  = join '|' => keys %escape;
72          return Continuity::Widget::DomNode->create( @_ )->to_string;  
73    sub html_escape {
74            my ( $self, $html ) = @_;
75            $html =~ s/($escape_re)/$escape{$1}/g;
76            return $html;
77    }
78    
79    sub html_dump {
80            my ( $self, $dump ) = @_;
81            $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 popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }
90    sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }
91    
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 111  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 155  sub page { Line 194  sub page {
194          my $status_line = '';          my $status_line = '';
195    
196          unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup };          unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup };
197          unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup };  #       unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup };
198            unshift @status, { 'INC' => Frey::INC->new->as_markup };
199    
200          foreach my $part ( @status ) {          foreach my $part ( @status ) {
201                  foreach my $name ( keys %$part ) {                  foreach my $name ( keys %$part ) {
202                          my $content = $part->{$name};                          $status_line .= $self->popup( $name, $part->{$name} );
                         if ( ref($content) ) {  
                                 $content = '<code>' . dump($content) . '</code>';  
                                 my $l = length($content);  
                                 $content = qq|<span>$l bytes</span>| if $l > $self->dump_max_bytes;  
                         } else {  
                                 $content = qq|<span>$content</span>|;  
                         }  
                         warn "### part [$name] = ", length( $content ), " bytes" if $self->debug;  
                         $status_line .= qq|<span class="frey-popup">$name $content</span>\n|;  
203                  }                  }
204          }          }
205    
# Line 299  sub status_parts { Line 330  sub status_parts {
330          warn "## status parts ", dump( map { keys %$_ } @status );          warn "## status parts ", dump( map { keys %$_ } @status );
331  }  }
332    
333    =for debug
334    
335  sub DEMOLISH {  sub DEMOLISH {
336          my ( $self ) = @_;          my ( $self ) = @_;
337          warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status;          warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status;
338  }  }
339    
340    =cut
341    
342  =head2 add_icon  =head2 add_icon
343    
344    Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png    Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png
# Line 321  sub icon_path { Line 356  sub icon_path {
356                  warn "# $class from $self icon_path $path";                  warn "# $class from $self icon_path $path";
357                  return $path;                  return $path;
358          } else {          } else {
359                  warn "TODO: add $path icon for $class";                  $self->TODO( "add $path icon for $class" );
360                  return undef;                  return undef;
361          }          }
362  }  }
# Line 357  my $warn_colors = { Line 392  my $warn_colors = {
392  };  };
393    
394  my $multiline_markers = {  my $multiline_markers = {
395          '(' => '\)',          '(' => ')',
396          '{' => '}',          '{' => '}',
397          '[' => '\]',          '[' => ']',
398          '"' => '"',          '"' => '"',
399  };  };
400    
# Line 372  sub log_path { Line 407  sub log_path {
407    
408  sub warnings_html {  sub warnings_html {
409          my ($self,$level) = shift;          my ($self,$level) = shift;
410          $level ||= $self->debug;          $level ||= $self->debug,
411          my $path = $self->log_path;          my $path = $self->log_path;
412    
413          my $warnings;          my $warnings;
# Line 384  sub warnings_html { Line 419  sub warnings_html {
419                  chomp;                  chomp;
420                  $line++;                  $line++;
421    
                 if ( $multiline_end ) {  
                         undef $multiline_end if m{^$multiline_end};  
                         next;  
                 }  
   
422                  my $style = '';                  my $style = '';
423    
424                  if ( m{^(#*)\s+} ) {                  if ( $multiline_end ) {
425                            if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) {
426    #                               warn "## $line end of $multiline_end in '$_'\n";
427                                    undef $multiline_end;
428                            } else {
429    #                               warn "## $line skipped\n";
430                            }
431                    } elsif ( m{^(#*)\s+} ) {
432                          my $l = $1 ? length($1) : 0;                          my $l = $1 ? length($1) : 0;
433                          $multiline_end = $multiline_markers->{$1} if m{($multiline_re)$};                          if ( $l > $level ) {
434                          next if $l > $level;                                  undef $multiline_end;
435                          warn "## multiline_end: $multiline_end $l > $level for '$_'" if $multiline_end;                                  $multiline_end = $multiline_markers->{$1} if m{($multiline_re)$};
436                          undef $multiline_end;  #                               warn "## $line start $1 .. $multiline_end level $l > $level for '$_'\n" if $multiline_end;
437                                    next;
438                            }
439    
440                          $style = $warn_colors->{$1}                          $style = $warn_colors->{$1}
441                                  ? ' style="color:' . $warn_colors->{$1} . '"'                                  ? ' style="color:' . $warn_colors->{$1} . '"'
442                                  : '';                                  : '';
443    
444                          $warnings .= qq|<tt$style>$_</tt> <small> <a target="editor" href="/editor+$path+$line">+$line</a> </small> <br/>|;                          my $msg = $self->html_escape( $_ );
445                            my $spacer = ' ';
446                            if ( length($msg) > $self->html_dump_width ) {
447                                    $msg = substr( $msg, 0, $self->html_dump_width );
448                                    $spacer = '&hellip;'
449                            }
450                            $msg =~ s{^\s}{&nbsp;}g;
451                            $warnings .= qq|<tt$style>$msg</tt>$spacer<small><a target="editor" href="/editor+$path+$line">+$line</a></small><br/>|;
452                          # FIXME <tt> should be <code> but CSS hates me                          # FIXME <tt> should be <code> but CSS hates me
453                  }                  }
454          }          }
# Line 415  sub warnings_html { Line 461  sub warnings_html {
461                  ;                  ;
462  }  }
463    
464    
465    =head2 backtrace
466    
467    Show backtrace with links to editor
468    
469      my @backtrace = $self->backtrace;
470    
471    =cut
472    
473    sub backtrace {
474            my ($self) = @_;
475    
476            my @backtrace;
477            foreach ( 0 .. 5 ) {
478                    my (
479                            $package,$path,$line
480                            # subroutine hasargs
481                            # wantarray evaltext is_require
482                            # hints bitmask hinthash
483                    ) = caller($_) or last;
484    
485                    push @backtrace,
486                            qq|via $package from $path <a target="editor" href="/editor+$path+$line">$path</a>|;
487            }
488            warn "# backtrace: ", dump( @backtrace ) if @backtrace;
489            return @backtrace;
490    }
491    
492  1;  1;

Legend:
Removed from v.532  
changed lines
  Added in v.568

  ViewVC Help
Powered by ViewVC 1.1.26