/[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 482 by dpavlin, Thu Nov 20 15:23:13 2008 UTC revision 961 by dpavlin, Wed Jan 7 19:41:48 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';
5    
6  use Frey::Types;  #use Continuity::Widget::DomNode;
   
 use Continuity::Widget::DomNode;  
7  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
8  use Carp qw/confess/;  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;
19    
20  has 'head' => (  use Frey::SVK;
         is => 'rw',  
         isa => 'ArrayRef[Str]',  
         default => sub { [ 'static/frey.css' ] },  
 );  
21    
22  has 'status' => (  our @head;
23          is => 'rw',  sub head { @head }
         isa => 'ArrayRef[HashRef[Str]]',  
         lazy => 1,  
         default => sub { [  
                 { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup },  
                 { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },  
         ] },  
 );  
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 56  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    
 =head2 inline_smaller_than  
   
 Inline JavaScript and CSS smaller than this size into page reducing  
 round-trips to server.  
   
 =cut  
   
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',
64    );
65    
66    has 'html_dump_width' => (
67            documentation => 'crop longer lines in dumps',
68            is => 'rw',
69            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
71            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  sub dom2html {  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
82  #       warn "## dom2html ",dump( @_ );  my $escape_re  = join '|' => keys %escape;
83          return Continuity::Widget::DomNode->create( @_ )->to_string;  
84    sub html_escape {
85            my ( $self, $html ) = @_;
86            $html =~ s/($escape_re)/$escape{$1}/g;
87            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 _inline_path {  sub html_dump {
99            my ( $self, $dump ) = @_;
100            $dump = dump( $dump ) if ref($dump);
101            my $width = $self->html_dump_width;
102            $dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs;
103            $dump = $self->html_escape( $dump );
104            $dump =~ s{\Q...\E}{&hellip;}gs;
105    #       $dump =~ $self->html_links( $dump ); # FIXME include this
106            return "<code>$dump</code>";
107    }
108    
109    sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }
110    sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }
111    
112    our $re_html = qr{<(?:!--|(\w+)|[^>]+)/?>}s; # relaxed html check for one semi-valid tag
113    
114    sub popup_dropdown {
115            my ( $self, $type, $name, $content, $full ) = @_;
116    
117            $content = $self->html_dump($content) if ref $content;
118    
119            $content = qq|<span>$content</span>| unless $content =~ m{^\s*<(span|a|code).+?/\1>\s*};
120    
121            $content =~ s{<span>(<code>[^<]+</code>)</span>}{$1} && $self->TODO("code wrapped in span");
122    
123            warn "## $type [$name] = ", length( $content ), " bytes"; # if $self->debug; # FIXME
124    
125            if ( $name =~ m{::} && $name !~ $re_html ) {
126                    return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|;
127            } elsif ( $name =~ s{^\s*(<a)\s+}{$1 class="frey-$type"} ) {
128                    return qq|$name $content\n|;
129            } else {
130                    return qq|<span class="frey-$type">$name $content</span>\n|;
131            }
132    }
133    
134    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 {
142          my $self = shift;          my $self = shift;
143          my $out = '';          my $out = '';
144          foreach my $path ( @{ $self->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|<!-- $path --><script type="text/javascript">\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|<!-- $path --><style type="text/css">\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 120  sub add_head { Line 178  sub add_head {
178          return if ! defined $path || $path eq '';          return if ! defined $path || $path eq '';
179          $path =~ s!^/!!;          $path =~ s!^/!!;
180    
181          if ( $path =~ m{<.*>}s ) {          if ( $path =~ $re_html ) {
182                  push @{ $self->head }, $path;                  push @head, $path;
183          } elsif ( -e $path ) {          } elsif ( -e $path ) {
184                  if ( $path =~ m/\.(?:js|css)$/ ) {                  if ( $path =~ m/\.(?:js|css)$/ ) {
185                          push @{ $self->head }, $path;                          push @head, $path;
186                  } else {                  } else {
187                          confess "can't add_head( $path ) it's not js or css";                          confess "can't add_head( $path ) it's not js or css";
188                  }                  }
# Line 135  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 {
236            my ($self,$css) = @_;
237            $self->_add_css_js( 'css', $css );
238    }
239    
240    sub add_js {
241            my ($self,$js) = @_;
242            $self->_add_css_js( 'js', $js );
243    }
244    
245  our $reload_counter = 0;  our $reload_counter = 0;
246    
247    
248  =head2 page  =head2 html_page
249    
250    $self->page(    $self->html_page(
251          title => 'page title',          title => 'page title',
252          head  => '<!-- optional head markup -->',          head  => '<!-- optional head markup -->',
253          body  => '<b>Page Body</b>',          body  => '<b>Page Body</b>',
# Line 148  our $reload_counter = 0; Line 255  our $reload_counter = 0;
255    
256  =cut  =cut
257    
258  sub page {  our @status;
259    sub status { @status };
260    
261    our $icon_html;
262    
263    sub html_page {
264          my $self = shift;          my $self = shift;
265          my $a = {@_};          my $a = {@_};
266    
267          $reload_counter++;          $reload_counter++;
268    
269          my $status_line = '';          my $status_line = '';
270          foreach my $part ( @{ $self->status } ) {  
271                  if ( ref($part) ne 'HASH' ) {          foreach my $part ( @status ) {
                         warn "part not hash ",dump( $part ) ;  
                         #$self->status( $part );  
                         next;  
                 }  
272                  foreach my $name ( keys %$part ) {                  foreach my $name ( keys %$part ) {
273                          my $content = $part->{$name};                          $status_line .= $self->popup( $name, $part->{$name} );
                         if ( ref($content) ) {  
                                 $content = '<code>' . dump($content) . '</code>';  
                                 my $l = length($content);  
                                 $content = qq|<span>$l bytes</span>| if $l > $self->dump_max_bytes;  
                         } else {  
                                 $content = qq|<span>$content</span>|;  
                         }  
                         warn "### part [$name] = ", length( $content ), " bytes" if $self->debug;  
                         $status_line .= qq|<span class="frey-popup">$name $content</span>\n|;  
274                  }                  }
275          }          }
276    
# Line 179  sub page { Line 278  sub page {
278          $url =~ s{\?reload=\d+}{};          $url =~ s{\?reload=\d+}{};
279    
280          my $body = $a->{body};          my $body = $a->{body};
281          $body ||= $self->as_markup if $self->can('as_markup');          if ( ! $body ) {
282          if ( $self->content_type !~ m{html} ) {                  my $run = $a->{run} || 'as_markup';
283                    warn "# no body, invoke $self->$run on ", ref($self);
284                    $body = $self->$run;
285            }
286            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 188  sub page { Line 291  sub page {
291                  $body = '<!-- no body -->';                  $body = '<!-- no body -->';
292          }          }
293    
294          my $warn_colors = {          $status_line .= $self->warnings_html;
                 '#'  => '#444',  
                 '##' => '#888',  
         };  
   
         $status_line  
                 .= qq|<span class="frey-popup">warn<span>|  
                 . $self->editor_links(  
                         join("", map {  
                                 warn "# $_";  
                                 my $style = '';  
                                 $style = $warn_colors->{$1}  
                                         ? ' style="color:' . $warn_colors->{$1} . '"'  
                                         : ''  
                                         if m{^(#+)};  
                                 qq|<tt$style>$_</tt><br/>|; # XXX <tt> should be <code> but CSS hates me  
                         } $self->warnings )  
                 )  
                 . qq|</span></span>|  
                 if $self->warnings;  
295    
296          my      ($exit,$description) = ('exit','stop server');          my      ($exit,$description) = ('exit','stop server');
297                  ($exit,$description) = ('restart','restart server')                  ($exit,$description) = ('restart','restart server')
# Line 216  sub page { Line 300  sub page {
300          my $right =          my $right =
301                  qq|                  qq|
302                          <span class="right">                          <span class="right">
303                          <a title="reload"  href="/reload$url"><code>$url</code></a>                          <a title="reload $url"  href="/reload$url">reload</a>
304                          <a title="$description" href="/exit$url">$exit</a>                          <a title="$description" href="/exit$url" target="exit">$exit</a>
305                          </span>                          </span>
306                  |;                  |;
307    
308            my $svk = Frey::SVK->new;
309            my $info = $svk->info;
310            my $revision = $svk->info->{Revision} || '';
311            $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};
312    
313            $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 -->' ),
329                  ( $a->{head} || '' ),                  ( $a->{head} || '' ),
330                  qq|                  qq|
331                  </head><body>                  </head><body>
332                  $body                  $body
333                  <div class="frey-status-line">                  <div class="frey-status-line">
334                          <a href="/">Frey</a> $Frey::VERSION                          <a href="/">Frey</a> $Frey::VERSION $revision
335                          $status_line                          $status_line
336                          $right                          $right
337                  </div>                  </div>
# Line 255  Create HTML editor link with optional li Line 355  Create HTML editor link with optional li
355  sub editor {  sub editor {
356          my ( $self, $class, $line, $title ) = @_;          my ( $self, $class, $line, $title ) = @_;
357          confess "need class" unless $class;          confess "need class" unless $class;
358          $line ||= 1;          if ( ! defined $title ) {
359                    $title  = "edit $class";
360                    $title .= " line $line" if $line;
361            }
362            $line  ||= 1;
363          qq|<a target="editor" href="/editor+$class+$line"| .          qq|<a target="editor" href="/editor+$class+$line"| .
364          ( $title ? qq| title="$title"| : '' ) .          ( $title ? qq| title="$title"| : '' ) .
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
382    
383            # 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 ")([\w:]+)(")}          $error =~ s{(via (?:package)\s+"?)([\w:]+)("?)}
388                  {$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
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
394            # FIXME replace with link to Frey::Introspect data
395            $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            $error =~ s{(class ")([\w:]+)(")}
403                    {$1<a target="$2" href="/$2" title="introspect $2">$2</a>$3}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
416    
417    This method will return error to browser and backtrace unless
418    error message ends with LF C<\n> just like L<warn>
419    
420    =cut
421    
422  sub error {  sub error {
423          my $self = shift;          my $self = shift;
424          my $error = join(" ", @_);          my $error = join(" ", @_);
425    
426          my @backtrace = $self->backtrace;          my $fatal = '';
427          $error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace;          my $backtrace = '';
428    
429            if ( $error !~ m{\n$} ) {
430                    if ( my @backtrace = $self->backtrace ) {
431                            $backtrace =
432                                      "\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            $self->add_icon('error');
441            $error = $self->html_links( $error );
442            return qq|<pre class="frey-error$fatal">$error $backtrace</pre>| ;
443    }
444    
445    =head1 Status line
446    
447    =head2 add_status
448    
449      $self->add_status( { name => { some => 'data' } } );
450    
451      $self->add_status( "append to last status popup" );
452    
453    =cut
454    
455    sub add_status {
456            my ( $self, $data ) = @_;
457            die "no data" unless $data;
458            if ( ref $data  ) {
459                    push @status, $data;
460            } else {
461                    if ( defined $status[ $#status ] ) {
462                            $status[ $#status ]->{ '+' } = $data;
463                    } else {
464                            push @status, { '+' => $data };
465                    }
466            }
467    }
468    
469    =head2 clean_status
470    
471    Called at beginning of each request
472    
473      $self->clean_status;
474    
475    =cut
476    
477    sub clean_status {
478            my ($self) = shift;
479            @head = ( 'static/frey.css' );
480            @status = (
481                    { 'ClassBrowser' => Frey::Class::Browser->new( usage_sort => 1, usage_on_top => 0 )->as_markup },
482                    { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },
483                    { 'INC' => Frey::INC->new->as_markup },
484            );
485            $icon_html = '';
486    }
487    
488    =head2 status_parts
489    
490    Dump all status line parts
491    
492      $self->status_parts
493    
494    =cut
495    
496    sub status_parts {
497            warn "## status parts ", dump( map { keys %$_ } @status );
498    }
499    
500    =for debug
501    
502    sub DEMOLISH {
503            my ( $self ) = @_;
504            warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status;
505    }
506    
507    =cut
508    
509    =head2 add_icon
510    
511      Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png
512      Frey::Foo->add_icon('warning'); # /static/icons/Frey/Foo/warning.png
513    
514    =cut
515    
516    sub icon_path {
517            my ($self,$class,$variant) = @_;
518    
519            sub icon_exists {
520                    my $class = shift;
521                    $class =~ s{::}{/}g;
522                    $class .= "/$variant" if $variant;
523                    my $icon_path = 'static/icons/' . $class . '.png';
524                    return $icon_path if -e $icon_path;
525                    return;
526            }
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;
539            }
540    
541            warn "# $class from $self icon_path $path" if $self->debug;
542            return $path;
543    }
544    
545    sub add_icon {
546            my ($self,$variant) = @_;
547    
548            my $class = $self->class if $self->can('class');
549            #$class ||= $self->title;
550            $class ||= ref($self);
551            my $icon_path = $self->icon_path( $class, $variant ) || return;
552    
553            $icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|;
554            warn "# using icon $icon_path";
555    
556    =for later
557    
558            # FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work!
559            my $ico_path = $icon_path;
560            $ico_path =~ s{png$}{ico};
561            if ( ! -e $ico_path ) {
562                    system "convert $icon_path $ico_path";
563                    warn "# convert $icon_path $ico_path : $@";
564            }
565            $icon_html .= qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path">| if -e $ico_path;
566    
567    =cut
568    
569    }
570    
571    my $warn_colors = {
572            '#'  => '#444',
573            '##' => '#888',
574    };
575    
576    my $multiline_markers = {
577            '(' => ')',
578            '{' => '}',
579            '[' => ']',
580            '"' => '"',
581    };
582    
583    =for later
584    
585    my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']';
586    warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re";
587    
588    =cut
589    
590    sub log_path {
591            $Frey::Bootstrap::log_path || die "no log_path?";
592    }
593    
594    our $pwd = `pwd`;
595    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 {
609            my ($self,$level) = shift;
610            $level ||= $self->debug,
611            my $path = $self->log_path;
612    
613            my $max = 30;
614            my $pos = 0;
615            my @warnings = ( '' x $max ); # XXX circualar buffer for 50 lines
616            my $line = $last_log_line;
617            my $multiline_end;
618    
619            # XXX do we really want to do this every time?
620            my $css = qq|/* short css classes for levels */\n|;
621            my $level_to_class;
622            foreach ( keys %$warn_colors ) {
623                    my $l = length($_);
624                    my $class = 'l' . $l;
625                    $css .= qq|.$class { color: $warn_colors->{$_} }\n|;
626                    $level_to_class->{ $_ } = $class;
627            }
628            $self->add_css( $css );
629    
630            open(my $log, '<', $path)    || die "can't open $path: $!";
631            seek($log, $last_log_pos, 0) || warn "can't seek: $!";
632            while(<$log>) {
633                    chomp;
634                    $line++;
635    
636                    next if m{^\s+(Mojo|Class::MOP|Moose)::};
637    
638                    my $style = '';
639    
640    =for filter
641    
642                    if ( $multiline_end ) {
643                            if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) {
644    #                               warn "## $line end of $multiline_end in '$_'\n";
645                                    undef $multiline_end;
646                            } else {
647    #                               warn "## $line skipped\n";
648                            }
649                    } elsif ( m{^(#*)\s+} ) {
650                            my $l = $1 ? length($1) : 0;
651                            if ( $l > $level ) {
652                                    undef $multiline_end;
653                                    $multiline_end = $multiline_markers->{$1} if m{($multiline_re)$};
654    #                               warn "## $line start $1 .. $multiline_end level $l > $level for '$_'\n" if $multiline_end;
655                                    next;
656                            }
657    
658    =cut
659                    if ( m{^(#*)} ) {
660    
661                            my $level = $1;
662                            my $msg = $self->strip_full_path( $_ );
663    
664                            my $spacer = ' ';
665                            my $real_msg = expand( $msg );
666                            if ( length($real_msg) > $self->html_dump_width ) {
667                                    
668                                    $real_msg = substr( $msg, 0, $self->html_dump_width );
669                                    $msg = unexpand( $real_msg );
670                                    $spacer = '&hellip;'
671                            }
672    
673                            $msg = $self->html_escape( $msg );
674    
675                            if ( my $class = $level_to_class->{ $level } ) {
676                                    $msg = qq|<span class="$class">$msg</span>|;
677                            }
678    
679                            #$msg .= $spacer . qq|<a target="editor" href="/editor+$path+$line" style="float: right;">$line</a>\n|;
680                            $msg = qq|<a target="editor" href="/editor+$path+$line" style="float: right;">$line</a>$msg|
681                                    . ( $spacer ? $spacer : '' )
682                                    . "\n"; # XXX <pre> needs this
683    
684                            $warnings[ $pos++ % $max ] = $msg;
685                    }
686            }
687            $last_log_pos = tell($log);
688            $last_log_line = $line;
689            warn "log has $line lines tell position $last_log_pos";
690            close($log) || die "can't close $path: $!";
691    
692            my $size = -s $path;
693    
694            my $warnings = join("",
695                    map { $warnings[ ( $pos + $_ ) % $max ] || '' } 0 .. ( $max - 1 )
696            );
697    
698            my $s = length($warnings);
699    
700          return          return
701                  qq|<pre class="frey-error">|                  # need to wrap editor link into span so we can have links in warnings
702                  . $self->editor_links( $error ) .                    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                  qq|</pre>|                  . $self->html_links( $warnings )
704                    . qq|</code></span>|
705                  ;                  ;
706  }  }
707    
708    
709    =head2 backtrace
710    
711    Show backtrace with links to editor
712    
713      my @backtrace = $self->backtrace;
714    
715    =cut
716    
717    sub backtrace {
718            my ($self) = @_;
719    
720            my @backtrace;
721            foreach ( 1 .. 5 ) { # 0 = backtrace
722                    my (
723                            $package,$path,$line
724                            # subroutine hasargs
725                            # wantarray evaltext is_require
726                            # hints bitmask hinthash
727                    ) = caller($_) or last;
728    
729                    push @backtrace,
730                            qq|via "$package" at $path line $line|;
731            }
732            #warn "# backtrace: ", dump( @backtrace ) if @backtrace;
733            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.482  
changed lines
  Added in v.961

  ViewVC Help
Powered by ViewVC 1.1.26