blib/lib/Pod/HTML2Pod.pm

filter: Statementstmt 58.4% 341 / 583 Branchbran 35.5% 111 / 312 Conditioncond 17.0% 17 / 100 MC/DCmcdc 0.0% 0 / 56 Subroutinesub 95.2% 40 / 42 Podpod 3.3% 1 / 30 totaltotal 45.4% 510 / 1123 CC 203 SCAR 47.3
CC
203
Cov
47%
convert
342.6
a_tweak
294.3
lists_render
181.8
CRAP 112.8
↑ Summary
1  
2 2 require 5;
3 # Time-stamp: "2004-12-29 18:41:19 AST"
4  
5 package Pod::HTML2Pod;
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use integer; # haul aaaaaaaaaass!
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use UNIVERSAL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use HTML::TreeBuilder 3.01 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use HTML::Element 3.05 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use HTML::Tagset (); # presumably used by HTML::TreeBuilder anyhow
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use HTML::Entities (); # presumably used by HTML::Parser anyhow
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use vars qw($Debug $VERSION %Phrasal %Char2ent
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 $nbsp $E_slash $E_vbar $counter);
16  
17 2 $VERSION = '4.05';
18 2 ▶$Debug = 0 unless defined $Debug;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Pod::HTML2Pod::Debug 2 0 2
19  
20 =head1 NAME
21  
22 Pod::HTML2Pod -- translate HTML into POD
23  
24 =head1 SYNOPSIS
25  
26 # Use the program 'html2pod' that comes in this dist, or:
27 use Pod::HTML2Pod;
28 print Pod::HTML2Pod::convert(
29 'file' => 'my_stuff.html', # input file
30 'a_href' => 1, # try converting links
31 );
32  
33 =head1 DESCRIPTION
34  
35 Larry Wall once said (1999-08-27, on the C<pod-people> list, I
36 do believe): "The whole point of pod is to get people to document stuff
37 they wouldn't document in any other form."
38  
39 To that end, I wrote this module so that people who are unpracticed
40 with POD but in a hurry to simply document their programs or modules,
41 could write their documentation in simple HTML, and convert that to
42 POD. That's what this module does.
43  
44 Specifically, this module bends over backwards to try to turn even
45 vaguely plausable HTML into POD -- and when in doubt, it simply ignores
46 things that it doesn't know about, or can't render.
47  
48 =head1 FUNCTIONS
49  
50 This module provides one documented function, which it does not export:
51  
52 =over
53  
54 =item Pod::HTML2Pod::convert( ...options... )
55  
56 =back
57  
58 This returns a single scalar value containing the converted POD text,
59 with some comments after the end.
60  
61 This function takes options:
62  
63 =over
64  
65 =item 'file' => FILENAME,
66  
67 Specifies that the HTML code is to be read from the filename given.
68  
69 =item 'handle' => *HANDLE,
70  
71 Specifies that the HTML code is to be read from the open filehandle
72 given (e.g., C<$fh_obj>, C<*HANDLE>, C<*HANDLE{IO}>, etc.) If you
73 specify this, but fail to specify an actual handle object, inscrutible
74 errors may result.
75  
76 =item 'content' => STRING,
77  
78 Specifies that the HTML code is in the string given. (Alternately,
79 pass a reference to the scalar: C<'content' =E<gt> \$stuff>.)
80  
81 =item 'tree' => OBJ,
82  
83 Specifies that the HTML document is contained in the given
84 HTML::TreeBuilder object (or HTML::Element object, at least).
85  
86 =item 'a_name' => BOOLEAN,
87  
88 Specifies whether you want to try converting C<E<lt>a name="..."E<gt>>
89 elements. By default this is off -- i.e., such elements are ignored.
90  
91 =item 'a_href' => BOOLEAN,
92  
93 Specifies whether you want to try converting C<E<lt>a href="..."E<gt>>
94 elements. By default this is off -- i.e., such elements are ignored.
95 If on, bear in mind that relative URLs cannot be properly converted to
96 POD -- any relative URLs will be complained about in comments after
97 the end of the document. Normal absolute URLs will be treated as best
98 they can be. Note that URLs beginning "pod:..." will be turned into
99 POD links to whatever follows; that is, "pod:Getopt::Std" is turned
100 into C<LE<lt>Getopt::StdE<gt>>
101  
102 =item 'debug' => INTEGER,
103  
104 Puts Pod::HTML2Pod into verbose debug mode for the duration of
105 processing this this HTML document. INTEGER can be 0 for no debug
106 output, 1 for a moderate amount that will cause the HTML syntax tree
107 to be be dumped at the start of the conversion, and 2 for that plus a
108 dump of the intermediate POD doctree, plus a few more inscrutible
109 diagnostic messages. Looking at the trees dumped might be helpful in
110 making sense of error messages that refer to a particular node in the
111 parse tree.
112  
113 =item
114  
115 =back
116  
117 =head1 GUIDELINES
118  
119 Don't write crappy HTML and expect this module to understand it.
120  
121 Don't take the output of C<pod2html> and feed it to this, just because
122 you think it'd be neat to try it. You'll just learn really unpleasant
123 things about C<Pod::Html> -- and that's fine if that means you'll use
124 it to improve C<Pod::Html>, but it's rather the long way around.
125  
126 However, I<do> use this module to convert simple HTML into POD,
127 bearing in mind these simple truths:
128  
129 POD can't do tables, images, forms, imagemaps, layers, CSS, embedded
130 Java applets or any other kind of object, FONT, or BLINK. So don't
131 try to do any of these things.
132  
133 Use C<E<lt>h1E<gt>> and C<E<lt>h2E<gt>> for headings.
134  
135 If you want to have a block of literal example code, put it in a
136 C<E<lt>preE<gt>>.
137  
138 Keep things simple.
139  
140 Remember: Just because it comes I<out> of Pod::HTML2Pod doesn't mean
141 it's happy normal pod. You can do lots of things in HTML that will
142 produce POD that is strange but technically legal (like having huge
143 and complex content in a C<E<lt>h1E<gt>>/C<=head1>) but that will make
144 perldoc scream bloody murder about nroff macros stretched past their
145 limit.
146  
147 Try to avoid using a WYSIWYG HTML editor, as they often produce scary
148 source. Ditto for taking selecting "Save as... HTML" in your word
149 processor. You can always try it, but look at the HTML to survey the
150 damage before you try converting it to POD.
151  
152 Always look at the POD that's been output by HTML2Pod -- never just
153 blindly include it.
154  
155 Consider starting from this template:
156  
157 <html>
158 <head>
159 <title>Things::Stuff</title>
160 <!-- html2pod ignores everything outside the body anyway -->
161 </head>
162 <body>
163 <h1>NAME</h1>
164
165 Things::Stuff -- does some things with stuff
166
167 <h1>SYNOPSIS</h1>
168 <!-- example code -->
169 <pre>
170 use HTML::Stuff;
171 do some more stuff;
172 la la la la la;
173 oogah;
174 </pre>
175
176 <h1>DESCRIPTION</h1>
177
178 This module does things with stuff. It exports these functions:
179
180 <dl>
181 <dt><code>thingify( ... )</code>
182 <dd>This function takes stuff, and returns their value as things.
183
184 <dt><code>destuffulate( ... )</code>
185 <dd>This function returns the things, from stuff.
186 <p>It will throw a fatal exception if applied to things.
187 <br>So don't do that.
188
189 <dt><code>enthinction( ... )</code>
190 <dd>This is where I run out of ways to make up silly sentences
191 involving "thing" and "stuff". Mostly.
192
193 </dl>
194
195 <h2>Caveats and WYA's</h2>
196
197 Things to be wary of:
198
199 <ul>
200 <li>The things.
201 <li>And the stuff
202 <p>Don't forget about that stuff. Gotta keep an eye on that.
203 </ul>
204
205 <h1>BUGS</h1>
206
207 Stuff is hard.
208
209 <h1>SEE ALSO</h1>
210
211 <a href="pod:Class::Classless">Class::Classless</a>,
212 <a href="pod:strict">strict</a>,
213 <a href="pod:Lingua::EN::Numbers::Ordinate"
214 >Lingua::EN::Numbers::Ordinate</a>,
215 <a href="pod:perlvar">perlvar</a>,
216
217 <!-- I use the secret-sauce 'pod:' scheme as a back door for making
218 simple cross-references to POD man pages -->
219
220 <h1>COPYRIGHT</h1>
221
222 Copyright 2000, Joey Jo-Jo Jr. Shabadoo.
223
224 <!-- just one suggested phrasing for the license... -->
225 <p>This library is free software; you can redistribute it and/or modify
226 it under the same terms as Perl itself.
227
228 <h1>AUTHOR</h1>
229 Joey Jo-Jo Jr. Shabadoo, <code>jojojo@shabadoo.int</code>
230 </body>
231 </html>
232  
233 =head1 BUG REPORTS
234  
235 If you do find a case where this converter misinterprets what you
236 consider straightforward HTML (which you should really really have run
237 thru an HTML syntax checker, by the way!), report it to me as a bug, at
238 C<sburke@cpan.org>.
239  
240 Be sure to include the entire document that causes the error -- then
241 specify exactly what you consider the error to be.
242  
243 =head1 BUGS AND CAVEATS
244  
245 * Doesn't try to turn "smart quotes" characters into simple " and '.
246 Maybe should?
247  
248 * Fails to turn
249  
250 foo thing&nbsp;bar&nbsp;baz quux
251  
252 into
253  
254 foo S<thing bar baz> quux
255  
256 I.e., currently just turns C<&nbsp;>'s into normal spaces.
257  
258 * Numeric entities (C<EE<lt>numE<gt>>) are used when necessary -- but these
259 are not understood by some older POD converters.
260  
261 * No HTML that you provide will turn into C<FE<lt>...E<gt>>
262  
263 * Currently maps
264  
265 <A HREF="foo">bar</A>
266  
267 to
268  
269 X<foo>bar
270  
271 but is this correct?
272  
273 =head1 SEE ALSO
274  
275 L<perlpod>, L<Pod::Html>, L<HTML::TreeBuilder>
276  
277 And HTML Tidy, at C<http://www.w3.org/People/Raggett/tidy/>
278  
279 =head1 COPYRIGHT
280  
281 Copyright (c) 2000 Sean M. Burke. 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.
285  
286 =head1 AUTHOR
287  
288 Sean M. Burke C<sburke@cpan.org>
289  
290 =cut
291  
292 # TODO: test whether anchors and references to them actually work
293 # in extremis? (see what recent pod2html versions do to them?)
294  
295 #--------------------------------------------------------------------------
296  
297 1 ▶sub convert {
Subroutine convert: called CC=33 SCAR=58.4
Pod documented
298 1 ▶ Carp::croak(__PACKAGE__ . '::convert needs parameters!')unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 1 1
299 1 ▶ Carp::croak(
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 1 1
300 "odd number of elements in options to " . __PACKAGE__ . "::convert")
301 if @_ % 2;
302  
303 1 my %o = @_;
304 1 local($Debug) = $Debug;
305 0 ▶ if(exists $o{'debug'}) { $Debug = $o{'debug'} }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $o{"debug"}) 0 1 1
306
307 1 my $tree = HTML::TreeBuilder->new();
308
309 1 $tree->ignore_ignorable_whitespace(1);
310
311 1 my $comments = [ __PACKAGE__ . ' conversion notes:' ];
312
313 1 ▶ if(exists $o{'tree'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $o{'tree'}) { } 0 1 1
314 0 $tree->delete; # never mind that one
315 0 $tree = $o{'tree'};
316 0 ▶ die "but the 'tree' value is undef" unless defined $tree;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tree 0 0 0
317 0 ▶ die "but the 'tree' value isn't an object" unless ref $tree;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tree 0 0 0
318 0 ▶ die "but the 'tree' value object's class isn't based on HTML::Element"
Branch: 1 branch
BranchTrueFalseTotal
unless $tree->isa("HTML::Element") 0 0 0
319 unless $tree->isa('HTML::Element');
320 0 $tree = $tree->clone;
321  
322 } else {
323  
324 1 ▶ if(exists $o{'file'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $o{"file"}) 0 1 1
325 0 ▶ die "File $o{'file'} doesn't exist" unless -e $o{'file'};
Branch: 1 branch
BranchTrueFalseTotal
unless -e $o{"file"} 0 0 0
326 0 local(*IN);
327 0 ▶ open(IN, "<$o{'file'}") or die "Can't open $o{'file'}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open IN, qq[<$o{"file"}] or die qq[Can't open $o{"file"}: $!] 0 0 0
328 0 $o{'handle'} = *IN{IO};
329 0 ++$o{'_close_after'};
330 0 ▶ print "Input from $o{'file'} ($o{'handle'})\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::HTML2Pod::Debug 0 0 0
331 0 push @$comments, "#From file $o{'file'}";
332 }
333  
334 1 ▶ if(exists $o{'handle'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $o{"handle"}) 0 1 1
335 0 local $/;
336 0 my $fh = $o{'handle'};
337 0 my $x;
338 0 $x = <$fh>;
339 0 ▶ close($fh) if $o{'_close_after'};
Branch: 1 branch
BranchTrueFalseTotal
if $o{"_close_after"} 0 0 0
340 0 $o{'content'} = \$x;
341 0 ▶ print "Input from handle ($o{'handle'})\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::HTML2Pod::Debug 0 0 0
342 }
343  
344 1 ▶ if(exists $o{'content'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $o{'content'}) { } 1 0 1
345 1 my($content_r, $is_copy);
346 1 ▶ if(!defined $o{'content'}) { # undef content?
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $o{'content'}) { } 0 1 1
elsif (ref $o{'content'}) { } 0 1 1
347 0 die "content is undef";
348 } elsif(ref $o{'content'}) { # scalar ref
349 die "content only accepts scalars or scalar refs"
350 0 ▶ unless ref $o{'content'} eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $o{"content"} eq "SCALAR" 0 0 0
351 0 $content_r = $o{'content'};
352 0 $is_copy = 0;
353 } else { # simple scalar
354 1 $content_r = \$o{'content'};
355 1 $is_copy = 1;
356 }
357  
358 # Nativize newlines, if possible and if need be.
359 # Otherwise PREs will be hard to reckon.
360 1 ▶ if("\n" ne "\cm" and "\n" ne "\cm\cj" and "\n" ne "\cj") {
Branch: 1 branch
BranchTrueFalseTotal
if (do { $$content_r =~ /(\cm\cj|\cm|\cj)/ }) 0 1 1
361 print "I don't recognize what \"\\n\" means on this system!" if $Debug;
362 0 } elsif($$content_r =~ m/(\cm\cj|\cm|\cj)/) {
363 0 my $nl = $1;
364 0 ▶ if($nl eq "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($nl eq "\n") { } 0 0 0
365 # no-op
366 0 ▶ print "# Already in native newline format\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::HTML2Pod::Debug 0 0 0
367 } else {
368 0 ▶ unless($is_copy) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_copy) 0 0 0
369 0 my $x = $$content_r;
370 0 $content_r = \$x; # copy
371 0 $is_copy = 1;
372 }
373 0 ▶ if($nl eq "\cm") {
Branch: 3 branches
BranchTrueFalseTotal
if ($nl eq "\r") { } 0 0 0
elsif ($nl eq "\n") { } 0 0 0
elsif ($nl eq "\r\n") { } 0 0 0
374 0 $$content_r =~ tr/\cm/\n/;
375 0 ▶ print "# Nativizing newlines from \\cm to \\n\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::HTML2Pod::Debug 0 0 0
376 } elsif($nl eq "\cj") {
377 0 $$content_r =~ tr/\cj/\n/;
378 0 ▶ print "# Nativizing newlines from \\cj to \\n\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::HTML2Pod::Debug 0 0 0
379 } elsif($nl eq "\cm\cj") {
380 0 $$content_r =~ tr/\cj//d;
381 0 $$content_r =~ tr/\cm/\n/ unless "\cm" eq "\n";
382 0 ▶ print "# Nativizing newlines from \\cm\\cj to \\n\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::HTML2Pod::Debug 0 0 0
383 }
384 }
385 }
386  
387 1 push @$comments,
388 '# ' . length($$content_r) . ' bytes of input';
389 1 $tree->parse($$content_r);
390 1 $tree->eof;
391 1 delete $o{'content'};
392 } else {
393 0 die "No input source specified?";
394 }
395 }
396  
397 {
398 # The BODY is all we need. Discard the rest.
399 1 ▶ my $body = $tree->find_by_tag_name('body') || die "No BODY in tree?";
Condition50%1/2
$tree->find_by_tag_name('body') || die('No BODY in tree?')
l!l
10
MC/DC0%0/1
$tree->find_by_tag_name('body') || die('No BODY in tree?')
$tree->find_by_tag_name('body')
Truth table50%1/2
A
A= $tree->find_by_tag_name('body')
Aresult
11
00
400 1 $body->detach;
401 1 $tree->delete;
402 1 $tree = $body;
403 }
404  
405 1 ▶ push @$comments, scalar(localtime) . ' ' . ($ENV{'USER'} || '');
Condition50%1/2
$ENV{'USER'} || ''
l!l
01
MC/DC0%0/1
$ENV{'USER'} || ''
$ENV{'USER'}
Truth table50%1/2
A
A= $ENV{'USER'}
Aresult
11
00
406 1 $tree->attr('_pod_comments', $comments);
407
408 # More options:
409 1 ▶ if($o{'a_name'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o{'a_name'}) { } 0 1 1
410 0 $tree->attr('_a_name', 1);
411 0 push @$comments, " Will try to render <a name='...'>";
412 } else {
413 1 push @$comments,
414 " No a_name switch not specified, so will not try to render <a name='...'>";
415 }
416 1 ▶ if($o{'a_href'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o{'a_href'}) { } 0 1 1
417 0 $tree->attr('_a_href', 1);
418 0 push @$comments, " Will try to render <a href='...'>";
419 } else {
420 1 push @$comments,
421 " No a_href switch not specified, so will not try to render <a href='...'>";
422 }
423
424 1 twist_tree($tree);
425  
426 1 my $rendering_r = tree_as_pod($tree);
427 1 $tree->delete;
428 1 return $$rendering_r;
429 }
430  
431 ###########################################################################
432 #
433 # The code below this point is not happy nice readable undocumented code.
434 # It is angry cryptic code, of the sort that you will find little use in
435 # reading.
436 #
437 # When I first thought of writing this module, several years ago, I had
438 # noble dreams that I could write some sort of universal markup-language
439 # mixmaster, which would only need be fed some information about the
440 # source language and the target language, and a few simple facts about
441 # what constructs are equivalent (that HTML "h1" is POD "head1", for
442 # example), and then magic would happen, and documents would be converted.
443 #
444 # Well, I've not yet found that mixmaster, so I've had to write some
445 # very spooky crusty strange code. It seems to work rather well when fed
446 # simple HTML, and seems to degrade gracefully when fed too-complex HTML.
447 #
448 # The code can be used as-is, but it's not conceivably adaptable to other
449 # tasks, or even easily maintainable, regrettably. However, as HTML or
450 # POD are not likely to mutate significantly any time soon, I think
451 # substantial maintenance will not be needed -- just minor tweaking or
452 # bugfixes on my part.
453 #
454 ###########################################################################
455 # SO STOP READING NOW, IF YOU VALUE YOUR SANITY
456 ###########################################################################
457 #
458 # Stay away!
459 # STAY AWAY!
460 # Stay away!
461 # You might end up like me!
462 #
463 # It's the pain
464 # that keeps us alive,
465 # but that beauty is all that we need to survive.
466 #
467 # That damned beauty is all that we need to survive.
468 #
469 # -- David Byrne, "They Are In Love"
470 #
471 ###########################################################################
472  
473 # Initialization code:
474  
475 # TODO: replace this with a hardwired table?
476 2 %Phrasal = %HTML::Tagset::isPhraseMarkup;
477 2 delete @Phrasal{'br', 'hr'};
478 2 for (qw(~literal ~texticle)) { $Phrasal{$_} = 1 }
479 2 ▶$counter = 0 unless defined $counter;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Pod::HTML2Pod::counter 2 0 2
480  
481 2 ▶$Debug = 2 unless defined $Debug;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Pod::HTML2Pod::Debug 0 2 2
482  
483 # Fill out Char2ent:
484 {
485 2 ▶ die "\%HTML::Entities::char2entity is empty?"
Branch: 1 branch
BranchTrueFalseTotal
unless keys %HTML::Entities::char2entity 0 2 2
486 unless keys %HTML::Entities::char2entity;
487
488 2 my($c,$e);
489 2 ▶ while(($c,$e) = each(%HTML::Entities::char2entity)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %HTML::Entities::char2entity) 816 2 818
490 816 ▶ if($e =~ m{^&#(\d+);$}s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e =~ /^&#(\d+);$/s) { } 312 504 816
elsif ($e =~ /^&([^;]+);$/s) { } 504 0 504
491 312 $Char2ent{$c} = "E<$1>";
492 #print "num $e => E<$1>\n";
493 # &#123; => E<123>
494 } elsif($e =~ m{^&([^;]+);$}s) {
495 504 $Char2ent{$c} = "E<$1>";
496 #print "eng $e => E<$1>\n";
497 # &eacute; => E<eacute>
498 } else {
499 0 warn "Unknown thingy in %HTML::Entities::char2entity: $e"
500 # if $^W;
501 }
502 }
503
504 # Points of difference between HTML entities and POD entities:
505
506 2 $Char2ent{"\xA0"} = "E<160>"; # there is no E<nbsp>
507
508 2 $Char2ent{"\xAB"} = "E<lchevron>";
509 2 $Char2ent{"\xBB"} = "E<rchevron>";
510 # Altho new POD processors also know E<laquo> and E<raquo>
511
512 # Old POD processors don't know these two -- so leave numeric
513 # $Char2ent{'/'} = 'E<sol>';
514 # $Char2ent{'|'} = 'E<verbar>';
515 }
516  
517 # Set up some initial values we'll need later.
518 2 ▶unless(defined $nbsp) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Pod::HTML2Pod::nbsp) 2 0 2
519 2 my $nb = '&nbsp;';
520 2 HTML::Entities::decode_entities($nb);
521 2 ▶ if(!defined $nb) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $nb) { } 0 2 2
elsif ($nb eq '') { } 0 2 2
elsif ($nb eq '&nbsp;') { } 0 2 2
elsif ($nb eq ' ') { } 0 2 2
522 0 die "&nbsp; decodes to undef?";
523 } elsif($nb eq '') {
524 0 die "&nbsp; decodes to empty-string?";
525 } elsif($nb eq '&nbsp;') {
526 0 die "&nbsp; doesn't decode?";
527 } elsif($nb eq ' ') {
528 0 $nbsp = undef;
529 } else {
530 2 $nbsp = $nb;
531 }
532 }
533  
534 2 ▶unless(defined $E_slash) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Pod::HTML2Pod::E_slash) 2 0 2
535 2 my $x = '/';
536 2 encode_entities_harder($x);
537 2 ▶ if(!defined $x or !length $x) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $x && length $x) { } 0 2 2
elsif ($x eq '/') { } 0 2 2
elsif ($x =~ /^E<[^>]+>$/s) { } 2 0 2
Condition33%1/3
defined $x && length $x
!ll&&!rl&&r
002
MC/DC0%0/2
defined $x && length $x
defined $x length $x
Truth table33%1/3
A && B
A= defined $xB= length $x
ABresult
0X0
100
111
538 0 die "'/' encodes to nothing??";
539 } elsif($x eq '/') {
540 # no-op
541 } elsif($x =~ m{^E<[^>]+>$}s) {
542 2 $E_slash = $x;
543 } else {
544 0 die "'/' encodes as $x?!";
545 }
546 }
547  
548 2 ▶unless(defined $E_vbar) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Pod::HTML2Pod::E_vbar) 2 0 2
549 2 my $x = '|';
550 2 encode_entities_harder($x);
551 2 ▶ if(!defined $x or !length $x) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $x && length $x) { } 0 2 2
elsif ($x eq '|') { } 0 2 2
elsif ($x =~ /^E<[^>]+>$/s) { } 2 0 2
Condition33%1/3
defined $x && length $x
!ll&&!rl&&r
002
MC/DC0%0/2
defined $x && length $x
defined $x length $x
Truth table33%1/3
A && B
A= defined $xB= length $x
ABresult
0X0
100
111
552 0 die "'|' encodes to nothing??";
553 } elsif($x eq '|') {
554 # no-op
555 } elsif($x =~ m{^E<[^>]+>$}s) {
556 2 $E_vbar = $x;
557 } else {
558 0 die "'|' encodes as $x?!";
559 }
560 }
561  
562 # Last chance to save your sanity: stop reading now...
563  
564 #--------------------------------------------------------------------------
565  
566 # TODO: make all P's go byebye once we've texticulated?
567  
568 1 ▶sub twist_tree {
Subroutine twist_tree: called CC=3 SCAR=11.0
Pod undocumented
569 1 my $tree = $_[0];
570  
571 1 html_node_name($tree);
572  
573 1 delete_unknowns($tree);
574  
575 1 special_splice_div($tree);
576  
577 1 ▶ print("Input tree:\n"), $tree->dump, sleep(0) if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::HTML2Pod::Debug 0 1 1
578  
579 1 prune_by_tag_name( $tree,
580 [qw~ script style ~],
581 [qw~ map style isindex select textarea del input embed bgsound basefont ~],
582 );
583  
584 1 splice_by_tag_name($tree,
585 [qw~
586 big small acronym sub sup multicol
587 applet param object
588 table tr caption col thead tbody tfoot colgroup
589 noscript center font bdo fieldset ins
590 form label legend button link layer object
591 span abbr blink strike wbr
592 frame frameset ilayer layer nolayer
593 address nobr
594 ~],
595 );
596  
597 1 remap_tags($tree, {qw~
598 td p
599 th p
600 em i
601 strong b
602 cite i
603 code code
604 tt code
605 kbd code
606 samp code
607 var i
608 dfn b
609 listing pre
610 plaintext pre
611 xmp pre
612 dd p
613 ~});
614 # CODE for C<>
615 # I for I<>
616 # B for B<>
617  
618 # TODO: Warn of cases where heading has too-complex text in it?
619  
620 1 p_unnest($tree);
621  
622 1 pre_render($tree);
623 1 q_render($tree);
624  
625 1 images_render($tree);
626 1 hr_render($tree);
627 1 br_render($tree);
628 1 lists_render($tree);
629 #wrangle_body_children($tree);
630  
631 1 literalize_text_under($tree);
632  
633 1 winge_about_phrasal_paradoxes($tree);
634  
635 1 texticulate($tree);
636 1 promote_some_secondary_children($tree);
637 1 goodify_p_elements($tree);
638  
639 1 render_headings($tree); # busts up the headings
640  
641 1 a_tweak($tree);
642 #bust_up($tree, qw~h1 h2 h3 h4 h5 h6 p~);
643  
644 1 pod_node_name($tree);
645 1 ▶ $tree->dump, sleep(0) if $Debug > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::HTML2Pod::Debug > 1 0 1 1
646 1 return;
647 }
648  
649 #==========================================================================
650 # Subs below here are in no particular order. Ahwell.
651  
652 1 ▶sub a_tweak {
Subroutine a_tweak: called CC=18 SCAR=56.8
Pod undocumented
653
654 #Scratch:
655 1 my($a_name, $parent, $grandparent, $gptag, @cl, $text);
656
657 1 foreach my $a ($_[0]->find_by_tag_name('a')) {
658 # The configuration we're after looks like this:
659 # <h1 -pod-id="h1_1" id="`h1_1" was-tag="h1"> @0.0
660 # <~texticle -pod-id="~texticle_1" id="``G55"> @0.0.0
661 # <a -pod-id="a_1" id="`a_1" name="NAME"> @0.0.0.0
662 # NAME @0.0.0.0.0
663 0 $a_name = $a->attr('name');
664 0 ▶ next unless defined $a_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a_name 0 0 0
665
666 0 ▶ $parent = $a->parent || next;
Condition0%0/2
$a->parent || (next)
l!l
00
MC/DC0%0/1
$a->parent || (next)
$a->parent
Truth table0%0/2
A
A= $a->parent
Aresult
11
00
667 0 ▶ next unless $parent->tag eq '~texticle';
Branch: 1 branch
BranchTrueFalseTotal
unless $parent->tag eq "~texticle" 0 0 0
668 0 ▶ $grandparent = $parent->parent || next;
Condition0%0/2
$parent->parent || (next)
l!l
00
MC/DC0%0/1
$parent->parent || (next)
$parent->parent
Truth table0%0/2
A
A= $parent->parent
Aresult
11
00
669 0 $gptag = $grandparent->tag;
670 0 ▶ next unless $gptag eq 'h1' or $gptag eq 'h2' or $gptag eq 'item';
Branch: 1 branch
BranchTrueFalseTotal
unless $gptag eq "h1" or $gptag eq "h2" or $gptag eq "item" 0 0 0
Condition0%0/6
$gptag eq "h1" or $gptag eq "h2" or $gptag eq "item"
l!l&&r!l&&!r
000
$gptag eq "h1" or $gptag eq "h2"
l!l&&r!l&&!r
000
MC/DC0%0/3
$gptag eq "h1" or $gptag eq "h2" or $gptag eq "item"
$gptag eq "h1" $gptag eq "h2" $gptag eq "item"
Truth table0%0/4
A or B or C
A= $gptag eq "h1"B= $gptag eq "h2"C= $gptag eq "item"
ABCresult
1XX1
01X1
0011
0000
671 0 ▶ next unless $parent->content_list == 1
Branch: 1 branch
BranchTrueFalseTotal
unless $parent->content_list == 1 and $grandparent->content_list == 1 0 0 0
Condition0%0/3
$parent->content_list == 1 and $grandparent->content_list == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$parent->content_list == 1 and $grandparent->content_list == 1
$parent->content_list == 1 $grandparent->content_list == 1
Truth table0%0/3
A and B
A= $parent->content_list == 1B= $grandparent->content_list == 1
ABresult
0X0
100
111
672 and $grandparent->content_list == 1; # only child of an only child
673 0 @cl = $a->content_list; # with one child, a texticle
674 0 ▶ next unless @cl == 1 and ref $cl[0] and $cl[0]->tag eq '~literal';
Branch: 1 branch
BranchTrueFalseTotal
unless @cl == 1 and ref $cl[0] and $cl[0]->tag eq "~literal" 0 0 0
Condition0%0/6
@cl == 1 and ref $cl[0] and $cl[0]->tag eq "~literal"
!ll&&!rl&&r
000
@cl == 1 and ref $cl[0]
!ll&&!rl&&r
000
MC/DC0%0/3
@cl == 1 and ref $cl[0] and $cl[0]->tag eq "~literal"
@cl == 1 ref $cl[0] $cl[0]->tag eq "~literal"
Truth table0%0/4
A and B and C
A= @cl == 1B= ref $cl[0]C= $cl[0]->tag eq "~literal"
ABCresult
0XX0
10X0
1100
1111
675 0 $text = $cl[0]->attr('text');
676 0 ▶ next unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
677 0 $text =~ s/^\s+//s;
678 0 $text =~ s/\s+$//s;
679 0 ▶ if($a_name eq $text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a_name eq $text) { } 0 0 0
680 0 $a->replace_with_content;
681 0 ▶ print "a_tweak applies to ", $a->attr('id'), "\n" if $Debug > 1
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::HTML2Pod::Debug > 1 0 0 0
682 } else {
683 0 ▶ print "a_tweak can't apply to ",
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::HTML2Pod::Debug > 1 0 0 0
684 $a->attr('id'), ": [$a_name] ne [$text]\n"
685 if $Debug > 1;
686 # hack can't apply
687 }
688 }
689
690 1 return;
691 }
692  
693 1 ▶sub p_unnest {
Subroutine p_unnest: called CC=3 SCAR=18.4
Pod undocumented
694 1 my $tree = $_[0];
695 # Now, p's can't nest in HTML, but once we've spliced out and remapped
696 # things, we can end up with p's containing p's in our parse tree:
697 # <table><td> Foo <table><td> Bar </td></table> baz </td></table>
698 # = <p> Foo <p> Bar </p> Baz </p>
699 1 foreach my $p (reverse $tree->find_by_tag_name('p')) {
700 0 ▶ if($p->parent->tag eq 'p') {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->parent->tag eq "p") 0 0 0
701 0 my @c = $p->detach_content;
702 0 $p->replace_with(
703 HTML::Element->new( 'br',
704 'id', '``G' . ++$counter),
705 @c,
706 HTML::Element->new( 'br',
707 'id', '``G' . ++$counter),
708 );
709 }
710 }
711 }
712  
713 #==========================================================================
714  
715 1 ▶sub delete_unknowns {
Subroutine delete_unknowns: called CC=3 SCAR=12.3
Pod undocumented
716 1 my $tree = $_[0];
717 1 my $map_r = $tree->tagname_map;
718 1 delete @$map_r{keys %HTML::Tagset::isKnown};
719 1 my($tag, $elements);
720 1 ▶ while(($tag,$elements) = each %$map_r) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$map_r) 0 1 1
721 0 commentate($tree, join ", ",
722 "# Unknown \"$tag\" elements deleted: ",
723 map $_->attr('id'), @$elements
724 );
725 0 foreach my $e (@$elements) { $e->replace_with_content }
726 }
727 1 return;
728 }
729  
730 #==========================================================================
731 1 ▶sub special_splice_div {
Subroutine special_splice_div: called CC=2 SCAR=7.6
Pod undocumented
732 1 foreach my $div ($_[0]->find_by_tag_name('div', 'iframe')) {
733 0 $div->replace_with(
734 HTML::Element->new( 'br',
735 'id', '``G' . ++$counter),
736 $div->content_list(),
737 HTML::Element->new( 'br',
738 'id', '``G' . ++$counter),
739 );
740 }
741 1 return;
742 }
743  
744 #==========================================================================
745  
746 1 ▶sub winge_about_phrasal_paradoxes {
Subroutine winge_about_phrasal_paradoxes: called CC=6 SCAR=25.0
Pod undocumented
747 1 my $tree = $_[0];
748 1 my @non_phrasal_children;
749 1 foreach my $p (reverse $tree->find_by_tag_name(keys %Phrasal)) {
750 1 @non_phrasal_children = ();
751 1 foreach my $c ($p->content_list) {
752 push @non_phrasal_children, $c
753 0 ▶ if ref $c and not $Phrasal{$c->tag};
Branch: 1 branch
BranchTrueFalseTotal
if ref $c and not $Pod::HTML2Pod::Phrasal{$c->tag} 0 0 0
Condition0%0/3
ref $c and not $Pod::HTML2Pod::Phrasal{$c->tag}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $c and not $Pod::HTML2Pod::Phrasal{$c->tag}
ref $c not $Pod::HTML2Pod::Phrasal{$c->tag}
Truth table0%0/3
A and not(B)
A= ref $cB= not $Pod::HTML2Pod::Phrasal{$c->tag}
ABresult
0X0
100
111
754 }
755 1 ▶ if(@non_phrasal_children) {
Branch: 1 branch
BranchTrueFalseTotal
if (@non_phrasal_children) 0 1 1
756 0 my $tag = $p->tag;
757 0 commentate( $tree,
758 join '',
759 " Deleting phrasal \"$tag\" element (",
760 $p->attr('id'),
761 ") because it has super-phrasal elements (",
762 join(", ",
763 map $_->attr('id'), @non_phrasal_children
764 ), ") as children.",
765 )
766 ;
767 0 $p->replace_with_content;
768 }
769 }
770 1 return;
771 }
772  
773 #==========================================================================
774  
775 0 ▶sub commentate {
Subroutine commentate: not called CC=1 SCAR=6.9
Pod undocumented
776 0 my $tree = shift;
777 0 push @{ $tree->attr('_pod_comments') }, @_;
778 0 return;
779 }
780  
781 #==========================================================================
782  
783 1 ▶sub html_node_name {
Subroutine html_node_name: called CC=4 SCAR=13.9
Pod undocumented
784 1 my $map_r = $_[0]->tagname_map;
785  
786 1 my($name, $nodes);
787 1 ▶ while(($name, $nodes) = each %$map_r) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$map_r) 2 1 3
788 2 my $counter = 0;
789 2 foreach my $node (@$nodes) {
790 2 ++$counter;
791 2 ▶ $node->attr('id',
Condition50%1/2
$node->attr('id') || '`' . $name . '_' . $counter
l!l
02
MC/DC0%0/1
$node->attr('id') || '`' . $name . '_' . $counter
$node->attr('id')
Truth table50%1/2
A
A= $node->attr('id')
Aresult
11
00
792 $node->attr('id') || ( '`' . $name . '_' . $counter )
793 )
794 ;
795 }
796 }
797  
798 1 return;
799 }
800  
801 1 ▶sub pod_node_name {
Subroutine pod_node_name: called CC=3 SCAR=11.0
Pod undocumented
802 1 my $map_r = $_[0]->tagname_map;
803  
804 1 my($name, $nodes);
805 1 ▶ while(($name, $nodes) = each %$map_r) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$map_r) 4 1 5
806 4 my $counter = 0;
807 4 foreach my $node (@$nodes) {
808 4 ++$counter;
809 4 $node->attr('-pod-id',
810 $name . '_' . $counter
811 )
812 ;
813 }
814 }
815  
816 1 return;
817 }
818  
819 #==========================================================================
820  
821 1 ▶sub render_headings {
Subroutine render_headings: called CC=14 SCAR=33.2
Pod undocumented
822 1 my $tree = $_[0];
823 1 my $map_r = $tree->tagname_map;
824 1 my @levels = sort grep m/^h[1-9]+$/s, keys %$map_r;
825 1 my @headings;
826  
827 1 ▶ if(@levels == 0) { # no headings!?!
Branch: 1 branch
BranchTrueFalseTotal
if (@levels == 0) { } 0 1 1
828 # TODO: insert something?
829 } else {
830 1 ▶ print "# Highest heading level: $levels[0] Making that =head1\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::HTML2Pod::Debug 0 1 1
831 if $Debug;
832 1 foreach my $h (@{$map_r->{shift @levels}}) {
833 1 push @headings, $h;
834 1 $h->attr('was-tag', $h->tag);
835 1 $h->attr('_tag', 'h1');
836 }
837 # And, for any sub-primary levels...
838 1 ▶ print "# Lower levels: @levels. Making those =head2\n"
Branch: 1 branch
BranchTrueFalseTotal
if @levels and $Pod::HTML2Pod::Debug 0 1 1
Condition33%1/3
@levels and $Pod::HTML2Pod::Debug
!ll&&!rl&&r
100
MC/DC0%0/2
@levels and $Pod::HTML2Pod::Debug
@levels $Pod::HTML2Pod::Debug
Truth table33%1/3
A and B
A= @levelsB= $Pod::HTML2Pod::Debug
ABresult
0X0
100
111
839 if @levels and $Debug;
840 0 foreach my $h (map @{$map_r->{$_}}, @levels) {
841 0 push @headings, $h;
842 0 $h->attr('was-tag', $h->tag);
843 0 $h->attr('_tag', 'h2');
844 }
845 }
846  
847 1 foreach my $h (@headings) {
848 1 ▶ if($h->parent->is_inside('h1', 'h2')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h->parent->is_inside("h1", "h2")) 0 1 1
849 # Don't put headings inside other headings. It's just stupid.
850 0 $h->replace_with_content;
851 0 undef($h);
852 }
853 }
854  
855 1 foreach my $h (grep defined($_), @headings) {
856 1 my @c = $h->content_list;
857 1 ▶ if(!@c) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @c) { } 0 1 1
elsif ($c[0]->tag ne '~texticle') { } 0 1 1
858 0 $h->delete;
859 } elsif($c[0]->tag ne '~texticle') {
860 0 $h->replace_with_content;
861 # Don't have things other than texticles in headings
862 } else {
863 1 ▶ if(@c > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@c > 1) 0 1 1
864 # promote all but the first element
865 0 $h->detach_content;
866 0 $h->push_content(shift @c);
867 0 $h->postinsert(@c);
868 # SHOULD HAVE HAPPENED ANYWAY.
869 }
870 # else @c is just one element, a texticle -- which is ideal.
871 1 ▶ commentate($tree,
Branch: 1 branch
BranchTrueFalseTotal
unless $h->parent eq $tree 0 1 1
872 "# Icky: heading " . $h->attr('id')
873 . " not immediately under body."
874 ) unless $h->parent eq $tree;
875 }
876 }
877  
878 1 return;
879 }
880  
881 #--------------------------------------------------------------------------
882  
883 1 ▶sub goodify_p_elements {
Subroutine goodify_p_elements: called CC=8 SCAR=31.0
Pod undocumented
884 1 foreach my $x ($_[0], $_[0]->find_by_tag_name('over', 'item')) {
885 1 my $dirty;
886 1 my @children = $x->content_list;
887
888 1 ▶ for(my $i = 0; $i < @children; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @children) 1 1 2
889 1 ▶ if($children[$i]->tag eq 'p') {
Branch: 1 branch
BranchTrueFalseTotal
if ($children[$i]->tag eq "p") 0 1 1
890 0 my $p = $children[$i];
891 0 my @p_content = $p->detach_content;
892 0 $p->delete;
893 0 $dirty = 1;
894  
895 # Replace the p in the list with its content, and update $i:
896 0 splice @children, $i, 1, @p_content;
897 0 $i += scalar(@p_content) - 1;
898 # Properly,
899 # Leaves $i alone if @p_content == 1.
900 # Decrements $i if @p_content == 0.
901 # Adds to $i appropriately for other sizes of @p_content.
902 }
903 }
904
905 1 ▶ if($dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dirty) 0 1 1
906 0 $x->detach_content;
907 0 $x->push_content(@children);
908 }
909 }
910  
911 1 my @c;
912 # /Try/ to delete all p's
913 1 foreach my $p ($_[0]->find_by_tag_name('p')) {
914 0 @c = $p->content_list;
915 0 ▶ if(!@c) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @c) { } 0 0 0
elsif (@c == grep({$_->tag eq '~texticle';} @c)) { } 0 0 0
916 0 $p->delete; # always right?
917  
918 0 } elsif(@c == grep {; $_->tag eq '~texticle'} @c) {
919 #all texticles!
920 0 $p->replace_with_content;
921 } else {
922 0 print
923 "# Odd: content of p (",
924 $p->attr('id'),
925 ") is not all texticles: [",
926 join(' ', map $_->tag, @c), "]\n"
927 ;
928 # Shouldn't happen, I think.
929 }
930 }
931  
932 1 return;
933 }
934  
935 #--------------------------------------------------------------------------
936  
937 1 ▶sub promote_some_secondary_children {
Subroutine promote_some_secondary_children: called CC=4 SCAR=19.7
Pod undocumented
938 1 foreach my $x (reverse($_[0]->find_by_tag_name('item', 'h1' .. 'h6'))) {
939 1 my @c = $x->content_list;
940 1 ▶ if(@c > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@c > 1) 0 1 1
941 # Take all children after the first, and move them up to
942 # being right sisters of this node.
943 0 ▶ print
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::HTML2Pod::Debug 0 0 0
944 "# Promote_some_secondary_children applies to ",
945 $x->attr('id'),
946 ": (",
947 join(", ", map $_->attr('id'), @c), ")\n" if $Debug;
948 0 $x->detach_content;
949 0 $x->push_content(shift @c);
950 0 $x->postinsert(@c);
951 #print "Done\n" if $Debug;
952 }
953 }
954 #print "Returning\n" if $Debug;
955 1 return;
956 }
957  
958 2 ▶sub literalize_text_under {
Subroutine literalize_text_under: called CC=4 SCAR=13.9
Pod undocumented
959 # Traverse tree, turning text segments into ~literal pseudoelements
960 2 my $node = $_[0];
961 2 my(@children, $dirty);
962 2 foreach my $c (@children = $node->content_list) {
963 2 ▶ if(ref $c) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c) { } 1 1 2
964 1 literalize_text_under($c);
965 } else {
966 1 $dirty = 1;
967 1 $c = HTML::Element->new('~literal', 'text' => $c,
968 'id', '``G' . ++$counter);
969 }
970 }
971 2 ▶ if($dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dirty) 1 1 2
972 1 $node->detach_content;
973 1 $node->push_content(@children);
974 }
975 2 return;
976 }
977  
978 #--------------------------------------------------------------------------
979  
980 3 ▶sub texticulate {
Subroutine texticulate: called CC=10 SCAR=25.3
Pod undocumented
981 # group ~literals and phrasals into texticles
982 # -- maximally high-and-merged phrasal/text groups
983 3 my $node = $_[0];
984 3 my $dirty;
985 3 my(@children) = $node->content_list;
986  
987 #foreach my $c (@children) {
988 # texticulate($c);
989 #}
990  
991 #print "Applying to $node = ", $node->tag, "\n";
992  
993 3 ▶ if(! $Phrasal{$node->tag}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Pod::HTML2Pod::Phrasal{$node->tag}) 2 1 3
994 # Only non-phrasals can have texticles as children!
995 2 my $last_tag;
996 2 ▶ for(my $i = 0; $i < @children; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @children) 2 2 4
997 2 texticulate($children[$i]); # RECURSE!
998 2 ▶ next unless $Phrasal{$children[$i]->tag};
Branch: 1 branch
BranchTrueFalseTotal
unless $Pod::HTML2Pod::Phrasal{$children[$i]->tag} 1 1 2
999
1000 1 ▶ if($i == 0
Branch: 2 branches
BranchTrueFalseTotal
if ($i == 0 or not $Pod::HTML2Pod::Phrasal{$last_tag = $children[$i - 1]->tag}) { } 1 0 1
elsif ($last_tag eq '~texticle') { } 0 0 0
Condition33%1/3
$i == 0 or not $Pod::HTML2Pod::Phrasal{$last_tag = $children[$i - 1]->tag}
l!l&&r!l&&!r
100
MC/DC0%0/2
$i == 0 or not $Pod::HTML2Pod::Phrasal{$last_tag = $children[$i - 1]->tag}
$i == 0 not $Pod::HTML2Pod::Phrasal{$last_tag = $children[$i - 1]->tag}
Truth table33%1/3
A or not(B)
A= $i == 0B= not $Pod::HTML2Pod::Phrasal{$last_tag = $children[$i - 1]->tag}
ABresult
1X1
011
000
1001 or
1002 !$Phrasal{
1003 $last_tag = $children[$i - 1]->tag
1004 }
1005 ) {
1006 # start a new texticle group
1007 1 $dirty = 1;
1008 1 my $old = $children[$i];
1009 1 $children[$i] = HTML::Element->new('~texticle',
1010 'id', '``G' . ++$counter);
1011 1 $children[$i]->push_content($old); # and demote the phrasal to under it
1012 } elsif($last_tag eq '~texticle') {
1013 # move this under preceding texticle
1014 0 $dirty = 1;
1015 0 $children[$i - 1]->push_content( splice @children, $i, 1 );
1016 0 --$i;
1017 } else {
1018 0 die "SPORK 1231233312!";
1019 }
1020 }
1021
1022 #if(0) {
1023 # foreach my $c (@children) {
1024 # # Now fold the texticular content up
1025 # if($c->tag eq '~texticle') {
1026 # $c->attr('~folded' => [$c->detach_content]);
1027 # }
1028 # }
1029 #}
1030 }
1031  
1032 # Now delete all br's!
1033 # (Would it be better to delete BRs only adjacent to a texticle?)
1034 3 ▶ for(my $i = 0; $i < @children; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @children) 2 3 5
1035 2 ▶ if($children[$i]->tag eq 'br') {
Branch: 1 branch
BranchTrueFalseTotal
if ($children[$i]->tag eq "br") 0 2 2
1036 0 splice @children, $i, 1;
1037 0 --$i;
1038 0 $dirty = 1;
1039 }
1040 }
1041 # So, the only purpose/effect of BRs is that they serve as barriers
1042 # to unifying adjacent phrasal elements under a common texticle.
1043 # Once we've unified things, we just delete them from the tree.
1044  
1045 3 ▶ if($dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dirty) 1 2 3
1046 1 $node->detach_content;
1047 1 $node->push_content(@children);
1048 }
1049 }
1050  
1051 #==========================================================================
1052  
1053 1 ▶sub remap_tags {
Subroutine remap_tags: called CC=3 SCAR=11.9
Pod undocumented
1054 1 my($tree, $hr) = @_;
1055 1 ▶ die unless $hr and ref($hr) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $hr and ref $hr eq "HASH" 0 1 1
Condition33%1/3
$hr and ref $hr eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/2
$hr and ref $hr eq "HASH"
$hr ref $hr eq "HASH"
Truth table33%1/3
A and B
A= $hrB= ref $hr eq "HASH"
ABresult
0X0
100
111
1056 1 my($recursor, $tag);
1057 2 ▶ $recursor = sub {
Subroutine __ANON__: called CC=6 SCAR=20.7
1058 2 foreach my $c ($_[0]->content_list) {
1059 2 ▶ if(ref $c) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c) 1 1 2
1060 1 ▶ if(($tag = $c->tag) and defined $tag and exists $hr->{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag = $c->tag and defined $tag and exists $hr->{$tag}) 0 1 1
Condition33%2/6
$tag = $c->tag and defined $tag and exists $hr->{$tag}
!ll&&!rl&&r
010
$tag = $c->tag and defined $tag
!ll&&!rl&&r
001
MC/DC0%0/3
$tag = $c->tag and defined $tag and exists $hr->{$tag}
$tag = $c->tag defined $tag exists $hr->{$tag}
Truth table25%1/4
A and B and C
A= $tag = $c->tagB= defined $tagC= exists $hr->{$tag}
ABCresult
0XX0
10X0
1100
1111
1061 0 $c->attr('_tag', $hr->{$tag});
1062 }
1063 1 $recursor->($c); # recurse!
1064 }
1065 }
1066 2 return;
1067 1 };
1068
1069 1 $recursor->($tree); # Run the recursion.
1070
1071 1 undef $recursor; # So the lambda's refcount can hit 0, and can GC.
1072 1 return;
1073 }
1074  
1075 #--------------------------------------------------------------------------
1076  
1077 0 ▶sub wrangle_body_children {
Subroutine wrangle_body_children: not called CC=4 SCAR=30.0
Pod undocumented
1078 0 my $tree = $_[0];
1079 0 my @children = $tree->content_list;
1080 0 my $dirty = 0;
1081  
1082 0 my $c;
1083 0 $tree->normalize_content; # NB: doesn't recurse
1084  
1085 0 ▶ for(my $i = 0; $i < @children; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @children) 0 0 0
1086 0 my $c = $children[$i];
1087 0 ▶ if(!ref($c)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $c) 0 0 0
1088 # put under a new p
1089 0 $dirty = 1;
1090 (
1091 0 $children[$i] = HTML::Element->new('p', 'superimplicit' => 1,
1092 'id', '``G' . ++$counter
1093 )
1094 )->push_content($c);
1095 #} elsif($c->tag eq 'hr') {
1096 # # do anything special?
1097 }
1098 }
1099  
1100 0 ▶ if($dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dirty) 0 0 0
1101 0 $tree->detach_content;
1102 0 $tree->push_content(@children);
1103 }
1104  
1105 0 return;
1106 }
1107  
1108 #--------------------------------------------------------------------------
1109  
1110 2 ▶sub lists_render { # Recursive.
Subroutine lists_render: called CC=18 SCAR=52.0
Pod undocumented
1111 2 my $node = $_[0];
1112 2 my $tag;
1113 2 ▶ if(($tag = $node->tag) eq 'ul' or $tag eq 'menu') {
Branch: 4 branches
BranchTrueFalseTotal
if (($tag = $node->tag) eq 'ul' or $tag eq 'menu') { } 0 2 2
elsif ($tag eq 'ol') { } 0 2 2
elsif ($tag eq 'dl') { } 0 2 2
elsif ($tag eq 'blockquote') { } 0 2 2
Condition33%1/3
($tag = $node->tag) eq "ul" or $tag eq "menu"
l!l&&r!l&&!r
002
MC/DC0%0/2
($tag = $node->tag) eq "ul" or $tag eq "menu"
($tag = $node->tag) eq "ul" $tag eq "menu"
Truth table33%1/3
A or B
A= ($tag = $node->tag) eq "ul"B= $tag eq "menu"
ABresult
1X1
011
000
1114 0 $node->attr('was-tag', $tag);
1115 0 $node->attr('_tag', 'over');
1116 0 foreach my $c ($node->content_list) {
1117 0 ▶ next unless ref($c) and $c->tag eq 'li';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c and $c->tag eq "li" 0 0 0
Condition0%0/3
ref $c and $c->tag eq "li"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $c and $c->tag eq "li"
ref $c $c->tag eq "li"
Truth table0%0/3
A and B
A= ref $cB= $c->tag eq "li"
ABresult
0X0
100
111
1118 0 $c->attr('_tag', 'item');
1119 0 $c->unshift_content('* ');
1120 # TODO: support bullet types other than this?
1121 }
1122  
1123 } elsif($tag eq 'ol') {
1124 0 $node->attr('was-tag', $tag);
1125 0 $node->attr('_tag', 'over');
1126 0 my $x = 0;
1127 0 foreach my $c ($node->content_list) {
1128 0 ▶ next unless ref($c) and $c->tag eq 'li';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c and $c->tag eq "li" 0 0 0
Condition0%0/3
ref $c and $c->tag eq "li"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $c and $c->tag eq "li"
ref $c $c->tag eq "li"
Truth table0%0/3
A and B
A= ref $cB= $c->tag eq "li"
ABresult
0X0
100
111
1129 0 $c->attr('_tag', 'item');
1130 0 $c->unshift_content(++$x . '. ');
1131 # TODO: support number styles other than this?
1132 }
1133  
1134 } elsif($tag eq 'dl') {
1135 0 $node->attr('was-tag', $tag);
1136 0 $node->attr('_tag', 'over');
1137 0 my $tag;
1138 0 foreach my $c ($node->content_list) {
1139 0 ▶ next unless ref($c);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c 0 0 0
1140 0 ▶ if(($tag = $c->tag) eq 'dt') {
Branch: 2 branches
BranchTrueFalseTotal
if (($tag = $c->tag) eq 'dt') { } 0 0 0
elsif ($tag eq 'dd') { } 0 0 0
1141 0 $c->attr('was-tag', $tag);
1142 0 $c->attr('_tag', 'item');
1143 } elsif($tag eq 'dd') {
1144 0 $c->attr('was-tag', $tag);
1145 0 $c->attr('_tag', 'item');
1146 # Altho really, earlier on, we will have turned all dd's into p's!
1147 # This code is here just in case we decide that that wasn't
1148 # such a hot idea.
1149 # Instead of turning dd's into items, consider replacing with
1150 # content, with a br on each side? Or too late for that?
1151 }
1152 # else just moooove along
1153 }
1154  
1155 } elsif($tag eq 'blockquote') { # not really a list, but hey.
1156 0 $node->attr('was-tag', $tag);
1157 0 $node->attr('_tag', 'over');
1158 }
1159  
1160 # In any case, recurse...
1161 2 foreach my $c ($node->content_list) {
1162 2 ▶ lists_render($c) if ref $c;
Branch: 1 branch
BranchTrueFalseTotal
if ref $c 1 1 2
1163 }
1164 }
1165  
1166 #--------------------------------------------------------------------------
1167  
1168 1 ▶sub br_render {
Subroutine br_render: called CC=1 SCAR=0.0
Pod undocumented
1169 # render BRs.
1170  
1171 # TODO: anything necessary?
1172  
1173 1 return;
1174 }
1175  
1176  
1177 1 ▶sub hr_render {
Subroutine hr_render: called CC=3 SCAR=16.7
Pod undocumented
1178 1 my $tree = $_[0];
1179 1 my $alt;
1180 1 foreach my $hr ($tree->find_by_tag_name('hr')) {
1181 0 ▶ if($hr->parent->tag eq 'body') {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr->parent->tag eq 'body') { } 0 0 0
1182 # Special sauce. SPECIAL SAUCE!
1183 0 $hr->attr('_tag', 'p');
1184 0 $hr->attr('was-tag', 'hr');
1185 0 $hr->push_content('----');
1186 } else {
1187 0 $hr->replace_with(
1188 $hr->new('br', 'was-tag' => 'hr', 'id' => '``G' . ++$counter),
1189 '----',
1190 $hr->new('br', 'was-tag' => 'hr', 'id' => '``G' . ++$counter),
1191 );
1192 }
1193 }
1194 1 return;
1195 }
1196  
1197  
1198 1 ▶sub pre_render {
Subroutine pre_render: called CC=12 SCAR=48.8
Pod undocumented
1199 1 my $tree = $_[0];
1200 1 foreach my $p ($tree->find_by_tag_name('pre')) {
1201 # Delete left or right ignorable WS nodes...
1202 {
1203 0 my $left = $p->left;
1204 #print "Left of $p is $left\n";
1205 0 ▶ if(defined $left and !ref $left and $left =~ m<^\s*$>s) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $left and not ref $left and $left =~ /^\s*$/s) 0 0 0
Condition0%0/6
defined $left and not ref $left and $left =~ /^\s*$/s
!ll&&!rl&&r
000
defined $left and not ref $left
!ll&&!rl&&r
000
MC/DC0%0/3
defined $left and not ref $left and $left =~ /^\s*$/s
defined $left not ref $left $left =~ /^\s*$/s
Truth table0%0/4
A and not(B) and C
A= defined $leftB= not ref $leftC= $left =~ /^\s*$/s
ABCresult
0XX0
10X0
1100
1111
1206 # all nil or WS.
1207 #print "Delendum left at", $p->attr('id') || $p->address, "!\n";
1208 0 $p->parent->splice_content($p->pindex - 1, 1); # delete preceding WS.
1209 }
1210 }
1211 {
1212 0 my $right = $p->right;
1213 0 ▶ if(defined $right and !ref $right and $right =~ m<^\s*$>s) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $right and not ref $right and $right =~ /^\s*$/s) 0 0 0
Condition0%0/6
defined $right and not ref $right and $right =~ /^\s*$/s
!ll&&!rl&&r
000
defined $right and not ref $right
!ll&&!rl&&r
000
MC/DC0%0/3
defined $right and not ref $right and $right =~ /^\s*$/s
defined $right not ref $right $right =~ /^\s*$/s
Truth table0%0/4
A and not(B) and C
A= defined $rightB= not ref $rightC= $right =~ /^\s*$/s
ABCresult
0XX0
10X0
1100
1111
1214 # all nil or WS.
1215 #print "Delendum right at", $p->attr('id') || $p->address, "!\n";
1216 0 $p->parent->splice_content($p->pindex + 1, 1); # delete following WS.
1217 }
1218 }
1219  
1220 # Now acually render, simply...
1221 0 my $text_content = $p->as_text;
1222 0 ▶ unless($text_content =~ m/\S+/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($text_content =~ /\S+/) 0 0 0
1223 0 $p->delete;
1224 0 next;
1225 }
1226
1227 0 $text_content =~ s/^\n+//s; # Kill leading newlines
1228 0 $text_content =~ s/\n+$//s; # Kill trailing newlines
1229  
1230 0 my $left = $p->left;
1231 0 ▶ if($left and ref($left) and $left->tag eq 'pre') {
Branch: 1 branch
BranchTrueFalseTotal
if ($left and ref $left and $left->tag eq 'pre') { } 0 0 0
Condition0%0/6
$left and ref $left and $left->tag eq "pre"
!ll&&!rl&&r
000
$left and ref $left
!ll&&!rl&&r
000
MC/DC0%0/3
$left and ref $left and $left->tag eq "pre"
$left ref $left $left->tag eq "pre"
Truth table0%0/4
A and B and C
A= $leftB= ref $leftC= $left->tag eq "pre"
ABCresult
0XX0
10X0
1100
1111
1232 # prepend to the immediately preceding pre's content
1233 ${
1234 0 $left->attr('~pre_content_r')
1235 } .= "\n" . $text_content;
1236 0 $p->delete;
1237 } else {
1238 0 $p->delete_content;
1239 0 $p->attr('~pre_content_r', \$text_content);
1240 #print "Pre content [[",$text_content,"]]\n";
1241 }
1242 }
1243 1 return;
1244 }
1245  
1246 1 ▶sub q_render {
Subroutine q_render: called CC=2 SCAR=9.2
Pod undocumented
1247 1 my $tree = $_[0];
1248 1 foreach my $q ($tree->find_by_tag_name('q')) {
1249 0 $q->push_content('"');
1250 0 $q->unshift_content('"');
1251 0 $q->replace_with_content;
1252 }
1253 1 return;
1254 }
1255  
1256 1 ▶sub images_render {
Subroutine images_render: called CC=4 SCAR=23.2
Pod undocumented
1257 1 my $tree = $_[0];
1258 1 foreach my $img ($tree->find_by_tag_name('img')) {
1259 0 my $alt;
1260 0 ▶ if(defined($alt = $img->attr('alt'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($alt = $img->attr('alt'))) { } 0 0 0
1261 0 $img->replace_with($alt);
1262 } else {
1263 0 ▶ $img->replace_with(
Branch: 1 branch
BranchTrueFalseTotal
$Pod::HTML2Pod::Debug ? : 0 0 0
1264 $Debug ?
1265 ('[IMAGE' . $img->attr('id') . ']') :
1266 '[IMAGE]'
1267 );
1268 #?? $img->delete;
1269 }
1270 }
1271 1 return;
1272 }
1273  
1274 #--------------------------------------------------------------------------
1275  
1276 1 ▶sub prune_by_tag_name {
Subroutine prune_by_tag_name: called CC=4 SCAR=14.8
Pod undocumented
1277 1 my($tree, @o) = @_;
1278 1 foreach my $o (@o) {
1279 2 ▶ foreach my $x ($tree->find_by_tag_name(ref $o ? @$o : $o)) {
Branch: 1 branch
BranchTrueFalseTotal
ref $o ? : 2 0 2
1280 0 $x->delete;
1281 }
1282 }
1283 1 return;
1284 }
1285  
1286 1 ▶sub splice_by_tag_name {
Subroutine splice_by_tag_name: called CC=4 SCAR=14.8
Pod undocumented
1287 1 my($tree, @o) = @_;
1288 1 foreach my $o (@o) {
1289 1 ▶ foreach my $x ($tree->find_by_tag_name(ref $o ? @$o : $o)) {
Branch: 1 branch
BranchTrueFalseTotal
ref $o ? : 1 0 1
1290 0 $x->replace_with_content;
1291 }
1292 }
1293 1 return;
1294 }
1295  
1296 #--------------------------------------------------------------------------
1297 1 ▶sub tree_as_pod {
Subroutine tree_as_pod: called CC=8 SCAR=24.8
Pod undocumented
1298 1 my $tree = $_[0];
1299  
1300 1 my @lines;
1301 1 my $comments = $tree->attr('_pod_comments');
1302  
1303 1 my $bender;
1304  
1305 3 ▶ $bender = sub {
Subroutine __ANON__: called CC=18 SCAR=39.7
1306 3 my(@post, $node);
1307 3 my $tag = ($node = $_[0])->tag;
1308  
1309 3 ▶ if($tag eq 'body') {
Branch: 7 branches
BranchTrueFalseTotal
if ($tag eq 'body') { } 1 2 3
elsif ($tag eq 'pre') { } 0 2 2
elsif ($tag eq 'over') { } 0 2 2
elsif ($tag eq 'item') { } 0 2 2
elsif ($tag eq 'h1') { } 1 1 2
elsif ($tag eq 'h2') { } 0 1 1
elsif ($tag eq '~texticle') { } 1 0 1
1310 # no-op
1311 } elsif($tag eq 'pre') {
1312 0 push @lines, ${$node->attr('~pre_content_r')};
1313 0 ▶ $lines[-1] =~ s/^/ /gm if $lines[-1] =~ m/^\S/m;
Branch: 1 branch
BranchTrueFalseTotal
if $lines[-1] =~ /^\S/m 0 0 0
1314 # bump everything over if there's any lines that start with
1315 # anything non-spaceys
1316 0 ▶ while($lines[-1] =~ s/\n\n/\n \n/) { }
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[-1] =~ s/\n\n/\n \n/) 0 0 0
1317 # have there be no zero-length lines.
1318 } elsif($tag eq 'over') {
1319 0 push @lines, "=over";
1320 0 push @post, "=back";
1321 } elsif($tag eq 'item') {
1322 0 push @lines, "=item";
1323 } elsif($tag eq 'h1') {
1324 1 push @lines, "=head1";
1325 } elsif($tag eq 'h2') {
1326 0 push @lines, "=head2";
1327 } elsif($tag eq '~texticle') {
1328 1 my $text = render_texticle($tree,$node);
1329 1 $text =~ s/^\s+//s;
1330 1 $text =~ s/\s+$//s;
1331 1 $text =~ s/^=/E<61>/s;
1332 # So that this can't be mistaken for a directive -- on the
1333 # off chance that text content starts with a '='
1334
1335 #$text = "{$text}";
1336
1337 1 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if (@lines and $lines[-1] =~ /^=(\w{1,10})$/s and $1 eq "item" || $1 eq "head1" || $1 eq "head2") 1 0 1
Condition33%4/12
@lines and $lines[-1] =~ /^=(\w{1,10})$/s and $1 eq "item" || $1 eq "head1" || $1 eq "head2"
!ll&&!rl&&r
001
@lines and $lines[-1] =~ /^=(\w{1,10})$/s
!ll&&!rl&&r
001
$1 eq "item" or $1 eq "head1" or $1 eq "head2"
l!l&&r!l&&!r
100
$1 eq "item" or $1 eq "head1"
l!l&&r!l&&!r
010
MC/DC0%0/5
@lines and $lines[-1] =~ /^=(\w{1,10})$/s and $1 eq "item" || $1 eq "head1" || $1 eq "head2"
@lines $lines[-1] =~ /^=(\w{1,10})$/s $1 eq "item" $1 eq "head1" $1 eq "head2"
Truth table16%1/6
A and B and C or D or E
A= @linesB= $lines[-1] =~ /^=(\w{1,10})$/sC= $1 eq "item"D= $1 eq "head1"E= $1 eq "head2"
ABCDEresult
0XXXX0
10XXX0
110000
111XX1
1101X1
110011
1338 @lines and
1339 $lines[-1] =~ m/^=(\w{1,10})$/s and
1340 ( $1 eq 'item' or $1 eq 'head1' or $1 eq 'head2' )
1341 ) {
1342 # Merge this text with the directive:
1343 1 $text = pop(@lines) . ' ' . $text;
1344 }
1345
1346 1 push @lines, wrap72_dammit($text);
1347 1 $lines[-1] =~ s/\s+$//s; # Make REALLY sure there's no tailing WS
1348 1 ▶ pop @lines unless length $lines[-1]; # Sanity check.
Branch: 1 branch
BranchTrueFalseTotal
unless length $lines[-1] 0 1 1
1349
1350 1 return;
1351 # Don't recurse under texticles (because nothing should be there!)
1352 } else {
1353 0 ▶ print "unrenderable element \"$tag\" in phrasal-pass\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::HTML2Pod::Debug 0 0 0
1354 }
1355
1356 2 foreach my $c ($node->content_list) {
1357 2 $bender->($c);
1358 }
1359
1360 2 push @lines, @post;
1361 2 return;
1362 1 };
1363 1 $bender->($tree);
1364 1 undef $bender;
1365  
1366 1 ▶ unshift @lines, "=pod" unless @lines and $lines[0] =~ m<^=>s;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines and $lines[0] =~ /^=/s 0 1 1
Condition33%1/3
@lines and $lines[0] =~ /^=/s
!ll&&!rl&&r
001
MC/DC0%0/2
@lines and $lines[0] =~ /^=/s
@lines $lines[0] =~ /^=/s
Truth table33%1/3
A and B
A= @linesB= $lines[0] =~ /^=/s
ABresult
0X0
100
111
1367  
1368 1 push @lines, "=cut\n\n"; # get extra double-newline at end
1369  
1370 1 my $pod = join "\n\n", @lines;
1371  
1372 1 ▶ if($comments and @$comments) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comments and @$comments) 1 0 1
Condition33%1/3
$comments and @$comments
!ll&&!rl&&r
001
MC/DC0%0/2
$comments and @$comments
$comments @$comments
Truth table33%1/3
A and B
A= $commentsB= @$comments
ABresult
0X0
100
111
1373 1 foreach my $c (@$comments) {
1374 5 $c =~ tr<\cm\cj>< >s;
1375 5 ▶ $c = "#" . $c unless $c =~ m<^\s*#>s;
Branch: 1 branch
BranchTrueFalseTotal
unless $c =~ /^\s*#/s 4 1 5
1376 }
1377 1 $pod .= join "\n", @$comments, '';
1378 }
1379  
1380 1 ▶ sleep(0), print("#Start pod\n\n$pod\n"), sleep(0) if $Debug > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::HTML2Pod::Debug > 1 0 1 1
1381 1 return \$pod;
1382 }
1383  
1384 #--------------------------------------------------------------------------
1385 1 ▶sub render_texticle {
Subroutine render_texticle: called CC=1 SCAR=2.1
Pod undocumented
1386 1 my($tree, $t) = @_;
1387 1 my $text = '';
1388 1 my $bender;
1389
1390 1 my $a_name = $tree->attr('_a_name');
1391 1 my $a_href = $tree->attr('_a_href');
1392
1393 1 my $under_l_count = 0;
1394 2 ▶ $bender = sub {
Subroutine __ANON__: called CC=21 SCAR=51.9
1395 2 my $tag = (my $node = $_[0])->tag;
1396 2 my $post = '>';
1397 2 my $decr_under_l_count_post = 0;
1398 2 ▶ if($tag eq '~texticle') {
Branch: 6 branches
BranchTrueFalseTotal
if ($tag eq '~texticle') { } 1 1 2
elsif ($tag eq '~literal') { } 1 0 1
elsif ($tag eq 'code') { } 0 0 0
elsif ($tag eq 'i') { } 0 0 0
elsif ($tag eq 'b') { } 0 0 0
elsif ($tag eq 'a') { } 0 0 0
1399 # no-op -- just a container
1400 1 $post = '';
1401 } elsif($tag eq '~literal') {
1402 1 my $content = $node->attr('text');
1403 #print "Text from ~literal : ", $node->attr('text'), "\n";
1404 1 ▶ $content =~ s/\Q$nbsp/ /og if defined $nbsp;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Pod::HTML2Pod::nbsp 1 0 1
1405 # Kill nbsps. Why?
1406 # First off, most of them are lame editor artifacts.
1407 # Second off, actually treating them correctly (with S<...>)
1408 # would be a real pain.
1409  
1410 1 ▶ if($under_l_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($under_l_count) { } 0 1 1
1411 0 encode_entities_harder($content);
1412 } else {
1413 1 encode_entities($content);
1414 }
1415 #if(defined $E_slash) {
1416 # # Delete at least most of the optional E<sol>'s
1417 # while( $content =~ s{^([^<>]*)\Q$E_slash\E}{$1/}so ) {}
1418 # while( $content =~ s{\Q$E_slash\E([^<>]*)$}{/$1}so ) {}
1419 #}
1420 #if(defined $E_vbar) {
1421 # # Delete at least most of the optional E<verbar>'s
1422 # while( $content =~ s{^([^<>]*)\Q$E_vbar\E}{$1|}so ) {}
1423 # while( $content =~ s{\Q$E_vbar\E([^<>]*)$}{|$1}so ) {}
1424 #}
1425 1 ▶ print "\$text is undef?" unless defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 1 1
1426 1 $text .= $content;
1427 1 $post = '';
1428 } elsif($tag eq 'code') {
1429 0 $text .= 'C<';
1430 } elsif($tag eq 'i') {
1431 0 $text .= 'I<';
1432 } elsif($tag eq 'b') {
1433 0 $text .= 'B<';
1434 } elsif($tag eq 'a') {
1435 0 my($name, $href);
1436 0 ▶ $name = $a_name ? $node->attr('name') : undef;
Branch: 1 branch
BranchTrueFalseTotal
$a_name ? : 0 0 0
1437 0 ▶ $href = $a_href ? $node->attr('href') : undef;
Branch: 1 branch
BranchTrueFalseTotal
$a_href ? : 0 0 0
1438 0 $post = '';
1439  
1440 0 ▶ if(defined $name and length $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name and length $name) 0 0 0
Condition0%0/3
defined $name and length $name
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table0%0/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
1441 0 $text .= 'X<' . $name . '>';
1442 }
1443  
1444 0 ▶ if(defined $href and length $href) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $href and length $href) 0 0 0
Condition0%0/3
defined $href and length $href
!ll&&!rl&&r
000
MC/DC0%0/2
defined $href and length $href
defined $href length $href
Truth table0%0/3
A and B
A= defined $hrefB= length $href
ABresult
0X0
100
111
1445 0 encode_entities($href);
1446 #print "{Link text:{$href}}\n";
1447 0 ▶ if($href =~ s/^#//s) {
Branch: 3 branches
BranchTrueFalseTotal
if ($href =~ s/^#//s) { } 0 0 0
elsif ($href =~ s/^pod://s) { } 0 0 0
elsif ($href =~ /^[-+.a-z0-9A-Z]+\:[^:]/s) { } 0 0 0
1448 # internal relative href
1449 0 $text .= 'L<';
1450 0 $post .= "|/$href>";
1451 0 ++$under_l_count;
1452 0 $decr_under_l_count_post = 1;
1453 } elsif($href =~ s/^pod://s) {
1454 # Pass that thru.
1455 # A back door for making straightforward pod links.
1456 0 $text .= 'L<';
1457 0 $post .= "|$href>";
1458 0 ++$under_l_count;
1459 0 $decr_under_l_count_post = 1;
1460 } elsif($href =~ m<^[-+.a-z0-9A-Z]+\:[^:]>s) {
1461 # It matches RFC 1738's idea of an absolute URL.
1462 # Pass it thru: the podulator should detect that it's a URL
1463 # and handle appropriately.
1464 0 $post .= " ($href)";
1465 } else {
1466 # a relative link??
1467 0 $href = $href;
1468 0 commentate($t->root, "# Untranslatable link: \"$href\"");
1469 }
1470 }
1471 } else {
1472 0 print "Unrenderable sub-phrasal element $tag: ignoring\n";
1473 0 $post = '';
1474 }
1475  
1476 # Recurse!
1477 2 foreach my $c ($node->content_list) {
1478 1 $bender->($c);
1479 }
1480
1481 # Now, post-order things:
1482
1483 2 $text .= $post;
1484 2 ▶ $under_l_count-- if $decr_under_l_count_post;
Branch: 1 branch
BranchTrueFalseTotal
if $decr_under_l_count_post 0 2 2
1485 2 return;
1486 1 };
1487 1 $bender->($t);
1488 1 undef $bender;
1489  
1490 1 $text =~ s/\s+/ /g;
1491  
1492 # A weensy bit of cleanup:
1493 1 $text =~ s/ ?> ?$/>/s;
1494 1 $text =~ s/^((?:\w<)+) ([^>])/$1$2/;
1495  
1496 #print "{$text}\n";
1497  
1498 1 return $text;
1499 }
1500  
1501 #--------------------------------------------------------------------------
1502 sub COLMAX () {72}
1503  
1504 1 ▶sub wrap72_dammit {
Subroutine wrap72_dammit: called CC=8 SCAR=26.8
Pod undocumented
1505 # All because Text::Wrap::wrap DIES when it hits an unwrappably
1506 # large text chunk, DAMMIT.
1507  
1508 # So this is a stupid wrapper: knows nothing about tabs or anything.
1509 1 my $text = '';
1510 1 my $col = 0;
1511 1 foreach my $w (split /\s+/, $_[0]) {
1512 2 ▶ next unless length $w;
Branch: 1 branch
BranchTrueFalseTotal
unless length $w 0 2 2
1513 2 ▶ if(length($w) >= COLMAX) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $w >= 72) { } 0 2 2
elsif (1 + $col + length($w) < 72) { } 2 0 2
1514 # Unwrappably large chunk.
1515 0 ▶ if($col) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col) { } 0 0 0
1516 0 $text .= "\n$w\n";
1517 } else {
1518 0 $text .= "$w\n";
1519 }
1520 0 $col = 0;
1521 } elsif ((1 + $col + length $w) < COLMAX) {
1522 # The word will fit on /this/ line
1523 2 ▶ if($col) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col) { } 1 1 2
1524 1 $text .= " $w";
1525 1 $col += 1 + length $w;
1526 } else {
1527 1 $text .= $w ;
1528 1 $col += length $w;
1529 }
1530 } else {
1531 # Start a new line
1532 0 ▶ if($col) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col) { } 0 0 0
1533 0 $text .= "\n$w";
1534 } else {
1535 0 $text .= $w; # never applies?
1536 }
1537 0 $col = length $w;
1538 }
1539 }
1540 1 $text =~ s/\n+$//s; # nix and trailing newlines
1541  
1542 1 return $text;
1543 }
1544  
1545  
1546 #==========================================================================
1547 # Adapted from Gisle Aas's HTML::Entities::encode_entities:
1548  
1549 1 ▶sub encode_entities {
Subroutine encode_entities: called CC=1 SCAR=0.0
Pod undocumented
1550 1 $_[0] =~ s/([^\n\t !-;=?-~])/$Char2ent{$1}/g;
1551 # Encode control chars, high bit chars and '<' and '>'
1552 1 return;
1553 }
1554  
1555 4 ▶sub encode_entities_harder {
Subroutine encode_entities_harder: called CC=1 SCAR=0.0
Pod undocumented
1556 4 $_[0] =~ s/([^\n\t !\#\$%\'-.0-=?-{}~])/$Char2ent{$1}/g;
1557 # Encode control chars, high bit chars and '<', '&', '>', '"',
1558 # '|', '/'
1559 4 return;
1560 }
1561  
1562 #--------------------------------------------------------------------------
1563  
1564 __END__
↑ Summary