blib/lib/Pod/POM/View/HTML.pm

filter: Statementstmt 89.5% 145 / 162 Branchbran 56.4% 35 / 62 Conditioncond 27.2% 3 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 96.7% 30 / 31 Podpod 80.0% 20 / 25 totaltotal 78.4% 233 / 297 CC 37 SCAR 21.8
CC
37
Cov
78%
view
33.8
view_seq_link
17.7
make_href
6.6
CRAP 8.9
« blib/lib/Pod/POM.pm ↑ Summary blib/lib/Pod/POM/View/Pod.pm »
1 #============================================================= -*-Perl-*-
2 #
3 # Pod::POM::View::HTML
4 #
5 # DESCRIPTION
6 # HTML view of a Pod Object Model.
7 #
8 # AUTHOR
9 # Andy Wardley <abw@kfs.org>
10 #
11 # COPYRIGHT
12 # Copyright (C) 2000 Andy Wardley. All Rights Reserved.
13 #
14 # This module is free software; you can redistribute it and/or
15 # modify it under the same terms as Perl itself.
16 #
17 # REVISION
18 # $Id: HTML.pm 84 2009-08-20 21:07:00Z ford $
19 #
20 #========================================================================
21  
22 package Pod::POM::View::HTML;
23 5 $Pod::POM::View::HTML::VERSION = '2.01';
24 5 require 5.006;
25  
26 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 5 ▶use Pod::POM::View;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 ▶use parent qw( Pod::POM::View );
Subroutine BEGIN: called CC=1 SCAR=0.0
31 5 ▶use vars qw( $DEBUG $ERROR $AUTOLOAD );
Subroutine BEGIN: called CC=1 SCAR=0.0
32 5 ▶use Text::Wrap;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 5 ▶$DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Pod::POM::View::HTML::DEBUG 5 0 5
35 5 my $HTML_PROTECT = 0;
36 5 my @OVER;
37  
38 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
39 0 my $class = shift;
40 0 ▶ my $self = $class->SUPER::new(@_)
Condition0%0/2
$class->SUPER::new(@_) || (return)
l!l
00
MC/DC0%0/1
$class->SUPER::new(@_) || (return)
$class->SUPER::new(@_)
Truth table0%0/2
A
A= $class->SUPER::new(@_)
Aresult
11
00
41 || return;
42  
43 # initalise stack for maintaining info for nested lists
44 0 $self->{ OVER } = [];
45  
46 0 return $self;
47 }
48  
49  
50 2 ▶sub view {
Subroutine view: called CC=7 SCAR=35.2
Pod documented
51 2 my ($self, $type, $item) = @_;
52  
53 2 ▶ if ($type =~ s/^seq_//) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type =~ s/^seq_//) { } 2 0 2
elsif (UNIVERSAL::isa($item, 'HASH')) { } 0 0 0
elsif (not ref $item) { } 0 0 0
54 2 return $item;
55 }
56 elsif (UNIVERSAL::isa($item, 'HASH')) {
57 0 ▶ if (defined $item->{ content }) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $item->{'content'}) { } 0 0 0
elsif (defined $item->{'text'}) { } 0 0 0
58 0 return $item->{ content }->present($self);
59 }
60 elsif (defined $item->{ text }) {
61 0 my $text = $item->{ text };
62 0 ▶ return ref $text ? $text->present($self) : $text;
Branch: 1 branch
BranchTrueFalseTotal
ref $text ? : 0 0 0
63 }
64 else {
65 0 return '';
66 }
67 }
68 elsif (! ref $item) {
69 0 return $item;
70 }
71 else {
72 0 return '';
73 }
74 }
75  
76  
77 14 ▶sub view_pod {
Subroutine view_pod: called CC=1 SCAR=0.0
Pod documented
78 14 my ($self, $pod) = @_;
79 14 return "<html>\n<body bgcolor=\"#ffffff\">\n"
80 . $pod->content->present($self)
81 . "</body>\n</html>\n";
82 }
83  
84  
85 32 ▶sub view_head1 {
Subroutine view_head1: called CC=1 SCAR=0.0
Pod documented
86 32 my ($self, $head1) = @_;
87 32 my $title = $head1->title->present($self);
88 32 return "<h1>$title</h1>\n\n"
89 . $head1->content->present($self);
90 }
91  
92  
93 8 ▶sub view_head2 {
Subroutine view_head2: called CC=1 SCAR=0.0
Pod documented
94 8 my ($self, $head2) = @_;
95 8 my $title = $head2->title->present($self);
96 8 return "<h2>$title</h2>\n"
97 . $head2->content->present($self);
98 }
99  
100  
101 5 ▶sub view_head3 {
Subroutine view_head3: called CC=1 SCAR=0.0
Pod documented
102 5 my ($self, $head3) = @_;
103 5 my $title = $head3->title->present($self);
104 5 return "<h3>$title</h3>\n"
105 . $head3->content->present($self);
106 }
107  
108  
109 7 ▶sub view_head4 {
Subroutine view_head4: called CC=1 SCAR=0.0
Pod documented
110 7 my ($self, $head4) = @_;
111 7 my $title = $head4->title->present($self);
112 7 return "<h4>$title</h4>\n"
113 . $head4->content->present($self);
114 }
115  
116  
117 14 ▶sub view_over {
Subroutine view_over: called CC=5 SCAR=16.1
Pod documented
118 14 my ($self, $over) = @_;
119 14 my ($start, $end, $strip);
120 14 my $items = $over->item();
121  
122 14 ▶ if (@$items) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$items) { } 11 3 14
123  
124 11 my $first_title = $items->[0]->title();
125  
126 11 ▶ if ($first_title =~ /^\s*\*\s*/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($first_title =~ /^\s*\*\s*/) { } 5 6 11
elsif ($first_title =~ /^\s*\d+\.?\s*/) { } 3 3 6
127 # '=item *' => <ul>
128 5 $start = "<ul>\n";
129 5 $end = "</ul>\n";
130 5 $strip = qr/^\s*\*\s*/;
131 }
132 elsif ($first_title =~ /^\s*\d+\.?\s*/) {
133 # '=item 1.' or '=item 1 ' => <ol>
134 3 $start = "<ol>\n";
135 3 $end = "</ol>\n";
136 3 $strip = qr/^\s*\d+\.?\s*/;
137 }
138 else {
139 3 $start = "<ul>\n";
140 3 $end = "</ul>\n";
141 3 $strip = '';
142 }
143  
144 11 ▶ my $overstack = ref $self ? $self->{ OVER } : \@OVER;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 11 11
145 11 push(@$overstack, $strip);
146 11 my $content = $over->content->present($self);
147 11 pop(@$overstack);
148
149 11 return $start
150 . $content
151 . $end;
152 }
153 else {
154 3 return "<blockquote>\n"
155 . $over->content->present($self)
156 . "</blockquote>\n";
157 }
158 }
159  
160  
161 28 ▶sub view_item {
Subroutine view_item: called CC=6 SCAR=18.0
Pod documented
162 28 my ($self, $item) = @_;
163  
164 28 ▶ my $over = ref $self ? $self->{ OVER } : \@OVER;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 28 28
165 28 my $title = $item->title();
166 28 my $strip = $over->[-1];
167  
168 28 ▶ if (defined $title) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $title) 28 0 28
169 28 ▶ $title = $title->present($self) if ref $title;
Branch: 1 branch
BranchTrueFalseTotal
if ref $title 25 3 28
170 28 ▶ $title =~ s/$strip// if $strip;
Branch: 1 branch
BranchTrueFalseTotal
if $strip 20 8 28
171 28 ▶ if (length $title) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $title) 14 14 28
172 14 my $anchor = $title;
173 14 $anchor =~ s/^\s*|\s*$//g; # strip leading and closing spaces
174 14 $anchor =~ s/\W/_/g;
175 14 $title = qq{<a name="item_$anchor"></a><b>$title</b>};
176 }
177 }
178  
179 28 return '<li>'
180 . "$title\n"
181 . $item->content->present($self)
182 . "</li>\n";
183 }
184  
185  
186 1 ▶sub view_for {
Subroutine view_for: called CC=2 SCAR=7.1
Pod documented
187 1 my ($self, $for) = @_;
188 1 ▶ return '' unless $for->format() =~ /\bhtml\b/;
Branch: 1 branch
BranchTrueFalseTotal
unless $for->format =~ /\bhtml\b/ 0 1 1
189 1 return $for->text()
190 . "\n\n";
191 }
192
193  
194 4 ▶sub view_begin {
Subroutine view_begin: called CC=2 SCAR=6.9
Pod documented
195 4 my ($self, $begin) = @_;
196 4 ▶ return '' unless $begin->format() =~ /\bhtml\b/;
Branch: 1 branch
BranchTrueFalseTotal
unless $begin->format =~ /\bhtml\b/ 2 2 4
197 2 $HTML_PROTECT++;
198 2 my $output = $begin->content->present($self);
199 2 $HTML_PROTECT--;
200 2 return $output;
201 }
202
203  
204 94 ▶sub view_textblock {
Subroutine view_textblock: called CC=2 SCAR=6.9
Pod documented
205 94 my ($self, $text) = @_;
206 94 ▶ return $HTML_PROTECT ? "$text\n" : "<p>$text</p>\n";
Branch: 1 branch
BranchTrueFalseTotal
$HTML_PROTECT ? : 3 91 94
207 }
208  
209  
210 6 ▶sub view_verbatim {
Subroutine view_verbatim: called CC=2 SCAR=6.9
Pod documented
211 6 my ($self, $text) = @_;
212 6 for ($text) {
213 6 s/&/&amp;/g;
214 6 s/</&lt;/g;
215 6 s/>/&gt;/g;
216 }
217 6 return "<pre>$text</pre>\n\n";
218 }
219  
220  
221 11 ▶sub view_seq_bold {
Subroutine view_seq_bold: called CC=1 SCAR=0.0
Pod documented
222 11 my ($self, $text) = @_;
223 11 return "<b>$text</b>";
224 }
225  
226  
227 11 ▶sub view_seq_italic {
Subroutine view_seq_italic: called CC=1 SCAR=0.0
Pod documented
228 11 my ($self, $text) = @_;
229 11 return "<i>$text</i>";
230 }
231  
232  
233 10 ▶sub view_seq_code {
Subroutine view_seq_code: called CC=1 SCAR=0.0
Pod documented
234 10 my ($self, $text) = @_;
235 10 return "<code>$text</code>";
236 }
237  
238 4 ▶sub view_seq_file {
Subroutine view_seq_file: called CC=1 SCAR=0.0
Pod documented
239 4 my ($self, $text) = @_;
240 4 return "<i>$text</i>";
241 }
242  
243 4 ▶sub view_seq_space {
Subroutine view_seq_space: called CC=1 SCAR=0.0
Pod undocumented
244 4 my ($self, $text) = @_;
245 4 $text =~ s/\s/&nbsp;/g;
246 4 return $text;
247 }
248  
249  
250 14 ▶sub view_seq_entity {
Subroutine view_seq_entity: called CC=1 SCAR=0.0
Pod documented
251 14 my ($self, $entity) = @_;
252 14 return "&$entity;"
253 }
254  
255  
256 2 ▶sub view_seq_index {
Subroutine view_seq_index: called CC=1 SCAR=0.0
Pod documented
257 2 return '';
258 }
259  
260  
261 3 ▶sub view_seq_link {
Subroutine view_seq_link: called CC=12 SCAR=28.7
Pod documented
262 3 my ($self, $link) = @_;
263  
264 # view_seq_text has already taken care of L<http://example.com/>
265 3 ▶ if ($link =~ /^<a href=/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link =~ /^<a href=/) 0 3 3
266 0 return $link;
267 }
268  
269 # full-blown URL's are emitted as-is
270 3 ▶ if ($link =~ m{^\w+://}s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link =~ m[^\w+://]s) 0 3 3
271 0 return make_href($link);
272 }
273  
274 3 $link =~ s/\n/ /g; # undo line-wrapped tags
275  
276 3 my $orig_link = $link;
277 3 my $linktext;
278 # strip the sub-title and the following '|' char
279 3 ▶ if ( $link =~ s/^ ([^|]+) \| //x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link =~ s/^ ([^|]+) \| //x) 1 2 3
280 1 $linktext = $1;
281 }
282  
283 # make sure sections start with a /
284 3 $link =~ s|^"|/"|;
285  
286 3 my $page;
287 my $section;
288 3 ▶ if ($link =~ m|^ (.*?) / "? (.*?) "? $|x) { # [name]/"section"
Branch: 2 branches
BranchTrueFalseTotal
if ($link =~ m[^ (.*?) / "? (.*?) "? $]x) { } 0 3 3
elsif ($link =~ /\s/) { } 0 3 3
289 0 ($page, $section) = ($1, $2);
290 }
291 elsif ($link =~ /\s/) { # this must be a section with missing quotes
292 0 ($page, $section) = ('', $link);
293 }
294 else {
295 3 ($page, $section) = ($link, '');
296 }
297  
298 # warning; show some text.
299 3 ▶ $linktext = $orig_link unless defined $linktext;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $linktext 2 1 3
300  
301 3 my $url = '';
302 3 ▶ if (defined $page && length $page) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $page and length $page) 3 0 3
Condition33%1/3
defined $page and length $page
!ll&&!rl&&r
003
MC/DC0%0/2
defined $page and length $page
defined $page length $page
Truth table33%1/3
A and B
A= defined $pageB= length $page
ABresult
0X0
100
111
303 3 $url = $self->view_seq_link_transform_path($page);
304 }
305  
306 # append the #section if exists
307 3 ▶ $url .= "#$section" if defined $url and
Branch: 1 branch
BranchTrueFalseTotal
if defined $url and defined $section and length $section 0 3 3
Condition33%2/6
defined $url and defined $section and length $section
!ll&&!rl&&r
300
defined $url and defined $section
!ll&&!rl&&r
300
MC/DC0%0/3
defined $url and defined $section and length $section
defined $url defined $section length $section
Truth table25%1/4
A and B and C
A= defined $urlB= defined $sectionC= length $section
ABCresult
0XX0
10X0
1100
1111
308 defined $section and length $section;
309  
310 3 return make_href($url, $linktext);
311 }
312  
313  
314 # should be sub-classed if extra transformations are needed
315 #
316 # for example a sub-class may search for the given page and return a
317 # relative path to it.
318 #
319 # META: where this functionality should be documented? This module
320 # doesn't have docs section
321 #
322 3 ▶sub view_seq_link_transform_path {
Subroutine view_seq_link_transform_path: called CC=1 SCAR=0.0
Pod undocumented
323 3 my($self, $page) = @_;
324  
325 # right now the default transform doesn't check whether the link
326 # is not dead (i.e. whether there is a corresponding file.
327 # therefore we don't link L<>'s other than L<http://>
328 # subclass to change the default (and of course add validation)
329  
330 # this is the minimal transformation that will be required if enabled
331 # $page = "$page.html";
332 # $page =~ s|::|/|g;
333 #print "page $page\n";
334 3 return undef;
335 }
336  
337  
338 3 ▶sub make_href {
Subroutine make_href: called CC=4 SCAR=18.9
Pod undocumented
339 3 my($url, $title) = @_;
340  
341 3 ▶ if (!defined $url) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $url) 3 0 3
342 3 ▶ return defined $title ? "<i>$title</i>" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $title ? : 3 0 3
343 }
344  
345 0 ▶ $title = $url unless defined $title;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $title 0 0 0
346 #print "$url, $title\n";
347 0 return qq{<a href="$url">$title</a>};
348 }
349  
350  
351  
352  
353 # this code has been borrowed from Pod::Html
354 5 my $urls = '(' . join ('|',
355 qw{
356 http
357 telnet
358 mailto
359 news
360 gopher
361 file
362 wais
363 ftp
364 } ) . ')';
365 5 my $ltrs = '\w';
366 5 my $gunk = '/#~:.?+=&%@!\-';
367 5 my $punc = '.:!?\-;';
368 5 my $any = "${ltrs}${gunk}${punc}";
369  
370 295 ▶sub view_seq_text {
Subroutine view_seq_text: called CC=3 SCAR=11.0
Pod undocumented
371 295 my ($self, $text) = @_;
372  
373 295 ▶ unless ($HTML_PROTECT) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($HTML_PROTECT) 295 0 295
374 295 for ($text) {
375 295 s/&/&amp;/g;
376 295 s/</&lt;/g;
377 295 s/>/&gt;/g;
378 }
379 }
380  
381 295 $text =~ s{
382 \b # start at word boundary
383 ( # begin $1 {
384 $urls : # need resource and a colon
385 (?!:) # Ignore File::, among others.
386 [$any] +? # followed by one or more of any valid
387 # character, but be conservative and
388 # take only what you need to....
389 ) # end $1 }
390 (?= # look-ahead non-consumptive assertion
391 [$punc]* # either 0 or more punctuation followed
392 (?: # followed
393 [^$any] # by a non-url char
394 | # or
395 $ # end of the string
396 ) #
397 | # or else
398 $ # then end of the string
399 )
400 }{<a href="$1">$1</a>}igox;
401  
402 295 return $text;
403 }
404  
405 113 ▶sub encode {
Subroutine encode: called CC=1 SCAR=0.0
Pod undocumented
406 113 my($self,$text) = @_;
407 113 require Encode;
408 113 return Encode::encode("ascii",$text,Encode::FB_XMLCREF());
409 }
410  
411 5 1;
412  
413 =head1 NAME
414  
415 Pod::POM::View::HTML - create HTML views of POM objects
416  
417 =head1 DESCRIPTION
418  
419 HTML view of a Pod Object Model.
420  
421 =head1 METHODS
422  
423 =over 4
424  
425 =item C<view($self, $type, $item)>
426  
427 =item C<view_pod($self, $pod)>
428  
429 =item C<view_head1($self, $head1)>
430  
431 =item C<view_head2($self, $head2)>
432  
433 =item C<view_head3($self, $head3)>
434  
435 =item C<view_head4($self, $head4)>
436  
437 =item C<view_over($self, $over)>
438  
439 =item C<view_item($self, $item)>
440  
441 =item C<view_for($self, $for)>
442  
443 =item C<view_begin($self, $begin)>
444  
445 =item C<view_textblock($self, $textblock)>
446  
447 =item C<view_verbatim($self, $verbatim)>
448  
449 =item C<view_meta($self, $meta)>
450  
451 =item C<view_seq_bold($self, $text)>
452  
453 Returns the text of a C<BE<lt>E<gt>> sequence enclosed in a C<E<lt>b<E<gt>> element.
454  
455 =item C<view_seq_italic($self, $text)>
456  
457 Returns the text of a C<IE<lt>E<gt>> sequence enclosed in a C<E<lt>i<E<gt>> element.
458  
459 =item C<view_seq_code($self, $text)>
460  
461 Returns the text of a C<CE<lt>E<gt>> sequence enclosed in a C<E<lt>code<E<gt>> element.
462  
463 =item C<view_seq_file($self, $text)>
464  
465 =item C<view_seq_entity($self, $text)>
466  
467 =item C<view_seq_index($self, $text)>
468  
469 Returns an empty string. Index sequences are suppressed in HTML view.
470  
471 =item C<view_seq_link($self, $text)>
472  
473 =back
474  
475 =head1 AUTHOR
476  
477 Andy Wardley E<lt>abw@kfs.orgE<gt>
478  
479 =head1 COPYRIGHT AND LICENSE
480  
481 Copyright (C) 2000 Andy Wardley. All Rights Reserved.
482  
483 This module is free software; you can redistribute it and/or
484 modify it under the same terms as Perl itself.
485  
486 =cut
« blib/lib/Pod/POM.pm ↑ Summary blib/lib/Pod/POM/View/Pod.pm »