/[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 588 by dpavlin, Fri Nov 28 15:07:03 2008 UTC revision 857 by dpavlin, Tue Dec 16 14:10:09 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/;  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;
# Line 22  sub head { @head } Line 25  sub head { @head }
25  has 'request_url' => (  has 'request_url' => (
26          is => 'rw',          is => 'rw',
27          isa => 'Uri', coerce => 1,          isa => 'Uri', coerce => 1,
28          default => '/',          required => 1,
29            default => sub {
30                    carp "undefined request_url";
31                    '/';
32            },
33  );  );
34    
35  has 'title' => (  has 'title' => (
# Line 61  has 'html_dump_width' => ( Line 68  has 'html_dump_width' => (
68          is => 'rw',          is => 'rw',
69          isa => 'Int',          isa => 'Int',
70  #       required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them  #       required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them
71          default => 128,          default => 250,
72  );  );
73    
74  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
# Line 80  sub html_dump { Line 87  sub html_dump {
87          $dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs;          $dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs;
88          $dump = $self->html_escape( $dump );          $dump = $self->html_escape( $dump );
89          $dump =~ s{\Q...\E}{&hellip;}gs;          $dump =~ s{\Q...\E}{&hellip;}gs;
90  #       $dump =~ $self->editor_links( $dump ); # FIXME include this  #       $dump =~ $self->html_links( $dump ); # FIXME include this
91          return "<code>$dump</code>";          return "<code>$dump</code>";
92  }  }
93    
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+).+?/\1|[^>]+/?)>}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 98  sub popup_dropdown { Line 105  sub popup_dropdown {
105    
106          $content =~ s{<span>(<code>[^<]+</code>)</span>}{$1} && $self->TODO("code wrapped in span");          $content =~ s{<span>(<code>[^<]+</code>)</span>}{$1} && $self->TODO("code wrapped in span");
107    
108          warn "## $type [$name] = ", length( $content ), " bytes" if $self->debug;          warn "## $type [$name] = ", length( $content ), " bytes"; # if $self->debug; # FIXME
109    
110          if ( $name =~ m{::} && $name !~ $re_html ) {          if ( $name =~ m{::} && $name !~ $re_html ) {
111                  return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|;                  return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|;
# Line 109  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 119  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|<!-- $path --><script type="text/javascript">\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|<!-- $path --><style type="text/css">\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 167  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 ( $what eq 'js' ) {
190                            $head = qq|
191                                    <$tag type="$type" src="$content">
192                                    /* via $package at $path line $line */
193                                    </$tag>
194                            |;
195                    } else {
196                            $head = qq|
197                                    <link rel="stylesheet" type="$type" href="$content">
198                                    <!-- via $package at $path line $line -->
199                            |;
200                    }
201            } else {
202                    $head = qq|
203                            <$tag type="$type">
204                            /* via $package at $path line $line */
205                            $content
206                            </$tag>
207                    |;
208            };
209            $self->add_head( $head );
210    }
211    
212  sub add_css {  sub add_css {
213          my ($self,$css) = @_;          my ($self,$css) = @_;
214          my ( $package, $path, $line ) = caller;          $self->_add_css_js( 'css', $css );
215          $self->add_head( qq|  }
216          <style type="text/css">  
217          /* via $package at $path line $line */  sub add_js {
218          $css          my ($self,$js) = @_;
219          </style>          $self->_add_css_js( 'js', $js );
         | );  
220  }  }
221    
222  our $reload_counter = 0;  our $reload_counter = 0;
# Line 216  sub page { Line 257  sub page {
257          $url =~ s{\?reload=\d+}{};          $url =~ s{\?reload=\d+}{};
258    
259          my $body = $a->{body};          my $body = $a->{body};
260          $body ||= $self->as_markup if $self->can('as_markup');          if ( ! $body ) {
261                    my $run = $a->{run} || 'as_markup';
262                    warn "# no body, invoke $self->$run on ", ref($self);
263                    $body = $self->$run;
264            }
265          if ( $self->content_type !~ m{html} ) {          if ( $self->content_type !~ m{html} ) {
266                  warn "# return only $self body ", $self->content_type;                  warn "# return only $self body ", $self->content_type;
267                  return $body                  return $body
# Line 235  sub page { Line 280  sub page {
280                  qq|                  qq|
281                          <span class="right">                          <span class="right">
282                          <a title="reload $url"  href="/reload$url">reload</a>                          <a title="reload $url"  href="/reload$url">reload</a>
283                          <a title="$description" href="/exit$url">$exit</a>                          <a title="$description" href="/exit$url" target="exit">$exit</a>
284                          </span>                          </span>
285                  |;                  |;
286    
# Line 291  sub editor { Line 336  sub editor {
336          qq|>$class</a>|;          qq|>$class</a>|;
337  }  }
338    
339  =head2 editor_links  =head2 html_links
340    
341  Create HTML links to editor for perl error message  Create HTML links to editor for perl error message
342    
343    my $html = $self->editor_links( $error )    my $html = $self->html_links( $error )
344    
345  =cut  =cut
346    
347  sub editor_links {  sub html_links {
348          my ( $self, $error ) = @_;          my ( $self, $error ) = @_;
349    
350            $error = $self->strip_full_path( $error );
351    
352    #       $error =~ s[(bless\({\s+.+?\s+},\s+)("[^"]+")(\) at)][<span class="frey-dropdown">$1<code>$2</code>$3</span>]gs; # FIXME insert bless hiding back
353    
354            # perl's backtrace
355          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
356                  {at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;                  {at <a target="editor" href="/editor+$1+$2" title="vi $1 +$2">$1</a> line $2}gsm;
357    
358            $error =~ s{(via (?:package)\s+"?)([\w:]+)("?)}
359                    {$1<a target="$2" href="/$2" title="introspect $2">$2</a>$3}gsm
360            || # or anything that looks like "Class::Name"
361            $error =~ s{"(\w+(?:::\w+)+)"}
362                    {"<a target="$1" href="/$1" title="introspect $1">$1</a>"}gsm;
363    
364            # method error messages
365            # FIXME replace with link to Frey::Introspect data
366            $error =~ s{(method ")(\w+)(" via)}
367                    {$1<a target="$2" href="/Frey::Shell::Grep/as_markup?pattern=$2" title="grep $2">$2</a>$3}gsm;
368    
369            # link paths to editor
370            $error =~ s{((?:lib|t)/[\S]+)\s+(\d+\s+bytes)}
371                    {<a target="editor" href="/editor+$1+1" title="vi $1 [$2]">$1</a>}gsm;
372    
373          $error =~ s{(via (?:package) "?)([\w:]+)("?)}          $error =~ s{(class ")([\w:]+)(")}
374                  {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;                  {$1<a target="$2" href="/$2" title="introspect $2">$2</a>$3}gsm;
375    
376          return $error;          return $error;
377  }  }
378    
379    sub html_self {
380            my $self = shift;
381            my $html = $self;
382            $html =~ s{([\w:]+)=}{<a target="$1" href="/$1" title="introspect $1">$1</a>=}gsm;
383            return $html;
384    }
385    
386    =head2 error
387    
388    This method will return error to browser and backtrace unless
389    error message ends with LF C<\n> just like L<warn>
390    
391    =cut
392    
393  sub error {  sub error {
394          my $self = shift;          my $self = shift;
395          my $error = join(" ", @_);          my $error = join(" ", @_);
396    
397          my @backtrace = $self->backtrace;          my $fatal = '';
398          $error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace;          my $backtrace = '';
399    
400            if ( $error !~ m{\n$} ) {
401                    if ( my @backtrace = $self->backtrace ) {
402                            $backtrace =
403                                      "\n" . $self->html_self . "->error backtrace\n\t"
404                                    . $self->html_links( join( "\n\t", @backtrace ) )
405                                    ;
406                            $fatal = qq| frey-fatal|;
407                    }
408            }
409    
410          warn "ERROR: $error\n";          warn "ERROR: $error\n";
411          return          $self->add_icon('error');
412                  qq|<pre class="frey-error">|          $error = $self->html_links( $error );
413                  . $self->editor_links( $error ) .          return qq|<pre class="frey-error$fatal">$error $backtrace</pre>| ;
                 qq|</pre>|  
                 ;  
414  }  }
415    
416  =head1 Status line  =head1 Status line
# Line 362  sub clean_status { Line 449  sub clean_status {
449          my ($self) = shift;          my ($self) = shift;
450          @head = ( 'static/frey.css' );          @head = ( 'static/frey.css' );
451          @status = (          @status = (
452                  { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup },                  { 'ClassBrowser' => Frey::Class::Browser->new( usage_sort => 1, usage_on_top => 0 )->as_markup },
453                  { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },                  { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },
454                  { 'INC' => Frey::INC->new->as_markup },                  { 'INC' => Frey::INC->new->as_markup },
455          );          );
456          $icon_html = '';          $icon_html = '';
457  }  }
# Line 399  sub DEMOLISH { Line 486  sub DEMOLISH {
486    
487  sub icon_path {  sub icon_path {
488          my ($self,$class,$variant) = @_;          my ($self,$class,$variant) = @_;
489          my $icon = $class;  
490          $icon =~ s{::}{/}g;          sub icon_exists {
491          $icon .= "/$variant" if $variant;                  my $class = shift;
492          my $path = 'static/icons/' . $icon . '.png';                  $class =~ s{::}{/}g;
493          if ( -e $path ) {                  $class .= "/$variant" if $variant;
494                  warn "# $class from $self icon_path $path" if $self->debug;                  my $icon_path = 'static/icons/' . $class . '.png';
495                  return $path;                  return $icon_path if -e $icon_path;
496          } else {                  return;
497                  $self->TODO( "add $path icon for $class" );          }
498    
499            my $path = icon_exists( $class );
500            if ( ! $path ) {
501                    my $super_class = $class;
502                    while ( $super_class =~ s{::[^:]+$}{} && ! $path ) {
503                            $path = icon_exists( $super_class ) unless $super_class eq 'Frey'; # don't default on Frey icon
504                    }
505            }
506    
507            if ( ! $path ) {
508                    $self->TODO( "add icon for $class" . ( $variant ? " variant $variant" : '' ) );
509                  return undef;                  return undef;
510          }          }
511    
512            warn "# $class from $self icon_path $path" if $self->debug;
513            return $path;
514  }  }
515    
516  sub add_icon {  sub add_icon {
517          my ($self,$variant) = @_;          my ($self,$variant) = @_;
518    
519          my $class = ref($self);          my $class = $self->class if $self->can('class');
520          $class = $self->class if $self->can('class');          #$class ||= $self->title;
521            $class ||= ref($self);
522          my $icon_path = $self->icon_path( $class, $variant ) || return;          my $icon_path = $self->icon_path( $class, $variant ) || return;
523    
524          $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 449  my $multiline_markers = { Line 551  my $multiline_markers = {
551          '"' => '"',          '"' => '"',
552  };  };
553    
554    =for later
555    
556  my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']';  my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']';
557  warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re";  warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re";
558    
559    =cut
560    
561  sub log_path {  sub log_path {
562          $Frey::Bootstrap::log_path || warn "no log_path?";          $Frey::Bootstrap::log_path || die "no log_path?";
563  }  }
564    
565    our $pwd = `pwd`;
566    chomp $pwd;
567    
568    sub strip_full_path {
569            my ($self, $msg) = @_;
570            # Mojo seems to expand warn messages to full path which is annoying
571            $msg =~ s{/[^/]+/\.\./}{/}gs;
572            $msg =~ s{$pwd/*}{}gs;
573            return $msg;
574    }
575    
576    our $last_log_pos  = 0;
577    our $last_log_line = 0;
578    
579  sub warnings_html {  sub warnings_html {
580          my ($self,$level) = shift;          my ($self,$level) = shift;
581          $level ||= $self->debug,          $level ||= $self->debug,
582          my $path = $self->log_path;          my $path = $self->log_path;
583    
584          my $max = 50;          my $max = 30;
585          my $pos = 0;          my $pos = 0;
586          my @warnings = ( '' x $max ); # XXX circualar buffer for 50 lines          my @warnings = ( '' x $max ); # XXX circualar buffer for 50 lines
587          my $line = 0;          my $line = $last_log_line;
588          my $multiline_end;          my $multiline_end;
589    
590          open(my $log, '<', $path) || die "can't open $path: $!";          # XXX do we really want to do this every time?
591            my $css = qq|/* short css classes for levels */\n|;
592            my $level_to_class;
593            foreach ( keys %$warn_colors ) {
594                    my $l = length($_);
595                    my $class = 'l' . $l;
596                    $css .= qq|.$class { color: $warn_colors->{$_} }\n|;
597                    $level_to_class->{ $_ } = $class;
598            }
599            $self->add_css( $css );
600    
601            open(my $log, '<', $path)    || die "can't open $path: $!";
602            seek($log, $last_log_pos, 0) || warn "can't seek: $!";
603          while(<$log>) {          while(<$log>) {
604                  chomp;                  chomp;
605                  $line++;                  $line++;
606    
607                    next if m{^\s+(Mojo|Class::MOP|Moose)::};
608    
609                  my $style = '';                  my $style = '';
610    
611  =for filter  =for filter
# Line 493  sub warnings_html { Line 627  sub warnings_html {
627                          }                          }
628    
629  =cut  =cut
630                  if ( m{^(#*)\s+} ) { # FIXME                  if ( m{^(#*)} ) {
631    
632                          $style = $warn_colors->{$1}                          my $level = $1;
633                                  ? ' style="color:' . $warn_colors->{$1} . '"'                          my $msg = $self->strip_full_path( $_ );
                                 : '';  
634    
                         my $msg = $self->html_escape( $_ );  
635                          my $spacer = ' ';                          my $spacer = ' ';
636                          if ( length($msg) > $self->html_dump_width ) {                          my $real_msg = expand( $msg );
637                                  $msg = substr( $msg, 0, $self->html_dump_width );                          if ( length($real_msg) > $self->html_dump_width ) {
638                                    
639                                    $real_msg = substr( $msg, 0, $self->html_dump_width );
640                                    $msg = unexpand( $real_msg );
641                                  $spacer = '&hellip;'                                  $spacer = '&hellip;'
642                          }                          }
643                          $warnings[ $pos++ % $max ]  
644                                  = $msg                          $msg = $self->html_escape( $msg );
645  #                               = ( $style ? qq|<span$style>$msg</span>| : $msg )  
646                                  . $spacer                          if ( my $class = $level_to_class->{ $level } ) {
647                                  . qq|<a target="editor" href="/editor+$path+$line" style="float: right;">+$line</a><br/>|;                                  $msg = qq|<span class="$class">$msg</span>|;
648                          # FIXME <tt> should be <code> but CSS hates me                          }
649    
650                            #$msg .= $spacer . qq|<a target="editor" href="/editor+$path+$line" style="float: right;">$line</a>\n|;
651                            $msg = qq|<a target="editor" href="/editor+$path+$line" style="float: right;">$line</a>$msg|
652                                    . ( $spacer ? $spacer : '' )
653                                    . "\n"; # XXX <pre> needs this
654    
655                            $warnings[ $pos++ % $max ] = $msg;
656                  }                  }
657          }          }
658            $last_log_pos = tell($log);
659            $last_log_line = $line;
660            warn "log has $line lines tell position $last_log_pos";
661          close($log) || die "can't close $path: $!";          close($log) || die "can't close $path: $!";
662    
663          my $size = -s $path;          my $size = -s $path;
664    
665          my $warnings = join('',          my $warnings = join("",
666                  map { $warnings[ ( $pos + $_ ) % $max ] || '' } 0 .. $max                  map { $warnings[ ( $pos + $_ ) % $max ] || '' } 0 .. ( $max - 1 )
667          );          );
668    
669          my $s = length($warnings);          my $s = length($warnings);
670    
671          return          return
672                  # need to wrap into span so we can have links in warnings                  # need to wrap editor link into span so we can have links in warnings
673                    qq|<span class="frey-popup"><a target="editor" href="/editor+$path+$line" title="$path \| $size -> $s bytes \| $line -> $pos lines \| level $level">warn</a><code>|                    qq|<span class="frey-popup"><a target="editor" href="/editor+$path+$line" title="$path \| $size -> $s bytes \| $line -> $pos lines \| level $level">warn</a><code>|
674                  . $warnings                  . $self->html_links( $warnings )
675  #               . $self->editor_links( $warnings )                  . qq|</code></span>|
                 . qq|</code></span></a>|  
676                  ;                  ;
677  }  }
678    
# Line 545  sub backtrace { Line 689  sub backtrace {
689          my ($self) = @_;          my ($self) = @_;
690    
691          my @backtrace;          my @backtrace;
692          foreach ( 0 .. 5 ) {          foreach ( 1 .. 5 ) { # 0 = backtrace
693                  my (                  my (
694                          $package,$path,$line                          $package,$path,$line
695                          # subroutine hasargs                          # subroutine hasargs
# Line 554  sub backtrace { Line 698  sub backtrace {
698                  ) = caller($_) or last;                  ) = caller($_) or last;
699    
700                  push @backtrace,                  push @backtrace,
701                          qq|via $package at $path line $line|;                          qq|via "$package" at $path line $line|;
702          }          }
703          warn "# backtrace: ", dump( @backtrace ) if @backtrace;          #warn "# backtrace: ", dump( @backtrace ) if @backtrace;
704          return @backtrace;          return @backtrace;
705  }  }
706    
707    =head2 checkbox
708    
709    Generate checkbox html markup from some attribute
710    
711      my $html = $self->checkbox('attribute_name', $value);
712    
713    =cut
714    
715    sub checkbox {
716            my ($self,$name,$value) = @_;
717            my $checked = '';
718            my $all_checkboxes = eval { $self->$name };
719            warn "ERROR tried to get checkbox value for '$name' which is unknown: $@" if $@;
720            $all_checkboxes = [ $all_checkboxes ] unless ref($all_checkboxes) eq 'ARRAY'; # sigh, too chatty
721            $checked = ' checked' if grep { defined $_ && $_ eq $value } @$all_checkboxes;
722            warn "# checkbox $name $value $checked\t", $self->dump( $self->$name );
723            qq|<input name="$name" value="$value" type="checkbox"$checked>|;
724    }
725    
726    =head2 strip
727    
728    Strip whitespace around content
729    
730      my $stripped = strip('  no more whitespace around this   ');
731    
732    =cut
733    
734    sub strip {
735            my $t = shift;
736            $t =~ s{^\s+}{}gs;
737            $t =~ s{>\s+<}{><}gs;
738            $t =~ s{\s+$}{}gs;
739            return $t;
740    }
741    
742  1;  1;

Legend:
Removed from v.588  
changed lines
  Added in v.857

  ViewVC Help
Powered by ViewVC 1.1.26