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

trunk/lib/Frey/Web.pm revision 588 by dpavlin, Fri Nov 28 15:07:03 2008 UTC branches/zimbardo/lib/Frey/Web.pm revision 1172 by dpavlin, Sun Jul 5 21:40:16 2009 UTC
# Line 1  Line 1 
1  package Frey::Web;  package Frey::Web;
2  use Moose::Role;  use Moose::Role;
3    
4  with 'Frey::Session';  with 'Frey::Session', 'Frey::Class::Icon';
5    
 use Frey::Types;  
   
 #use Continuity::Widget::DomNode;  
6  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
7  use Carp qw/confess cluck/;  use Carp qw/confess cluck carp/;
8  use File::Slurp;  use File::Slurp;
9    use Text::Tabs; # expand, unexpand
10    use Digest::MD5 qw/md5/;
11    
12    use lib 'lib';
13    
14    use Frey::Types;
15    
16  use Frey::Bookmarklet;  use Frey::Bookmarklet;
17  use Frey::ClassBrowser;  use Frey::Class::Browser;
18  use Frey::INC;  use Frey::INC;
19    
20  use Frey::SVK;  use Frey::SVK;
# Line 22  sub head { @head } Line 25  sub head { @head }
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 61  has 'html_dump_width' => ( Line 68  has 'html_dump_width' => (
68          is => 'rw',          is => 'rw',
69          isa => 'Int',          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  #       required => 1, # FIXME we can't have required fields with defaults because Frey::Action isn't smart enough and asks for them
71          default => 128,          default => 250,
72    );
73    
74    has 'wrap_in_page' => (
75            documentation => 'wrap full html page with status bar around content',
76            is => 'rw',
77            isa => 'Bool',
78            default => 1,
79  );  );
80    
81  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
# Line 69  my $escape_re  = join '|' => keys %escap Line 83  my $escape_re  = join '|' => keys %escap
83    
84  sub html_escape {  sub html_escape {
85          my ( $self, $html ) = @_;          my ( $self, $html ) = @_;
86            return '' unless defined $html;
87          $html =~ s/($escape_re)/$escape{$1}/g;          $html =~ s/($escape_re)/$escape{$1}/g;
88          return $html;          return $html;
89  }  }
90    
91    # from Mojo::ByteStream
92    sub url_escape {
93            my ( $self, $url, $pattern ) = @_;
94            $pattern ||= 'A-Za-z0-9\-\.\_\~';
95            $url =~ s/([^$pattern])/sprintf('%%%02X',ord($1))/ge;
96            return $url;
97    }
98    
99  sub html_dump {  sub html_dump {
100          my ( $self, $dump ) = @_;          my ( $self, $dump ) = @_;
101          $dump = dump( $dump ) if ref($dump);          $dump = dump( $dump ) if ref($dump);
# Line 80  sub html_dump { Line 103  sub html_dump {
103          $dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs;          $dump =~ s{(\n[^\n]{$width})([^\n]+?)([^\n]{5})}{\n$1...$3}gs;
104          $dump = $self->html_escape( $dump );          $dump = $self->html_escape( $dump );
105          $dump =~ s{\Q...\E}{&hellip;}gs;          $dump =~ s{\Q...\E}{&hellip;}gs;
106  #       $dump =~ $self->editor_links( $dump ); # FIXME include this  #       $dump =~ $self->html_links( $dump ); # FIXME include this
107          return "<code>$dump</code>";          return "<code>$dump</code>";
108  }  }
109    
110  sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }  sub popup    { my $self = shift; $self->popup_dropdown('popup',    @_); }
111  sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }  sub dropdown { my $self = shift; $self->popup_dropdown('dropdown', @_); }
112    
113  our $re_html = qr{<(?:!--.+?--|(\w+).+?/\1)>}s; # relaxed html check for one semi-valid tag  our $re_html = qr{<(?:!--|(\w+)|[^>]+)/?>}s; # relaxed html check for one semi-valid tag
114    
115  sub popup_dropdown {  sub popup_dropdown {
116          my ( $self, $type, $name, $content, $full ) = @_;          my ( $self, $type, $name, $content, $full ) = @_;
# Line 98  sub popup_dropdown { Line 121  sub popup_dropdown {
121    
122          $content =~ s{<span>(<code>[^<]+</code>)</span>}{$1} && $self->TODO("code wrapped in span");          $content =~ s{<span>(<code>[^<]+</code>)</span>}{$1} && $self->TODO("code wrapped in span");
123    
124          warn "## $type [$name] = ", length( $content ), " bytes" if $self->debug;          warn "## $type [$name] = ", length( $content ), " bytes"; # if $self->debug; # FIXME
125    
126          if ( $name =~ m{::} && $name !~ $re_html ) {          if ( $name =~ m{::} && $name !~ $re_html ) {
127                  return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|;                  return qq|<a class="frey-$type" target="$name" href="/$name">$name $content</a>\n|;
# Line 109  sub popup_dropdown { Line 132  sub popup_dropdown {
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 {
# Line 120  sub _head_html { Line 145  sub _head_html {
145          foreach my $path ( @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 167  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 {  sub add_css {
240          my ($self,$css) = @_;          my ($self,$css) = @_;
241          my ( $package, $path, $line ) = caller;          $self->_add_css_js( 'css', $css );
242          $self->add_head( qq|  }
243          <style type="text/css">  
244          /* via $package at $path line $line */  sub add_js {
245          $css          my ($self,$js) = @_;
246          </style>          $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 194  our $reload_counter = 0; Line 262  our $reload_counter = 0;
262  our @status;  our @status;
263  sub status { @status };  sub status { @status };
264    
265  our $icon_html;  sub html_page {
   
 sub page {  
266          my $self = shift;          my $self = shift;
267          my $a = {@_};          my $a = {@_};
268    
         warn "## page ",dump($a);  
   
269          $reload_counter++;          $reload_counter++;
270    
271          my $status_line = '';          my $status_line = '';
# Line 216  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 233  sub page { Line 301  sub page {
301    
302          my $right =          my $right =
303                  qq|                  qq|
304                          <span class="right">                          <span class="frey-status-right">
305                          <a title="reload $url"  href="/reload$url">reload</a>                          <a title="reload $url"  href="/reload$url">reload</a>
306                          <a title="$description" href="/exit$url">$exit</a>                          <a title="$description" href="/exit$url" target="exit">$exit</a>
307                          </span>                          </span>
308                  |;                  |;
309    
# Line 244  sub page { Line 312  sub page {
312          my $revision = $svk->info->{Revision} || '';          my $revision = $svk->info->{Revision} || '';
313          $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};          $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};
314    
315          $self->add_icon unless $icon_html;          $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            $self->add_icon;
324    
325            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            $self->add_css(qq|
334                    body {
335                            padding-bottom: 3em; /* don't overlap status line */
336                    }
337            |);
338    
339          my $html = join("\n",          my $html = join("\n",
340                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,                  qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
341                  $self->_head_html,                  $self->_head_html,
342                  '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',                  qq|<title>$title</title>|,
343                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',                  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
344                  ( $icon_html || '<!-- no icon -->' ),                  ( $self->icon_html ),
345                  ( $a->{head} || '' ),                  ( $a->{head} || '' ),
346                  qq|                  qq|
347                  </head><body>                  </head><body>
348                  $body                  $body
349                  <div class="frey-status-line">                  $status_line
                         <a href="/">Frey</a> $Frey::VERSION $revision  
                         $status_line  
                         $right  
                 </div>  
350              </body></html>              </body></html>
351                  |,                  |,
352          );          );
# Line 291  sub editor { Line 377  sub editor {
377          qq|>$class</a>|;          qq|>$class</a>|;
378  }  }
379    
380  =head2 editor_links  =head2 html_links
381    
382  Create HTML links to editor for perl error message  Create HTML links to editor for perl error message
383    
384    my $html = $self->editor_links( $error )    my $html = $self->html_links( $error )
385    
386  =cut  =cut
387    
388  sub editor_links {  sub html_links {
389          my ( $self, $error ) = @_;          my ( $self, $error ) = @_;
390    
391            $error = $self->strip_full_path( $error );
392    
393    #       $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            # perl's backtrace
396          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}          $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
397                  {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;
398    
399          $error =~ s{(via (?:package) "?)([\w:]+)("?)}          $error =~ s{(via (?:package)\s+"?)([\w:]+)("?)}
400                  {$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
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    
405            # method error messages
406            # 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    
410            # link paths to editor
411            $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    
414            $error =~ s{(class ")([\w:]+)(")}
415                    {$1<a target="$2" href="/$2" title="introspect $2">$2</a>$3}gsm;
416    
417          return $error;          return $error;
418  }  }
419    
420    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    =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  sub error {  sub error {
435          my $self = shift;          my $self = shift;
436          my $error = join(" ", @_);          my $error = join(" ", @_);
437    
438          my @backtrace = $self->backtrace;          if ( $error =~ m{<error>(.+)</error>}s ) {
439          $error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace;                  return qq|<div class="frey-error frey-fatal">$1</div>|;
440            }
441    
442            my $fatal = '';
443            my $backtrace = '';
444    
445            if ( $error !~ m{\n$} ) {
446                    if ( my @backtrace = $self->backtrace ) {
447                            $backtrace =
448                                      "\n" . $self->html_self . "->error backtrace\n\t"
449                                    . $self->html_links( join( "\n\t", @backtrace ) )
450                                    ;
451                            $fatal = qq| frey-fatal|;
452                    }
453            }
454    
455          warn "ERROR: $error\n";          warn "ERROR: $error\n";
456          return          $self->add_icon('error');
457                  qq|<pre class="frey-error">|          $error = $self->html_links( $error );
458                  . $self->editor_links( $error ) .          return qq|<pre class="frey-error$fatal">$error $backtrace</pre>| ;
                 qq|</pre>|  
                 ;  
459  }  }
460    
461  =head1 Status line  =head1 Status line
# Line 338  sub error { Line 470  sub error {
470    
471  sub add_status {  sub add_status {
472          my ( $self, $data ) = @_;          my ( $self, $data ) = @_;
473          push @status, { 'X' => [ $self->backtrace ] };          die "no data" unless $data;
474          if ( ref($data) ) {          if ( ref $data  ) {
475                  push @status, $data;                  push @status, $data;
476          } else {          } else {
477                  if ( defined $status[ $#status ] ) {                  if ( defined $status[ $#status ] ) {
# Line 354  sub add_status { Line 486  sub add_status {
486    
487  Called at beginning of each request  Called at beginning of each request
488    
489    $self->clean_status;    $self->setup_request;
490    
491  =cut  =cut
492    
493  sub clean_status {  sub setup_request {
494          my ($self) = shift;          my ($self) = shift;
495            warn "## clean_status";
496          @head = ( 'static/frey.css' );          @head = ( 'static/frey.css' );
497          @status = (          @status = (
498                  { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup },                  { 'ClassBrowser' => Frey::Class::Browser->new( usage_sort => 1, usage_on_top => 0 )->as_markup },
499                  { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },                  { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup },
500                  { 'INC' => Frey::INC->new->as_markup },                  { 'INC' => Frey::INC->new->as_markup },
501          );          );
         $icon_html = '';  
502  }  }
503    
504  =head2 status_parts  =head2 status_parts
# Line 390  sub DEMOLISH { Line 522  sub DEMOLISH {
522    
523  =cut  =cut
524    
 =head2 add_icon  
   
   Frey::Foo->add_icon;            # /static/icons/Frey/Foo.png  
   Frey::Foo->add_icon('warning'); # /static/icons/Frey/Foo/warning.png  
   
 =cut  
   
 sub icon_path {  
         my ($self,$class,$variant) = @_;  
         my $icon = $class;  
         $icon =~ s{::}{/}g;  
         $icon .= "/$variant" if $variant;  
         my $path = 'static/icons/' . $icon . '.png';  
         if ( -e $path ) {  
                 warn "# $class from $self icon_path $path" if $self->debug;  
                 return $path;  
         } else {  
                 $self->TODO( "add $path icon for $class" );  
                 return undef;  
         }  
 }  
   
 sub add_icon {  
         my ($self,$variant) = @_;  
   
         my $class = ref($self);  
         $class = $self->class if $self->can('class');  
         my $icon_path = $self->icon_path( $class, $variant ) || return;  
   
         $icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|;  
         warn "# using icon $icon_path";  
   
 =for later  
   
         # FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work!  
         my $ico_path = $icon_path;  
         $ico_path =~ s{png$}{ico};  
         if ( ! -e $ico_path ) {  
                 system "convert $icon_path $ico_path";  
                 warn "# convert $icon_path $ico_path : $@";  
         }  
         $icon_html .= qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path">| if -e $ico_path;  
   
 =cut  
   
 }  
   
525  my $warn_colors = {  my $warn_colors = {
526          '#'  => '#444',          '#'  => '#444',
527          '##' => '#888',          '##' => '#888',
# Line 449  my $multiline_markers = { Line 534  my $multiline_markers = {
534          '"' => '"',          '"' => '"',
535  };  };
536    
537    =for later
538    
539  my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']';  my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']';
540  warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re";  warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re";
541    
542    =cut
543    
544  sub log_path {  sub log_path {
545          $Frey::Bootstrap::log_path || warn "no log_path?";          $Frey::Bootstrap::log_path || die "no log_path?";
546  }  }
547    
548    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    our $last_log_pos  = 0;
560    our $last_log_line = 0;
561    
562  sub warnings_html {  sub warnings_html {
563          my ($self,$level) = shift;          my ($self,$level) = shift;
564          $level ||= $self->debug,          $level ||= $self->debug,
565          my $path = $self->log_path;          my $path = $self->log_path;
566    
567          my $max = 50;          my $max = 30;
568          my $pos = 0;          my $pos = 0;
569          my @warnings = ( '' x $max ); # XXX circualar buffer for 50 lines          my @warnings = ( '' x $max ); # XXX circualar buffer for 50 lines
570          my $line = 0;          my $line = $last_log_line;
571          my $multiline_end;          my $multiline_end;
572    
573          open(my $log, '<', $path) || die "can't open $path: $!";          # 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            open(my $log, '<', $path)    || die "can't open $path: $!";
585            seek($log, $last_log_pos, 0) || warn "can't seek: $!";
586          while(<$log>) {          while(<$log>) {
587                  chomp;                  chomp;
588                  $line++;                  $line++;
589    
590                    next if m{^\s+(Mojo|Class::MOP|Moose)::};
591    
592                  my $style = '';                  my $style = '';
593    
594  =for filter  =for filter
# Line 493  sub warnings_html { Line 610  sub warnings_html {
610                          }                          }
611    
612  =cut  =cut
613                  if ( m{^(#*)\s+} ) { # FIXME                  if ( m{^(#*)} ) {
614    
615                          $style = $warn_colors->{$1}                          my $level = $1;
616                                  ? ' style="color:' . $warn_colors->{$1} . '"'                          my $msg = $self->strip_full_path( $_ );
                                 : '';  
617    
                         my $msg = $self->html_escape( $_ );  
618                          my $spacer = ' ';                          my $spacer = ' ';
619                          if ( length($msg) > $self->html_dump_width ) {                          my $real_msg = expand( $msg );
620                                  $msg = substr( $msg, 0, $self->html_dump_width );                          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                                  $spacer = '&hellip;'                                  $spacer = '&hellip;'
625                          }                          }
626                          $warnings[ $pos++ % $max ]  
627                                  = $msg                          $msg = $self->html_escape( $msg );
628  #                               = ( $style ? qq|<span$style>$msg</span>| : $msg )  
629                                  . $spacer                          if ( my $class = $level_to_class->{ $level } ) {
630                                  . qq|<a target="editor" href="/editor+$path+$line" style="float: right;">+$line</a><br/>|;                                  $msg = qq|<span class="$class">$msg</span>|;
631                          # FIXME <tt> should be <code> but CSS hates me                          }
632    
633                            #$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    
638                            $warnings[ $pos++ % $max ] = $msg;
639                  }                  }
640          }          }
641            $last_log_pos = tell($log);
642            $last_log_line = $line;
643            warn "log has $line lines tell position $last_log_pos";
644          close($log) || die "can't close $path: $!";          close($log) || die "can't close $path: $!";
645    
646          my $size = -s $path;          my $size = -s $path;
647    
648          my $warnings = join('',          my $warnings = join("",
649                  map { $warnings[ ( $pos + $_ ) % $max ] || '' } 0 .. $max                  map { $warnings[ ( $pos + $_ ) % $max ] || '' } 0 .. ( $max - 1 )
650          );          );
651    
652          my $s = length($warnings);          my $s = length($warnings);
653    
654          return          return
655                  # need to wrap into span so we can have links in warnings                  # need to wrap editor link into span so we can have links in warnings
656                    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>|                    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                  . $warnings                  . $self->html_links( $warnings )
658  #               . $self->editor_links( $warnings )                  . qq|</code></span>|
                 . qq|</code></span></a>|  
659                  ;                  ;
660  }  }
661    
# Line 545  sub backtrace { Line 672  sub backtrace {
672          my ($self) = @_;          my ($self) = @_;
673    
674          my @backtrace;          my @backtrace;
675          foreach ( 0 .. 5 ) {          foreach ( 1 .. 5 ) { # 0 = backtrace
676                  my (                  my (
677                          $package,$path,$line                          $package,$path,$line
678                          # subroutine hasargs                          # subroutine hasargs
# Line 554  sub backtrace { Line 681  sub backtrace {
681                  ) = caller($_) or last;                  ) = caller($_) or last;
682    
683                  push @backtrace,                  push @backtrace,
684                          qq|via $package at $path line $line|;                          qq|via "$package" at $path line $line|;
685          }          }
686          warn "# backtrace: ", dump( @backtrace ) if @backtrace;          #warn "# backtrace: ", dump( @backtrace ) if @backtrace;
687          return @backtrace;          return @backtrace;
688  }  }
689    
690    =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            my $all_checkboxes = eval { $self->$name };
702            warn "ERROR tried to get checkbox value for '$name' which is unknown: $@" if $@;
703            $all_checkboxes = [ $all_checkboxes ] unless ref($all_checkboxes) eq 'ARRAY'; # sigh, too chatty
704            $checked = ' checked' if grep { defined $_ && $_ eq $value } @$all_checkboxes;
705            warn "# checkbox $name $value $checked\t", $self->dump( $self->$name );
706            qq|<input name="$name" value="$value" type="checkbox"$checked>|;
707    }
708    
709    =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    no Moose::Role;
726    
727  1;  1;

Legend:
Removed from v.588  
changed lines
  Added in v.1172

  ViewVC Help
Powered by ViewVC 1.1.26