/[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 538 by dpavlin, Wed Nov 26 17:36:02 2008 UTC revision 1165 by dpavlin, Thu Jul 2 18:28:17 2009 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', 'Frey::Class::Icon';
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/;  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;
19    
20  use Frey::SVK;  use Frey::SVK;
21    
22  has 'head' => (  our @head;
23          is => 'rw',  sub head { @head }
         isa => 'ArrayRef[Str]',  
         default => sub { [ 'static/frey.css' ] },  
 );  
24    
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 47  has 'dump_max_bytes' => ( Line 53  has 'dump_max_bytes' => (
53          is => 'rw',          is => 'rw',
54          isa => 'Int',          isa => 'Int',
55          default => 4096,          default => 4096,
56          documentation => 'Maximum dump size sent to browser before truncation',          documentation => 'maximum dump size sent to browser before truncation',
57  );  );
58    
59  has 'inline_smaller_than' => (  has 'inline_smaller_than' => (
60          is => 'rw',          is => 'rw',
61          isa => 'Int',          isa => 'Int',
62          default => 10240,          default => 10240,
63          documentation => 'Inline JavaScript and CSS to reduce round-trips',          documentation => 'inline JavaScript and CSS to reduce round-trips',
64  );  );
65    
66  has 'chop_warning' => (  has 'html_dump_width' => (
67            documentation => 'crop longer lines in dumps',
68          is => 'rw',          is => 'rw',
69          isa => 'Int',          isa => 'Int',
70          default => 80,  #       required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them
71          documentation => 'Crop lines longer',          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;');
# Line 69  my $escape_re  = join '|' => keys %escap Line 83  my $escape_re  = join '|' => keys %escap
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 = shift;          my ( $self, $dump ) = @_;
101          $self->html_escape( dump( @_ ) );          $dump = dump( $dump ) if ref($dump);
102            my $width = $self->html_dump_width;
103            $dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs;
104            $dump = $self->html_escape( $dump );
105            $dump =~ s{\Q...\E}{&hellip;}gs;
106    #       $dump =~ $self->html_links( $dump ); # FIXME include this
107            return "<code>$dump</code>";
108  }  }
109    
110  sub dom2html {  sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }
111  #       warn "## dom2html ",dump( @_ );  sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }
112          return Continuity::Widget::DomNode->create( @_ )->to_string;  
113    our $re_html = qr{<(?:!--|(\w+)|[^>]+)/?>}s; # relaxed html check for one semi-valid tag
114    
115    sub popup_dropdown {
116            my ( $self, $type, $name, $content, $full ) = @_;
117    
118            $content = $self->html_dump($content) if ref $content;
119    
120            $content = qq|<span>$content</span>| unless $content =~ m{^\s*<(span|a|code).+?/\1>\s*};
121    
122            $content =~ s{<span>(<code>[^<]+</code>)</span>}{$1} && $self->TODO("code wrapped in span");
123    
124            warn "## $type [$name] = ", length( $content ), " bytes"; # if $self->debug; # FIXME
125    
126            if ( $name =~ m{::} && $name !~ $re_html ) {
127                    return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|;
128            } elsif ( $name =~ s{^\s*(<a)\s+}{$1 class="frey-$type"} ) {
129                    return qq|$name $content\n|;
130            } else {
131                    return qq|<span class="frey-$type">$name $content</span>\n|;
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 {
143          my $self = shift;          my $self = shift;
144          my $out = '';          my $out = '';
145          foreach my $path ( @{ $self->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|<!-- $path --><script type="text/javascript">\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|<!-- $path --><style type="text/css">\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 126  sub add_head { Line 179  sub add_head {
179          return if ! defined $path || $path eq '';          return if ! defined $path || $path eq '';
180          $path =~ s!^/!!;          $path =~ s!^/!!;
181    
182          if ( $path =~ m{<.*>}s ) {          if ( $path =~ $re_html ) {
183                  push @{ $self->head }, $path;                  push @head, $path;
184          } elsif ( -e $path ) {          } elsif ( -e $path ) {
185                  if ( $path =~ m/\.(?:js|css)$/ ) {                  if ( $path =~ m/\.(?:js|css)$/ ) {
186                          push @{ $self->head }, $path;                          push @head, $path;
187                  } else {                  } else {
188                          confess "can't add_head( $path ) it's not js or css";                          confess "can't add_head( $path ) it's not js or css";
189                  }                  }
# Line 141  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 {
240            my ($self,$css) = @_;
241            $self->_add_css_js( 'css', $css );
242    }
243    
244    sub add_js {
245            my ($self,$js) = @_;
246            $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 157  our $reload_counter = 0; Line 262  our $reload_counter = 0;
262  our @status;  our @status;
263  sub status { @status };  sub status { @status };
264    
265  our $icon_html;  sub html_page {
   
 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|;  
 }  
   
 sub page {  
266          my $self = shift;          my $self = shift;
267          my $a = {@_};          my $a = {@_};
268    
         warn "## page ",dump($a);  
   
269          $reload_counter++;          $reload_counter++;
270    
271          my $status_line = '';          my $status_line = '';
272    
         unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup };  
 #       unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup };  
   
273          foreach my $part ( @status ) {          foreach my $part ( @status ) {
274                  foreach my $name ( keys %$part ) {                  foreach my $name ( keys %$part ) {
275                          $status_line .= $self->popup( $name, $part->{$name} );                          $status_line .= $self->popup( $name, $part->{$name} );
# Line 197  sub page { Line 280  sub page {
280          $url =~ s{\?reload=\d+}{};          $url =~ s{\?reload=\d+}{};
281    
282          my $body = $a->{body};          my $body = $a->{body};
283          $body ||= $self->as_markup if $self->can('as_markup');          if ( ! $body ) {
284          if ( $self->content_type !~ m{html} ) {                  my $run = $a->{run} || 'as_markup';
285                    warn "# no body, invoke $self->$run on ", ref($self);
286                    $body = $self->$run;
287            }
288            if ( $self->content_type !~ m{html} || ! $self->wrap_in_page ) {
289                  warn "# return only $self body ", $self->content_type;                  warn "# return only $self body ", $self->content_type;
290                  return $body                  return $body
291          } elsif ( ! defined $body ) {          } elsif ( ! defined $body ) {
# Line 208  sub page { Line 295  sub page {
295    
296          $status_line .= $self->warnings_html;          $status_line .= $self->warnings_html;
297    
         my $inc_html;  
         {  
                 my $inc;  
                 map {  
                         s{.pm$}{};  
                         my $class = $_;  
                         s[/][}->{]g;  
                         $class =~ s[/][::]g;  
                         eval '$inc->{' . $_ . '} = $class';  
                 } sort keys %INC;  
                 $inc_html = dump( $inc );  
                 $inc_html =~ s{\s+=>\s+\d+}{}gs;  
                 $inc_html =~ s{(['"]?)(\w+)\1\s+=>\s+(['"]?)([\w:]*\2)\3}{<a target="$4" href="/$4" title="$4">$2</a>}gs;  
                 $inc_html =~ s{\s+=>\s+}{ }gs;  
                 $inc_html =~ s{,}{}gs;  
         }  
   
         $status_line .= $self->popup( INC => "<small>$inc_html</small>" );  
   
298          my      ($exit,$description) = ('exit','stop server');          my      ($exit,$description) = ('exit','stop server');
299                  ($exit,$description) = ('restart','restart server')                  ($exit,$description) = ('restart','restart server')
300                  if $ENV{FREY_RESTART}; # tune labels on exit link                  if $ENV{FREY_RESTART}; # tune labels on exit link
301    
302          my $right =          my $right =
303                  qq|                  qq|
304                          <span class="right">                          <span class="frey-status-right">
305                          <a title="reload $url"  href="/reload$url">reload</a>                          <a title="reload $url"  href="/reload$url">reload</a>
306                          <a title="$description" href="/exit$url">$exit</a>                          <a title="$description" href="/exit$url" target="exit">$exit</a>
307                          </span>                          </span>
308                  |;                  |;
309    
310          my $info = Frey::SVK->info;          my $svk = Frey::SVK->new;
311          my $revision = Frey::SVK->info->{Revision} || '';          my $info = $svk->info;
312            my $revision = $svk->info->{Revision} || '';
313          $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};          $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};
314    
315          $self->add_icon unless $icon_html;          $self->add_icon;
316    
317            my $title = undef
318                    || $a->{title}
319                    || $self->title
320                    || ref($self)
321                    ;
322    
323    #       $title =~ s{(\w)\w+::}{$1:}g; # XXX compress names of classes
324    
325            $self->add_css(qq|
326                    body {
327                            padding-bottom: 3em; /* don't overlap status line */
328                    }
329            |);
330    
331          my $html = join("\n",          my $html = join("\n",
332                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
333                  $self->_head_html,                  $self->_head_html,
334                  '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',                  qq|<title>$title</title>|,
335                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
336                  ( $icon_html || '<!-- no icon -->' ),                  ( $self->icon_html ),
337                  ( $a->{head} || '' ),                  ( $a->{head} || '' ),
338                  qq|                  qq|
339                  </head><body>                  </head><body>
# Line 290  sub editor { Line 373  sub editor {
373          qq|>$class</a>|;          qq|>$class</a>|;
374  }  }
375    
376  =head2 editor_links  =head2 html_links
377    
378  Create HTML links to editor for perl error message  Create HTML links to editor for perl error message
379    
380    my $html = $self->editor_links( $error )    my $html = $self->html_links( $error )
381    
382  =cut  =cut
383    
384  sub editor_links {  sub html_links {
385          my ( $self, $error ) = @_;          my ( $self, $error ) = @_;
386    
387            $error = $self->strip_full_path( $error );
388    
389    #       $error =~ s[(bless\({\s+.+?\s+},\s+)("[^"]+")(\) at)][<span class="frey-dropdown">$1<code>$2</code>$3</span>]gs; # FIXME insert bless hiding back
390    
391            # perl's backtrace
392          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
393                  {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;
394    
395            $error =~ s{(via (?:package)\s+"?)([\w:]+)("?)}
396                    {$1<a target="$2" href="/$2" title="introspect $2">$2</a>$3}gsm
397            || # or anything that looks like "Class::Name"
398            $error =~ s{"(\w+(?:::\w+)+)"}
399                    {"<a target="$1" href="/$1" title="introspect $1">$1</a>"}gsm;
400    
401            # method error messages
402            # FIXME replace with link to Frey::Introspect data
403            $error =~ s{(method ")(\w+)(" via)}
404                    {$1<a target="$2" href="/Frey::Shell::Grep/as_markup?pattern=$2" title="grep $2">$2</a>$3}gsm;
405    
406            # link paths to editor
407            $error =~ s{((?:lib|t)/[\S]+)\s+(\d+\s+bytes)}
408                    {<a target="editor" href="/editor+$1+1" title="vi $1 [$2]">$1</a>}gsm;
409    
410          $error =~ s{(via package ")([\w:]+)(")}          $error =~ s{(class ")([\w:]+)(")}
411                  {$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;
412    
413          return $error;          return $error;
414  }  }
415    
416    sub html_self {
417            my $self = shift;
418            my $html = $self;
419            $html =~ s{([\w:]+)=}{<a target="$1" href="/$1" title="introspect $1">$1</a>=}gsm;
420            return $html;
421    }
422    
423    =head2 error
424    
425    This method will return error to browser and backtrace unless
426    error message ends with LF C<\n> just like L<warn>
427    
428    =cut
429    
430  sub error {  sub error {
431          my $self = shift;          my $self = shift;
432          my $error = join(" ", @_);          my $error = join(" ", @_);
433    
434          my @backtrace = $self->backtrace;          if ( $error =~ m{<error>(.+)</error>}s ) {
435          $error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace;                  return qq|<div class="frey-error frey-fatal">$1</div>|;
436            }
437    
438          warn "ERROR: $error\n";          my $fatal = '';
439          return          my $backtrace = '';
                 qq|<pre class="frey-error">|  
                 . $self->editor_links( $error ) .  
                 qq|</pre>|  
                 ;  
 }  
440    
441  sub add_status {          if ( $error !~ m{\n$} ) {
442          my ( $self, $data ) = @_;                  if ( my @backtrace = $self->backtrace ) {
443          push @status, $data;                          $backtrace =
444  }                                    "\n" . $self->html_self . "->error backtrace\n\t"
445                                    . $self->html_links( join( "\n\t", @backtrace ) )
446                                    ;
447                            $fatal = qq| frey-fatal|;
448                    }
449            }
450    
451  sub clean_status {          warn "ERROR: $error\n";
452          @status = ();          $self->add_icon('error');
453          $icon_html = '';          $error = $self->html_links( $error );
454            return qq|<pre class="frey-error$fatal">$error $backtrace</pre>| ;
455  }  }
456    
457  sub status_parts {  =head1 Status line
         warn "## status parts ", dump( map { keys %$_ } @status );  
 }  
458    
459  sub DEMOLISH {  =head2 add_status
         my ( $self ) = @_;  
         warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status;  
 }  
460    
461  =head2 add_icon    $self->add_status( { name => { some => 'data' } } );
462    
463    Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png    $self->add_status( "append to last status popup" );
   Frey::Foo->add_icon('warning'); # /static/icons/Frey/Foo/warning.png  
464    
465  =cut  =cut
466    
467  sub icon_path {  sub add_status {
468          my ($self,$class,$variant) = @_;          my ( $self, $data ) = @_;
469          my $icon = $class;          die "no data" unless $data;
470          $icon =~ s{::}{/}g;          if ( ref $data  ) {
471          $icon .= "/$variant" if $variant;                  push @status, $data;
         my $path = 'static/icons/' . $icon . '.png';  
         if ( -e $path ) {  
                 warn "# $class from $self icon_path $path";  
                 return $path;  
472          } else {          } else {
473                  warn "TODO: add $path icon for $class";                  if ( defined $status[ $#status ] ) {
474                  return undef;                          $status[ $#status ]->{ '+' } = $data;
475                    } else {
476                            push @status, { '+' => $data };
477                    }
478          }          }
479  }  }
480    
481  sub add_icon {  =head2 clean_status
         my ($self,$variant) = @_;  
482    
483          my $class = ref($self);  Called at beginning of each request
         $class = $self->class if $self->can('class');  
         my $icon_path = $self->icon_path( $class, $variant ) || return;  
484    
485          $icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|;    $self->setup_request;
         warn "# using icon $icon_path";  
486    
487  =for later  =cut
488    
489          # FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work!  sub setup_request {
490          my $ico_path = $icon_path;          my ($self) = shift;
491          $ico_path =~ s{png$}{ico};          warn "## clean_status";
492          if ( ! -e $ico_path ) {          @head = ( 'static/frey.css' );
493                  system "convert $icon_path $ico_path";          @status = (
494                  warn "# convert $icon_path $ico_path : $@";                  { 'ClassBrowser' => Frey::Class::Browser->new( usage_sort => 1, usage_on_top => 0 )->as_markup },
495          }                  { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },
496          $icon_html .= qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path">| if -e $ico_path;                  { 'INC' => Frey::INC->new->as_markup },
497            );
498    }
499    
500    =head2 status_parts
501    
502    Dump all status line parts
503    
504      $self->status_parts
505    
506  =cut  =cut
507    
508    sub status_parts {
509            warn "## status parts ", dump( map { keys %$_ } @status );
510    }
511    
512    =for debug
513    
514    sub DEMOLISH {
515            my ( $self ) = @_;
516            warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status;
517  }  }
518    
519    =cut
520    
521  my $warn_colors = {  my $warn_colors = {
522          '#'  => '#444',          '#'  => '#444',
523          '##' => '#888',          '##' => '#888',
# Line 403  my $multiline_markers = { Line 530  my $multiline_markers = {
530          '"' => '"',          '"' => '"',
531  };  };
532    
533    =for later
534    
535  my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']';  my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']';
536  warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re";  warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re";
537    
538    =cut
539    
540  sub log_path {  sub log_path {
541          $Frey::Bootstrap::log_path || warn "no log_path?";          $Frey::Bootstrap::log_path || die "no log_path?";
542  }  }
543    
544    our $pwd = `pwd`;
545    chomp $pwd;
546    
547    sub strip_full_path {
548            my ($self, $msg) = @_;
549            # Mojo seems to expand warn messages to full path which is annoying
550            $msg =~ s{/[^/]+/\.\./}{/}gs;
551            $msg =~ s{$pwd/*}{}gs;
552            return $msg;
553    }
554    
555    our $last_log_pos  = 0;
556    our $last_log_line = 0;
557    
558  sub warnings_html {  sub warnings_html {
559          my ($self,$level) = shift;          my ($self,$level) = shift;
560          $level ||= $self->debug,          $level ||= $self->debug,
561          my $path = $self->log_path;          my $path = $self->log_path;
562    
563          my $warnings;          my $max = 30;
564          my $line = 0;          my $pos = 0;
565            my @warnings = ( '' x $max ); # XXX circualar buffer for 50 lines
566            my $line = $last_log_line;
567          my $multiline_end;          my $multiline_end;
568    
569          open(my $log, '<', $path) || die "can't open $path: $!";          # XXX do we really want to do this every time?
570            my $css = qq|/* short css classes for levels */\n|;
571            my $level_to_class;
572            foreach ( keys %$warn_colors ) {
573                    my $l = length($_);
574                    my $class = 'l' . $l;
575                    $css .= qq|.$class { color: $warn_colors->{$_} }\n|;
576                    $level_to_class->{ $_ } = $class;
577            }
578            $self->add_css( $css );
579    
580            open(my $log, '<', $path)    || die "can't open $path: $!";
581            seek($log, $last_log_pos, 0) || warn "can't seek: $!";
582          while(<$log>) {          while(<$log>) {
583                  chomp;                  chomp;
584                  $line++;                  $line++;
585    
586                    next if m{^\s+(Mojo|Class::MOP|Moose)::};
587    
588                  my $style = '';                  my $style = '';
589    
590    =for filter
591    
592                  if ( $multiline_end ) {                  if ( $multiline_end ) {
593                          if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) {                          if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) {
594  #                               warn "## $line end of $multiline_end in '$_'\n";  #                               warn "## $line end of $multiline_end in '$_'\n";
# Line 442  sub warnings_html { Line 605  sub warnings_html {
605                                  next;                                  next;
606                          }                          }
607    
608                          $style = $warn_colors->{$1}  =cut
609                                  ? ' style="color:' . $warn_colors->{$1} . '"'                  if ( m{^(#*)} ) {
610                                  : '';  
611                            my $level = $1;
612                            my $msg = $self->strip_full_path( $_ );
613    
                         my $msg = $self->html_escape( $_ );  
614                          my $spacer = ' ';                          my $spacer = ' ';
615                          if ( length($msg) > $self->chop_warning ) {                          my $real_msg = expand( $msg );
616                                  $msg = substr( $msg, 0, $self->chop_warning );                          if ( length($real_msg) > $self->html_dump_width ) {
617                                    
618                                    $real_msg = substr( $msg, 0, $self->html_dump_width );
619                                    $msg = unexpand( $real_msg );
620                                  $spacer = '&hellip;'                                  $spacer = '&hellip;'
621                          }                          }
622                          $msg =~ s{^\s}{&nbsp;}g;  
623                          $warnings .= qq|<tt$style>$msg</tt>$spacer<small><a target="editor" href="/editor+$path+$line">+$line</a></small><br/>|;                          $msg = $self->html_escape( $msg );
624                          # FIXME <tt> should be <code> but CSS hates me  
625                            if ( my $class = $level_to_class->{ $level } ) {
626                                    $msg = qq|<span class="$class">$msg</span>|;
627                            }
628    
629                            #$msg .= $spacer . qq|<a target="editor" href="/editor+$path+$line" style="float: right;">$line</a>\n|;
630                            $msg = qq|<a target="editor" href="/editor+$path+$line" style="float: right;">$line</a>$msg|
631                                    . ( $spacer ? $spacer : '' )
632                                    . "\n"; # XXX <pre> needs this
633    
634                            $warnings[ $pos++ % $max ] = $msg;
635                  }                  }
636          }          }
637            $last_log_pos = tell($log);
638            $last_log_line = $line;
639            warn "log has $line lines tell position $last_log_pos";
640          close($log) || die "can't close $path: $!";          close($log) || die "can't close $path: $!";
641    
642            my $size = -s $path;
643    
644            my $warnings = join("",
645                    map { $warnings[ ( $pos + $_ ) % $max ] || '' } 0 .. ( $max - 1 )
646            );
647    
648            my $s = length($warnings);
649    
650          return          return
651                    qq|<span class="frey-popup"><a target="editor" href="/editor+$path+$line" title="open $path level $level">warn</a><span>|                  # need to wrap editor link into span so we can have links in warnings
652                  . $self->editor_links( $warnings )                    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>|
653                  . qq|</span></span>|                  . $self->html_links( $warnings )
654                    . qq|</code></span>|
655                  ;                  ;
656  }  }
657    
658    
659    =head2 backtrace
660    
661    Show backtrace with links to editor
662    
663      my @backtrace = $self->backtrace;
664    
665    =cut
666    
667    sub backtrace {
668            my ($self) = @_;
669    
670            my @backtrace;
671            foreach ( 1 .. 5 ) { # 0 = backtrace
672                    my (
673                            $package,$path,$line
674                            # subroutine hasargs
675                            # wantarray evaltext is_require
676                            # hints bitmask hinthash
677                    ) = caller($_) or last;
678    
679                    push @backtrace,
680                            qq|via "$package" at $path line $line|;
681            }
682            #warn "# backtrace: ", dump( @backtrace ) if @backtrace;
683            return @backtrace;
684    }
685    
686    =head2 checkbox
687    
688    Generate checkbox html markup from some attribute
689    
690      my $html = $self->checkbox('attribute_name', $value);
691    
692    =cut
693    
694    sub checkbox {
695            my ($self,$name,$value) = @_;
696            my $checked = '';
697            my $all_checkboxes = eval { $self->$name };
698            warn "ERROR tried to get checkbox value for '$name' which is unknown: $@" if $@;
699            $all_checkboxes = [ $all_checkboxes ] unless ref($all_checkboxes) eq 'ARRAY'; # sigh, too chatty
700            $checked = ' checked' if grep { defined $_ && $_ eq $value } @$all_checkboxes;
701            warn "# checkbox $name $value $checked\t", $self->dump( $self->$name );
702            qq|<input name="$name" value="$value" type="checkbox"$checked>|;
703    }
704    
705    =head2 strip
706    
707    Strip whitespace around content
708    
709      my $stripped = strip('  no more whitespace around this   ');
710    
711    =cut
712    
713    sub strip {
714            my $t = shift;
715            $t =~ s{^\s+}{}gs;
716            $t =~ s{>\s+<}{><}gs;
717            $t =~ s{\s+$}{}gs;
718            return $t;
719    }
720    
721    no Moose::Role;
722    
723  1;  1;

Legend:
Removed from v.538  
changed lines
  Added in v.1165

  ViewVC Help
Powered by ViewVC 1.1.26