/[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 674 by dpavlin, Tue Dec 2 01:49:49 2008 UTC revision 957 by dpavlin, Wed Jan 7 17:50:03 2009 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 70  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    
# Line 79  sub html_escape { Line 87  sub html_escape {
87          return $html;          return $html;
88  }  }
89    
90    # from Mojo::ByteStream
91    sub url_escape {
92            my ( $self, $url, $pattern ) = @_;
93            $pattern ||= 'A-Za-z0-9\-\.\_\~';
94            $url =~ s/([^$pattern])/sprintf('%%%02X',ord($1))/ge;
95            return $url;
96    }
97    
98  sub html_dump {  sub html_dump {
99          my ( $self, $dump ) = @_;          my ( $self, $dump ) = @_;
100          $dump = dump( $dump ) if ref($dump);          $dump = dump( $dump ) if ref($dump);
# Line 86  sub html_dump { Line 102  sub html_dump {
102          $dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs;          $dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs;
103          $dump = $self->html_escape( $dump );          $dump = $self->html_escape( $dump );
104          $dump =~ s{\Q...\E}{&hellip;}gs;          $dump =~ s{\Q...\E}{&hellip;}gs;
105  #       $dump =~ $self->editor_links( $dump ); # FIXME include this  #       $dump =~ $self->html_links( $dump ); # FIXME include this
106          return "<code>$dump</code>";          return "<code>$dump</code>";
107  }  }
108    
109  sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }  sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }
110  sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }  sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }
111    
112  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
113    
114  sub popup_dropdown {  sub popup_dropdown {
115          my ( $self, $type, $name, $content, $full ) = @_;          my ( $self, $type, $name, $content, $full ) = @_;
# Line 115  sub popup_dropdown { Line 131  sub popup_dropdown {
131          }          }
132  }  }
133    
134  sub _inline_path {  sub _inline {
135          my ( $self, $path ) = @_;          my ( $self, $path ) = @_;
136          -s $path < $self->inline_smaller_than;          return unless defined $path;
137            warn "# _inline $path";
138            -e $path && -s $path < $self->inline_smaller_than && -s $path;
139  }  }
140    
141  sub _head_html {  sub _head_html {
# Line 126  sub _head_html { Line 144  sub _head_html {
144          foreach my $path ( @head ) {          foreach my $path ( @head ) {
145                  $path =~ s!^/!!;                  $path =~ s!^/!!;
146                  if ( $path =~ m/\.js$/ ) {                  if ( $path =~ m/\.js$/ ) {
147                          $out .= $self->_inline_path( $path ) ?                          my $size;
148                                  qq|<script type="text/javascript">\n/* inline $path */\n\n| . read_file($path) . qq|\n</script>| :                          $out .= $size = _inline( $path ) ?
149                                    qq|<script type="text/javascript">\n/* inline $path $size bytes */\n\n| . read_file($path) . qq|\n</script>| :
150                                  qq|<script type="text/javascript" src="/$path"></script>|;                                  qq|<script type="text/javascript" src="/$path"></script>|;
151                  } elsif ( $path =~ m/\.css$/ ) {                  } elsif ( $path =~ m/\.css$/ ) {
152                          $out .= $self->_inline_path( $path ) ?                          my $size;
153                                  qq|<style type="text/css">\n/* inline $path */\n\n| . read_file( $path ) . qq|\n</style>| :                          $out .= $size = _inline( $path ) ?
154                                    qq|<style type="text/css">\n/* inline $path $size bytes */\n\n| . read_file( $path ) . qq|\n</style>| :
155                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
156                  } elsif ( $path =~ m{<.+>}s ) {                  } elsif ( $path =~ m{<.+>}s ) {
157                          $out .= $path;                          $out .= $path;
# Line 173  sub add_head { Line 193  sub add_head {
193    
194  }  }
195    
196    sub _add_css_js {
197            my ( $self, $what, $content ) = @_;
198    
199            my $tag  = $what eq 'css' ? 'style'    : 'script';
200            my $type = $what eq 'css' ? 'text/css' : 'text/javascript';
201            my $head;
202    
203            my ( $package, $path, $line ) = caller(1);
204    
205            $content = "/$content" if $content !~ m{[\n\r]} && -e $content;
206            if ( $content =~ $re_html ) {
207                    $head = qq|
208                            $content
209                            <!-- $type via $package at $path line $line -->
210                    |;
211            } elsif ( $content =~ m{^(/\w+|https?://)} && $content !~ m{[\n\r]} ) {
212                    if ( $what eq 'js' ) {
213                            $head = qq|
214                                    <$tag type="$type" src="$content">
215                                    /* $what via $package at $path line $line */
216                                    </$tag>
217                            |;
218                    } else {
219                            $head = qq|
220                                    <link rel="stylesheet" type="$type" href="$content">
221                                    <!-- $what via $package at $path line $line -->
222                            |;
223                    }
224            } else {
225                    $head = qq|
226                            <$tag type="$type">
227                            /* via $package at $path line $line */
228                            $content
229                            </$tag>
230                    |;
231            };
232            $self->add_head( $head );
233    }
234    
235  sub add_css {  sub add_css {
236          my ($self,$css) = @_;          my ($self,$css) = @_;
237          my ( $package, $path, $line ) = caller;          $self->_add_css_js( 'css', $css );
238          $self->add_head( qq|  }
239          <style type="text/css">  
240          /* via $package at $path line $line */  sub add_js {
241          $css          my ($self,$js) = @_;
242          </style>          $self->_add_css_js( 'js', $js );
         | );  
243  }  }
244    
245  our $reload_counter = 0;  our $reload_counter = 0;
# Line 206  sub page { Line 264  sub page {
264          my $self = shift;          my $self = shift;
265          my $a = {@_};          my $a = {@_};
266    
         warn "## page ",dump($a);  
   
267          $reload_counter++;          $reload_counter++;
268    
269          my $status_line = '';          my $status_line = '';
# Line 225  sub page { Line 281  sub page {
281          if ( ! $body ) {          if ( ! $body ) {
282                  my $run = $a->{run} || 'as_markup';                  my $run = $a->{run} || 'as_markup';
283                  warn "# no body, invoke $self->$run on ", ref($self);                  warn "# no body, invoke $self->$run on ", ref($self);
284                  eval {                  $body = $self->$run;
                         $body = $self->$run;  
                 };  
                 $body = $self->error( $@, '' ) if $@;  
285          }          }
286          if ( $self->content_type !~ m{html} ) {          if ( $self->content_type !~ m{html} || ! $self->wrap_in_page ) {
287                  warn "# return only $self body ", $self->content_type;                  warn "# return only $self body ", $self->content_type;
288                  return $body                  return $body
289          } elsif ( ! defined $body ) {          } elsif ( ! defined $body ) {
# Line 259  sub page { Line 312  sub page {
312    
313          $self->add_icon unless $icon_html;          $self->add_icon unless $icon_html;
314    
315            my $title = undef
316                    || $a->{title}
317                    || $self->title
318                    || ref($self)
319                    ;
320    
321    #       $title =~ s{(\w)\w+::}{$1:}g; # XXX compress names of classes
322    
323          my $html = join("\n",          my $html = join("\n",
324                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
325                  $self->_head_html,                  $self->_head_html,
326                  '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',                  qq|<title>$title</title>|,
327                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
328                  ( $icon_html || '<!-- no icon -->' ),                  ( $icon_html || '<!-- no icon -->' ),
329                  ( $a->{head} || '' ),                  ( $a->{head} || '' ),
# Line 304  sub editor { Line 365  sub editor {
365          qq|>$class</a>|;          qq|>$class</a>|;
366  }  }
367    
368  =head2 editor_links  =head2 html_links
369    
370  Create HTML links to editor for perl error message  Create HTML links to editor for perl error message
371    
372    my $html = $self->editor_links( $error )    my $html = $self->html_links( $error )
373    
374  =cut  =cut
375    
376  sub editor_links {  sub html_links {
377          my ( $self, $error ) = @_;          my ( $self, $error ) = @_;
378    
379            $error = $self->strip_full_path( $error );
380    
381  #       $error =~ s[(bless\({\s+.+?\s+},\s+)("[^"]+")(\) at)][<span class="frey-dropdown">$1<code>$2</code>$3</span>]gs; # FIXME insert bless hiding back  #       $error =~ s[(bless\({\s+.+?\s+},\s+)("[^"]+")(\) at)][<span class="frey-dropdown">$1<code>$2</code>$3</span>]gs; # FIXME insert bless hiding back
382    
383          # perl's backtrace          # perl's backtrace
384          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
385                  {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;
386    
387          $error =~ s{(via (?:package)\s+"?)([\w:]+)("?)}          $error =~ s{(via (?:package)\s+"?)([\w:]+)("?)}
388                  {$1<a target="$2" href="/$2">$2</a>$3}gsm;                  {$1<a target="$2" href="/$2" title="introspect $2">$2</a>$3}gsm
389            || # or anything that looks like "Class::Name"
390            $error =~ s{"(\w+(?:::\w+)+)"}
391                    {"<a target="$1" href="/$1" title="introspect $1">$1</a>"}gsm;
392    
393          # method error messages          # method error messages
394  #       $error =~ s{(method ")(\w+)"}          # FIXME replace with link to Frey::Introspect data
395  #               {$1<a target="/Frey::Shell::Grep?pattern=$2">$2</a>"}gsm; # FIXME replace with link to Frey::Introspect data          $error =~ s{(method ")(\w+)(" via)}
396                    {$1<a target="$2" href="/Frey::Shell::Grep/as_markup?pattern=$2" title="grep $2">$2</a>$3}gsm;
397    
398            # link paths to editor
399            $error =~ s{((?:lib|t)/[\S]+)\s+(\d+\s+bytes)}
400                    {<a target="editor" href="/editor+$1+1" title="vi $1 [$2]">$1</a>}gsm;
401    
402          # anything that looks like "Class::Name"          $error =~ s{(class ")([\w:]+)(")}
403          $error =~ s{"(\w+(?:::\w+)+)"}                  {$1<a target="$2" href="/$2" title="introspect $2">$2</a>$3}gsm;
                 {"<a target="$1" href="/$1">$1</a>"}gsm;  
404    
405          return $error;          return $error;
406  }  }
407    
408    sub html_self {
409            my $self = shift;
410            my $html = $self;
411            $html =~ s{([\w:]+)=}{<a target="$1" href="/$1" title="introspect $1">$1</a>=}gsm;
412            return $html;
413    }
414    
415  =head2 error  =head2 error
416    
417  This method will return error to browser and backtrace unless  This method will return error to browser and backtrace unless
# Line 347  sub error { Line 424  sub error {
424          my $error = join(" ", @_);          my $error = join(" ", @_);
425    
426          my $fatal = '';          my $fatal = '';
427            my $backtrace = '';
428    
429          if ( $error !~ m{\n$} ) {          if ( $error !~ m{\n$} ) {
430                  if ( my @backtrace = $self->backtrace ) {                  if ( my @backtrace = $self->backtrace ) {
431                          $error .= "\n\t" . join( "\n\t", @backtrace );                          $backtrace =
432                          $fatal = qq| class="fatal"|;                                    "\n" . $self->html_self . "->error backtrace\n\t"
433                                    . $self->html_links( join( "\n\t", @backtrace ) )
434                                    ;
435                            $fatal = qq| frey-fatal|;
436                  }                  }
437          }          }
438    
439          warn "ERROR: $error\n";          warn "ERROR: $error\n";
440          return          $self->add_icon('error');
441                  qq|<pre class="frey-error$fatal">|          $error = $self->html_links( $error );
442                  . $self->editor_links( $error ) .          return qq|<pre class="frey-error$fatal">$error $backtrace</pre>| ;
                 qq|</pre>|  
                 ;  
443  }  }
444    
445  =head1 Status line  =head1 Status line
# Line 375  sub error { Line 454  sub error {
454    
455  sub add_status {  sub add_status {
456          my ( $self, $data ) = @_;          my ( $self, $data ) = @_;
457          push @status, { 'X' => [ $self->backtrace ] };          die "no data" unless $data;
458          if ( ref($data) ) {          if ( ref $data  ) {
459                  push @status, $data;                  push @status, $data;
460          } else {          } else {
461                  if ( defined $status[ $#status ] ) {                  if ( defined $status[ $#status ] ) {
# Line 399  sub clean_status { Line 478  sub clean_status {
478          my ($self) = shift;          my ($self) = shift;
479          @head = ( 'static/frey.css' );          @head = ( 'static/frey.css' );
480          @status = (          @status = (
481                  { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup },                  { 'ClassBrowser' => Frey::Class::Browser->new( usage_sort => 1, usage_on_top => 0 )->as_markup },
482                  { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },                  { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },
483                  { 'INC' => Frey::INC->new->as_markup },                  { 'INC' => Frey::INC->new->as_markup },
484          );          );
# Line 436  sub DEMOLISH { Line 515  sub DEMOLISH {
515    
516  sub icon_path {  sub icon_path {
517          my ($self,$class,$variant) = @_;          my ($self,$class,$variant) = @_;
518          my $icon = $class;  
519          $icon =~ s{::}{/}g;          sub icon_exists {
520          $icon .= "/$variant" if $variant;                  my $class = shift;
521          my $path = 'static/icons/' . $icon . '.png';                  $class =~ s{::}{/}g;
522          if ( -e $path ) {                  $class .= "/$variant" if $variant;
523                  warn "# $class from $self icon_path $path" if $self->debug;                  my $icon_path = 'static/icons/' . $class . '.png';
524                  return $path;                  return $icon_path if -e $icon_path;
525          } else {                  return;
526                  $self->TODO( "add $path icon for $class" );          }
527    
528            my $path = icon_exists( $class );
529            if ( ! $path ) {
530                    my $super_class = $class;
531                    while ( $super_class =~ s{::[^:]+$}{} && ! $path ) {
532                            $path = icon_exists( $super_class ) unless $super_class eq 'Frey'; # don't default on Frey icon
533                    }
534            }
535    
536            if ( ! $path ) {
537                    $self->TODO( "add icon for $class" . ( $variant ? " variant $variant" : '' ) );
538                  return undef;                  return undef;
539          }          }
540    
541            warn "# $class from $self icon_path $path" if $self->debug;
542            return $path;
543  }  }
544    
545  sub add_icon {  sub add_icon {
546          my ($self,$variant) = @_;          my ($self,$variant) = @_;
547    
548          my $class = ref($self);          my $class = $self->class if $self->can('class');
549          $class = $self->class if $self->can('class');          #$class ||= $self->title;
550            $class ||= ref($self);
551          my $icon_path = $self->icon_path( $class, $variant ) || return;          my $icon_path = $self->icon_path( $class, $variant ) || return;
552    
553          $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 497  sub log_path { Line 591  sub log_path {
591          $Frey::Bootstrap::log_path || die "no log_path?";          $Frey::Bootstrap::log_path || die "no log_path?";
592  }  }
593    
 our $last_log_pos  = 0;  
 our $last_log_line = 0;  
   
594  our $pwd = `pwd`;  our $pwd = `pwd`;
595  chomp $pwd;  chomp $pwd;
596    
597    sub strip_full_path {
598            my ($self, $msg) = @_;
599            # Mojo seems to expand warn messages to full path which is annoying
600            $msg =~ s{/[^/]+/\.\./}{/}gs;
601            $msg =~ s{$pwd/*}{}gs;
602            return $msg;
603    }
604    
605    our $last_log_pos  = 0;
606    our $last_log_line = 0;
607    
608  sub warnings_html {  sub warnings_html {
609          my ($self,$level) = shift;          my ($self,$level) = shift;
610          $level ||= $self->debug,          $level ||= $self->debug,
# Line 557  sub warnings_html { Line 659  sub warnings_html {
659                  if ( m{^(#*)} ) {                  if ( m{^(#*)} ) {
660    
661                          my $level = $1;                          my $level = $1;
662                          my $msg = $_;                          my $msg = $self->strip_full_path( $_ );
   
                         # Mojo seems to expand warn messages to full path which is annoying  
                         $msg =~ s{/[^/]+/\.\./}{/}gs;  
                         $msg =~ s{$pwd/*}{}gs;  
663    
664                          my $spacer = ' ';                          my $spacer = ' ';
665                          my $real_msg = expand( $msg );                          my $real_msg = expand( $msg );
# Line 602  sub warnings_html { Line 700  sub warnings_html {
700          return          return
701                  # need to wrap editor link into span so we can have links in warnings                  # need to wrap editor link into span so we can have links in warnings
702                    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>|
703                  . $self->editor_links( $warnings )                  . $self->html_links( $warnings )
704                  . qq|</code></span>|                  . qq|</code></span>|
705                  ;                  ;
706  }  }
# Line 620  sub backtrace { Line 718  sub backtrace {
718          my ($self) = @_;          my ($self) = @_;
719    
720          my @backtrace;          my @backtrace;
721          foreach ( 0 .. 5 ) {          foreach ( 1 .. 5 ) { # 0 = backtrace
722                  my (                  my (
723                          $package,$path,$line                          $package,$path,$line
724                          # subroutine hasargs                          # subroutine hasargs
# Line 635  sub backtrace { Line 733  sub backtrace {
733          return @backtrace;          return @backtrace;
734  }  }
735    
736    =head2 checkbox
737    
738    Generate checkbox html markup from some attribute
739    
740      my $html = $self->checkbox('attribute_name', $value);
741    
742    =cut
743    
744    sub checkbox {
745            my ($self,$name,$value) = @_;
746            my $checked = '';
747            my $all_checkboxes = eval { $self->$name };
748            warn "ERROR tried to get checkbox value for '$name' which is unknown: $@" if $@;
749            $all_checkboxes = [ $all_checkboxes ] unless ref($all_checkboxes) eq 'ARRAY'; # sigh, too chatty
750            $checked = ' checked' if grep { defined $_ && $_ eq $value } @$all_checkboxes;
751            warn "# checkbox $name $value $checked\t", $self->dump( $self->$name );
752            qq|<input name="$name" value="$value" type="checkbox"$checked>|;
753    }
754    
755    =head2 strip
756    
757    Strip whitespace around content
758    
759      my $stripped = strip('  no more whitespace around this   ');
760    
761    =cut
762    
763    sub strip {
764            my $t = shift;
765            $t =~ s{^\s+}{}gs;
766            $t =~ s{>\s+<}{><}gs;
767            $t =~ s{\s+$}{}gs;
768            return $t;
769    }
770    
771  1;  1;

Legend:
Removed from v.674  
changed lines
  Added in v.957

  ViewVC Help
Powered by ViewVC 1.1.26