/[Frey]/trunk/lib/Frey/Web.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/Frey/Web.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 385 by dpavlin, Mon Nov 17 20:14:12 2008 UTC revision 1045 by dpavlin, Thu Mar 12 20:56:04 2009 UTC
# Line 1  Line 1 
1  package Frey::Web;  package Frey::Web;
2  use Moose::Role;  use Moose::Role;
3    
4  use Continuity::Widget::DomNode;  with 'Frey::Session';
5    
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    
11    use lib 'lib';
12    
13    use Frey::Types;
14    
15    use Frey::Bookmarklet;
16    use Frey::Class::Browser;
17    use Frey::INC;
18    
19    use Frey::SVK;
20    
21    our @head;
22    sub head { @head }
23    
24  has 'head' => (  has 'request_url' => (
25          is => 'rw',          is => 'rw',
26          isa => 'ArrayRef[Str]',          isa => 'Uri', coerce => 1,
27          default => sub { [ 'static/frey.css' ] },          required => 1,
28            default => sub {
29                    carp "undefined request_url";
30                    '/';
31            },
32  );  );
33    
34  =head2 inline_smaller_than  has 'title' => (
35            is => 'rw',
36            isa => 'Str',
37            lazy => 1,
38            default => sub {
39                    my ($self) = @_;
40                    ref($self);
41            },
42    );
43    
44  Inline JavaScript and CSS smaller than this size into page reducing  has 'content_type' => (
45  round-trips to server.          is => 'rw',
46            isa => 'Str',
47            default => 'text/html',
48            documentation => 'Content-type header',
49    );
50    
51  =cut  has 'dump_max_bytes' => (
52            is => 'rw',
53            isa => 'Int',
54            default => 4096,
55            documentation => 'maximum dump size sent to browser before truncation',
56    );
57    
58  has 'inline_smaller_than' => (  has 'inline_smaller_than' => (
59          is => 'rw',          is => 'rw',
60          isa => 'Int',          isa => 'Int',
61          default => 10240,          default => 10240,
62            documentation => 'inline JavaScript and CSS to reduce round-trips',
63  );  );
64    
65  sub dom2html {  has 'html_dump_width' => (
66  #       warn "## dom2html ",dump( @_ );          documentation => 'crop longer lines in dumps',
67          return Continuity::Widget::DomNode->create( @_ )->to_string;          is => 'rw',
68            isa => 'Int',
69    #       required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them
70            default => 250,
71    );
72    
73    has 'wrap_in_page' => (
74            documentation => 'wrap full html page with status bar around content',
75            is => 'rw',
76            isa => 'Bool',
77            default => 1,
78    );
79    
80    my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
81    my $escape_re  = join '|' => keys %escape;
82    
83    sub html_escape {
84            my ( $self, $html ) = @_;
85            return '' unless defined $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 ) {
157                            $out .= $path;
158                  } else {                  } else {
159                          confess "don't know how to render $path";                          confess "don't know how to render $path";
160                  }                  }
# Line 62  sub _head_html { Line 169  sub _head_html {
169    
170    my $size = $o->add_head( 'path/to/external.css' );    my $size = $o->add_head( 'path/to/external.css' );
171    
172      $o->add_head( '<!-- html content -->' );
173    
174  =cut  =cut
175    
176  sub add_head {  sub add_head {
# Line 69  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 ( -e $path ) {          if ( $path =~ $re_html ) {
182                    push @head, $path;
183            } 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                  }                  }
189                    return -s $path;
190          } else {          } else {
191                  confess "can't find $path: $!";                  confess "can't find $path: $!";
192          }          }
193    
194          return -s $path;  }
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 && $what ne 'js' ) {
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 95  our $reload_counter = 0; Line 255  our $reload_counter = 0;
255    
256  =cut  =cut
257    
258  use Frey::Bookmarklet;  our @status;
259  use Frey::ClassBrowser;  sub status { @status };
260    
261  sub page {  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 $html = qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|          my $status_line = '';
270          . $self->_head_html  
271          . '<title>' . ( $a->{title} || ref($self) ) . '</title>'          foreach my $part ( @status ) {
272          . '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'                  foreach my $name ( keys %$part ) {
273          . ( $a->{head} || '' )                          $status_line .= $self->popup( $name, $part->{$name} );
274          . '</head><body>'                  }
275          . ( $a->{body} || '<!-- no body -->' )          }
276          . qq|<div class="frey-status-line">  
277                  <a href="/">Frey</a> $Frey::VERSION          my $url = $self->request_url;
278                  <a href="?reload=$reload_counter"><code>| . dump( $ENV{'REQUEST_URI'} ) . qq|</code></a>          $url =~ s{\?reload=\d+}{};
279                  <span class="frey-popup">Bookmarklets<span>| . Frey::Bookmarklet->markup . qq|</span></span>  
280                  <span class="frey-popup">ClassBrowser<span>| . Frey::ClassBrowser->markup . qq|</span></span>          my $body = $a->{body};
281                  <span class="frey-popup">ENV<span><code>| . dump( %ENV ) . qq|</code></span></span>          if ( ! $body ) {
282                    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;
288                    return $body
289            } elsif ( ! defined $body ) {
290                    warn "# no body";
291                    $body = '<!-- no body -->';
292            }
293    
294            $status_line .= $self->warnings_html;
295    
296            my      ($exit,$description) = ('exit','stop server');
297                    ($exit,$description) = ('restart','restart server')
298                    if $ENV{FREY_RESTART}; # tune labels on exit link
299    
300            my $right =
301                    qq|
302                            <span class="frey-status-right">
303                            <a title="reload $url"  href="/reload$url">reload</a>
304                            <a title="$description" href="/exit$url" target="exit">$exit</a>
305                            </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            $self->add_css(qq|
324                    body {
325                            padding-bottom: 3em; /* don't overlap status line */
326                    }
327            |);
328    
329            my $html = join("\n",
330                    qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
331                    $self->_head_html,
332                    qq|<title>$title</title>|,
333                    '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
334                    ( $icon_html || '<!-- no icon -->' ),
335                    ( $a->{head} || '' ),
336                    qq|
337                    </head><body>
338                    $body
339                    <div class="frey-status-line">
340                            <a href="/">Frey</a> $Frey::VERSION $revision
341                            $status_line
342                            $right
343                  </div>                  </div>
344              </body></html>              </body></html>
345          |;                  |,
346            );
347    
348          warn "## >>> page ",length($html), " bytes\n" if $self->debug;          warn "## >>> page ",length($html), " bytes\n" if $self->debug;
349    
350          return $html;          return $html;
351  }  }
352    
353  sub error {  =head2 editor
354    
355    Create HTML editor link with optional line and title
356    
357      my $html = $self->editor( $class, $line, $title );
358    
359    =cut
360    
361    sub editor {
362            my ( $self, $class, $line, $title ) = @_;
363            confess "need class" unless $class;
364            if ( ! defined $title ) {
365                    $title  = "edit $class";
366                    $title .= " line $line" if $line;
367            }
368            $line  ||= 1;
369            qq|<a target="editor" href="/editor+$class+$line"| .
370            ( $title ? qq| title="$title"| : '' ) .
371            qq|>$class</a>|;
372    }
373    
374    =head2 html_links
375    
376    Create HTML links to editor for perl error message
377    
378      my $html = $self->html_links( $error )
379    
380    =cut
381    
382    sub html_links {
383          my ( $self, $error ) = @_;          my ( $self, $error ) = @_;
384          warn $error;  
385          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}{ <a href="/editor$1+$2" target="editor">$1</a> line $2}gsm;          $error = $self->strip_full_path( $error );
386          return qq|<pre class="frey-error">$error</pre>|;  
387    #       $error =~ s[(bless\({\s+.+?\s+},\s+)("[^"]+")(\) at)][<span class="frey-dropdown">$1<code>$2</code>$3</span>]gs; # FIXME insert bless hiding back
388    
389            # perl's backtrace
390            $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
391                    {at <a target="editor" href="/editor+$1+$2" title="vi $1 +$2">$1</a> line $2}gsm;
392    
393            $error =~ s{(via (?:package)\s+"?)([\w:]+)("?)}
394                    {$1<a target="$2" href="/$2" title="introspect $2">$2</a>$3}gsm
395            || # or anything that looks like "Class::Name"
396            $error =~ s{"(\w+(?:::\w+)+)"}
397                    {"<a target="$1" href="/$1" title="introspect $1">$1</a>"}gsm;
398    
399            # method error messages
400            # FIXME replace with link to Frey::Introspect data
401            $error =~ s{(method ")(\w+)(" via)}
402                    {$1<a target="$2" href="/Frey::Shell::Grep/as_markup?pattern=$2" title="grep $2">$2</a>$3}gsm;
403    
404            # link paths to editor
405            $error =~ s{((?:lib|t)/[\S]+)\s+(\d+\s+bytes)}
406                    {<a target="editor" href="/editor+$1+1" title="vi $1 [$2]">$1</a>}gsm;
407    
408            $error =~ s{(class ")([\w:]+)(")}
409                    {$1<a target="$2" href="/$2" title="introspect $2">$2</a>$3}gsm;
410    
411            return $error;
412    }
413    
414    sub html_self {
415            my $self = shift;
416            my $html = $self;
417            $html =~ s{([\w:]+)=}{<a target="$1" href="/$1" title="introspect $1">$1</a>=}gsm;
418            return $html;
419    }
420    
421    =head2 error
422    
423    This method will return error to browser and backtrace unless
424    error message ends with LF C<\n> just like L<warn>
425    
426    =cut
427    
428    sub error {
429            my $self = shift;
430            my $error = join(" ", @_);
431    
432            my $fatal = '';
433            my $backtrace = '';
434    
435            if ( $error !~ m{\n$} ) {
436                    if ( my @backtrace = $self->backtrace ) {
437                            $backtrace =
438                                      "\n" . $self->html_self . "->error backtrace\n\t"
439                                    . $self->html_links( join( "\n\t", @backtrace ) )
440                                    ;
441                            $fatal = qq| frey-fatal|;
442                    }
443            }
444    
445            warn "ERROR: $error\n";
446            $self->add_icon('error');
447            $error = $self->html_links( $error );
448            return qq|<pre class="frey-error$fatal">$error $backtrace</pre>| ;
449    }
450    
451    =head1 Status line
452    
453    =head2 add_status
454    
455      $self->add_status( { name => { some => 'data' } } );
456    
457      $self->add_status( "append to last status popup" );
458    
459    =cut
460    
461    sub add_status {
462            my ( $self, $data ) = @_;
463            die "no data" unless $data;
464            if ( ref $data  ) {
465                    push @status, $data;
466            } else {
467                    if ( defined $status[ $#status ] ) {
468                            $status[ $#status ]->{ '+' } = $data;
469                    } else {
470                            push @status, { '+' => $data };
471                    }
472            }
473    }
474    
475    =head2 clean_status
476    
477    Called at beginning of each request
478    
479      $self->clean_status;
480    
481    =cut
482    
483    sub clean_status {
484            my ($self) = shift;
485            @head = ( 'static/frey.css' );
486            @status = (
487                    { 'ClassBrowser' => Frey::Class::Browser->new( usage_sort => 1, usage_on_top => 0 )->as_markup },
488                    { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },
489                    { 'INC' => Frey::INC->new->as_markup },
490            );
491            $icon_html = '';
492    }
493    
494    =head2 status_parts
495    
496    Dump all status line parts
497    
498      $self->status_parts
499    
500    =cut
501    
502    sub status_parts {
503            warn "## status parts ", dump( map { keys %$_ } @status );
504    }
505    
506    =for debug
507    
508    sub DEMOLISH {
509            my ( $self ) = @_;
510            warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status;
511    }
512    
513    =cut
514    
515    =head2 add_icon
516    
517      Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png
518      Frey::Foo->add_icon('warning'); # /static/icons/Frey/Foo/warning.png
519    
520    =cut
521    
522    sub icon_path {
523            my ($self,$class,$variant) = @_;
524    
525            sub icon_exists {
526                    my $class = shift;
527                    $class =~ s{::}{/}g;
528                    $class .= "/$variant" if $variant;
529                    my $icon_path = 'static/icons/' . $class . '.png';
530                    return $icon_path if -e $icon_path;
531                    return;
532            }
533    
534            my $path = icon_exists( $class );
535            if ( ! $path ) {
536                    my $super_class = $class;
537                    while ( $super_class =~ s{::[^:]+$}{} && ! $path ) {
538                            $path = icon_exists( $super_class ) unless $super_class eq 'Frey'; # don't default on Frey icon
539                    }
540            }
541    
542            if ( ! $path ) {
543                    $self->TODO( "add icon for $class" . ( $variant ? " variant $variant" : '' ) );
544                    return undef;
545            }
546    
547            warn "# $class from $self icon_path $path" if $self->debug;
548            return $path;
549    }
550    
551    sub add_icon {
552            my ($self,$variant) = @_;
553    
554            my $class = $self->class if $self->can('class');
555            #$class ||= $self->title;
556            $class ||= ref($self);
557            my $icon_path = $self->icon_path( $class, $variant ) || return;
558    
559            $icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|;
560            warn "# using icon $icon_path";
561    
562    =for later
563    
564            # FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work!
565            my $ico_path = $icon_path;
566            $ico_path =~ s{png$}{ico};
567            if ( ! -e $ico_path ) {
568                    system "convert $icon_path $ico_path";
569                    warn "# convert $icon_path $ico_path : $@";
570            }
571            $icon_html .= qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path">| if -e $ico_path;
572    
573    =cut
574    
575    }
576    
577    my $warn_colors = {
578            '#'  => '#444',
579            '##' => '#888',
580    };
581    
582    my $multiline_markers = {
583            '(' => ')',
584            '{' => '}',
585            '[' => ']',
586            '"' => '"',
587    };
588    
589    =for later
590    
591    my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']';
592    warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re";
593    
594    =cut
595    
596    sub log_path {
597            $Frey::Bootstrap::log_path || die "no log_path?";
598    }
599    
600    our $pwd = `pwd`;
601    chomp $pwd;
602    
603    sub strip_full_path {
604            my ($self, $msg) = @_;
605            # Mojo seems to expand warn messages to full path which is annoying
606            $msg =~ s{/[^/]+/\.\./}{/}gs;
607            $msg =~ s{$pwd/*}{}gs;
608            return $msg;
609    }
610    
611    our $last_log_pos  = 0;
612    our $last_log_line = 0;
613    
614    sub warnings_html {
615            my ($self,$level) = shift;
616            $level ||= $self->debug,
617            my $path = $self->log_path;
618    
619            my $max = 30;
620            my $pos = 0;
621            my @warnings = ( '' x $max ); # XXX circualar buffer for 50 lines
622            my $line = $last_log_line;
623            my $multiline_end;
624    
625            # XXX do we really want to do this every time?
626            my $css = qq|/* short css classes for levels */\n|;
627            my $level_to_class;
628            foreach ( keys %$warn_colors ) {
629                    my $l = length($_);
630                    my $class = 'l' . $l;
631                    $css .= qq|.$class { color: $warn_colors->{$_} }\n|;
632                    $level_to_class->{ $_ } = $class;
633            }
634            $self->add_css( $css );
635    
636            open(my $log, '<', $path)    || die "can't open $path: $!";
637            seek($log, $last_log_pos, 0) || warn "can't seek: $!";
638            while(<$log>) {
639                    chomp;
640                    $line++;
641    
642                    next if m{^\s+(Mojo|Class::MOP|Moose)::};
643    
644                    my $style = '';
645    
646    =for filter
647    
648                    if ( $multiline_end ) {
649                            if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) {
650    #                               warn "## $line end of $multiline_end in '$_'\n";
651                                    undef $multiline_end;
652                            } else {
653    #                               warn "## $line skipped\n";
654                            }
655                    } elsif ( m{^(#*)\s+} ) {
656                            my $l = $1 ? length($1) : 0;
657                            if ( $l > $level ) {
658                                    undef $multiline_end;
659                                    $multiline_end = $multiline_markers->{$1} if m{($multiline_re)$};
660    #                               warn "## $line start $1 .. $multiline_end level $l > $level for '$_'\n" if $multiline_end;
661                                    next;
662                            }
663    
664    =cut
665                    if ( m{^(#*)} ) {
666    
667                            my $level = $1;
668                            my $msg = $self->strip_full_path( $_ );
669    
670                            my $spacer = ' ';
671                            my $real_msg = expand( $msg );
672                            if ( length($real_msg) > $self->html_dump_width ) {
673                                    
674                                    $real_msg = substr( $msg, 0, $self->html_dump_width );
675                                    $msg = unexpand( $real_msg );
676                                    $spacer = '&hellip;'
677                            }
678    
679                            $msg = $self->html_escape( $msg );
680    
681                            if ( my $class = $level_to_class->{ $level } ) {
682                                    $msg = qq|<span class="$class">$msg</span>|;
683                            }
684    
685                            #$msg .= $spacer . qq|<a target="editor" href="/editor+$path+$line" style="float: right;">$line</a>\n|;
686                            $msg = qq|<a target="editor" href="/editor+$path+$line" style="float: right;">$line</a>$msg|
687                                    . ( $spacer ? $spacer : '' )
688                                    . "\n"; # XXX <pre> needs this
689    
690                            $warnings[ $pos++ % $max ] = $msg;
691                    }
692            }
693            $last_log_pos = tell($log);
694            $last_log_line = $line;
695            warn "log has $line lines tell position $last_log_pos";
696            close($log) || die "can't close $path: $!";
697    
698            my $size = -s $path;
699    
700            my $warnings = join("",
701                    map { $warnings[ ( $pos + $_ ) % $max ] || '' } 0 .. ( $max - 1 )
702            );
703    
704            my $s = length($warnings);
705    
706            return
707                    # need to wrap editor link into span so we can have links in warnings
708                      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>|
709                    . $self->html_links( $warnings )
710                    . qq|</code></span>|
711                    ;
712    }
713    
714    
715    =head2 backtrace
716    
717    Show backtrace with links to editor
718    
719      my @backtrace = $self->backtrace;
720    
721    =cut
722    
723    sub backtrace {
724            my ($self) = @_;
725    
726            my @backtrace;
727            foreach ( 1 .. 5 ) { # 0 = backtrace
728                    my (
729                            $package,$path,$line
730                            # subroutine hasargs
731                            # wantarray evaltext is_require
732                            # hints bitmask hinthash
733                    ) = caller($_) or last;
734    
735                    push @backtrace,
736                            qq|via "$package" at $path line $line|;
737            }
738            #warn "# backtrace: ", dump( @backtrace ) if @backtrace;
739            return @backtrace;
740    }
741    
742    =head2 checkbox
743    
744    Generate checkbox html markup from some attribute
745    
746      my $html = $self->checkbox('attribute_name', $value);
747    
748    =cut
749    
750    sub checkbox {
751            my ($self,$name,$value) = @_;
752            my $checked = '';
753            my $all_checkboxes = eval { $self->$name };
754            warn "ERROR tried to get checkbox value for '$name' which is unknown: $@" if $@;
755            $all_checkboxes = [ $all_checkboxes ] unless ref($all_checkboxes) eq 'ARRAY'; # sigh, too chatty
756            $checked = ' checked' if grep { defined $_ && $_ eq $value } @$all_checkboxes;
757            warn "# checkbox $name $value $checked\t", $self->dump( $self->$name );
758            qq|<input name="$name" value="$value" type="checkbox"$checked>|;
759    }
760    
761    =head2 strip
762    
763    Strip whitespace around content
764    
765      my $stripped = strip('  no more whitespace around this   ');
766    
767    =cut
768    
769    sub strip {
770            my $t = shift;
771            $t =~ s{^\s+}{}gs;
772            $t =~ s{>\s+<}{><}gs;
773            $t =~ s{\s+$}{}gs;
774            return $t;
775  }  }
776    
777  1;  1;

Legend:
Removed from v.385  
changed lines
  Added in v.1045

  ViewVC Help
Powered by ViewVC 1.1.26