Formatter.pm

filter: Statementstmt 47.9% 186 / 388 Branchbran 44.1% 30 / 68 Conditioncond 21.2% 7 / 33 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 36.3% 56 / 154 Podpod 4.1% 6 / 146 totaltotal 35.2% 285 / 808 CC 50 SCAR 14.6
CC
50
Cov
46%
set_version_tag
24.1
scale_font_for
20.0
configure
12.0
CRAP 4.3
« FormatPS.pm ↑ Summary
1 package HTML::Formatter;
2  
3 # ABSTRACT: Base class for HTML formatters
4  
5  
6 11 ▶use 5.006_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use HTML::Element 3.15 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # We now use Smart::Comments in place of the old DEBUG framework.
14 # this should be commented out in release versions....
15 ##use Smart::Comments;
16  
17 11 our $VERSION = '2.16'; # VERSION
18 11 our $AUTHORITY = 'cpan:NIGELM'; # AUTHORITY
19  
20 #
21 # A typical formatter will not use all of the features of this
22 # class. But it will use some, as best fits the mapping
23 # of HTML to the particular output format.
24 #
25  
26 # ------------------------------------------------------------------------
27  
28  
29 29 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
30 29 my ( $class, %arg ) = @_;
31  
32 29 my $self = bless { $class->default_values }, $class;
33 29 ▶ $self->configure( \%arg ) if keys %arg;
Branch: 1 branch
BranchTrueFalseTotal
if keys %arg 6 23 29
34  
35 29 return $self;
36 }
37  
38 # ------------------------------------------------------------------------
39 29 ▶sub default_values {
Subroutine default_values: called CC=1 SCAR=0.0
Pod undocumented
40 29 ();
41 }
42  
43 # ------------------------------------------------------------------------
44 0 ▶sub configure {
Subroutine configure: not called CC=3 SCAR=24.8
Pod undocumented
45 0 my ( $self, $arg ) = @_;
46  
47 0 for ( keys %$arg ) {
48 0 ▶ warn "Unknown configure argument '$_'" if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
49 }
50  
51 0 return $self;
52 }
53  
54 # ------------------------------------------------------------------------
55 21 ▶sub massage_tree {
Subroutine massage_tree: called CC=2 SCAR=7.0
Pod undocumented
56 21 my ( $self, $html ) = @_;
57  
58 21 ▶ return if $html->tag eq 'p'; # sanity
Branch: 1 branch
BranchTrueFalseTotal
if $html->tag eq "p" 0 21 21
59  
60 ### Before massaging: $html->dump()
61  
62 21 $html->simplify_pres();
63  
64 # Does anything else need doing?
65 ### After massaging: $html->dump()
66  
67 21 return;
68 }
69  
70 # ------------------------------------------------------------------------
71  
72  
73 0 ▶sub format_from_file { return shift->format_file(@_); }
Subroutine format_from_file: not called CC=1 SCAR=6.9
Pod documented
74  
75 6 ▶sub format_file {
Subroutine format_file: called CC=4 SCAR=14.6
Pod documented
76 6 my ( $self, $filename, @params ) = @_;
77  
78 6 ▶ $self = $self->new(@params) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 6 0 6
79  
80 6 ▶ croak "What filename to format from?"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename and length $filename 0 6 6
Condition33%1/3
defined $filename and length $filename
!ll&&!rl&&r
006
MC/DC0%0/2
defined $filename and length $filename
defined $filename length $filename
Truth table33%1/3
A and B
A= defined $filenameB= length $filename
ABresult
0X0
100
111
81 unless ( defined($filename) and length($filename) );
82  
83 6 my $tree = $self->_default_tree();
84 6 $tree->parse_file($filename);
85  
86 6 my $out = $self->format($tree);
87 6 $tree->delete;
88  
89 6 return $out;
90 }
91  
92 # ------------------------------------------------------------------------
93  
94  
95 # ------------------------------------------------------------------------
96 8 ▶sub format_from_string { shift->format_string(@_) }
Subroutine format_from_string: called CC=1 SCAR=0.0
Pod documented
97  
98 15 ▶sub format_string {
Subroutine format_string: called CC=3 SCAR=11.0
Pod documented
99 15 my ( $self, $content, @params ) = @_;
100  
101 15 ▶ $self = $self->new(@params) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 2 13 15
102  
103 15 ▶ croak "What string to format?" unless defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 15 15
104  
105 15 my $tree = $self->_default_tree();
106 15 $tree->parse($content);
107 15 $tree->eof();
108 15 undef $content;
109  
110 15 my $out = $self->format($tree);
111 15 $tree->delete;
112  
113 15 return $out;
114 }
115  
116 # ------------------------------------------------------------------------
117 21 ▶sub _default_tree {
Subroutine _default_tree: called CC=1 SCAR=0.0
118 21 require HTML::TreeBuilder;
119 21 my $t = HTML::TreeBuilder->new;
120  
121 # If nothing else works, try using these parser options:s
122 #$t->implicit_body_p_tag(1);
123 #$t->p_strict(1);
124  
125 21 return $t;
126 }
127  
128 # ------------------------------------------------------------------------
129  
130  
131 21 ▶sub format {
Subroutine format: called CC=4 SCAR=14.0
Pod documented
132 21 my ( $self, $html ) = @_;
133  
134 21 ▶ croak "Usage: \$formatter->format(\$tree)" unless ( defined($html) and ref($html) and $html->can('tag') );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $html and ref $html and $html->can("tag") 0 21 21
Condition33%2/6
defined $html and ref $html and $html->can("tag")
!ll&&!rl&&r
0021
defined $html and ref $html
!ll&&!rl&&r
0021
MC/DC0%0/3
defined $html and ref $html and $html->can("tag")
defined $html ref $html $html->can("tag")
Truth table25%1/4
A and B and C
A= defined $htmlB= ref $htmlC= $html->can("tag")
ABCresult
0XX0
10X0
1100
1111
135  
136 #### Tree to format: $html->dump
137  
138 21 $self->set_version_tag($html);
139 21 $self->massage_tree($html);
140 21 $self->begin($html);
141 21 $html->number_lists();
142  
143 # Per-iteration scratch:
144 21 my ( $node, $start, $depth, $tag, $func );
145 $html->traverse(
146 506 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
147 506 ( $node, $start, $depth ) = @_;
148 506 ▶ if ( ref $node ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node) { } 389 117 506
149 389 $tag = $node->tag;
150 389 ▶ $func = $tag . '_' . ( $start ? "start" : "end" );
Branch: 1 branch
BranchTrueFalseTotal
$start ? : 207 182 389
151  
152 # Use ->can so that we can recover if
153 # a handler is not defined for the tag.
154 389 ▶ if ( $self->can($func) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($func)) { } 388 1 389
155 ### Calling : (' ' x $depth) . $func
156 388 return $self->$func($node);
157 }
158 else {
159 ### Skipping: (' ' x $depth) . $func
160 1 return 1;
161 }
162 }
163 else {
164 117 $self->textflow($node);
165 }
166 117 1;
167 }
168 21 );
169  
170 21 $self->end($html);
171  
172 21 return join( '', @{ $self->{output} } );
173 }
174  
175 # ------------------------------------------------------------------------
176 21 ▶sub begin {
Subroutine begin: called CC=1 SCAR=0.0
Pod undocumented
177 21 my $self = shift;
178  
179 # Flags
180 21 $self->{anchor} = 0;
181 21 $self->{underline} = 0;
182 21 $self->{bold} = 0;
183 21 $self->{italic} = 0;
184 21 $self->{center} = 0;
185  
186 21 $self->{superscript} = 0;
187 21 $self->{subscript} = 0;
188 21 $self->{strikethrough} = 0;
189  
190 21 $self->{center_stack} = []; # push and pop 'center' states to it
191 21 $self->{nobr} = 0;
192  
193 21 $self->{'font_size'} = [3]; # last element is current size
194 21 $self->{basefont_size} = [3];
195  
196 21 $self->{vspace} = undef; # vertical space (dimension)
197  
198 21 $self->{output} = [];
199 }
200  
201 # ------------------------------------------------------------------------
202 0 ▶sub end { }
Subroutine end: not called CC=1 SCAR=0.0
Pod undocumented
203  
204 # ------------------------------------------------------------------------
205 21 ▶sub set_version_tag {
Subroutine set_version_tag: called CC=8 SCAR=31.8
Pod undocumented
206 21 my ( $self, $html ) = @_;
207  
208 21 ▶ if ($html) {
Branch: 2 branches
BranchTrueFalseTotal
if ($html) { } 21 0 21
elsif ($HTML::Parser::VERSION) { } 0 0 0
209 21 ▶ $self->{'version_tag'} = sprintf(
Branch: 1 branch
BranchTrueFalseTotal
$HTML::Parser::VERSION ? : 21 0 21
Condition50%2/4
$self->VERSION || '?'
l!l
210
$html->VERSION || '?'
l!l
210
MC/DC0%0/1
$self->VERSION || '?'
$self->VERSION
MC/DC0%0/1
$html->VERSION || '?'
$html->VERSION
Truth table50%1/2
A
A= $self->VERSION
Aresult
11
00
Truth table50%1/2
A
A= $html->VERSION
Aresult
11
00
210 "%s (v%s, using %s v%s%s)",
211 ref($self), $self->VERSION || '?',
212 ref($html),
213 $html->VERSION || '?',
214 $HTML::Parser::VERSION ? ", and HTML::Parser v$HTML::Parser::VERSION" : ''
215 );
216 }
217 elsif ($HTML::Parser::VERSION) {
218 0 ▶ $self->{'version_tag'} =
Condition0%0/2
$self->VERSION || '?'
l!l
00
MC/DC0%0/1
$self->VERSION || '?'
$self->VERSION
Truth table0%0/2
A
A= $self->VERSION
Aresult
11
00
219 sprintf( "%s (v%s, using %s)", ref($self), $self->VERSION || "?", "HTML::Parser v$HTML::Parser::VERSION", );
220 }
221 else {
222 0 ▶ $self->{'version_tag'} = sprintf( "%s (v%s)", ref($self), $self->VERSION || '?', );
Condition0%0/2
$self->VERSION || '?'
l!l
00
MC/DC0%0/1
$self->VERSION || '?'
$self->VERSION
Truth table0%0/2
A
A= $self->VERSION
Aresult
11
00
223 }
224 }
225  
226 # ------------------------------------------------------------------------
227 7 ▶sub version_tag { shift->{'version_tag'} }
Subroutine version_tag: called CC=1 SCAR=0.0
Pod undocumented
228  
229 # ------------------------------------------------------------------------
230 21 ▶sub html_start { 1; }
Subroutine html_start: called CC=1 SCAR=0.0
Pod undocumented
231 21 ▶sub html_end { }
Subroutine html_end: called CC=1 SCAR=0.0
Pod undocumented
232 21 ▶sub body_start { 1; }
Subroutine body_start: called CC=1 SCAR=0.0
Pod undocumented
233 21 ▶sub body_end { }
Subroutine body_end: called CC=1 SCAR=0.0
Pod undocumented
234 15 ▶sub head_start { 0; }
Subroutine head_start: called CC=1 SCAR=0.0
Pod undocumented
235 0 ▶sub script_start { 0; }
Subroutine script_start: not called CC=1 SCAR=6.9
Pod undocumented
236 0 ▶sub style_start { 0; }
Subroutine style_start: not called CC=1 SCAR=6.9
Pod undocumented
237 0 ▶sub frameset_start { 0; }
Subroutine frameset_start: not called CC=1 SCAR=6.9
Pod undocumented
238  
239 # ------------------------------------------------------------------------
240 0 ▶sub header_start {
Subroutine header_start: not called CC=3 SCAR=24.8
Pod undocumented
241 0 my ( $self, undef, $node ) = @_;
242  
243 0 my $align = $node->attr('align');
244 0 ▶ if ( defined($align) && lc($align) eq 'center' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $align and lc $align eq "center") 0 0 0
Condition0%0/3
defined $align and lc $align eq "center"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $align and lc $align eq "center"
defined $align lc $align eq "center"
Truth table0%0/3
A and B
A= defined $alignB= lc $align eq "center"
ABresult
0X0
100
111
245 0 $self->{center}++;
246 }
247 0 1;
248 }
249  
250 # ------------------------------------------------------------------------
251 0 ▶sub header_end {
Subroutine header_end: not called CC=3 SCAR=24.8
Pod undocumented
252 0 my ( $self, undef, $node ) = @_;
253  
254 0 my $align = $node->attr('align');
255 0 ▶ if ( defined($align) && lc($align) eq 'center' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $align and lc $align eq "center") 0 0 0
Condition0%0/3
defined $align and lc $align eq "center"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $align and lc $align eq "center"
defined $align lc $align eq "center"
Truth table0%0/3
A and B
A= defined $alignB= lc $align eq "center"
ABresult
0X0
100
111
256 0 $self->{center}--;
257 }
258 }
259  
260 # ------------------------------------------------------------------------
261 4 ▶sub h1_start { shift->header_start( 1, @_ ) }
Subroutine h1_start: called CC=1 SCAR=0.0
Pod undocumented
262 4 ▶sub h2_start { shift->header_start( 2, @_ ) }
Subroutine h2_start: called CC=1 SCAR=0.0
Pod undocumented
263 0 ▶sub h3_start { shift->header_start( 3, @_ ) }
Subroutine h3_start: not called CC=1 SCAR=6.9
Pod undocumented
264 0 ▶sub h4_start { shift->header_start( 4, @_ ) }
Subroutine h4_start: not called CC=1 SCAR=6.9
Pod undocumented
265 0 ▶sub h5_start { shift->header_start( 5, @_ ) }
Subroutine h5_start: not called CC=1 SCAR=6.9
Pod undocumented
266 0 ▶sub h6_start { shift->header_start( 6, @_ ) }
Subroutine h6_start: not called CC=1 SCAR=6.9
Pod undocumented
267  
268 # ------------------------------------------------------------------------
269 4 ▶sub h1_end { shift->header_end( 1, @_ ) }
Subroutine h1_end: called CC=1 SCAR=0.0
Pod undocumented
270 4 ▶sub h2_end { shift->header_end( 2, @_ ) }
Subroutine h2_end: called CC=1 SCAR=0.0
Pod undocumented
271 0 ▶sub h3_end { shift->header_end( 3, @_ ) }
Subroutine h3_end: not called CC=1 SCAR=6.9
Pod undocumented
272 0 ▶sub h4_end { shift->header_end( 4, @_ ) }
Subroutine h4_end: not called CC=1 SCAR=6.9
Pod undocumented
273 0 ▶sub h5_end { shift->header_end( 5, @_ ) }
Subroutine h5_end: not called CC=1 SCAR=6.9
Pod undocumented
274 0 ▶sub h6_end { shift->header_end( 6, @_ ) }
Subroutine h6_end: not called CC=1 SCAR=6.9
Pod undocumented
275  
276 4 ▶sub br_start { my $self = shift; $self->vspace( 0, 1 ); }
Subroutine br_start: called CC=1 SCAR=0.0
Pod undocumented
277 0 ▶sub hr_start { my $self = shift; $self->vspace(1); 1; }
Subroutine hr_start: not called CC=1 SCAR=6.9
Pod undocumented
278  
279 # ------------------------------------------------------------------------
280 0 ▶sub img_start {
Subroutine img_start: not called CC=2 SCAR=17.9
Pod undocumented
281 0 my ( $self, $node ) = @_;
282  
283 0 my $alt = $node->attr('alt');
284 0 ▶ $self->out( defined($alt) ? $alt : "[IMAGE]" );
Branch: 1 branch
BranchTrueFalseTotal
defined $alt ? : 0 0 0
285 }
286  
287 # ------------------------------------------------------------------------
288 3 ▶sub a_start { shift->{anchor}++; 1; }
Subroutine a_start: called CC=1 SCAR=0.0
Pod undocumented
289 3 ▶sub a_end { shift->{anchor}--; }
Subroutine a_end: called CC=1 SCAR=0.0
Pod undocumented
290 0 ▶sub u_start { shift->{underline}++; 1; }
Subroutine u_start: not called CC=1 SCAR=6.9
Pod undocumented
291 0 ▶sub u_end { shift->{underline}--; }
Subroutine u_end: not called CC=1 SCAR=6.9
Pod undocumented
292 3 ▶sub b_start { shift->{bold}++; 1; }
Subroutine b_start: called CC=1 SCAR=0.0
Pod undocumented
293 3 ▶sub b_end { shift->{bold}--; }
Subroutine b_end: called CC=1 SCAR=0.0
Pod undocumented
294 3 ▶sub tt_start { shift->{teletype}++; 1; }
Subroutine tt_start: called CC=1 SCAR=0.0
Pod undocumented
295 3 ▶sub tt_end { shift->{teletype}--; }
Subroutine tt_end: called CC=1 SCAR=0.0
Pod undocumented
296 3 ▶sub i_start { shift->{italic}++; 1; }
Subroutine i_start: called CC=1 SCAR=0.0
Pod undocumented
297 3 ▶sub i_end { shift->{italic}--; }
Subroutine i_end: called CC=1 SCAR=0.0
Pod undocumented
298 0 ▶sub center_start { shift->{center}++; 1; }
Subroutine center_start: not called CC=1 SCAR=6.9
Pod undocumented
299 0 ▶sub center_end { shift->{center}--; }
Subroutine center_end: not called CC=1 SCAR=6.9
Pod undocumented
300  
301 # ------------------------------------------------------------------------
302 0 ▶sub div_start { # interesting only for its 'align' attribute
Subroutine div_start: not called CC=3 SCAR=24.8
Pod undocumented
303 0 my ( $self, $node ) = @_;
304  
305 0 my $align = $node->attr('align');
306 0 ▶ if ( defined($align) && lc($align) eq 'center' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $align and lc $align eq "center") 0 0 0
Condition0%0/3
defined $align and lc $align eq "center"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $align and lc $align eq "center"
defined $align lc $align eq "center"
Truth table0%0/3
A and B
A= defined $alignB= lc $align eq "center"
ABresult
0X0
100
111
307 0 return $self->center_start;
308 }
309 0 1;
310 }
311  
312 # ------------------------------------------------------------------------
313 0 ▶sub div_end {
Subroutine div_end: not called CC=3 SCAR=24.8
Pod undocumented
314 0 my ( $self, $node ) = @_;
315  
316 0 my $align = $node->attr('align');
317 0 ▶ if ( defined($align) && lc($align) eq 'center' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $align and lc $align eq "center") 0 0 0
Condition0%0/3
defined $align and lc $align eq "center"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $align and lc $align eq "center"
defined $align lc $align eq "center"
Truth table0%0/3
A and B
A= defined $alignB= lc $align eq "center"
ABresult
0X0
100
111
318 0 return $self->center_end;
319 }
320 }
321  
322 # ------------------------------------------------------------------------
323 0 ▶sub nobr_start { shift->{nobr}++; 1; }
Subroutine nobr_start: not called CC=1 SCAR=6.9
Pod undocumented
324 0 ▶sub nobr_end { shift->{nobr}--; }
Subroutine nobr_end: not called CC=1 SCAR=6.9
Pod undocumented
325 0 ▶sub wbr_start { 1; }
Subroutine wbr_start: not called CC=1 SCAR=6.9
Pod undocumented
326  
327 # ------------------------------------------------------------------------
328 0 ▶sub font_start {
Subroutine font_start: not called CC=3 SCAR=24.8
Pod undocumented
329 0 my ( $self, $elem ) = @_;
330  
331 0 my $size = $elem->attr('size');
332 0 ▶ return 1 unless ( defined($size) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size 0 0 0
333 0 ▶ if ( $size =~ /^\s*[+\-]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size =~ /^\s*[+\-]/) 0 0 0
334 0 my $base = $self->{basefont_size}[-1];
335  
336 # yes, base it on the most recent one
337 0 $size = $base + $size;
338 }
339 0 push @{ $self->{'font_size'} }, $size;
340 0 $self->new_font_size($size);
341 0 1;
342 }
343  
344 # ------------------------------------------------------------------------
345 0 ▶sub font_end {
Subroutine font_end: not called CC=2 SCAR=17.9
Pod undocumented
346 0 my ( $self, $elem ) = @_;
347 0 my $size = $elem->attr('size');
348 0 ▶ return unless defined $size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size 0 0 0
349 0 pop @{ $self->{'font_size'} };
350 0 $self->restore_font_size( $self->{'font_size'}[-1] );
351 }
352  
353 # ------------------------------------------------------------------------
354 0 ▶sub big_start {
Subroutine big_start: not called CC=1 SCAR=6.9
Pod undocumented
355 0 my $self = $_[0];
356 0 push @{ $self->{'font_size'} }, $self->{basefont_size}[-1] + 1; # same as font size="+1"
357 0 $self->new_font_size( $self->{'font_size'}[-1] );
358 0 1;
359 }
360  
361 # ------------------------------------------------------------------------
362 0 ▶sub small_start {
Subroutine small_start: not called CC=1 SCAR=6.9
Pod undocumented
363 0 my $self = $_[0];
364 0 push @{ $self->{'font_size'} }, $self->{basefont_size}[-1] - 1, # same as font size="-1"
365 ;
366 0 $self->new_font_size( $self->{'font_size'}[-1] );
367 0 1;
368 }
369  
370 # ------------------------------------------------------------------------
371 0 ▶sub big_end {
Subroutine big_end: not called CC=1 SCAR=6.9
Pod undocumented
372 0 my $self = $_[0];
373 0 pop @{ $self->{'font_size'} };
374 0 $self->restore_font_size( $self->{'font_size'}[-1] );
375 0 1;
376 }
377  
378 # ------------------------------------------------------------------------
379 0 ▶sub small_end {
Subroutine small_end: not called CC=1 SCAR=6.9
Pod undocumented
380 0 my $self = $_[0];
381 0 pop @{ $self->{'font_size'} };
382 0 $self->restore_font_size( $self->{'font_size'}[-1] );
383 0 1;
384 }
385  
386 # ------------------------------------------------------------------------
387 0 ▶sub basefont_start {
Subroutine basefont_start: not called CC=2 SCAR=17.9
Pod undocumented
388 0 my ( $self, $elem ) = @_;
389 0 my $size = $elem->attr('size');
390 0 ▶ return unless defined $size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size 0 0 0
391 0 push( @{ $self->{basefont_size} }, $size );
392 0 1;
393 }
394  
395 # ------------------------------------------------------------------------
396 0 ▶sub basefont_end {
Subroutine basefont_end: not called CC=2 SCAR=17.9
Pod undocumented
397 0 my ( $self, $elem ) = @_;
398 0 my $size = $elem->attr('size');
399 0 ▶ return unless defined $size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size 0 0 0
400 0 pop( @{ $self->{basefont_size} } );
401 }
402  
403 # ------------------------------------------------------------------------
404 #
405 # Override in subclasses, if you like.
406 #
407 0 ▶sub new_font_size { } #my( $self, $font_size_number ) = @_;
Subroutine new_font_size: not called CC=1 SCAR=0.0
Pod undocumented
408 0 ▶sub restore_font_size { } #my( $self, $font_size_number ) = @_;
Subroutine restore_font_size: not called CC=1 SCAR=0.0
Pod undocumented
409  
410 # ------------------------------------------------------------------------
411 0 ▶sub q_start { shift->out(q<">); 1; }
Subroutine q_start: not called CC=1 SCAR=6.9
Pod undocumented
412 0 ▶sub q_end { shift->out(q<">); 1; }
Subroutine q_end: not called CC=1 SCAR=6.9
Pod undocumented
413 0 ▶sub sup_start { shift->{superscript}++; 1; }
Subroutine sup_start: not called CC=1 SCAR=6.9
Pod undocumented
414 0 ▶sub sup_end { shift->{superscript}--; 1; }
Subroutine sup_end: not called CC=1 SCAR=6.9
Pod undocumented
415 0 ▶sub sub_start { shift->{subscript}++; 1; }
Subroutine sub_start: not called CC=1 SCAR=6.9
Pod undocumented
416 0 ▶sub sub_end { shift->{subscript}--; 1; }
Subroutine sub_end: not called CC=1 SCAR=6.9
Pod undocumented
417 0 ▶sub strike_start { shift->{strikethrough}++; 1; }
Subroutine strike_start: not called CC=1 SCAR=6.9
Pod undocumented
418 0 ▶sub strike_end { shift->{strikethrough}--; 1; }
Subroutine strike_end: not called CC=1 SCAR=6.9
Pod undocumented
419 0 ▶sub s_start { shift->strike_start(@_); }
Subroutine s_start: not called CC=1 SCAR=6.9
Pod undocumented
420 0 ▶sub s_end { shift->strike_end(@_); }
Subroutine s_end: not called CC=1 SCAR=6.9
Pod undocumented
421 0 ▶sub dfn_start { 1; }
Subroutine dfn_start: not called CC=1 SCAR=6.9
Pod undocumented
422 0 ▶sub dfn_end { 1; }
Subroutine dfn_end: not called CC=1 SCAR=6.9
Pod undocumented
423 0 ▶sub abbr_start { 1; }
Subroutine abbr_start: not called CC=1 SCAR=6.9
Pod undocumented
424 0 ▶sub abbr_end { 1; }
Subroutine abbr_end: not called CC=1 SCAR=6.9
Pod undocumented
425 0 ▶sub acronym_start { 1; }
Subroutine acronym_start: not called CC=1 SCAR=6.9
Pod undocumented
426 0 ▶sub acronym_end { 1; }
Subroutine acronym_end: not called CC=1 SCAR=6.9
Pod undocumented
427 0 ▶sub span_start { 1; }
Subroutine span_start: not called CC=1 SCAR=6.9
Pod undocumented
428 0 ▶sub span_end { 1; }
Subroutine span_end: not called CC=1 SCAR=6.9
Pod undocumented
429 0 ▶sub ins_start { 1; }
Subroutine ins_start: not called CC=1 SCAR=6.9
Pod undocumented
430 0 ▶sub ins_end { 1; }
Subroutine ins_end: not called CC=1 SCAR=6.9
Pod undocumented
431 0 ▶sub del_start { 0; } # Don't render the del'd bits
Subroutine del_start: not called CC=1 SCAR=6.9
Pod undocumented
432 0 ▶sub del_end { 0; }
Subroutine del_end: not called CC=1 SCAR=6.9
Pod undocumented
433  
434 # ------------------------------------------------------------------------
435 11 my @Size_magic_numbers = (
436 0.60, 0.75, 0.89, 1, 1.20, 1.50, 2.00, 3.00
437  
438 # #0 #1 #2 #3 #4 #5 #6 #7
439 #________________ - | + _________________________
440 # -3 -2 -1 0 +1 +2 +3 +4
441 );
442  
443 # ------------------------------------------------------------------------
444 0 ▶sub scale_font_for {
Subroutine scale_font_for: not called CC=4 SCAR=30.0
Pod undocumented
445 0 my ( $self, $reference_size ) = @_;
446  
447 # Mozilla's source, at
448 # http://lxr.mozilla.org/seamonkey/source/content/html/style/src/nsStyleUtil.cpp#299
449 # says:
450 # static PRInt32 sFontSizeFactors[8] = { 60,75,89,100,120,150,200,300 };
451 #
452 # For comparison, Gisle's earlier HTML::FormatPS has:
453 # | # size 0 1 2 3 4 5 6 7
454 # | @FontSizes = ( 5, 6, 8, 10, 12, 14, 18, 24, 32);
455 # ...and gets different sizing via just a scaling factor.
456  
457 0 ▶ my $size_number = int( defined( $_[2] ) ? $_[2] : $self->{'font_size'}[-1] );
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 0 0 0
458  
459 # force the size_number into range:
460 0 ▶ $size_number =
Branch: 2 branches
BranchTrueFalseTotal
$size_number < 0 ? : 0 0 0
$size_number > $#Size_magic_numbers ? : 0 0 0
461 ( $size_number < 0 ) ? 0
462 : ( $size_number > $#Size_magic_numbers ) ? $#Size_magic_numbers
463 : int($size_number);
464  
465 0 my $result = int( .5 + $reference_size * $Size_magic_numbers[$size_number] );
466  
467 ### Scale Font: sprintf("reference %s, size %s => %s", $reference_size, $size_number, $result);
468  
469 0 return $result;
470 }
471  
472 # ------------------------------------------------------------------------
473 # Aliases for logical markup:
474 5 ▶sub strong_start { shift->b_start(@_) }
Subroutine strong_start: called CC=1 SCAR=0.0
Pod undocumented
475 5 ▶sub strong_end { shift->b_end(@_) }
Subroutine strong_end: called CC=1 SCAR=0.0
Pod undocumented
476 0 ▶sub cite_start { shift->i_start(@_) }
Subroutine cite_start: not called CC=1 SCAR=6.9
Pod undocumented
477 0 ▶sub cite_end { shift->i_end(@_) }
Subroutine cite_end: not called CC=1 SCAR=6.9
Pod undocumented
478 5 ▶sub em_start { shift->i_start(@_) }
Subroutine em_start: called CC=1 SCAR=0.0
Pod undocumented
479 5 ▶sub em_end { shift->i_end(@_) }
Subroutine em_end: called CC=1 SCAR=0.0
Pod undocumented
480 0 ▶sub code_start { shift->tt_start(@_) }
Subroutine code_start: not called CC=1 SCAR=6.9
Pod undocumented
481 0 ▶sub code_end { shift->tt_end(@_) }
Subroutine code_end: not called CC=1 SCAR=6.9
Pod undocumented
482 0 ▶sub kbd_start { shift->tt_start(@_) }
Subroutine kbd_start: not called CC=1 SCAR=6.9
Pod undocumented
483 0 ▶sub kbd_end { shift->tt_end(@_) }
Subroutine kbd_end: not called CC=1 SCAR=6.9
Pod undocumented
484 0 ▶sub samp_start { shift->tt_start(@_) }
Subroutine samp_start: not called CC=1 SCAR=6.9
Pod undocumented
485 0 ▶sub samp_end { shift->tt_end(@_) }
Subroutine samp_end: not called CC=1 SCAR=6.9
Pod undocumented
486 0 ▶sub var_start { shift->tt_start(@_) }
Subroutine var_start: not called CC=1 SCAR=6.9
Pod undocumented
487 0 ▶sub var_end { shift->tt_end(@_) }
Subroutine var_end: not called CC=1 SCAR=6.9
Pod undocumented
488  
489 # ------------------------------------------------------------------------
490 66 ▶sub p_start {
Subroutine p_start: called CC=1 SCAR=0.0
Pod undocumented
491 66 my $self = shift;
492  
493 #$self->adjust_lm(0); # assert new paragraph
494 66 $self->vspace(1);
495  
496 # assert one line's worth of vertical space at para-start
497 66 $self->out('');
498 66 1;
499 }
500  
501 # ------------------------------------------------------------------------
502 66 ▶sub p_end {
Subroutine p_end: called CC=1 SCAR=0.0
Pod undocumented
503 66 shift->vspace(1); # assert one line's worth of vertical space at para-end
504 }
505  
506 # ------------------------------------------------------------------------
507 4 ▶sub pre_start {
Subroutine pre_start: called CC=1 SCAR=0.0
Pod undocumented
508 4 my $self = shift;
509  
510 4 $self->{pre}++;
511 4 $self->vspace(1); # assert one line's worth of vertical space at pre-start
512 4 1;
513 }
514  
515 # ------------------------------------------------------------------------
516 4 ▶sub pre_end {
Subroutine pre_end: called CC=1 SCAR=0.0
Pod undocumented
517 4 my $self = shift;
518  
519 4 $self->{pre}--; # assert one line's worth of vertical space at pre-end
520 4 $self->vspace(1);
521 }
522  
523 # ------------------------------------------------------------------------
524 0 ▶sub listing_start { shift->pre_start(@_) }
Subroutine listing_start: not called CC=1 SCAR=6.9
Pod undocumented
525 0 ▶sub listing_end { shift->pre_end(@_) }
Subroutine listing_end: not called CC=1 SCAR=6.9
Pod undocumented
526 0 ▶sub xmp_start { shift->pre_start(@_) }
Subroutine xmp_start: not called CC=1 SCAR=6.9
Pod undocumented
527 0 ▶sub xmp_end { shift->pre_end(@_) }
Subroutine xmp_end: not called CC=1 SCAR=6.9
Pod undocumented
528  
529 # ------------------------------------------------------------------------
530 3 ▶sub blockquote_start {
Subroutine blockquote_start: called CC=1 SCAR=0.0
Pod undocumented
531 3 my $self = shift;
532  
533 3 $self->vspace(1); # assert one line's worth of vertical space at blockquote-start
534 3 $self->adjust_lm(+2);
535 3 $self->adjust_rm(-2);
536 3 1;
537 }
538  
539 # ------------------------------------------------------------------------
540 3 ▶sub blockquote_end {
Subroutine blockquote_end: called CC=1 SCAR=0.0
Pod undocumented
541 3 my $self = shift;
542  
543 3 $self->vspace(1); # assert one line's worth of vertical space at blockquote-end
544 3 $self->adjust_lm(-2);
545 3 $self->adjust_rm(+2);
546 }
547  
548 # ------------------------------------------------------------------------
549 0 ▶sub address_start {
Subroutine address_start: not called CC=1 SCAR=6.9
Pod undocumented
550 0 my $self = shift;
551  
552 0 $self->vspace(1); # assert one line's worth of vertical space at address-para-start
553 0 $self->i_start(@_);
554 0 1;
555 }
556  
557 # ------------------------------------------------------------------------
558 0 ▶sub address_end {
Subroutine address_end: not called CC=1 SCAR=6.9
Pod undocumented
559 0 my $self = shift;
560  
561 0 $self->i_end(@_); # assert one line's worth of vertical space at address-para-end
562 0 $self->vspace(1);
563 }
564  
565 # ------------------------------------------------------------------------
566 # Handling of list elements
567 5 ▶sub ul_start {
Subroutine ul_start: called CC=1 SCAR=0.0
Pod undocumented
568 5 my $self = shift;
569  
570 5 $self->vspace(1); # assert one line's worth of vertical space at ul-start
571 5 $self->adjust_lm(+2);
572 5 1;
573 }
574  
575 # ------------------------------------------------------------------------
576 5 ▶sub ul_end {
Subroutine ul_end: called CC=1 SCAR=0.0
Pod undocumented
577 5 my $self = shift;
578  
579 5 $self->adjust_lm(-2); # assert one line's worth of vertical space at ul-end
580 5 $self->vspace(1);
581 }
582  
583 # ------------------------------------------------------------------------
584 22 ▶sub li_start {
Subroutine li_start: called CC=2 SCAR=7.0
Pod undocumented
585 22 my $self = shift;
586  
587 22 ▶ $self->bullet( shift->attr('_bullet') || '' );
Condition50%1/2
(shift())->attr('_bullet') || ''
l!l
220
MC/DC0%0/1
(shift())->attr('_bullet') || ''
(shift())->attr('_bullet')
Truth table50%1/2
A
A= (shift())->attr('_bullet')
Aresult
11
00
588 22 $self->adjust_lm(+2);
589 22 1;
590 }
591  
592 # ------------------------------------------------------------------------
593 14 ▶sub bullet { shift->out(@_); }
Subroutine bullet: called CC=1 SCAR=0.0
Pod undocumented
594  
595 # ------------------------------------------------------------------------
596 22 ▶sub li_end {
Subroutine li_end: called CC=1 SCAR=0.0
Pod undocumented
597 22 my $self = shift;
598  
599 22 $self->vspace(1);
600 22 $self->adjust_lm(-2);
601 }
602  
603 # ------------------------------------------------------------------------
604 0 ▶sub menu_start { shift->ul_start(@_) }
Subroutine menu_start: not called CC=1 SCAR=6.9
Pod undocumented
605 0 ▶sub menu_end { shift->ul_end(@_) }
Subroutine menu_end: not called CC=1 SCAR=6.9
Pod undocumented
606 0 ▶sub dir_start { shift->ul_start(@_) }
Subroutine dir_start: not called CC=1 SCAR=6.9
Pod undocumented
607 0 ▶sub dir_end { shift->ul_end(@_) }
Subroutine dir_end: not called CC=1 SCAR=6.9
Pod undocumented
608  
609 # ------------------------------------------------------------------------
610 5 ▶sub ol_start {
Subroutine ol_start: called CC=1 SCAR=0.0
Pod undocumented
611 5 my $self = shift;
612  
613 5 $self->vspace(1);
614 5 $self->adjust_lm(+2);
615 5 1;
616 }
617  
618 # ------------------------------------------------------------------------
619 5 ▶sub ol_end {
Subroutine ol_end: called CC=1 SCAR=0.0
Pod undocumented
620 5 my $self = shift;
621  
622 5 $self->adjust_lm(-2);
623 5 $self->vspace(1);
624 }
625  
626 # ------------------------------------------------------------------------
627 0 ▶sub dl_start {
Subroutine dl_start: not called CC=1 SCAR=6.9
Pod undocumented
628 0 my $self = shift;
629  
630 # $self->adjust_lm(+2);
631 0 $self->vspace(1); # assert one line's worth of vertical space at dl-start
632 0 1;
633 }
634  
635 # ------------------------------------------------------------------------
636 0 ▶sub dl_end {
Subroutine dl_end: not called CC=1 SCAR=6.9
Pod undocumented
637 0 my $self = shift;
638  
639 # $self->adjust_lm(-2);
640 0 $self->vspace(1); # assert one line's worth of vertical space at dl-end
641 }
642  
643 # ------------------------------------------------------------------------
644 0 ▶sub dt_start {
Subroutine dt_start: not called CC=1 SCAR=6.9
Pod undocumented
645 0 my $self = shift;
646  
647 0 $self->vspace(1); # assert one line's worth of vertical space at dt-start
648 0 1;
649 }
650  
651 # ------------------------------------------------------------------------
652 0 ▶sub dt_end { }
Subroutine dt_end: not called CC=1 SCAR=0.0
Pod undocumented
653  
654 # ------------------------------------------------------------------------
655 0 ▶sub dd_start {
Subroutine dd_start: not called CC=1 SCAR=6.9
Pod undocumented
656 0 my $self = shift;
657  
658 0 $self->adjust_lm(+6);
659 0 $self->vspace(0); # hm, what's that do? nothing?
660 0 1;
661 }
662  
663 # ------------------------------------------------------------------------
664 0 ▶sub dd_end {
Subroutine dd_end: not called CC=1 SCAR=6.9
Pod undocumented
665 0 my $self = shift;
666  
667 0 $self->vspace(1); # assert one line's worth of vertical space at dd-end
668 0 $self->adjust_lm(-6);
669 }
670  
671 # ------------------------------------------------------------------------
672  
673 # And now some things that are basically sane fall-throughs for classes
674 # that don't really handle tables or forms specially...
675  
676 # Things not formatted at all
677 0 ▶sub input_start { 0; }
Subroutine input_start: not called CC=1 SCAR=6.9
Pod undocumented
678 0 ▶sub textarea_start { 0; }
Subroutine textarea_start: not called CC=1 SCAR=6.9
Pod undocumented
679 0 ▶sub select_start { 0; }
Subroutine select_start: not called CC=1 SCAR=6.9
Pod undocumented
680 0 ▶sub option_start { 0; }
Subroutine option_start: not called CC=1 SCAR=6.9
Pod undocumented
681  
682 # ------------------------------------------------------------------------
683 0 ▶sub td_start {
Subroutine td_start: not called CC=1 SCAR=6.9
Pod undocumented
684 0 my $self = shift;
685  
686 0 push @{ $self->{'center_stack'} }, $self->{'center'};
687 0 $self->{center} = 0;
688  
689 0 $self->p_start(@_);
690 }
691  
692 # ------------------------------------------------------------------------
693 0 ▶sub td_end {
Subroutine td_end: not called CC=1 SCAR=6.9
Pod undocumented
694 0 my $self = shift;
695  
696 0 $self->{'center'} = pop @{ $self->{'center_stack'} };
697 0 $self->p_end(@_);
698 }
699  
700 # ------------------------------------------------------------------------
701 0 ▶sub th_start {
Subroutine th_start: not called CC=1 SCAR=6.9
Pod undocumented
702 0 my $self = shift;
703  
704 0 push @{ $self->{'center_stack'} }, $self->{'center'};
705 0 $self->{center} = 0;
706  
707 0 $self->p_start(@_);
708 0 $self->b_start(@_);
709 }
710  
711 # ------------------------------------------------------------------------
712 0 ▶sub th_end {
Subroutine th_end: not called CC=1 SCAR=6.9
Pod undocumented
713 0 my $self = shift;
714  
715 0 $self->b_end(@_);
716 0 $self->{'center'} = pop @{ $self->{'center_stack'} };
717 0 $self->p_end(@_);
718 }
719  
720 # But if you wanted to just SKIP tables and forms, you'd do this:
721 # sub table_start { shift->out('[TABLE NOT SHOWN]'); 0; }
722 # sub form_start { shift->out('[FORM NOT SHOWN]'); 0; }
723  
724 # ------------------------------------------------------------------------
725 117 ▶sub textflow {
Subroutine textflow: called CC=5 SCAR=16.1
Pod undocumented
726 117 my $self = shift;
727  
728 117 ▶ if ( $self->{pre} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'pre'}) { } 4 113 117
elsif ($self->{'blockquote'}) { } 1 112 113
729  
730 # Strip one leading and one trailing newline so that a <pre>
731 # tag can be placed on a line of its own without causing extra
732 # vertical space as part of the preformatted text.
733 4 $_[0] =~ s/\n$//;
734 4 $_[0] =~ s/^\n//;
735 4 $self->pre_out( $_[0] );
736 }
737 elsif ( $self->{blockquote} ) {
738 1 $_[0] =~ s/\A\s//;
739 1 $self->blockquote_out( $_[0] );
740 }
741 else {
742 112 $_[0] = $self->_convert_spacelike_characters_to_space( $_[0] );
743 112 for ( split( /(\s+)/, $_[0] ) ) {
744 3291 ▶ next unless length $_;
Branch: 1 branch
BranchTrueFalseTotal
unless length $_ 17 3274 3291
745 3274 $self->out($_);
746 }
747 }
748 }
749  
750 # ------------------------------------------------------------------------
751 221 ▶sub vspace {
Subroutine vspace: called CC=4 SCAR=13.9
Pod undocumented
752 221 my ( $self, $min, $add ) = @_;
753  
754 # This method sets the vspace attribute. When vspace is
755 # defined, then a new line should be started. If vspace
756 # is a nonzero value, then that should be taken as the
757 # number of lines to be skipped before following text
758 # is written out.
759 #
760 # You may think it odd to conflate the two concepts of
761 # ending this paragraph, and asserting how much space should
762 # follow; but it happens to work out pretty well.
763  
764 221 my $old = $self->{vspace};
765 221 ▶ if ( defined $old ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old) { } 96 125 221
766 96 my $new = $old;
767 96 ▶ $new += $add || 0;
Condition50%1/2
$add || 0
l!l
096
MC/DC0%0/1
$add || 0
$add
Truth table50%1/2
A
A= $add
Aresult
11
00
768 96 ▶ $new = $min if $new < $min;
Branch: 1 branch
BranchTrueFalseTotal
if $new < $min 4 92 96
769 96 $self->{vspace} = $new;
770 }
771 else {
772 125 $self->{vspace} = $min;
773 }
774 ### vspace: $self->{vspace}
775 221 $old;
776 }
777  
778 # ------------------------------------------------------------------------
779 2202 ▶sub collect { push( @{ shift->{output} }, @_ ); }
Subroutine collect: called CC=1 SCAR=0.0
Pod undocumented
780  
781 # ------------------------------------------------------------------------
782 0 ▶sub out { confess "Must be overridden by subclass"; } # Output a word
Subroutine out: not called CC=1 SCAR=6.9
Pod undocumented
783 0 ▶sub pre_out { confess "Must be overridden by subclass"; }
Subroutine pre_out: not called CC=1 SCAR=6.9
Pod undocumented
784 0 ▶sub adjust_lm { confess "Must be overridden by subclass"; }
Subroutine adjust_lm: not called CC=1 SCAR=6.9
Pod undocumented
785 0 ▶sub adjust_rm { confess "Must be overridden by subclass"; }
Subroutine adjust_rm: not called CC=1 SCAR=6.9
Pod undocumented
786  
787 # ------------------------------------------------------------------------
788 # Supplied with a string in bytes, takes any characters that look like
789 # they really should be spaces and turns them into spaces.
790 # Currently only handles the following characters:
791 # 0x00A0 NO-BREAK SPACE
792 # 0x00AD SOFT HYPHEN.
793  
794 112 ▶sub _convert_spacelike_characters_to_space {
Subroutine _convert_spacelike_characters_to_space: called CC=3 SCAR=11.0
795 112 my ( $self, $text ) = @_;
796  
797 112 ▶ return if !defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 112 112
798  
799 112 eval {
800 112 require Encode;
801 112 my $unicode_text = Encode::decode( 'UTF-8', $text );
802 108 ▶ if ( $unicode_text =~ s/ ( \xA0 | \xAD ) / /gx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unicode_text =~ s/ ( \xA0 | \xAD ) / /gx) 4 104 108
803 4 $text = Encode::encode( 'UTF-8', $unicode_text );
804 }
805 };
806 112 return $text;
807 }
808  
809 # ------------------------------------------------------------------------
810  
811  
812 11 1;
813  
814 __END__
« FormatPS.pm ↑ Summary