File Coverage

lib/Pod/PseudoPod/HTML.pm
Criterion Covered Total %
statement 157 163 96.3
branch 37 42 88.1
condition 1 2 50.0
subroutine 75 79 94.9
pod 3 73 4.1
total 273 359 76.0


line stmt bran cond sub pod time code
1             package Pod::PseudoPod::HTML;
2 5     5   8692 use strict;
  5         13  
  5         238  
3 5     5   33 use vars qw( $VERSION );
  5         10  
  5         418  
4             $VERSION = '0.18';
5 5     5   31 use Carp ();
  5         8  
  5         112  
6 5     5   26 use base qw( Pod::PseudoPod );
  5         11  
  5         4572  
7              
8 5     5   5604 use HTML::Entities 'encode_entities';
  5         31880  
  5         12144  
9              
10             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11              
12             sub new {
13 64     64 1 51785 my $self = shift;
14 64         333 my $new = $self->SUPER::new(@_);
15 64   50     755 $new->{'output_fh'} ||= *STDOUT{IO};
16 64         345 $new->accept_targets( 'html', 'HTML' );
17 64         1491 $new->accept_targets_as_text( qw(author blockquote comment caution
18             editor epigraph example figure important listing literal note
19             production programlisting screen sidebar table tip warning) );
20              
21 64         4945 $new->nix_X_codes(1);
22 64         986 $new->nbsp_for_S(1);
23 64         765 $new->add_css_tags(0);
24 64         192 $new->add_body_tags(0);
25 64         222 $new->codes_in_verbatim(1);
26 64         602 $new->{'scratch'} = '';
27 64         176 return $new;
28             }
29              
30             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31              
32             sub handle_text {
33             # escape special characters in HTML (<, >, &, etc)
34 109 100   109 0 518 $_[0]{'scratch'} .= $_[0]{'in_verbatim'} ? encode_entities( $_[1] ) : $_[1]
35             }
36              
37 43     43 0 159 sub start_Para { $_[0]{'scratch'} .= '<p>' }
38 6     6 0 20 sub start_Verbatim { $_[0]{'scratch'} .= '<pre><code>'; $_[0]{'in_verbatim'} = 1}
  6         23  
39              
40 1     1 0 4 sub start_head0 { $_[0]{'scratch'} = '<h1>' }
41 1     1 0 5 sub start_head1 { $_[0]{'scratch'} = '<h2>' }
42 1     1 0 4 sub start_head2 { $_[0]{'scratch'} = '<h3>' }
43 1     1 0 5 sub start_head3 { $_[0]{'scratch'} = '<h4>' }
44 1     1 0 4 sub start_head4 { $_[0]{'scratch'} = '<h5>' }
45              
46 2     2 0 10 sub start_item_bullet { $_[0]{'scratch'} .= '<li>' }
47 2     2 0 9 sub start_item_number { $_[0]{'scratch'} .= "<li>$_[1]{'number'}. " }
48 2     2 0 7 sub start_item_text { $_[0]{'scratch'} .= '<li>' }
49              
50 1     1 0 4 sub start_over_bullet { $_[0]{'scratch'} .= '<ul>'; $_[0]->emit() }
  1         4  
51 1     1 0 3 sub start_over_text { $_[0]{'scratch'} .= '<ul>'; $_[0]->emit() }
  1         4  
52 0     0 0 0 sub start_over_block { $_[0]{'scratch'} .= '<ul>'; $_[0]->emit() }
  0         0  
53 1     1 0 3 sub start_over_number { $_[0]{'scratch'} .= '<ol>'; $_[0]->emit() }
  1         4  
54              
55 1     1 0 3 sub end_over_bullet { $_[0]{'scratch'} .= '</ul>'; $_[0]->emit('nowrap') }
  1         5  
56 1     1 0 3 sub end_over_text { $_[0]{'scratch'} .= '</ul>'; $_[0]->emit('nowrap') }
  1         5  
