/[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 674 by dpavlin, Tue Dec 2 01:49:49 2008 UTC revision 1065 by dpavlin, Mon Apr 27 18:43:18 2009 UTC
# Line 3  use Moose::Role; Line 3  use Moose::Role;
3    
4  with 'Frey::Session';  with 'Frey::Session';
5    
 use Frey::Types;  
   
 #use Continuity::Widget::DomNode;  
6  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
7  use Carp qw/confess cluck carp/;  use Carp qw/confess cluck carp/;
8  use File::Slurp;  use File::Slurp;
9    use Text::Tabs; # expand, unexpand
10    use Digest::MD5 qw/md5/;
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    
84  sub html_escape {  sub html_escape {
85          my ( $self, $html ) = @_;          my ( $self, $html ) = @_;
86            return '' unless defined $html;
87          $html =~ s/($escape_re)/$escape{$1}/g;          $html =~ s/($escape_re)/$escape{$1}/g;
88          return $html;          return $html;
89  }  }
90    
91    # from Mojo::ByteStream
92    sub url_escape {
93            my ( $self, $url, $pattern ) = @_;
94            $pattern ||= 'A-Za-z0-9\-\.\_\~';
95            $url =~ s/([^$pattern])/sprintf('%%%02X',ord($1))/ge;
96            return $url;
97    }
98    
99  sub html_dump {  sub html_dump {
100          my ( $self, $dump ) = @_;          my ( $self, $dump ) = @_;
101          $dump = dump( $dump ) if ref($dump);          $dump = dump( $dump ) if ref($dump);
# Line 86  sub html_dump { Line 103  sub html_dump {
103          $dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs;          $dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs;
104          $dump = $self->html_escape( $dump );          $dump = $self->html_escape( $dump );
105          $dump =~ s{\Q...\E}{&hellip;}gs;          $dump =~ s{\Q...\E}{&hellip;}gs;
106  #       $dump =~ $self->editor_links( $dump ); # FIXME include this  #       $dump =~ $self->html_links( $dump ); # FIXME include this
107          return "<code>$dump</code>";          return "<code>$dump</code>";
108  }  }
109    
110  sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }  sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }
111  sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }  sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }
112    
113  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
114    
115  sub popup_dropdown {  sub popup_dropdown {
116          my ( $self, $type, $name, $content, $full ) = @_;          my ( $self, $type, $name, $content, $full ) = @_;
# Line 115  sub popup_dropdown { Line 132  sub popup_dropdown {
132          }          }
133  }  }
134    
135  sub _inline_path {  sub _inline {
136          my ( $self, $path ) = @_;          my ( $self, $path ) = @_;
137          -s $path < $self->inline_smaller_than;          return unless defined $path;
138            warn "# _inline $path";
139            -e $path && -s $path < $self->inline_smaller_than && -s $path;
140  }  }
141    
142  sub _head_html {  sub _head_html {
# Line 126  sub _head_html { Line 145  sub _head_html {
145          foreach my $path ( @head ) {          foreach my $path ( @head ) {
146                  $path =~ s!^/!!;                  $path =~ s!^/!!;
147                  if ( $path =~ m/\.js$/ ) {                  if ( $path =~ m/\.js$/ ) {
148                          $out .= $self->_inline_path( $path ) ?                          my $size;
149                                  qq|<script type="text/javascript">\n/* inline $path */\n\n| . read_file($path) . qq|\n</script>| :                          $out .= $size = _inline( $path ) ?
150                                    qq|<script type="text/javascript">\n/* inline $path $size bytes */\n\n| . read_file($path) . qq|\n</script>| :
151                                  qq|<script type="text/javascript" src="/$path"></script>|;                                  qq|<script type="text/javascript" src="/$path"></script>|;
152                  } elsif ( $path =~ m/\.css$/ ) {                  } elsif ( $path =~ m/\.css$/ ) {
153                          $out .= $self->_inline_path( $path ) ?                          my $size;
154                                  qq|<style type="text/css">\n/* inline $path */\n\n| . read_file( $path ) . qq|\n</style>| :                          $out .= $size = _inline( $path ) ?
155                                    qq|<style type="text/css">\n/* inline $path $size bytes */\n\n| . read_file( $path ) . qq|\n</style>| :
156                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;                                  qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
157                  } elsif ( $path =~ m{<.+>}s ) {                  } elsif ( $path =~ m{<.+>}s ) {
158                          $out .= $path;                          $out .= $path;
# Line 173  sub add_head { Line 194  sub add_head {
194    
195  }  }
196    
197    sub _add_css_js {
198            my ( $self, $what, $content ) = @_;
199    
200            my $md5 = md5( $content );
201            return if $self->{_add_css_js_seen}->{$what}->{$md5}++;
202    
203            my $tag  = $what eq 'css' ? 'style'    : 'script';
204            my $type = $what eq 'css' ? 'text/css' : 'text/javascript';
205            my $head;
206    
207            my ( $package, $path, $line ) = caller(1);
208    
209            $content = "/$content" if $content !~ m{[\n\r]} && -e $content;
210            if ( $content =~ $re_html && $what ne 'js' ) {
211                    $head = qq|
212                            $content
213                            <!-- $type via $package at $path line $line -->
214                    |;
215            } elsif ( $content =~ m{^(/\w+|https?://)} && $content !~ m{[\n\r]} ) {
216                    if ( $what eq 'js' ) {
217                            $head = qq|
218                                    <$tag type="$type" src="$content">
219                                    /* $what via $package at $path line $line */
220                                    </$tag>
221                            |;
222                    } else {
223                            $head = qq|
224                                    <link rel="stylesheet" type="$type" href="$content">
225                                    <!-- $what via $package at $path line $line -->
226                            |;
227                    }
228            } else {
229                    $head = qq|
230                            <$tag type="$type">
231                            /* via $package at $path line $line */
232                            $content
233                            </$tag>
234                    |;
235            };
236            $self->add_head( $head );
237    }
238    
239  sub add_css {  sub add_css {
240          my ($self,$css) = @_;          my ($self,$css) = @_;
241          my ( $package, $path, $line ) = caller;          $self->_add_css_js( 'css', $css );
242          $self->add_head( qq|  }
243          <style type="text/css">  
244          /* via $package at $path line $line */  sub add_js {
245          $css          my ($self,$js) = @_;
246          </style>          $self->_add_css_js( 'js', $js );
         | );  
247  }  }
248    
249  our $reload_counter = 0;  our $reload_counter = 0;
250    
251    
252  =head2 page  =head2 html_page
253    
254    $self->page(    $self->html_page(
255          title => 'page title',          title => 'page title',
256          head  => '<!-- optional head markup -->',          head  => '<!-- optional head markup -->',
257          body  => '<b>Page Body</b>',          body  => '<b>Page Body</b>',
# Line 202  sub status { @status }; Line 264  sub status { @status };
264    
265  our $icon_html;  our $icon_html;
266    
267  sub page {  sub html_page {
268          my $self = shift;          my $self = shift;
269          my $a = {@_};          my $a = {@_};
270    
         warn "## page ",dump($a);  
   
271          $reload_counter++;          $reload_counter++;
272    
273          my $status_line = '';          my $status_line = '';
# Line 225  sub page { Line 285  sub page {
285          if ( ! $body ) {          if ( ! $body ) {
286                  my $run = $a->{run} || 'as_markup';                  my $run = $a->{run} || 'as_markup';
287                  warn "# no body, invoke $self->$run on ", ref($self);                  warn "# no body, invoke $self->$run on ", ref($self);
288                  eval {                  $body = $self->$run;
                         $body = $self->$run;  
                 };  
                 $body = $self->error( $@, '' ) if $@;  
289          }          }
290          if ( $self->content_type !~ m{html} ) {          if ( $self->content_type !~ m{html} || ! $self->wrap_in_page ) {
291                  warn "# return only $self body ", $self->content_type;                  warn "# return only $self body ", $self->content_type;
292                  return $body                  return $body
293          } elsif ( ! defined $body ) {          } elsif ( ! defined $body ) {
# Line 246  sub page { Line 303  sub page {
303    
304          my $right =          my $right =
305                  qq|                  qq|
306                          <span class="right">                          <span class="frey-status-right">
307                          <a title="reload $url"  href="/reload$url">reload</a>                          <a title="reload $url"  href="/reload$url">reload</a>
308                          <a title="$description" href="/exit$url" target="exit">$exit</a>                          <a title="$description" href="/exit$url" target="exit">$exit</a>
309                          </span>                          </span>
# Line 259  sub page { Line 316  sub page {
316    
317          $self->add_icon unless $icon_html;          $self->add_icon unless $icon_html;
318    
319            my $title = undef
320                    || $a->{title}
321                    || $self->title
322                    || ref($self)
323                    ;
324    
325    #       $title =~ s{(\w)\w+::}{$1:}g; # XXX compress names of classes
326    
327            $self->add_css(qq|
328                    body {
329                            padding-bottom: 3em; /* don't overlap status line */
330                    }
331            |);
332    
333          my $html = join("\n",          my $html = join("\n",
334                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
335                  $self->_head_html,                  $self->_head_html,
336                  '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',                  qq|<title>$title</title>|,
337                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
338                  ( $icon_html || '<!-- no icon -->' ),                  ( $icon_html || '<!-- no icon -->' ),
339                  ( $a->{head} || '' ),                  ( $a->{head} || '' ),
# Line 304  sub editor { Line 375  sub editor {
375          qq|>$class</a>|;          qq|>$class</a>|;
376  }  }
377    
378  =head2 editor_links  =head2 html_links
379    
380  Create HTML links to editor for perl error message  Create HTML links to editor for perl error message
381    
382    my $html = $self->editor_links( $error )    my $html = $self->html_links( $error )
383    
384  =cut  =cut
385    
386  sub editor_links {  sub html_links {
387          my ( $self, $error ) = @_;          my ( $self, $error ) = @_;
388    
389            $error = $self->strip_full_path( $error );
390    
391  #       $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
392    
393          # perl's backtrace          # perl's backtrace
394          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
395                  {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;
396    
397          $error =~ s{(via (?:package)\s+"?)([\w:]+)("?)}          $error =~ s{(via (?:package)\s+"?)([\w:]+)("?)}
398                  {$1<a target="$2" href="/$2">$2</a>$3}gsm;                  {$1<a target="$2" href="/$2" title="introspect $2">$2</a>$3}gsm
399            || # or anything that looks like "Class::Name"
400            $error =~ s{"(\w+(?:::\w+)+)"}
401                    {"<a target="$1" href="/$1" title="introspect $1">$1</a>"}gsm;
402    
403          # method error messages          # method error messages
404  #       $error =~ s{(method ")(\w+)"}          # FIXME replace with link to Frey::Introspect data
405  #               {$1<a target="/Frey::Shell::Grep?pattern=$2">$2</a>"}gsm; # FIXME replace with link to Frey::Introspect data          $error =~ s{(method ")(\w+)(" via)}
406                    {$1<a target="$2" href="/Frey::Shell::Grep/as_markup?pattern=$2" title="grep $2">$2</a>$3}gsm;
407    
408            # link paths to editor
409            $error =~ s{((?:lib|t)/[\S]+)\s+(\d+\s+bytes)}
410                    {<a target="editor" href="/editor+$1+1" title="vi $1 [$2]">$1</a>}gsm;
411    
412          # anything that looks like "Class::Name"          $error =~ s{(class ")([\w:]+)(")}
413          $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;  
414    
415          return $error;          return $error;
416  }  }
417    
418    sub html_self {
419            my $self = shift;
420            my $html = $self;
421            $html =~ s{([\w:]+)=}{<a target="$1" href="/$1" title="introspect $1">$1</a>=}gsm;
422            return $html;
423    }
424    
425  =head2 error  =head2 error
426    
427  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 434  sub error {
434          my $error = join(" ", @_);          my $error = join(" ", @_);
435    
436          my $fatal = '';          my $fatal = '';
437            my $backtrace = '';
438    
439          if ( $error !~ m{\n$} ) {          if ( $error !~ m{\n$} ) {
440                  if ( my @backtrace = $self->backtrace ) {                  if ( my @backtrace = $self->backtrace ) {
441                          $error .= "\n\t" . join( "\n\t", @backtrace );                          $backtrace =
442                          $fatal = qq| class="fatal"|;                                    "\n" . $self->html_self . "->error backtrace\n\t"
443                                    . $self->html_links( join( "\n\t", @backtrace ) )
444                                    ;
445                            $fatal = qq| frey-fatal|;
446                  }                  }
447          }          }
448    
449          warn "ERROR: $error\n";          warn "ERROR: $error\n";
450          return          $self->add_icon('error');
451                  qq|<pre class="frey-error$fatal">|          $error = $self->html_links( $error );
452                  . $self->editor_links( $error ) .          return qq|<pre class="frey-error$fatal">$error $backtrace</pre>| ;
                 qq|</pre>|  
                 ;  
453  }  }
454    
455  =head1 Status line  =head1 Status line
# Line 375  sub error { Line 464  sub error {
464    
465  sub add_status {  sub add_status {
466          my ( $self, $data ) = @_;          my ( $self, $data ) = @_;
467          push @status, { 'X' => [ $self->backtrace ] };          die "no data" unless $data;
468          if ( ref($data) ) {          if ( ref $data  ) {
469                  push @status, $data;                  push @status, $data;
470          } else {          } else {
471                  if ( defined $status[ $#status ] ) {                  if ( defined $status[ $#status ] ) {
# Line 399  sub clean_status { Line 488  sub clean_status {
488          my ($self) = shift;          my ($self) = shift;
489          @head = ( 'static/frey.css' );          @head = ( 'static/frey.css' );
490          @status = (          @status = (
491                  { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup },                  { 'ClassBrowser' => Frey::Class::Browser->new( usage_sort => 1, usage_on_top => 0 )->as_markup },
492                  { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },                  { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },
493                  { 'INC' => Frey::INC->new->as_markup },                  { 'INC' => Frey::INC->new->as_markup },
494          );          );
# Line 436  sub DEMOLISH { Line 525  sub DEMOLISH {
525    
526  sub icon_path {  sub icon_path {
527          my ($self,$class,$variant) = @_;          my ($self,$class,$variant) = @_;
528          my $icon = $class;  
529          $icon =~ s{::}{/}g;          sub icon_exists {
530          $icon .= "/$variant" if $variant;                  my $class = shift;
531          my $path = 'static/icons/' . $icon . '.png';                  $class =~ s{::}{/}g;
532          if ( -e $path ) {                  $class .= "/$variant" if $variant;
533                  warn "# $class from $self icon_path $path" if $self->debug;                  my $icon_path = 'static/icons/' . $class . '.png';
534                  return $path;                  return $icon_path if -e $icon_path;
535          } else {                  return;
536                  $self->TODO( "add $path icon for $class" );          }
537    
538            my $path = icon_exists( $class );
539            if ( ! $path ) {
540                    my $super_class = $class;
541                    while ( $super_class =~ s{::[^:]+$}{} && ! $path ) {
542                            $path = icon_exists( $super_class ) unless $super_class eq 'Frey'; # don't default on Frey icon
543                    }
544            }
545    
546            if ( ! $path ) {
547                    $self->TODO( "add icon for $class" . ( $variant ? " variant $variant" : '' ) );
548                  return undef;                  return undef;
549          }          }
550    
551            warn "# $class from $self icon_path $path" if $self->debug;
552            return $path;
553  }  }
554    
555  sub add_icon {  sub add_icon {
556          my ($self,$variant) = @_;          my ($self,$variant) = @_;
557    
558          my $class = ref($self);          my $class = $self->class if $self->can('class');
559          $class = $self->class if $self->can('class');          #$class ||= $self->title;
560            $class ||= ref($self);
561          my $icon_path = $self->icon_path( $class, $variant ) || return;          my $icon_path = $self->icon_path( $class, $variant ) || return;
562    
563          $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 601  sub log_path {
601          $Frey::Bootstrap::log_path || die "no log_path?";          $Frey::Bootstrap::log_path || die "no log_path?";
602  }  }
603    
 our $last_log_pos  = 0;  
 our $last_log_line = 0;  
   
604  our $pwd = `pwd`;  our $pwd = `pwd`;
605  chomp $pwd;  chomp $pwd;
606    
607    sub strip_full_path {
608            my ($self, $msg) = @_;
609            # Mojo seems to expand warn messages to full path which is annoying
610            $msg =~ s{/[^/]+/\.\./}{/}gs;
611            $msg =~ s{$pwd/*}{}gs;
612            return $msg;
613    }
614    
615    our $last_log_pos  = 0;
616    our $last_log_line = 0;
617    
618  sub warnings_html {  sub warnings_html {
619          my ($self,$level) = shift;          my ($self,$level) = shift;
620          $level ||= $self->debug,          $level ||= $self->debug,
# Line 557  sub warnings_html { Line 669  sub warnings_html {
669                  if ( m{^(#*)} ) {                  if ( m{^(#*)} ) {
670    
671                          my $level = $1;                          my $level = $1;
672                          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;  
673    
674                          my $spacer = ' ';                          my $spacer = ' ';
675                          my $real_msg = expand( $msg );                          my $real_msg = expand( $msg );
# Line 602  sub warnings_html { Line 710  sub warnings_html {
710          return          return
711                  # 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
712                    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>|
713                  . $self->editor_links( $warnings )                  . $self->html_links( $warnings )
714                  . qq|</code></span>|                  . qq|</code></span>|
715                  ;                  ;
716  }  }
# Line 620  sub backtrace { Line 728  sub backtrace {
728          my ($self) = @_;          my ($self) = @_;
729    
730          my @backtrace;          my @backtrace;
731          foreach ( 0 .. 5 ) {          foreach ( 1 .. 5 ) { # 0 = backtrace
732                  my (                  my (
733                          $package,$path,$line                          $package,$path,$line
734                          # subroutine hasargs                          # subroutine hasargs
# Line 635  sub backtrace { Line 743  sub backtrace {
743          return @backtrace;          return @backtrace;
744  }  }
745    
746    =head2 checkbox
747    
748    Generate checkbox html markup from some attribute
749    
750      my $html = $self->checkbox('attribute_name', $value);
751    
752    =cut
753    
754    sub checkbox {
755            my ($self,$name,$value) = @_;
756            my $checked = '';
757            my $all_checkboxes = eval { $self->$name };
758            warn "ERROR tried to get checkbox value for '$name' which is unknown: $@" if $@;
759            $all_checkboxes = [ $all_checkboxes ] unless ref($all_checkboxes) eq 'ARRAY'; # sigh, too chatty
760            $checked = ' checked' if grep { defined $_ && $_ eq $value } @$all_checkboxes;
761            warn "# checkbox $name $value $checked\t", $self->dump( $self->$name );
762            qq|<input name="$name" value="$value" type="checkbox"$checked>|;
763    }
764    
765    =head2 strip
766    
767    Strip whitespace around content
768    
769      my $stripped = strip('  no more whitespace around this   ');
770    
771    =cut
772    
773    sub strip {
774            my $t = shift;
775            $t =~ s{^\s+}{}gs;
776            $t =~ s{>\s+<}{><}gs;
777            $t =~ s{\s+$}{}gs;
778            return $t;
779    }
780    
781  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26