/[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 460 by dpavlin, Wed Nov 19 17:57:48 2008 UTC revision 1124 by dpavlin, Tue Jun 30 13:58:38 2009 UTC
# Line 1  Line 1 
1  package Frey::Web;  package Frey::Web;
2  use Moose::Role;  use Moose::Role;
3    
4  use Frey::Types;  with 'Frey::Session', 'Frey::Class::Icon';
5    
 use Continuity::Widget::DomNode;  
6  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
7  use Carp qw/confess/;  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  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 { [  
                 { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },  
                 { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->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 47  has 'content_type' => ( Line 46  has 'content_type' => (
46          is => 'rw',          is => 'rw',
47          isa => 'Str',          isa => 'Str',
48          default => 'text/html',          default => 'text/html',
49            documentation => 'Content-type header',
50  );  );
51    
52  =head2 inline_smaller_than  has 'dump_max_bytes' => (
53            is => 'rw',
54  Inline JavaScript and CSS smaller than this size into page reducing          isa => 'Int',
55  round-trips to server.          default => 4096,
56            documentation => 'maximum dump size sent to browser before truncation',
57  =cut  );
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',
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            return '' unless defined $html;
87            $html =~ s/($escape_re)/$escape{$1}/g;
88            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 {
100            my ( $self, $dump ) = @_;
101            $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 popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }
111    sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }
112    
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 110  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 125  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 138  our $reload_counter = 0; Line 259  our $reload_counter = 0;
259    
260  =cut  =cut
261    
262  sub page {  our @status;
263    sub status { @status };
264    
265    sub html_page {
266          my $self = shift;          my $self = shift;
267          my $a = {@_};          my $a = {@_};
268    
269          $reload_counter++;          $reload_counter++;
270    
271          my $status_line = '';          my $status_line = '';
272          foreach my $part ( @{ $self->status } ) {  
273                  if ( ref($part) ne 'HASH' ) {          foreach my $part ( @status ) {
                         warn "part not hash ",dump( $part ) ;  
                         #$self->status( $part );  
                         next;  
                 }  
274                  foreach my $name ( keys %$part ) {                  foreach my $name ( keys %$part ) {
275                          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 > 1024;  
                         } 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|;  
276                  }                  }
277          }          }
278    
# Line 169  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 178  sub page { Line 293  sub page {
293                  $body = '<!-- no body -->';                  $body = '<!-- no body -->';
294          }          }
295    
296            $status_line .= $self->warnings_html;
297    
298            my      ($exit,$description) = ('exit','stop server');
299                    ($exit,$description) = ('restart','restart server')
300                    if $ENV{FREY_RESTART}; # tune labels on exit link
301    
302            my $right =
303                    qq|
304                            <span class="frey-status-right">
305                            <a title="reload $url"  href="/reload$url">reload</a>
306                            <a title="$description" href="/exit$url" target="exit">$exit</a>
307                            </span>
308                    |;
309    
310            my $svk = Frey::SVK->new;
311            my $info = $svk->info;
312            my $revision = $svk->info->{Revision} || '';
313            $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};
314    
315            $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                    ( $self->icon_html ),
337                  ( $a->{head} || '' ),                  ( $a->{head} || '' ),
338                  qq|                  qq|
339                  </head><body>                  </head><body>
340                  $body                  $body
341                  <div class="frey-status-line">                  <div class="frey-status-line">
342                          <a href="/">Frey</a> $Frey::VERSION                          <a href="/">Frey</a> $Frey::VERSION $revision
                         <a href="?reload=$reload_counter"><code>$url</code></a>  
343                          $status_line                          $status_line
344                            $right
345                  </div>                  </div>
346              </body></html>              </body></html>
347                  |,                  |,
# Line 201  sub page { Line 352  sub page {
352          return $html;          return $html;
353  }  }
354    
355    =head2 editor
356    
357    Create HTML editor link with optional line and title
358    
359      my $html = $self->editor( $class, $line, $title );
360    
361    =cut
362    
363    sub editor {
364            my ( $self, $class, $line, $title ) = @_;
365            confess "need class" unless $class;
366            if ( ! defined $title ) {
367                    $title  = "edit $class";
368                    $title .= " line $line" if $line;
369            }
370            $line  ||= 1;
371            qq|<a target="editor" href="/editor+$class+$line"| .
372            ( $title ? qq| title="$title"| : '' ) .
373            qq|>$class</a>|;
374    }
375    
376    =head2 html_links
377    
378    Create HTML links to editor for perl error message
379    
380      my $html = $self->html_links( $error )
381    
382    =cut
383    
384    sub html_links {
385            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+)}
393                    {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{(class ")([\w:]+)(")}
411                    {$1<a target="$2" href="/$2" title="introspect $2">$2</a>$3}gsm;
412    
413            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;          my $fatal = '';
435          foreach ( 0 .. 5 ) {          my $backtrace = '';
436                  my @caller = caller($_) or last;  
437                  my @description = ( qw/          if ( $error !~ m{\n$} ) {
438                          package filename line                  if ( my @backtrace = $self->backtrace ) {
439                          subroutine hasargs                          $backtrace =
440                          wantarray evaltext is_require                                    "\n" . $self->html_self . "->error backtrace\n\t"
441                          hints bitmask hinthash                                  . $self->html_links( join( "\n\t", @backtrace ) )
442                  /);                                  ;
443                  push @backtrace, join(' ',                          $fatal = qq| frey-fatal|;
444                          map {                  }
                                 $description[$_] . ': ' . dump $caller[$_]  
                         } ( 0 .. $#caller )  
                 );  
         }  
         if ( @backtrace ) {  
                 warn "# append backtrace: ", dump( @backtrace );  
                 $error .= "\n\t" . join( "\n\t", @backtrace );  
445          }          }
446    
447          warn "ERROR: $error\n";          warn "ERROR: $error\n";
448          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}{at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;          $self->add_icon('error');
449          $error =~ s{(via package ")([\w:]+)(")}{$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;          $error = $self->html_links( $error );
450          return qq|<pre class="frey-error">$error</pre>|;          return qq|<pre class="frey-error$fatal">$error $backtrace</pre>| ;
451    }
452    
453    =head1 Status line
454    
455    =head2 add_status
456    
457      $self->add_status( { name => { some => 'data' } } );
458    
459      $self->add_status( "append to last status popup" );
460    
461    =cut
462    
463    sub add_status {
464            my ( $self, $data ) = @_;
465            die "no data" unless $data;
466            if ( ref $data  ) {
467                    push @status, $data;
468            } else {
469                    if ( defined $status[ $#status ] ) {
470                            $status[ $#status ]->{ '+' } = $data;
471                    } else {
472                            push @status, { '+' => $data };
473                    }
474            }
475    }
476    
477    =head2 clean_status
478    
479    Called at beginning of each request
480    
481      $self->clean_status;
482    
483    =cut
484    
485    sub clean_status {
486            my ($self) = shift;
487            warn "## clean_status";
488            @head = ( 'static/frey.css' );
489            @status = (
490                    { 'ClassBrowser' => Frey::Class::Browser->new( usage_sort => 1, usage_on_top => 0 )->as_markup },
491                    { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },
492                    { 'INC' => Frey::INC->new->as_markup },
493            );
494    }
495    
496    =head2 status_parts
497    
498    Dump all status line parts
499    
500      $self->status_parts
501    
502    =cut
503    
504    sub status_parts {
505            warn "## status parts ", dump( map { keys %$_ } @status );
506    }
507    
508    =for debug
509    
510    sub DEMOLISH {
511            my ( $self ) = @_;
512            warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status;
513    }
514    
515    =cut
516    
517    my $warn_colors = {
518            '#'  => '#444',
519            '##' => '#888',
520    };
521    
522    my $multiline_markers = {
523            '(' => ')',
524            '{' => '}',
525            '[' => ']',
526            '"' => '"',
527    };
528    
529    =for later
530    
531    my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']';
532    warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re";
533    
534    =cut
535    
536    sub log_path {
537            $Frey::Bootstrap::log_path || die "no log_path?";
538    }
539    
540    our $pwd = `pwd`;
541    chomp $pwd;
542    
543    sub strip_full_path {
544            my ($self, $msg) = @_;
545            # Mojo seems to expand warn messages to full path which is annoying
546            $msg =~ s{/[^/]+/\.\./}{/}gs;
547            $msg =~ s{$pwd/*}{}gs;
548            return $msg;
549    }
550    
551    our $last_log_pos  = 0;
552    our $last_log_line = 0;
553    
554    sub warnings_html {
555            my ($self,$level) = shift;
556            $level ||= $self->debug,
557            my $path = $self->log_path;
558    
559            my $max = 30;
560            my $pos = 0;
561            my @warnings = ( '' x $max ); # XXX circualar buffer for 50 lines
562            my $line = $last_log_line;
563            my $multiline_end;
564    
565            # XXX do we really want to do this every time?
566            my $css = qq|/* short css classes for levels */\n|;
567            my $level_to_class;
568            foreach ( keys %$warn_colors ) {
569                    my $l = length($_);
570                    my $class = 'l' . $l;
571                    $css .= qq|.$class { color: $warn_colors->{$_} }\n|;
572                    $level_to_class->{ $_ } = $class;
573            }
574            $self->add_css( $css );
575    
576            open(my $log, '<', $path)    || die "can't open $path: $!";
577            seek($log, $last_log_pos, 0) || warn "can't seek: $!";
578            while(<$log>) {
579                    chomp;
580                    $line++;
581    
582                    next if m{^\s+(Mojo|Class::MOP|Moose)::};
583    
584                    my $style = '';
585    
586    =for filter
587    
588                    if ( $multiline_end ) {
589                            if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) {
590    #                               warn "## $line end of $multiline_end in '$_'\n";
591                                    undef $multiline_end;
592                            } else {
593    #                               warn "## $line skipped\n";
594                            }
595                    } elsif ( m{^(#*)\s+} ) {
596                            my $l = $1 ? length($1) : 0;
597                            if ( $l > $level ) {
598                                    undef $multiline_end;
599                                    $multiline_end = $multiline_markers->{$1} if m{($multiline_re)$};
600    #                               warn "## $line start $1 .. $multiline_end level $l > $level for '$_'\n" if $multiline_end;
601                                    next;
602                            }
603    
604    =cut
605                    if ( m{^(#*)} ) {
606    
607                            my $level = $1;
608                            my $msg = $self->strip_full_path( $_ );
609    
610                            my $spacer = ' ';
611                            my $real_msg = expand( $msg );
612                            if ( length($real_msg) > $self->html_dump_width ) {
613                                    
614                                    $real_msg = substr( $msg, 0, $self->html_dump_width );
615                                    $msg = unexpand( $real_msg );
616                                    $spacer = '&hellip;'
617                            }
618    
619                            $msg = $self->html_escape( $msg );
620    
621                            if ( my $class = $level_to_class->{ $level } ) {
622                                    $msg = qq|<span class="$class">$msg</span>|;
623                            }
624    
625                            #$msg .= $spacer . qq|<a target="editor" href="/editor+$path+$line" style="float: right;">$line</a>\n|;
626                            $msg = qq|<a target="editor" href="/editor+$path+$line" style="float: right;">$line</a>$msg|
627                                    . ( $spacer ? $spacer : '' )
628                                    . "\n"; # XXX <pre> needs this
629    
630                            $warnings[ $pos++ % $max ] = $msg;
631                    }
632            }
633            $last_log_pos = tell($log);
634            $last_log_line = $line;
635            warn "log has $line lines tell position $last_log_pos";
636            close($log) || die "can't close $path: $!";
637    
638            my $size = -s $path;
639    
640            my $warnings = join("",
641                    map { $warnings[ ( $pos + $_ ) % $max ] || '' } 0 .. ( $max - 1 )
642            );
643    
644            my $s = length($warnings);
645    
646            return
647                    # need to wrap editor link into span so we can have links in warnings
648                      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>|
649                    . $self->html_links( $warnings )
650                    . qq|</code></span>|
651                    ;
652    }
653    
654    
655    =head2 backtrace
656    
657    Show backtrace with links to editor
658    
659      my @backtrace = $self->backtrace;
660    
661    =cut
662    
663    sub backtrace {
664            my ($self) = @_;
665    
666            my @backtrace;
667            foreach ( 1 .. 5 ) { # 0 = backtrace
668                    my (
669                            $package,$path,$line
670                            # subroutine hasargs
671                            # wantarray evaltext is_require
672                            # hints bitmask hinthash
673                    ) = caller($_) or last;
674    
675                    push @backtrace,
676                            qq|via "$package" at $path line $line|;
677            }
678            #warn "# backtrace: ", dump( @backtrace ) if @backtrace;
679            return @backtrace;
680    }
681    
682    =head2 checkbox
683    
684    Generate checkbox html markup from some attribute
685    
686      my $html = $self->checkbox('attribute_name', $value);
687    
688    =cut
689    
690    sub checkbox {
691            my ($self,$name,$value) = @_;
692            my $checked = '';
693            my $all_checkboxes = eval { $self->$name };
694            warn "ERROR tried to get checkbox value for '$name' which is unknown: $@" if $@;
695            $all_checkboxes = [ $all_checkboxes ] unless ref($all_checkboxes) eq 'ARRAY'; # sigh, too chatty
696            $checked = ' checked' if grep { defined $_ && $_ eq $value } @$all_checkboxes;
697            warn "# checkbox $name $value $checked\t", $self->dump( $self->$name );
698            qq|<input name="$name" value="$value" type="checkbox"$checked>|;
699    }
700    
701    =head2 strip
702    
703    Strip whitespace around content
704    
705      my $stripped = strip('  no more whitespace around this   ');
706    
707    =cut
708    
709    sub strip {
710            my $t = shift;
711            $t =~ s{^\s+}{}gs;
712            $t =~ s{>\s+<}{><}gs;
713            $t =~ s{\s+$}{}gs;
714            return $t;
715  }  }
716    
717  1;  1;

Legend:
Removed from v.460  
changed lines
  Added in v.1124

  ViewVC Help
Powered by ViewVC 1.1.26