57 0     0 0 0 sub end_over_block { $_[0]{'scratch'} .= '</ul>'; $_[0]->emit('nowrap') }
  0         0  
58 1     1 0 4 sub end_over_number { $_[0]{'scratch'} .= '</ol>'; $_[0]->emit('nowrap') }
  1         3  
59              
60             # . . . . . Now the actual formatters:
61              
62 43     43 0 124 sub end_Para { $_[0]{'scratch'} .= '</p>'; $_[0]->emit() }
  43         114  
63             sub end_Verbatim {
64 6     6 0 18 $_[0]{'scratch'} .= '</code></pre>';
65 6         114 $_[0]{'in_verbatim'} = 0;
66 6         21 $_[0]->emit('nowrap');
67             }
68              
69 1     1 0 3 sub end_head0 { $_[0]{'scratch'} .= '</h1>'; $_[0]->emit() }
  1         4  
70 1     1 0 3 sub end_head1 { $_[0]{'scratch'} .= '</h2>'; $_[0]->emit() }
  1         5  
71 1     1 0 2 sub end_head2 { $_[0]{'scratch'} .= '</h3>'; $_[0]->emit() }
  1         5  
72 1     1 0 3 sub end_head3 { $_[0]{'scratch'} .= '</h4>'; $_[0]->emit() }
  1         4  
73 1     1 0 3 sub end_head4 { $_[0]{'scratch'} .= '</h5>'; $_[0]->emit() }
  1         5  
74              
75 2     2 0 5 sub end_item_bullet { $_[0]{'scratch'} .= '</li>'; $_[0]->emit() }
  2         6  
76 2     2 0 5 sub end_item_number { $_[0]{'scratch'} .= '</li>'; $_[0]->emit() }
  2         6  
77 2     2 0 6 sub end_item_text { $_[0]->emit() }
78              
79             sub start_sidebar {
80 3     3 0 7 my ($self, $flags) = @_;
81 3 50       10 $self->{'scratch'} = $self->{'css_tags'} ? '<div class="sidebar">' : '<blockquote>';
82 3 100       9 if ($flags->{'title'}) {
83 2         9 $self->{'scratch'} .= "\n<h3>" . $flags->{'title'} . "</h3>";
84             }
85 3         8 $self->emit('nowrap');
86             }
87              
88 3 50   3 0 11 sub end_sidebar { $_[0]{'scratch'} .= $_[0]->{'css_tags'} ? '</div>' : '</blockquote>'; $_[0]->emit() }
  3         10  
89              
90             sub start_figure {
91 3     3 0 5 my ($self, $flags) = @_;
92 3         7 $self->{'in_figure'} = 1;
93              
94 3 100       14 $self->{'figure_title'} = $flags->{'title'} if $flags->{'title'};
95             }
96              
97             sub end_figure {
98 3     3 0 6 my ($self, $flags) = @_;
99 3         5 $self->{'in_figure'} = 0;
100              
101 3 100       10 if ($self->{'figure_title'})
102             {
103 1         4 $self->{'scratch'} .= "<p><em>" . $self->{'figure_title'} . "</em></p>";
104 1         2 delete $self->{'figure_title'};
105             }
106              
107 3         7 $self->emit('nowrap');
108             }
109              
110             # This handles =begin and =for blocks of all kinds.
111             sub start_for {
112 18     18 0 32 my ($self, $flags) = @_;
113 18 100       77 if ($self->{'css_tags'}) {
114 2         5 $self->{'scratch'} .= '<div';
115 2 50       10 $self->{'scratch'} .= ' class="'.$flags->{'target'}.'"' if ($flags->{'target'});
116 2         4 $self->{'scratch'} .= '>';
117 2         7 $self->emit('nowrap');
118             }
119              
120             }
121             sub end_for {
122 18     18 0 30 my ($self) = @_;
123 18 100       67 if ($self->{'css_tags'}) {
124 2         4 $self->{'scratch'} .= '</div>';
125 2         6 $self->emit('nowrap');
126             }
127             }
128              
129             sub start_table {
130 7     7 0 13 my ($self, $flags) = @_;
131 7 100       19 if ($flags->{'title'}) {
132 2         9 $self->{'scratch'} .= "<p><em>Table: " . $flags->{'title'} . "</em></p>\n";
133             }
134 7         16 $self->{'scratch'} .= '<table>';
135 7         21 $self->emit('nowrap');
136             }
137              
138 7     7 0 13 sub end_table { $_[0]{'scratch'} .= '</table>'; $_[0]->emit('nowrap') }
  7         18  
