/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1194 - (hide annotations)
Wed Nov 4 21:49:05 2009 UTC (13 years, 3 months ago) by dpavlin
File size: 16615 byte(s)
suggest loading of Frey::Bootstrap
1 dpavlin 100 package Frey::Web;
2     use Moose::Role;
3    
4 dpavlin 1124 with 'Frey::Session', 'Frey::Class::Icon';
5 dpavlin 564
6 dpavlin 100 use Data::Dump qw/dump/;
7 dpavlin 653 use Carp qw/confess cluck carp/;
8 dpavlin 161 use File::Slurp;
9 dpavlin 816 use Text::Tabs; # expand, unexpand
10 dpavlin 1064 use Digest::MD5 qw/md5/;
11 dpavlin 100
12 dpavlin 816 use lib 'lib';
13    
14     use Frey::Types;
15    
16 dpavlin 410 use Frey::Bookmarklet;
17 dpavlin 793 use Frey::Class::Browser;
18 dpavlin 540 use Frey::INC;
19    
20 dpavlin 505 use Frey::SVK;
21 dpavlin 410
22 dpavlin 578 our @head;
23 dpavlin 584 sub head { @head }
24 dpavlin 121
25 dpavlin 392 has 'request_url' => (
26 dpavlin 388 is => 'rw',
27     isa => 'Uri', coerce => 1,
28 dpavlin 625 required => 1,
29 dpavlin 627 default => sub {
30 dpavlin 653 carp "undefined request_url";
31 dpavlin 627 '/';
32     },
33 dpavlin 388 );
34    
35 dpavlin 418 has 'title' => (
36     is => 'rw',
37     isa => 'Str',
38     lazy => 1,
39     default => sub {
40     my ($self) = @_;
41     ref($self);
42     },
43     );
44    
45 dpavlin 448 has 'content_type' => (
46     is => 'rw',
47     isa => 'Str',
48     default => 'text/html',
49 dpavlin 476 documentation => 'Content-type header',
50 dpavlin 448 );
51    
52 dpavlin 476 has 'dump_max_bytes' => (
53     is => 'rw',
54     isa => 'Int',
55     default => 4096,
56 dpavlin 543 documentation => 'maximum dump size sent to browser before truncation',
57 dpavlin 476 );
58    
59 dpavlin 161 has 'inline_smaller_than' => (
60     is => 'rw',
61     isa => 'Int',
62     default => 10240,
63 dpavlin 543 documentation => 'inline JavaScript and CSS to reduce round-trips',
64 dpavlin 161 );
65    
66 dpavlin 543 has 'html_dump_width' => (
67     documentation => 'crop longer lines in dumps',
68 dpavlin 535 is => 'rw',
69     isa => 'Int',
70 dpavlin 543 # required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them
71 dpavlin 634 default => 250,
72 dpavlin 535 );
73    
74 dpavlin 957 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 dpavlin 535 my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
82     my $escape_re = join '|' => keys %escape;
83    
84     sub html_escape {
85     my ( $self, $html ) = @_;
86 dpavlin 965 return '' unless defined $html;
87 dpavlin 535 $html =~ s/($escape_re)/$escape{$1}/g;
88     return $html;
89     }
90    
91 dpavlin 945 # 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 dpavlin 535 sub html_dump {
100 dpavlin 543 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 dpavlin 682 # $dump =~ $self->html_links( $dump ); # FIXME include this
107 dpavlin 543 return "<code>$dump</code>";
108 dpavlin 535 }
109    
110 dpavlin 546 sub popup { my $self = shift; $self->popup_dropdown('popup', @_); }
111     sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }
112    
113 dpavlin 859 our $re_html = qr{<(?:!--|(\w+)|[^>]+)/?>}s; # relaxed html check for one semi-valid tag
114 dpavlin 546
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 dpavlin 581 $content =~ s{<span>(<code>[^<]+</code>)</span>}{$1} && $self->TODO("code wrapped in span");
123    
124 dpavlin 591 warn "## $type [$name] = ", length( $content ), " bytes"; # if $self->debug; # FIXME
125 dpavlin 546
126     if ( $name =~ m{::} && $name !~ $re_html ) {
127     return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|;
128 dpavlin 588 } elsif ( $name =~ s{^\s*(<a)\s+}{$1 class="frey-$type"} ) {
129     return qq|$name $content\n|;
130 dpavlin 546 } else {
131     return qq|<span class="frey-$type">$name $content</span>\n|;
132     }
133     }
134    
135 dpavlin 865 sub _inline {
136 dpavlin 161 my ( $self, $path ) = @_;
137 dpavlin 881 return unless defined $path;
138 dpavlin 865 warn "# _inline $path";
139     -e $path && -s $path < $self->inline_smaller_than && -s $path;
140 dpavlin 161 }
141    
142 dpavlin 156 sub _head_html {
143     my $self = shift;
144 dpavlin 121 my $out = '';
145 dpavlin 577 foreach my $path ( @head ) {
146 dpavlin 121 $path =~ s!^/!!;
147 dpavlin 156 if ( $path =~ m/\.js$/ ) {
148 dpavlin 865 my $size;
149     $out .= $size = _inline( $path ) ?
150 dpavlin 857 qq|<script type="text/javascript">\n/* inline $path $size bytes */\n\n| . read_file($path) . qq|\n</script>| :
151 dpavlin 161 qq|<script type="text/javascript" src="/$path"></script>|;
152 dpavlin 156 } elsif ( $path =~ m/\.css$/ ) {
153 dpavlin 865 my $size;
154     $out .= $size = _inline( $path ) ?
155 dpavlin 857 qq|<style type="text/css">\n/* inline $path $size bytes */\n\n| . read_file( $path ) . qq|\n</style>| :
156 dpavlin 161 qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
157 dpavlin 446 } elsif ( $path =~ m{<.+>}s ) {
158 dpavlin 444 $out .= $path;
159 dpavlin 156 } else {
160     confess "don't know how to render $path";
161     }
162 dpavlin 163 $out .= "\n";
163 dpavlin 121 }
164     return $out;
165     }
166 dpavlin 100
167 dpavlin 154 =head2 add_head
168    
169     $o->add_head( 'path/to/external.js' );
170    
171     my $size = $o->add_head( 'path/to/external.css' );
172    
173 dpavlin 445 $o->add_head( '<!-- html content -->' );
174    
175 dpavlin 154 =cut
176    
177     sub add_head {
178     my ( $self, $path ) = @_;
179     return if ! defined $path || $path eq '';
180     $path =~ s!^/!!;
181    
182 dpavlin 546 if ( $path =~ $re_html ) {
183 dpavlin 577 push @head, $path;
184 dpavlin 444 } elsif ( -e $path ) {
185 dpavlin 156 if ( $path =~ m/\.(?:js|css)$/ ) {
186 dpavlin 577 push @head, $path;
187 dpavlin 154 } else {
188     confess "can't add_head( $path ) it's not js or css";
189     }
190 dpavlin 444 return -s $path;
191 dpavlin 154 } else {
192     confess "can't find $path: $!";
193     }
194    
195     }
196    
197 dpavlin 839 sub _add_css_js {
198     my ( $self, $what, $content ) = @_;
199 dpavlin 577
200 dpavlin 1064 my $md5 = md5( $content );
201 dpavlin 1065 return if $self->{_add_css_js_seen}->{$what}->{$md5}++;
202 dpavlin 1064
203 dpavlin 839 my $tag = $what eq 'css' ? 'style' : 'script';
204     my $type = $what eq 'css' ? 'text/css' : 'text/javascript';
205     my $head;
206    
207 dpavlin 838 my ( $package, $path, $line ) = caller(1);
208 dpavlin 813
209 dpavlin 922 $content = "/$content" if $content !~ m{[\n\r]} && -e $content;
210 dpavlin 1045 if ( $content =~ $re_html && $what ne 'js' ) {
211 dpavlin 908 $head = qq|
212     $content
213     <!-- $type via $package at $path line $line -->
214     |;
215 dpavlin 949 } elsif ( $content =~ m{^(/\w+|https?://)} && $content !~ m{[\n\r]} ) {
216 dpavlin 908 if ( $what eq 'js' ) {
217 dpavlin 839 $head = qq|
218     <$tag type="$type" src="$content">
219 dpavlin 859 /* $what via $package at $path line $line */
220 dpavlin 839 </$tag>
221     |;
222     } else {
223     $head = qq|
224     <link rel="stylesheet" type="$type" href="$content">
225 dpavlin 859 <!-- $what via $package at $path line $line -->
226 dpavlin 839 |;
227     }
228 dpavlin 813 } else {
229 dpavlin 839 $head = qq|
230 dpavlin 838 <$tag type="$type">
231 dpavlin 813 /* via $package at $path line $line */
232 dpavlin 838 $content
233     </$tag>
234 dpavlin 839 |;
235 dpavlin 813 };
236 dpavlin 839 $self->add_head( $head );
237 dpavlin 764 }
238    
239 dpavlin 838 sub add_css {
240     my ($self,$css) = @_;
241 dpavlin 839 $self->_add_css_js( 'css', $css );
242 dpavlin 838 }
243    
244     sub add_js {
245     my ($self,$js) = @_;
246 dpavlin 839 $self->_add_css_js( 'js', $js );
247 dpavlin 838 }
248    
249 dpavlin 142 our $reload_counter = 0;
250    
251 dpavlin 183
252 dpavlin 961 =head2 html_page
253 dpavlin 183
254 dpavlin 961 $self->html_page(
255 dpavlin 183 title => 'page title',
256     head => '<!-- optional head markup -->',
257     body => '<b>Page Body</b>',
258     );
259    
260     =cut
261    
262 dpavlin 519 our @status;
263     sub status { @status };
264    
265 dpavlin 961 sub html_page {
266 dpavlin 100 my $self = shift;
267 dpavlin 121 my $a = {@_};
268 dpavlin 100
269 dpavlin 142 $reload_counter++;
270    
271 dpavlin 388 my $status_line = '';
272 dpavlin 519
273     foreach my $part ( @status ) {
274 dpavlin 388 foreach my $name ( keys %$part ) {
275 dpavlin 537 $status_line .= $self->popup( $name, $part->{$name} );
276 dpavlin 388 }
277     }
278    
279 dpavlin 439 my $url = $self->request_url;
280     $url =~ s{\?reload=\d+}{};
281    
282 dpavlin 460 my $body = $a->{body};
283 dpavlin 591 if ( ! $body ) {
284     my $run = $a->{run} || 'as_markup';
285 dpavlin 611 warn "# no body, invoke $self->$run on ", ref($self);
286 dpavlin 835 $body = $self->$run;
287 dpavlin 591 }
288 dpavlin 957 if ( $self->content_type !~ m{html} || ! $self->wrap_in_page ) {
289 dpavlin 460 warn "# return only $self body ", $self->content_type;
290     return $body
291     } elsif ( ! defined $body ) {
292     warn "# no body";
293     $body = '<!-- no body -->';
294     }
295 dpavlin 448
296 dpavlin 532 $status_line .= $self->warnings_html;
297 dpavlin 482
298 dpavlin 477 my ($exit,$description) = ('exit','stop server');
299     ($exit,$description) = ('restart','restart server')
300     if $ENV{FREY_RESTART}; # tune labels on exit link
301    
302 dpavlin 473 my $right =
303     qq|
304 dpavlin 1033 <span class="frey-status-right">
305 dpavlin 519 <a title="reload $url" href="/reload$url">reload</a>
306 dpavlin 644 <a title="$description" href="/exit$url" target="exit">$exit</a>
307 dpavlin 473 </span>
308     |;
309    
310 dpavlin 653 my $svk = Frey::SVK->new;
311 dpavlin 577 my $info = $svk->info;
312     my $revision = $svk->info->{Revision} || '';
313 dpavlin 516 $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};
314 dpavlin 505
315 dpavlin 1167 $status_line = $ENV{FREY_DEV} ? qq|
316     <div class="frey-status-line">
317     <a href="/">Frey</a> $Frey::VERSION $revision
318     $status_line
319     $right
320     </div>
321     | : '';
322    
323 dpavlin 1109 $self->add_icon;
324 dpavlin 524
325 dpavlin 945 my $title = undef
326     || $a->{title}
327     || $self->title
328     || ref($self)
329     ;
330    
331     # $title =~ s{(\w)\w+::}{$1:}g; # XXX compress names of classes
332    
333 dpavlin 986 $self->add_css(qq|
334     body {
335     padding-bottom: 3em; /* don't overlap status line */
336     }
337     |);
338    
339 dpavlin 388 my $html = join("\n",
340     qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
341     $self->_head_html,
342 dpavlin 945 qq|<title>$title</title>|,
343 dpavlin 388 '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
344 dpavlin 1109 ( $self->icon_html ),
345 dpavlin 388 ( $a->{head} || '' ),
346     qq|
347 dpavlin 448 </head><body>
348     $body
349 dpavlin 1167 $status_line
350 dpavlin 210 </body></html>
351 dpavlin 388 |,
352     );
353 dpavlin 100
354 dpavlin 121 warn "## >>> page ",length($html), " bytes\n" if $self->debug;
355 dpavlin 100
356 dpavlin 121 return $html;
357 dpavlin 100 }
358    
359 dpavlin 480 =head2 editor
360    
361     Create HTML editor link with optional line and title
362    
363     my $html = $self->editor( $class, $line, $title );
364    
365     =cut
366    
367     sub editor {
368     my ( $self, $class, $line, $title ) = @_;
369     confess "need class" unless $class;
370 dpavlin 519 if ( ! defined $title ) {
371     $title = "edit $class";
372     $title .= " line $line" if $line;
373     }
374     $line ||= 1;
375 dpavlin 480 qq|<a target="editor" href="/editor+$class+$line"| .
376     ( $title ? qq| title="$title"| : '' ) .
377     qq|>$class</a>|;
378     }
379    
380 dpavlin 682 =head2 html_links
381 dpavlin 480
382     Create HTML links to editor for perl error message
383    
384 dpavlin 682 my $html = $self->html_links( $error )
385 dpavlin 480
386     =cut
387    
388 dpavlin 682 sub html_links {
389 dpavlin 468 my ( $self, $error ) = @_;
390    
391 dpavlin 682 $error = $self->strip_full_path( $error );
392    
393 dpavlin 591 # $error =~ s[(bless\({\s+.+?\s+},\s+)("[^"]+")(\) at)][<span class="frey-dropdown">$1<code>$2</code>$3</span>]gs; # FIXME insert bless hiding back
394    
395 dpavlin 671 # perl's backtrace
396 dpavlin 468 $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
397 dpavlin 677 {at <a target="editor" href="/editor+$1+$2" title="vi $1 +$2">$1</a> line $2}gsm;
398 dpavlin 468
399 dpavlin 674 $error =~ s{(via (?:package)\s+"?)([\w:]+)("?)}
400 dpavlin 677 {$1<a target="$2" href="/$2" title="introspect $2">$2</a>$3}gsm
401     || # or anything that looks like "Class::Name"
402     $error =~ s{"(\w+(?:::\w+)+)"}
403     {"<a target="$1" href="/$1" title="introspect $1">$1</a>"}gsm;
404 dpavlin 468
405 dpavlin 671 # method error messages
406 dpavlin 677 # FIXME replace with link to Frey::Introspect data
407     $error =~ s{(method ")(\w+)(" via)}
408     {$1<a target="$2" href="/Frey::Shell::Grep/as_markup?pattern=$2" title="grep $2">$2</a>$3}gsm;
409 dpavlin 671
410 dpavlin 678 # link paths to editor
411 dpavlin 682 $error =~ s{((?:lib|t)/[\S]+)\s+(\d+\s+bytes)}
412     {<a target="editor" href="/editor+$1+1" title="vi $1 [$2]">$1</a>}gsm;
413 dpavlin 671
414 dpavlin 682 $error =~ s{(class ")([\w:]+)(")}
415     {$1<a target="$2" href="/$2" title="introspect $2">$2</a>$3}gsm;
416    
417 dpavlin 468 return $error;
418     }
419    
420 dpavlin 837 sub html_self {
421     my $self = shift;
422     my $html = $self;
423     $html =~ s{([\w:]+)=}{<a target="$1" href="/$1" title="introspect $1">$1</a>=}gsm;
424     return $html;
425     }
426    
427 dpavlin 671 =head2 error
428    
429     This method will return error to browser and backtrace unless
430     error message ends with LF C<\n> just like L<warn>
431    
432     =cut
433    
434 dpavlin 350 sub error {
435 dpavlin 397 my $self = shift;
436     my $error = join(" ", @_);
437 dpavlin 460
438 dpavlin 1165 if ( $error =~ m{<error>(.+)</error>}s ) {
439     return qq|<div class="frey-error frey-fatal">$1</div>|;
440     }
441    
442 dpavlin 671 my $fatal = '';
443 dpavlin 837 my $backtrace = '';
444 dpavlin 460
445 dpavlin 671 if ( $error !~ m{\n$} ) {
446     if ( my @backtrace = $self->backtrace ) {
447 dpavlin 837 $backtrace =
448     "\n" . $self->html_self . "->error backtrace\n\t"
449     . $self->html_links( join( "\n\t", @backtrace ) )
450     ;
451 dpavlin 685 $fatal = qq| frey-fatal|;
452 dpavlin 671 }
453     }
454    
455 dpavlin 460 warn "ERROR: $error\n";
456 dpavlin 835 $self->add_icon('error');
457 dpavlin 837 $error = $self->html_links( $error );
458     return qq|<pre class="frey-error$fatal">$error $backtrace</pre>| ;
459 dpavlin 350 }
460    
461 dpavlin 577 =head1 Status line
462    
463     =head2 add_status
464    
465 dpavlin 581 $self->add_status( { name => { some => 'data' } } );
466 dpavlin 577
467     $self->add_status( "append to last status popup" );
468    
469     =cut
470    
471 dpavlin 507 sub add_status {
472     my ( $self, $data ) = @_;
473 dpavlin 939 die "no data" unless $data;
474     if ( ref $data ) {
475 dpavlin 577 push @status, $data;
476     } else {
477 dpavlin 581 if ( defined $status[ $#status ] ) {
478     $status[ $#status ]->{ '+' } = $data;
479     } else {
480     push @status, { '+' => $data };
481     }
482 dpavlin 577 }
483 dpavlin 507 }
484    
485 dpavlin 577 =head2 clean_status
486    
487     Called at beginning of each request
488    
489 dpavlin 1158 $self->setup_request;
490 dpavlin 577
491     =cut
492    
493 dpavlin 1158 sub setup_request {
494 dpavlin 577 my ($self) = shift;
495 dpavlin 1109 warn "## clean_status";
496 dpavlin 578 @head = ( 'static/frey.css' );
497 dpavlin 581 @status = (
498 dpavlin 796 { 'ClassBrowser' => Frey::Class::Browser->new( usage_sort => 1, usage_on_top => 0 )->as_markup },
499 dpavlin 653 { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },
500 dpavlin 1109 { 'INC' => Frey::INC->new->as_markup },
501 dpavlin 581 );
502 dpavlin 519 }
503    
504 dpavlin 577 =head2 status_parts
505    
506     Dump all status line parts
507    
508     $self->status_parts
509    
510     =cut
511    
512 dpavlin 519 sub status_parts {
513     warn "## status parts ", dump( map { keys %$_ } @status );
514     }
515    
516 dpavlin 568 =for debug
517    
518 dpavlin 518 sub DEMOLISH {
519     my ( $self ) = @_;
520 dpavlin 529 warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status;
521 dpavlin 518 }
522    
523 dpavlin 568 =cut
524    
525 dpavlin 532 my $warn_colors = {
526     '#' => '#444',
527     '##' => '#888',
528     };
529    
530     my $multiline_markers = {
531 dpavlin 535 '(' => ')',
532 dpavlin 532 '{' => '}',
533 dpavlin 535 '[' => ']',
534 dpavlin 532 '"' => '"',
535     };
536    
537 dpavlin 671 =for later
538    
539 dpavlin 532 my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']';
540     warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re";
541    
542 dpavlin 671 =cut
543    
544 dpavlin 532 sub log_path {
545 dpavlin 1194 $Frey::Bootstrap::log_path || die "no log_path did you load Frey::Bootstrap?";
546 dpavlin 532 }
547    
548 dpavlin 682 our $pwd = `pwd`;
549     chomp $pwd;
550    
551     sub strip_full_path {
552     my ($self, $msg) = @_;
553     # Mojo seems to expand warn messages to full path which is annoying
554     $msg =~ s{/[^/]+/\.\./}{/}gs;
555     $msg =~ s{$pwd/*}{}gs;
556     return $msg;
557     }
558    
559 dpavlin 648 our $last_log_pos = 0;
560     our $last_log_line = 0;
561    
562 dpavlin 532 sub warnings_html {
563     my ($self,$level) = shift;
564 dpavlin 535 $level ||= $self->debug,
565 dpavlin 532 my $path = $self->log_path;
566    
567 dpavlin 611 my $max = 30;
568 dpavlin 588 my $pos = 0;
569     my @warnings = ( '' x $max ); # XXX circualar buffer for 50 lines
570 dpavlin 648 my $line = $last_log_line;
571 dpavlin 532 my $multiline_end;
572    
573 dpavlin 611 # XXX do we really want to do this every time?
574     my $css = qq|/* short css classes for levels */\n|;
575     my $level_to_class;
576     foreach ( keys %$warn_colors ) {
577     my $l = length($_);
578     my $class = 'l' . $l;
579     $css .= qq|.$class { color: $warn_colors->{$_} }\n|;
580     $level_to_class->{ $_ } = $class;
581     }
582     $self->add_css( $css );
583    
584 dpavlin 648 open(my $log, '<', $path) || die "can't open $path: $!";
585     seek($log, $last_log_pos, 0) || warn "can't seek: $!";
586 dpavlin 532 while(<$log>) {
587     chomp;
588     $line++;
589    
590 dpavlin 634 next if m{^\s+(Mojo|Class::MOP|Moose)::};
591    
592 dpavlin 532 my $style = '';
593    
594 dpavlin 588 =for filter
595    
596 dpavlin 535 if ( $multiline_end ) {
597     if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) {
598 dpavlin 537 # warn "## $line end of $multiline_end in '$_'\n";
599 dpavlin 535 undef $multiline_end;
600     } else {
601 dpavlin 537 # warn "## $line skipped\n";
602 dpavlin 535 }
603     } elsif ( m{^(#*)\s+} ) {
604 dpavlin 532 my $l = $1 ? length($1) : 0;
605 dpavlin 535 if ( $l > $level ) {
606     undef $multiline_end;
607     $multiline_end = $multiline_markers->{$1} if m{($multiline_re)$};
608 dpavlin 537 # warn "## $line start $1 .. $multiline_end level $l > $level for '$_'\n" if $multiline_end;
609 dpavlin 535 next;
610     }
611 dpavlin 532
612 dpavlin 588 =cut
613 dpavlin 611 if ( m{^(#*)} ) {
614 dpavlin 588
615 dpavlin 611 my $level = $1;
616 dpavlin 682 my $msg = $self->strip_full_path( $_ );
617 dpavlin 532
618 dpavlin 535 my $spacer = ' ';
619 dpavlin 611 my $real_msg = expand( $msg );
620     if ( length($real_msg) > $self->html_dump_width ) {
621    
622     $real_msg = substr( $msg, 0, $self->html_dump_width );
623     $msg = unexpand( $real_msg );
624 dpavlin 535 $spacer = '&hellip;'
625     }
626 dpavlin 611
627     $msg = $self->html_escape( $msg );
628    
629     if ( my $class = $level_to_class->{ $level } ) {
630     $msg = qq|<span class="$class">$msg</span>|;
631     }
632    
633 dpavlin 613 #$msg .= $spacer . qq|<a target="editor" href="/editor+$path+$line" style="float: right;">$line</a>\n|;
634     $msg = qq|<a target="editor" href="/editor+$path+$line" style="float: right;">$line</a>$msg|
635     . ( $spacer ? $spacer : '' )
636     . "\n"; # XXX <pre> needs this
637 dpavlin 611
638     $warnings[ $pos++ % $max ] = $msg;
639 dpavlin 532 }
640     }
641 dpavlin 648 $last_log_pos = tell($log);
642     $last_log_line = $line;
643     warn "log has $line lines tell position $last_log_pos";
644 dpavlin 532 close($log) || die "can't close $path: $!";
645    
646 dpavlin 588 my $size = -s $path;
647    
648 dpavlin 611 my $warnings = join("",
649 dpavlin 613 map { $warnings[ ( $pos + $_ ) % $max ] || '' } 0 .. ( $max - 1 )
650 dpavlin 588 );
651    
652     my $s = length($warnings);
653    
654 dpavlin 532 return
655 dpavlin 611 # need to wrap editor link into span so we can have links in warnings
656 dpavlin 588 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>|
657 dpavlin 682 . $self->html_links( $warnings )
658 dpavlin 648 . qq|</code></span>|
659 dpavlin 532 ;
660     }
661    
662 dpavlin 543
663     =head2 backtrace
664    
665     Show backtrace with links to editor
666    
667     my @backtrace = $self->backtrace;
668    
669     =cut
670    
671     sub backtrace {
672     my ($self) = @_;
673    
674     my @backtrace;
675 dpavlin 837 foreach ( 1 .. 5 ) { # 0 = backtrace
676 dpavlin 543 my (
677     $package,$path,$line
678     # subroutine hasargs
679     # wantarray evaltext is_require
680     # hints bitmask hinthash
681     ) = caller($_) or last;
682    
683     push @backtrace,
684 dpavlin 674 qq|via "$package" at $path line $line|;
685 dpavlin 543 }
686 dpavlin 591 #warn "# backtrace: ", dump( @backtrace ) if @backtrace;
687 dpavlin 543 return @backtrace;
688     }
689    
690 dpavlin 720 =head2 checkbox
691    
692     Generate checkbox html markup from some attribute
693    
694     my $html = $self->checkbox('attribute_name', $value);
695    
696     =cut
697    
698     sub checkbox {
699     my ($self,$name,$value) = @_;
700     my $checked = '';
701 dpavlin 725 my $all_checkboxes = eval { $self->$name };
702     warn "ERROR tried to get checkbox value for '$name' which is unknown: $@" if $@;
703 dpavlin 720 $all_checkboxes = [ $all_checkboxes ] unless ref($all_checkboxes) eq 'ARRAY'; # sigh, too chatty
704 dpavlin 725 $checked = ' checked' if grep { defined $_ && $_ eq $value } @$all_checkboxes;
705 dpavlin 720 warn "# checkbox $name $value $checked\t", $self->dump( $self->$name );
706     qq|<input name="$name" value="$value" type="checkbox"$checked>|;
707     }
708    
709 dpavlin 781 =head2 strip
710    
711     Strip whitespace around content
712    
713     my $stripped = strip(' no more whitespace around this ');
714    
715     =cut
716    
717     sub strip {
718     my $t = shift;
719     $t =~ s{^\s+}{}gs;
720     $t =~ s{>\s+<}{><}gs;
721     $t =~ s{\s+$}{}gs;
722     return $t;
723     }
724    
725 dpavlin 1133 no Moose::Role;
726    
727 dpavlin 100 1;

  ViewVC Help
Powered by ViewVC 1.1.26