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

Contents of /branches/zimbardo/lib/Frey/Web.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 537 - (show annotations)
Wed Nov 26 16:34:25 2008 UTC (15 years, 4 months ago) by dpavlin
Original Path: trunk/lib/Frey/Web.pm
File size: 10010 byte(s)
extract popup generation
1 package Frey::Web;
2 use Moose::Role;
3
4 with 'Frey::Backtrace';
5
6 use Frey::Types;
7
8 use Continuity::Widget::DomNode;
9 use Data::Dump qw/dump/;
10 use Carp qw/confess cluck/;
11 use File::Slurp;
12
13 use Frey::Bookmarklet;
14 use Frey::ClassBrowser;
15 use Frey::SVK;
16
17 has 'head' => (
18 is => 'rw',
19 isa => 'ArrayRef[Str]',
20 default => sub { [ 'static/frey.css' ] },
21 );
22
23 has 'request_url' => (
24 is => 'rw',
25 isa => 'Uri', coerce => 1,
26 default => '/',
27 );
28
29 has 'title' => (
30 is => 'rw',
31 isa => 'Str',
32 lazy => 1,
33 default => sub {
34 my ($self) = @_;
35 ref($self);
36 },
37 );
38
39 has 'content_type' => (
40 is => 'rw',
41 isa => 'Str',
42 default => 'text/html',
43 documentation => 'Content-type header',
44 );
45
46 has 'dump_max_bytes' => (
47 is => 'rw',
48 isa => 'Int',
49 default => 4096,
50 documentation => 'Maximum dump size sent to browser before truncation',
51 );
52
53 has 'inline_smaller_than' => (
54 is => 'rw',
55 isa => 'Int',
56 default => 10240,
57 documentation => 'Inline JavaScript and CSS to reduce round-trips',
58 );
59
60 has 'chop_warning' => (
61 is => 'rw',
62 isa => 'Int',
63 default => 80,
64 documentation => 'Crop lines longer',
65 );
66
67 my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
68 my $escape_re = join '|' => keys %escape;
69
70 sub html_escape {
71 my ( $self, $html ) = @_;
72 $html =~ s/($escape_re)/$escape{$1}/g;
73 return $html;
74 }
75
76 sub html_dump {
77 my $self = shift;
78 $self->html_escape( dump( @_ ) );
79 }
80
81 sub dom2html {
82 # warn "## dom2html ",dump( @_ );
83 return Continuity::Widget::DomNode->create( @_ )->to_string;
84 }
85
86 sub _inline_path {
87 my ( $self, $path ) = @_;
88 -s $path < $self->inline_smaller_than;
89 }
90
91 sub _head_html {
92 my $self = shift;
93 my $out = '';
94 foreach my $path ( @{ $self->head } ) {
95 $path =~ s!^/!!;
96 if ( $path =~ m/\.js$/ ) {
97 $out .= $self->_inline_path( $path ) ?
98 qq|<!-- $path --><script type="text/javascript">\n| . read_file($path) . qq|\n</script>| :
99 qq|<script type="text/javascript" src="/$path"></script>|;
100 } elsif ( $path =~ m/\.css$/ ) {
101 $out .= $self->_inline_path( $path ) ?
102 qq|<!-- $path --><style type="text/css">\n| . read_file( $path ) . qq|\n</style>| :
103 qq|<link type="text/css" rel="stylesheet" href="/$path" media="screen">|;
104 } elsif ( $path =~ m{<.+>}s ) {
105 $out .= $path;
106 } else {
107 confess "don't know how to render $path";
108 }
109 $out .= "\n";
110 }
111 return $out;
112 }
113
114 =head2 add_head
115
116 $o->add_head( 'path/to/external.js' );
117
118 my $size = $o->add_head( 'path/to/external.css' );
119
120 $o->add_head( '<!-- html content -->' );
121
122 =cut
123
124 sub add_head {
125 my ( $self, $path ) = @_;
126 return if ! defined $path || $path eq '';
127 $path =~ s!^/!!;
128
129 if ( $path =~ m{<.*>}s ) {
130 push @{ $self->head }, $path;
131 } elsif ( -e $path ) {
132 if ( $path =~ m/\.(?:js|css)$/ ) {
133 push @{ $self->head }, $path;
134 } else {
135 confess "can't add_head( $path ) it's not js or css";
136 }
137 return -s $path;
138 } else {
139 confess "can't find $path: $!";
140 }
141
142 }
143
144 our $reload_counter = 0;
145
146
147 =head2 page
148
149 $self->page(
150 title => 'page title',
151 head => '<!-- optional head markup -->',
152 body => '<b>Page Body</b>',
153 );
154
155 =cut
156
157 our @status;
158 sub status { @status };
159
160 our $icon_html;
161
162 sub popup {
163 my ( $self, $name, $content, $full ) = @_;
164
165 if ( ref($content) ) {
166 $content = '<code>' . dump($content) . '</code>';
167 my $l = length($content);
168 $content = qq|<span>$l bytes</span>| if ! $full && $l > $self->dump_max_bytes;
169 } else {
170 $content = qq|<span>$content</span>|;
171 }
172
173 warn "## popup [$name] = ", length( $content ), " bytes" if $self->debug;
174 return qq|<span class="frey-popup">$name $content</span>\n|;
175 }
176
177 sub page {
178 my $self = shift;
179 my $a = {@_};
180
181 warn "## page ",dump($a);
182
183 $reload_counter++;
184
185 my $status_line = '';
186
187 unshift @status, { 'ClassBrowser' => Frey::ClassBrowser->new( usage_on_top => 0 )->as_markup };
188 # unshift @status, { 'Bookmarklets' => Frey::Bookmarklet->new->as_markup };
189
190 foreach my $part ( @status ) {
191 foreach my $name ( keys %$part ) {
192 $status_line .= $self->popup( $name, $part->{$name} );
193 }
194 }
195
196 my $url = $self->request_url;
197 $url =~ s{\?reload=\d+}{};
198
199 my $body = $a->{body};
200 $body ||= $self->as_markup if $self->can('as_markup');
201 if ( $self->content_type !~ m{html} ) {
202 warn "# return only $self body ", $self->content_type;
203 return $body
204 } elsif ( ! defined $body ) {
205 warn "# no body";
206 $body = '<!-- no body -->';
207 }
208
209 $status_line .= $self->warnings_html;
210
211 $status_line .= $self->popup( INC => { %INC }, 1 );
212
213 my ($exit,$description) = ('exit','stop server');
214 ($exit,$description) = ('restart','restart server')
215 if $ENV{FREY_RESTART}; # tune labels on exit link
216
217 my $right =
218 qq|
219 <span class="right">
220 <a title="reload $url" href="/reload$url">reload</a>
221 <a title="$description" href="/exit$url">$exit</a>
222 </span>
223 |;
224
225 my $info = Frey::SVK->info;
226 my $revision = Frey::SVK->info->{Revision} || '';
227 $revision = $1 if $info->{'Mirrored From'} =~ m{Rev\.\s+(\d+)};
228
229 $self->add_icon unless $icon_html;
230
231 my $html = join("\n",
232 qq|<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head>|,
233 $self->_head_html,
234 '<title>' . ( $self->title || $a->{title} || ref($self) ) . '</title>',
235 '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
236 ( $icon_html || '<!-- no icon -->' ),
237 ( $a->{head} || '' ),
238 qq|
239 </head><body>
240 $body
241 <div class="frey-status-line">
242 <a href="/">Frey</a> $Frey::VERSION $revision
243 $status_line
244 $right
245 </div>
246 </body></html>
247 |,
248 );
249
250 warn "## >>> page ",length($html), " bytes\n" if $self->debug;
251
252 return $html;
253 }
254
255 =head2 editor
256
257 Create HTML editor link with optional line and title
258
259 my $html = $self->editor( $class, $line, $title );
260
261 =cut
262
263 sub editor {
264 my ( $self, $class, $line, $title ) = @_;
265 confess "need class" unless $class;
266 if ( ! defined $title ) {
267 $title = "edit $class";
268 $title .= " line $line" if $line;
269 }
270 $line ||= 1;
271 qq|<a target="editor" href="/editor+$class+$line"| .
272 ( $title ? qq| title="$title"| : '' ) .
273 qq|>$class</a>|;
274 }
275
276 =head2 editor_links
277
278 Create HTML links to editor for perl error message
279
280 my $html = $self->editor_links( $error )
281
282 =cut
283
284 sub editor_links {
285 my ( $self, $error ) = @_;
286
287 $error =~ s{at\s+(\S+)\s+line\s+(\d+)}
288 {at <a target="editor" href="/editor+$1+$2">$1</a> line $2}gsm;
289
290 $error =~ s{(via package ")([\w:]+)(")}
291 {$1<a target="editor" href="/editor+$2+1">$2</a>$3}gsm;
292
293 return $error;
294 }
295
296 sub error {
297 my $self = shift;
298 my $error = join(" ", @_);
299
300 my @backtrace = $self->backtrace;
301 $error .= "\n\t" . join( "\n\t", @backtrace ) if @backtrace;
302
303 warn "ERROR: $error\n";
304 return
305 qq|<pre class="frey-error">|
306 . $self->editor_links( $error ) .
307 qq|</pre>|
308 ;
309 }
310
311 sub add_status {
312 my ( $self, $data ) = @_;
313 push @status, $data;
314 }
315
316 sub clean_status {
317 @status = ();
318 $icon_html = '';
319 }
320
321 sub status_parts {
322 warn "## status parts ", dump( map { keys %$_ } @status );
323 }
324
325 sub DEMOLISH {
326 my ( $self ) = @_;
327 warn "## $self DEMOLISH status ", $#status + 1, " elements ", dump( map { keys %$_ } @status ) if @status;
328 }
329
330 =head2 add_icon
331
332 Frey::Foo->add_icon; # /static/icons/Frey/Foo.png
333 Frey::Foo->add_icon('warning'); # /static/icons/Frey/Foo/warning.png
334
335 =cut
336
337 sub icon_path {
338 my ($self,$class,$variant) = @_;
339 my $icon = $class;
340 $icon =~ s{::}{/}g;
341 $icon .= "/$variant" if $variant;
342 my $path = 'static/icons/' . $icon . '.png';
343 if ( -e $path ) {
344 warn "# $class from $self icon_path $path";
345 return $path;
346 } else {
347 warn "TODO: add $path icon for $class";
348 return undef;
349 }
350 }
351
352 sub add_icon {
353 my ($self,$variant) = @_;
354
355 my $class = ref($self);
356 $class = $self->class if $self->can('class');
357 my $icon_path = $self->icon_path( $class, $variant ) || return;
358
359 $icon_html .= qq|<link rel="icon" type="image/png" href="/$icon_path">|;
360 warn "# using icon $icon_path";
361
362 =for later
363
364 # FIXME http://en.wikipedia.org/wiki/Favicon suggest just rel="icon" but that doesn't seem to work!
365 my $ico_path = $icon_path;
366 $ico_path =~ s{png$}{ico};
367 if ( ! -e $ico_path ) {
368 system "convert $icon_path $ico_path";
369 warn "# convert $icon_path $ico_path : $@";
370 }
371 $icon_html .= qq|<link rel="shortcut icon" type="image/x-icon" href="/$ico_path">| if -e $ico_path;
372
373 =cut
374
375 }
376
377 my $warn_colors = {
378 '#' => '#444',
379 '##' => '#888',
380 };
381
382 my $multiline_markers = {
383 '(' => ')',
384 '{' => '}',
385 '[' => ']',
386 '"' => '"',
387 };
388
389 my $multiline_re = '[\\' . join('\\', keys %$multiline_markers ) . ']';
390 warn "## multiline markers ", dump( $multiline_markers ), " -> $multiline_re";
391
392 sub log_path {
393 $Frey::Bootstrap::log_path || warn "no log_path?";
394 }
395
396 sub warnings_html {
397 my ($self,$level) = shift;
398 $level ||= $self->debug,
399 my $path = $self->log_path;
400
401 my $warnings;
402 my $line = 0;
403 my $multiline_end;
404
405 open(my $log, '<', $path) || die "can't open $path: $!";
406 while(<$log>) {
407 chomp;
408 $line++;
409
410 my $style = '';
411
412 if ( $multiline_end ) {
413 if ( m{^\Q$multiline_end\E} || m{^\s.+\Q$multiline_end\E;$} ) {
414 # warn "## $line end of $multiline_end in '$_'\n";
415 undef $multiline_end;
416 } else {
417 # warn "## $line skipped\n";
418 }
419 } elsif ( m{^(#*)\s+} ) {
420 my $l = $1 ? length($1) : 0;
421 if ( $l > $level ) {
422 undef $multiline_end;
423 $multiline_end = $multiline_markers->{$1} if m{($multiline_re)$};
424 # warn "## $line start $1 .. $multiline_end level $l > $level for '$_'\n" if $multiline_end;
425 next;
426 }
427
428 $style = $warn_colors->{$1}
429 ? ' style="color:' . $warn_colors->{$1} . '"'
430 : '';
431
432 my $msg = $self->html_escape( $_ );
433 my $spacer = ' ';
434 if ( length($msg) > $self->chop_warning ) {
435 $msg = substr( $msg, 0, $self->chop_warning );
436 $spacer = '&hellip;'
437 }
438 $msg =~ s{^\s}{&nbsp;}g;
439 $warnings .= qq|<tt$style>$msg</tt>$spacer<small><a target="editor" href="/editor+$path+$line">+$line</a></small><br/>|;
440 # FIXME <tt> should be <code> but CSS hates me
441 }
442 }
443 close($log) || die "can't close $path: $!";
444
445 return
446 qq|<span class="frey-popup"><a target="editor" href="/editor+$path+$line" title="open $path level $level">warn</a><span>|
447 . $self->editor_links( $warnings )
448 . qq|</span></span>|
449 ;
450 }
451
452 1;

  ViewVC Help
Powered by ViewVC 1.1.26