139              
140 1     1 0 3 sub start_headrow { $_[0]{'in_headrow'} = 1 }
141 1     1 0 3 sub start_bodyrows { $_[0]{'in_headrow'} = 0 }
142              
143 8     8 0 25 sub start_row { $_[0]{'scratch'} .= "<tr>\n\n" }
144 8     8 0 16 sub end_row { $_[0]{'scratch'} .= '</tr>'; $_[0]->emit() }
  8         19  
145              
146 14 100   14 0 60 sub start_cell { $_[0]{'scratch'} .= $_[0]{'in_headrow'} ? '<th>' : '<td>'; }
147             sub end_cell {
148 14     14 0 17 my $self = shift;
149 14 100       37 $self->{'scratch'} .= ($self->{'in_headrow'}) ? '</th>' : '</td>';
150 14         33 $self->emit('nowrap');
151             }
152              
153             sub start_Document {
154 61     61 0 89 my ($self) = @_;
155 61 100       241 if ($self->{'body_tags'}) {
156 2         6 $self->{'scratch'} .= "<html>\n<body>";
157 2 100       7 $self->{'scratch'} .= "\n<link rel='stylesheet' href='style.css' type='text/css'>" if $self->{'css_tags'};
158 2         5 $self->emit('nowrap');
159             }
160             }
161             sub end_Document {
162 61     61 0 89 my ($self) = @_;
163 61 100       238 if ($self->{'body_tags'}) {
164 2         3 $self->{'scratch'} .= "</body>\n</html>";
165 2         6 $self->emit('nowrap');
166             }
167             }
168              
169             # Handling code tags
170 1     1 0 4 sub start_A { $_[0]{'scratch'} .= '<a href="#' }
171 1     1 0 4 sub end_A { $_[0]{'scratch'} .= '">link</a>' }
172              
173 3     3 0 12 sub start_B { $_[0]{'scratch'} .= '<strong>' }
174 3     3 0 11 sub end_B { $_[0]{'scratch'} .= '</strong>' }
175              
176 1     1 0 5 sub start_C { $_[0]{'scratch'} .= '<code>' }
177 1     1 0 5 sub end_C { $_[0]{'scratch'} .= '</code>' }
178              
179 0     0 0 0 sub start_E { $_[0]{'scratch'} .= '&' }
180 0     0 0 0 sub end_E { $_[0]{'scratch'} .= ';' }
181              
182 4 100   4 0 23 sub start_F { $_[0]{'scratch'} .= ($_[0]{'in_figure'}) ? '<img src="' : '<em>' }
183 4 100   4 0 85 sub end_F { $_[0]{'scratch'} .= ($_[0]{'in_figure'}) ? '">' : '</em>' }
184              
185 1     1 0 6 sub start_G { $_[0]{'scratch'} .= '<sup>' }
186 1     1 0 5 sub end_G { $_[0]{'scratch'} .= '</sup>' }
187              
188 1     1 0 3 sub start_H { $_[0]{'scratch'} .= '<sub>' }
189 1     1 0 3 sub end_H { $_[0]{'scratch'} .= '</sub>' }
190              
191 1     1 0 5 sub start_I { $_[0]{'scratch'} .= '<em>' }
192 1     1 0 22 sub end_I { $_[0]{'scratch'} .= '</em>' }
193              
194             sub start_N {
195 2     2 0 3 my ($self) = @_;
196 2 100       10 $self->{'scratch'} .= '<font class="footnote">' if ($self->{'css_tags'});
197 2         7 $self->{'scratch'} .= ' (footnote: ';
198             }
199             sub end_N {
200 2     2 0 52 my ($self) = @_;
201 2         5 $self->{'scratch'} .= ')';
202 2 100       10 $self->{'scratch'} .= '</font>' if $self->{'css_tags'};
203             }
204              
205 1     1 0 4 sub start_R { $_[0]{'scratch'} .= '<em>' }
206 1     1 0 5 sub end_R { $_[0]{'scratch'} .= '</em>' }
207              
208 1 50   1 0 8 sub start_U { $_[0]{'scratch'} .= '<font class="url">' if $_[0]{'css_tags'} }
209 1 50   1 0 8 sub end_U { $_[0]{'scratch'} .= '</font>' if $_[0]{'css_tags'} }
210              
211 5     5 0 22 sub start_Z { $_[0]{'scratch'} .= '<a name="' }
212 5     5 0 20 sub end_Z { $_[0]{'scratch'} .= '">' }
213              
214             sub emit {
215 119     119 0 172 my($self, $nowrap) = @_;
216 119         232 my $out = $self->{'scratch'} . "\n";
217 119         122 print {$self->{'output_fh'}} $out, "\n";
  119         501  
218 119         1125 $self->{'scratch'} = '';
219 119         350 return;
220             }
221              
222             # Set additional options
223              
224 66     66 1 327 sub add_body_tags { $_[0]{'body_tags'} = $_[1] }
225 70     70 1 406 sub add_css_tags { $_[0]{'css_tags'} = $_[1] }
226              
227             # bypass built-in E<> handling to preserve entity encoding
228 20     20   657 sub _treat_Es {}
229              
230             1;
231              
232             __END__
233              
234             =head1 NAME
235              
236             Pod::PseudoPod::HTML -- format PseudoPod as HTML
237              
238             =head1 SYNOPSIS
239              
240             use Pod::PseudoPod::HTML;
241              
242             my $parser = Pod::PseudoPod::HTML->new();
243              
244             ...
245              
246             $parser->parse_file('path/to/file.pod');
247              
248             =head1 DESCRIPTION
249              
250             This class is a formatter that takes PseudoPod and renders it as
251             wrapped html.
252              
253             This is a subclass of L<Pod::PseudoPod> and inherits all its methods.
254              
255             =head1 METHODS
256              
257             =head2 add_body_tags
258              
259             $parser->add_body_tags(1);
260             $parser->parse_file($file);
261              
262             Adds beginning and ending "<html>" and "<body>" tags to the formatted
263             document.
264              
265             =head2 add_css_tags
266              
267             $parser->add_css_tags(1);
268             $parser->parse_file($file);
269              
270             Imports a css stylesheet to the html document and adds additional css
271             tags to url, footnote, and sidebar elements for a nicer display. If
272             you don't plan on writing a style.css file (or using the one provided
273             in "examples/"), you probably don't want this option on.
274              
275             =head1 SEE ALSO
276              
277             L<Pod::PseudoPod>, L<Pod::Simple>
278              
279             =head1 COPYRIGHT
280              
281             Copyright (c) 2003-2004 Allison Randal. All rights reserved.
282              
283             This library is free software; you can redistribute it and/or modify
284             it under the same terms as Perl itself. The full text of the license
285             can be found in the LICENSE file included with this module.
286              
287             This library is distributed in the hope that it will be useful, but
288             without any warranty; without even the implied warranty of
289             merchantability or fitness for a particular purpose.
290              
291             =head1 AUTHOR
292              
293             Allison Randal <allison@perl.org>
294              
295             =cut
296