blib/lib/HTML/GenToc.pm

filter: Statementstmt 81.1% 383 / 472 Branchbran 64.2% 171 / 266 Conditioncond 52.9% 81 / 153 MC/DCmcdc 29.5% 26 / 88 Subroutinesub 100% 15 / 15 Podpod 100% 9 / 9 totaltotal 68.3% 685 / 1003 CC 189 SCAR 50.0
CC
189
Cov
71%
output_toc
417.6
make_toc_list
65.3
make_anchors
59.3
CRAP 148.5
↑ Summary scripts/hypertoc »
1 package HTML::GenToc;
2 8 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 $HTML::GenToc::VERSION = '3.20';
4 }
5 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 HTML::GenToc - Generate a Table of Contents for HTML documents.
10  
11 =head1 VERSION
12  
13 version 3.20
14  
15 =head1 SYNOPSIS
16  
17 use HTML::GenToc;
18  
19 # create a new object
20 my $toc = new HTML::GenToc();
21  
22 my $toc = new HTML::GenToc(title=>"Table of Contents",
23 toc_entry=>{
24 H1=>1,
25 H2=>2
26 },
27 toc_end=>{
28 H1=>'/H1',
29 H2=>'/H2'
30 }
31 );
32  
33 # generate a ToC from a file
34 $toc->generate_toc(input=>$html_file,
35 footer=>$footer_file,
36 header=>$header_file
37 );
38  
39  
40 =head1 DESCRIPTION
41  
42 HTML::GenToc generates anchors and a table of contents for
43 HTML documents. Depending on the arguments, it will insert
44 the information it generates, or output to a string, a separate file
45 or STDOUT.
46  
47 While it defaults to taking H1 and H2 elements as the significant
48 elements to put into the table of contents, any tag can be defined
49 as a significant element. Also, it doesn't matter if the input
50 HTML code is complete, pure HTML, one can input pseudo-html
51 or page-fragments, which makes it suitable for using on templates
52 and HTML meta-languages such as WML.
53  
54 Also included in the distrubution is hypertoc, a script which uses the
55 module so that one can process files on the command-line in a
56 user-friendly manner.
57  
58 =head1 DETAILS
59  
60 The ToC generated is a multi-level level list containing links to the
61 significant elements. HTML::GenToc inserts the links into the ToC to
62 significant elements at a level specified by the user.
63  
64 B<Example:>
65  
66 If H1s are specified as level 1, than they appear in the first
67 level list of the ToC. If H2s are specified as a level 2, than
68 they appear in a second level list in the ToC.
69  
70 Information on the significant elements and what level they should occur
71 are passed in to the methods used by this object, or one can use the
72 defaults.
73  
74 There are two phases to the ToC generation. The first phase is to
75 put suitable anchors into the HTML documents, and the second phase
76 is to generate the ToC from HTML documents which have anchors
77 in them for the ToC to link to.
78  
79 For more information on controlling the contents of the created ToC, see
80 L</Formatting the ToC>.
81  
82 HTML::GenToc also supports the ability to incorporate the ToC into the HTML
83 document itself via the B<inline> option. See L</Inlining the ToC> for more
84 information.
85  
86 In order for HTML::GenToc to support linking to significant elements,
87 HTML::GenToc inserts anchors into the significant elements. One can
88 use HTML::GenToc as a filter, outputing the result to another file,
89 or one can overwrite the original file, with the original backed
90 up with a suffix (default: "org") appended to the filename.
91 One can also output the result to a string.
92  
93 =head1 METHODS
94  
95 Default arguments can be set when the object is created, and overridden
96 by setting arguments when the generate_toc method is called.
97 Arguments are given as a hash of arguments.
98  
99 =cut
100  
101 8 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 8 ▶use HTML::SimpleParse;
Subroutine BEGIN: called CC=1 SCAR=0.0
103 8 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 8 ▶use HTML::LinkList;
Subroutine BEGIN: called CC=1 SCAR=0.0
105  
106 #################################################################
107  
108 #---------------------------------------------------------------#
109 # Object interface
110 #---------------------------------------------------------------#
111  
112 =head2 Method -- new
113  
114 $toc = new HTML::GenToc();
115  
116 $toc = new HTML::GenToc(toc_entry=>\%my_toc_entry,
117 toc_end=>\%my_toc_end,
118 bak=>'bak',
119 ...
120 );
121  
122 Creates a new HTML::GenToc object.
123  
124 These arguments will be used as defaults in invocations of other methods.
125  
126 See L<generate_tod> for possible arguments.
127  
128 =cut
129 9 ▶sub new {
Subroutine new: called CC=3 SCAR=12.0
Pod documented
130 9 my $invocant = shift;
131  
132 9 ▶ my $class = ref($invocant) || $invocant; # Object or class name
Condition33%1/3
ref $invocant || $invocant
l!l&&r!l&&!r
090
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant $invocant
Truth table33%1/3
A || B
A= ref $invocantB= $invocant
ABresult
1X1
011
000
133 9 my $self = {
134 debug => 0,
135 bak => 'org',
136 entrysep => ', ',
137 footer => '',
138 inline => 0,
139 header => '',
140 input => '',
141 notoc_match => 'class="notoc"',
142 ol => 0,
143 ol_num_levels => 1,
144 overwrite => 0,
145 outfile => '-',
146 quiet => 0,
147 textonly => 0,
148 title => 'Table of Contents',
149 toclabel => '<h1>Table of Contents</h1>',
150 toc_tag => '^BODY',
151 toc_tag_replace => 0,
152 toc_only => 0,
153 # define TOC entry elements
154 toc_entry => {
155 'H1'=>1,
156 'H2'=>2,
157 },
158 # TOC entry element terminators
159 toc_end => {
160 'H1'=>'/H1',
161 'H2'=>'/H2',
162 },
163 useorg => 0,
164 @_
165 };
166  
167 # bless self
168 9 bless($self, $class);
169  
170 9 ▶ if ($self->{debug})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 9 9
171 {
172 0 print STDERR Dumper($self);
173 }
174  
175 9 return $self;
176 } # new
177  
178 =head2 generate_toc
179  
180 $toc->generate_toc(outfile=>"index2.html");
181  
182 my $result_str = $toc->generate_toc(to_string=>1);
183  
184 Generates a table of contents for the significant elements in the HTML
185 documents, optionally generating anchors for them first.
186  
187 B<Options>
188  
189 =over
190  
191 =item bak
192  
193 bak => I<string>
194  
195 If the input file/files is/are being overwritten (B<overwrite> is on), copy
196 the original file to "I<filename>.I<string>". If the value is empty, B<no>
197 backup file will be created.
198 (default:org)
199  
200 =item debug
201  
202 debug => 1
203  
204 Enable verbose debugging output. Used for debugging this module;
205 in other words, don't bother.
206 (default:off)
207  
208 =item entrysep
209  
210 entrysep => I<string>
211  
212 Separator string for non-<li> item entries
213 (default: ", ")
214  
215 =item filenames
216  
217 filenames => \@filenames
218  
219 The filenames to use when creating table-of-contents links.
220 This overrides the filenames given in the B<input> option,
221 and is expected to have exactly the same number of elements.
222 This can also be used when passing in string-content to the B<input>
223 option, to give a (fake) filename to use for the links relating
224 to that content.
225  
226 =item footer
227  
228 footer => I<file_or_string>
229  
230 Either the filename of the file containing footer text for ToC;
231 or a string containing the footer text.
232  
233 =item header
234  
235 header => I<file_or_string>
236  
237 Either the filename of the file containing header text for ToC;
238 or a string containing the header text.
239  
240 =item ignore_only_one
241  
242 ignore_only_one => 1
243  
244 If there would be only one item in the ToC, don't make a ToC.
245  
246 =item ignore_sole_first
247  
248 ignore_sole_first => 1
249  
250 If the first item in the ToC is of the highest level,
251 AND it is the only one of that level, ignore it.
252 This is useful in web-pages where there is only one H1 header
253 but one doesn't know beforehand whether there will be only one.
254  
255 =item inline
256  
257 inline => 1
258  
259 Put ToC in document at a given point.
260 See L</Inlining the ToC> for more information.
261  
262 =item input
263  
264 input => \@filenames
265  
266 input => $content
267  
268 This is expected to be either a reference to an array of filenames,
269 or a string containing content to process.
270  
271 The three main uses would be:
272  
273 =over
274  
275 =item (a)
276  
277 you have more than one file to process, so pass in multiple filenames
278  
279 =item (b)
280  
281 you have one file to process, so pass in its filename as the only array item
282  
283 =item (c)
284  
285 you have HTML content to process, so pass in just the content as a string
286  
287 =back
288  
289 (default:undefined)
290  
291 =item notoc_match
292  
293 notoc_match => I<string>
294  
295 If there are certain individual tags you don't wish to include in the
296 table of contents, even though they match the "significant elements",
297 then if this pattern matches contents inside the tag (not the body),
298 then that tag will not be included, either in generating anchors nor in
299 generating the ToC. (default: C<class="notoc">)
300  
301 =item ol
302  
303 ol => 1
304  
305 Use an ordered list for level 1 ToC entries.
306  
307 =item ol_num_levels
308  
309 ol_num_levels => 2
310  
311 The number of levels deep the OL listing will go if B<ol> is true.
312 If set to zero, will use an ordered list for all levels.
313 (default:1)
314  
315 =item overwrite
316  
317 overwrite => 1
318  
319 Overwrite the input file with the output.
320 (default:off)
321  
322 =item outfile
323  
324 outfile => I<file>
325  
326 File to write the output to. This is where the modified HTML
327 output goes to. Note that it doesn't make sense to use this option if you
328 are processing more than one file. If you give '-' as the filename, then
329 output will go to STDOUT.
330 (default: STDOUT)
331  
332 =item quiet
333  
334 quiet => 1
335  
336 Suppress informative messages. (default: off)
337  
338 =item textonly
339  
340 textonly => 1
341  
342 Use only text content in significant elements.
343  
344 =item title
345  
346 title => I<string>
347  
348 Title for ToC page (if not using B<header> or B<inline> or B<toc_only>)
349 (default: "Table of Contents")
350  
351 =item toc_after
352  
353 toc_after => \%toc_after_data
354  
355 %toc_after_data = { I<tag1> => I<suffix1>,
356 I<tag2> => I<suffix2>
357 };
358  
359 toc_after => { H2=>'</em>' }
360  
361 For defining layout of significant elements in the ToC.
362  
363 This expects a reference to a hash of
364 tag=>suffix pairs.
365  
366 The I<tag> is the HTML tag which marks the start of the element. The
367 I<suffix> is what is required to be appended to the Table of Contents
368 entry generated for that tag.
369  
370 (default: undefined)
371  
372 =item toc_before
373  
374 toc_before => \%toc_before_data
375  
376 %toc_before_data = { I<tag1> => I<prefix1>,
377 I<tag2> => I<prefix2>
378 };
379  
380 toc_before=>{ H2=>'<em>' }
381  
382 For defining the layout of significant elements in the ToC. The I<tag>
383 is the HTML tag which marks the start of the element. The I<prefix> is
384 what is required to be prepended to the Table of Contents entry
385 generated for that tag.
386  
387 (default: undefined)
388  
389 =item toc_end
390  
391 toc_end => \%toc_end_data
392  
393 %toc_end_data = { I<tag1> => I<endtag1>,
394 I<tag2> => I<endtag2>
395 };
396  
397 toc_end => { H1 => '/H1', H2 => '/H2' }
398  
399 For defining significant elements. The I<tag> is the HTML tag which
400 marks the start of the element. The I<endtag> the HTML tag which marks
401 the end of the element. When matching in the input file, case is
402 ignored (but make sure that all your I<tag> options referring to the
403 same tag are exactly the same!).
404  
405 =item toc_entry
406  
407 toc_entry => \%toc_entry_data
408  
409 %toc_entry_data = { I<tag1> => I<level1>,
410 I<tag2> => I<level2>
411 };
412  
413 toc_entry => { H1 => 1, H2 => 2 }
414  
415 For defining significant elements. The I<tag> is the HTML tag which marks
416 the start of the element. The I<level> is what level the tag is considered
417 to be. The value of I<level> must be numeric, and non-zero. If the value
418 is negative, consective entries represented by the significant_element will
419 be separated by the value set by B<entrysep> option.
420  
421 =item toclabel
422  
423 toclabel => I<string>
424  
425 HTML text that labels the ToC. Always used.
426 (default: "<h1>Table of Contents</h1>")
427  
428 =item toc_tag
429  
430 toc_tag => I<string>
431  
432 If a ToC is to be included inline, this is the pattern which is used to
433 match the tag where the ToC should be put. This can be a start-tag, an
434 end-tag or a comment, but the E<lt> should be left out; that is, if you
435 want the ToC to be placed after the BODY tag, then give "BODY". If you
436 want a special comment tag to make where the ToC should go, then include
437 the comment marks, for example: "!--toc--" (default:BODY)
438  
439 =item toc_tag_replace
440  
441 toc_tag_replace => 1
442  
443 In conjunction with B<toc_tag>, this is a flag to say whether the given tag
444 should be replaced, or if the ToC should be put after the tag.
445 This can be useful if your toc_tag is a comment and you don't need it
446 after you have the ToC in place.
447 (default:false)
448  
449 =item toc_only
450  
451 toc_only => 1
452  
453 Output only the Table of Contents, that is, the Table of Contents plus
454 the toclabel. If there is a B<header> or a B<footer>, these will also be
455 output.
456  
457 If B<toc_only> is false then if there is no B<header>, and B<inline> is
458 not true, then a suitable HTML page header will be output, and if there
459 is no B<footer> and B<inline> is not true, then a HTML page footer will
460 be output.
461  
462 (default:false)
463  
464 =item to_string
465  
466 to_string => 1
467  
468 Return the modified HTML output as a string. This I<does> override
469 other methods of output (unlike version 3.00). If I<to_string> is false,
470 the method will return 1 rather than a string.
471  
472 =item use_id
473  
474 use_id => 1
475  
476 Use id="I<name>" for anchors rather than <a name="I<name>"/> anchors.
477 However if an anchor already exists for a Significant Element, this
478 won't make an id for that particular element.
479  
480 =item useorg
481  
482 useorg => 1
483  
484 Use pre-existing backup files as the input source; that is, files of the
485 form I<infile>.I<bak> (see B<input> and B<bak>).
486  
487 =back
488  
489 =cut
490 31 ▶sub generate_toc ($%) {
Subroutine generate_toc: called CC=25 SCAR=33.0
Pod documented
491 31 my $self = shift;
492 my %args = (
493 make_anchors=>1,
494 make_toc=>1,
495 input=>undef,
496 filenames=>undef,
497 bak=>$self->{bak},
498 debug=>$self->{debug},
499 useorg=>$self->{useorg},
500 use_id=>$self->{use_id},
501 notoc_match=>$self->{notoc_match},
502 toc_entry=>$self->{toc_entry},
503 toc_end=>$self->{toc_end},
504 overwrite=>$self->{overwrite},
505 ol=>$self->{ol},
506 ol_num_levels=>$self->{ol_num_levels},
507 entrysep=>$self->{entrysep},
508 ignore_only_one=>$self->{ignore_only_one},
509 @_
510 31 );
511  
512 31 ▶ if ($args{debug})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"debug"}) 0 31 31
513 {
514 0 print STDERR Dumper(\%args);
515 }
516 31 ▶ if (!$args{input})
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"input"}) 0 31 31
517 {
518 0 warn "generate_toc: no input given\n";
519 0 return '';
520 }
521 #
522 # get the input
523 #
524 31 my @filenames = ();
525 31 my @input = ();
526 31 ▶ if (ref $args{input} eq "ARRAY")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{'input'} eq 'ARRAY') { } 23 8 31
527 {
528 23 @filenames = @{$args{input}};
529 23 my $i = 0;
530 23 my $fh_needs_closing = 0;
531 23 foreach my $fn (@filenames)
532 {
533 24 my $infn = $fn;
534 24 my $bakfile = $fn . "." . $args{bak};
535 24 ▶ if ($args{useorg}
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"useorg"} and $args{"bak"} and -e $bakfile) 0 24 24
Condition33%2/6
$args{"useorg"} and $args{"bak"} and -e $bakfile
!ll&&!rl&&r
2400
$args{"useorg"} and $args{"bak"}
!ll&&!rl&&r
2400
MC/DC0%0/3
$args{"useorg"} and $args{"bak"} and -e $bakfile
$args{"useorg"} $args{"bak"} -e $bakfile
Truth table25%1/4
A and B and C
A= $args{"useorg"}B= $args{"bak"}C= -e $bakfile
ABCresult
0XX0
10X0
1100
1111
536 && $args{bak}
537 && -e $bakfile)
538 {
539 # use the old backup files as source
540 0 $infn = $bakfile;
541 }
542 24 my $fh = undef;
543 # using '-' means STDIN
544 24 ▶ if ($infn eq '-')
Branch: 1 branch
BranchTrueFalseTotal
if ($infn eq '-') { } 0 24 24
545 {
546 0 $fh = *STDIN;
547 0 $fh_needs_closing = 0;
548 }
549 else
550 {
551 24 ▶ open ($fh, $infn) ||
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $infn or die "Error: unable to open ", $infn, ": $!\n" 24 0 24
552 die "Error: unable to open ", $infn, ": $!\n";
553 24 $fh_needs_closing = 1;
554 }
555  
556 24 my $content = '';
557 {
558 24 local $/; # slurp entire file
559 24 $content = <$fh>;
560 24 ▶ close ($fh) if ($fh_needs_closing);
Branch: 1 branch
BranchTrueFalseTotal
if $fh_needs_closing 24 0 24
561 }
562 24 $input[$i] = $content;
563  
564 24 $i++;
565 }
566 }
567 else
568 {
569 8 $filenames[0] = '';
570 8 $input[0] = $args{input};
571 }
572 # overwrite the filenames array if a replacement
573 # was passed in and has the same length
574 31 ▶ if (defined $args{filenames}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"filenames"} and @{$args{"filenames"};} and $#{$args{"filenames"};} == $#filenames) 6 25 31
Condition66%4/6
defined $args{"filenames"} and @{$args{"filenames"};} and $#{$args{"filenames"};} == $#filenames
!ll&&!rl&&r
2506
defined $args{"filenames"} and @{$args{"filenames"};}
!ll&&!rl&&r
2506
MC/DC33%1/3
defined $args{"filenames"} and @{$args{"filenames"};} and $#{$args{"filenames"};} == $#filenames
defined $args{"filenames"} @{$args{"filenames"};} $#{$args{"filenames"};} == $#filenames
Truth table50%2/4
A and B and C
A= defined $args{"filenames"}B= @{$args{"filenames"};}C= $#{$args{"filenames"};} == $#filenames
ABCresult
0XX0
10X0
1100
1111
575 6 && @{$args{filenames}}
576 6 && $#{$args{filenames}} == $#{filenames}
577 )
578 {
579 6 @filenames = @{$args{filenames}};
580 }
581  
582 #
583 # make the anchors
584 #
585 31 ▶ if ($args{make_anchors})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"make_anchors"}) 14 17 31
586 {
587 14 my $i = 0;
588 14 foreach my $fn (@filenames)
589 {
590 14 my $html_str = $input[$i];
591 14 $input[$i] = $self->make_anchors(%args,
592 filename=>$fn,
593 input=>$html_str);
594 14 $i++;
595 }
596 }
597  
598 #
599 # make the ToC
600 #
601 31 my $toc_str = '';
602 31 ▶ if ($args{make_toc})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"make_toc"}) 19 12 31
603 {
604 19 my %labels = ();
605 19 my @list_of_lists = ();
606 19 my $i = 0;
607 19 ▶ for (my $i = 0; $i < @filenames; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @filenames) 19 19 38
608 {
609 19 my @the_list = $self->make_toc_list(%args,
610 first_file=>$filenames[0],
611 labels=>\%labels,
612 filename=>$filenames[$i],
613 input=>$input[$i]);
614 19 ▶ if (!($args{ignore_only_one}
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"ignore_only_one"} and @the_list <= 1) 18 1 19
Condition66%2/3
$args{"ignore_only_one"} and @the_list <= 1
!ll&&!rl&&r
1801
MC/DC50%1/2
$args{"ignore_only_one"} and @the_list <= 1
$args{"ignore_only_one"} @the_list <= 1
Truth table66%2/3
A and B
A= $args{"ignore_only_one"}B= @the_list <= 1
ABresult
0X0
100
111
615 and @the_list <= 1))
616 {
617 18 push @list_of_lists, @the_list;
618 }
619 }
620 19 ▶ if (@list_of_lists > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (@list_of_lists > 0) 18 1 19
621 {
622 #
623 # create the appropriate format
624 #
625 18 my %formats = ();
626 # check for non-list entries, flagged by negative levels
627 18 ▶ while (my ($key, $val) = each %{$args{toc_entry}})
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$args{"toc_entry"};}) 39 18 57
628 {
629 39 ▶ if ($val < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($val < 0) 1 38 39
630 {
631 1 $formats{abs($val) - 1} = {};
632 1 $formats{abs($val) - 1}->{tree_head} = '<ul><li>';
633 1 $formats{abs($val) - 1}->{tree_foot} = "\n</li></ul>\n";
634 1 $formats{abs($val) - 1}->{item_sep} = $args{entrysep};
635 1 $formats{abs($val) - 1}->{pre_item} = '';
636 1 $formats{abs($val) - 1}->{post_item} = '';
637 }
638 }
639 # check for OL
640 18 ▶ if ($args{ol})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"ol"}) 4 14 18
641 {
642 4 $formats{0} = {};
643 4 $formats{0}->{tree_head} = '<ol>';
644 4 $formats{0}->{tree_foot} = "\n</ol>";
645 4 ▶ if ($args{ol_num_levels} > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"ol_num_levels"} > 0) 3 1 4
646 {
647 3 $formats{$args{ol_num_levels}} = {};
648 3 $formats{$args{ol_num_levels}}->{tree_head} = '<ul>';
649 3 $formats{$args{ol_num_levels}}->{tree_foot} = "\n</ul>";
650 }
651 }
652 18 $toc_str = HTML::LinkList::link_tree(
653 %args,
654 link_tree=>\@list_of_lists,
655 labels=>\%labels,
656 formats=>\%formats,
657 );
658 }
659 }
660  
661 #
662 # put the output
663 #
664 31 my $ret = $self->output_toc(
665 %args,
666 toc=>$toc_str,
667 input=>\@input,
668 filenames=>\@filenames,
669 );
670  
671 31 return $ret;
672  
673 } # generate_toc
674  
675 =head1 INTERNAL METHODS
676  
677 These methods are documented for developer purposes and aren't intended
678 to be used externally.
679  
680 =head2 make_anchor_name
681  
682 $toc->make_anchor_name(content=>$content,
683 anchors=>\%anchors);
684  
685 Makes the anchor-name for one anchor.
686 Bases the anchor on the content of the significant element.
687 Ensures that anchors are unique.
688  
689 =cut
690  
691 50 ▶sub make_anchor_name ($%) {
Subroutine make_anchor_name: called CC=5 SCAR=16.2
Pod documented
692 50 my $self = shift;
693 50 my %args = (
694 content=>'', # will be overwritten by one of @_
695 anchors=>undef,
696 @_
697 );
698 50 my $name = $args{content}; # the anchor name will most often be very close to the token content
699  
700 50 ▶ if ($name !~ /^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $name =~ /^\s*$/) { } 50 0 50
701 # generate a SEO-friendly anchor right from the token content
702 # The allowed character set is limited first by the URI specification
703 # for fragments, http://tools.ietf.org/html/rfc3986#section-2:
704 # characters then by the limitations of the values of 'id' and 'name'
705 # attributes: http://www.w3.org/TR/REC-html40/types.html#type-name
706 # Eventually, the only punctuation allowed in id values is [_.:-]
707  
708 # we need to replace [#&;] only when they are NOT part of an HTML
709 # entity. decode_entities saves us from crafting a nasty regexp
710 50 decode_entities($name);
711 # MediaWiki also uses the period, see
712 # http://en.wikipedia.org/wiki/Hierarchies#Ethics.2C_behavioral_psychology.2C_philosophies_of_identity
713 50 $name =~ s/([^\s\w_.:-])/'.'.sprintf('%02X', ord($1))/eg;
714  
715 50 $name =~ s/\s+/_/g;
716 # "ID and NAME tokens must begin with a letter ([A-Za-z])"
717 50 $name =~ s/^[^a-zA-Z]+//;
718 }
719 else
720 {
721 0 $name = 'id';
722 }
723 50 ▶ $name = 'id' if $name eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "" 0 50 50
724  
725 # check if it already exists; if so, add a number
726 50 my $anch_num = 1;
727 50 my $word_name = $name;
728 50 my $name_key = lc $name;
729 # Reference: http://www.w3.org/TR/REC-html40/struct/links.html#h-12.2.1
730 # Anchor names must be unique within a document. Anchor names that differ
731 # only in case may not appear in the same document.
732 50 ▶ while (defined $args{anchors}->{$name_key}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"anchors"}{$name_key} and $args{"anchors"}{$name_key}) 10 50 60
Condition66%2/3
defined $args{"anchors"}{$name_key} and $args{"anchors"}{$name_key}
!ll&&!rl&&r
50010
MC/DC50%1/2
defined $args{"anchors"}{$name_key} and $args{"anchors"}{$name_key}
defined $args{"anchors"}{$name_key} $args{"anchors"}{$name_key}
Truth table66%2/3
A and B
A= defined $args{"anchors"}{$name_key}B= $args{"anchors"}{$name_key}
ABresult
0X0
100
111
733 && $args{anchors}->{$name_key})
734 {
735 10 $name = $word_name . "_$anch_num";
736 10 $name_key = lc $name;
737 10 $anch_num++;
738 }
739  
740 50 return $name;
741 } # make_anchor_name
742  
743 =head2 make_anchors
744  
745 my $new_html = $toc->make_anchors(input=>$html,
746 notoc_match=>$notoc_match,
747 use_id=>$use_id,
748 toc_entry=>\%toc_entries,
749 toc_end=>\%toc_ends,
750 );
751  
752 Makes the anchors the given input string.
753 Returns a string.
754  
755 =cut
756  
757 14 ▶sub make_anchors ($%) {
Subroutine make_anchors: called CC=40 SCAR=40.8
Pod documented
758 14 my $self = shift;
759 my %args = (
760 input=>'',
761 notoc_match=>$self->{notoc_match},
762 use_id=>$self->{use_id},
763 toc_entry=>$self->{toc_entry},
764 toc_end=>$self->{toc_end},
765 debug=>$self->{debug},
766 quiet=>$self->{quiet},
767 @_
768 14 );
769 14 my $html_str = $args{input};
770  
771 print STDERR "Making anchors for ", $args{filename}, "...\n"
772 14 ▶ if (!$args{quiet} && $args{filename});
Branch: 1 branch
BranchTrueFalseTotal
if not $args{"quiet"} and $args{"filename"} 0 14 14
Condition66%2/3
not $args{"quiet"} and $args{"filename"}
!ll&&!rl&&r
1310
MC/DC0%0/2
not $args{"quiet"} and $args{"filename"}
not $args{"quiet"} $args{"filename"}
Truth table66%2/3
not(A) and B
A= not $args{"quiet"}B= $args{"filename"}
ABresult
0X0
100
111
773  
774 14 my @newhtml = ();
775 14 my %anchors = ();
776 # Note that the keys to the anchors hash should be lower-cased
777 # since anchor names that differ only in case are not allowed.
778  
779 # parse the HTML
780 14 my $hp = new HTML::SimpleParse();
781 14 $hp->text($html_str);
782 14 $hp->parse();
783  
784 14 my $tag;
785 my $endtag;
786 14 my $level = 0;
787 14 my $tmp;
788 14 my $adone = 0;
789 14 my $name = '';
790 # go through the HTML
791 14 my $tok;
792 my $next_tok;
793 14 my $i;
794 14 my $notoc = $args{notoc_match};
795 14 my @tree = $hp->tree();
796 14 ▶ while (@tree) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tree) 1102 14 1116
797 1102 $tok = shift @tree;
798 1102 $next_tok = $tree[0];
799 1102 ▶ if ($tok->{type} ne 'starttag')
Branch: 1 branch
BranchTrueFalseTotal
if ($tok->{"type"} ne "starttag") 754 348 1102
800 {
801 754 push @newhtml, $hp->execute($tok);
802 754 next;
803 }
804 # assert: we have a start tag
805 348 $level = 0;
806
807 # check if tag included in TOC (significant element)
808 348 foreach my $key (keys %{$args{toc_entry}}) {
809 689 ▶ if ($tok->{content} =~ /^$key/i
Branch: 1 branch
BranchTrueFalseTotal
if ($tok->{"content"} =~ /^$key/i and not $notoc && $tok->{"content"} =~ /$notoc/) 56 633 689
Condition83%5/6
$tok->{"content"} =~ /^$key/i and not $notoc && $tok->{"content"} =~ /$notoc/
!ll&&!rl&&r
628556
$notoc && $tok->{'content'} =~ /$notoc/
!ll&&!rl&&r
0565
MC/DC66%2/3
$tok->{"content"} =~ /^$key/i and not $notoc && $tok->{"content"} =~ /$notoc/
$tok->{"content"} =~ /^$key/i $notoc $tok->{'content'} =~ /$notoc/
Truth table75%3/4
A and not(B && C)
A= $tok->{"content"} =~ /^$key/iB= $notocC= $tok->{'content'} =~ /$notoc/
ABCresult
0XX0
1110
10X1
1101
810 && (!$notoc
811 || $tok->{content} !~ /$notoc/)) {
812 56 $tag = $key;
813 # level of significant element
814 56 $level = abs($args{toc_entry}->{$key});
815 # End tag of significant element
816 56 $endtag = $args{toc_end}->{$key};
817 56 last;
818 }
819 }
820 # if $level is not set, we didn't find a Significant tag
821 348 ▶ if (!$level) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($level) 292 56 348
822 292 push @newhtml, $hp->execute($tok);
823 292 next;
824 }
825 # assert: current tag is a Significant tag
826  
827 #
828 # Add A element or ID to document
829 #
830 56 my $name_in_anchor = 0;
831 56 $adone = 0;
832 56 $name = '';
833 56 my $sig_tok = $tok;
834 56 ▶ if ($tag =~ /title/i) { # TITLE tag is a special case
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /title/i) 0 56 56
835 0 $adone = 1;
836 }
837 56 ▶ if ($args{use_id})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'use_id'}) { } 9 47 56
838 {
839 # is there an existing ID?
840 9 ▶ if ($sig_tok->{content} =~ /ID\s*=\s*(['"])/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig_tok->{"content"} =~ /ID\s*=\s*(['"])/i) 1 8 9
841 1 my $q = $1;
842 1 ($name) = $sig_tok->{content} =~ m/ID\s*=\s*$q([^$q]*)$q/i;
843 1 ▶ if ($name)
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 1 0 1
844 {
845 1 $anchors{lc $name} = $name;
846 1 push @newhtml, $hp->execute($sig_tok);
847 1 $adone = 1;
848 }
849 else # if the ID has no name, remove it!
850 {
851 0 $sig_tok->{content} =~ s/ID\s*=\s*$q$q//i;
852 }
853 }
854 }
855 else # not adding ID, move right along
856 {
857 47 push @newhtml, $hp->execute($tok);
858 }
859 # Find the "name" of the significant element
860 # Don't consume the tree, because ID behaves differently from A
861 56 my $i = 0;
862 56 ▶ while (!$name && $i < @tree)
Branch: 1 branch
BranchTrueFalseTotal
if (not $name and $i < @tree) 55 56 111
Condition66%2/3
not $name and $i < @tree
!ll&&!rl&&r
56055
MC/DC50%1/2
not $name and $i < @tree
not $name $i < @tree
Truth table66%2/3
not(A) and B
A= not $nameB= $i < @tree
ABresult
0X0
100
111
863 {
864 55 $tok = $tree[$i];
865 55 $next_tok = $tree[$i + 1];
866 55 ▶ if ($tok->{type} eq 'text') {
Branch: 3 branches
BranchTrueFalseTotal
if ($tok->{'type'} eq 'text') { } 50 5 55
elsif (not $adone and $tok->{'type'} eq 'starttag' and $tok->{'content'} =~ /^A/i) { } 5 0 5
elsif ($tok->{'type'} eq 'starttag' or $tok->{'type'} eq 'endtag') { } 0 0 0
867 $name = $self->make_anchor_name(content=>$tok->{content},
868 50 anchors=>\%anchors);
869 # Anchor
870 ▶ } elsif (!$adone && $tok->{type} eq 'starttag'
Condition33%2/6
not $adone and $tok->{"type"} eq "starttag" and $tok->{"content"} =~ /^A/i
!ll&&!rl&&r
005
not $adone and $tok->{"type"} eq "starttag"
!ll&&!rl&&r
005
MC/DC0%0/3
not $adone and $tok->{"type"} eq "starttag" and $tok->{"content"} =~ /^A/i
not $adone $tok->{"type"} eq "starttag" $tok->{"content"} =~ /^A/i
Truth table25%1/4
not(A) and B and C
A= not $adoneB= $tok->{"type"} eq "starttag"C= $tok->{"content"} =~ /^A/i
ABCresult
0XX0
10X0
1100
1111
871 && $tok->{content} =~ /^A/i)
872 {
873 5 ▶ if ($tok->{content} =~ /NAME\s*=\s*(['"])/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tok->{'content'} =~ /NAME\s*=\s*(['"])/i) { } 5 0 5
elsif ($next_tok->{'type'} eq 'text') { } 0 0 0
874 5 my $q = $1;
875 5 ($name) = $tok->{content} =~ m/NAME\s*=\s*$q([^$q]*)$q/i;
876 5 $name_in_anchor = 1;
877 } elsif ($next_tok->{type} eq 'text') {
878 $name = $self->make_anchor_name(content=>$next_tok->{content},
879 0 anchors=>\%anchors);
880 }
881 ▶ } elsif ($tok->{type} eq 'starttag'
Condition0%0/3
$tok->{"type"} eq "starttag" or $tok->{"type"} eq "endtag"
l!l&&r!l&&!r
000
MC/DC0%0/2
$tok->{"type"} eq "starttag" or $tok->{"type"} eq "endtag"
$tok->{"type"} eq "starttag" $tok->{"type"} eq "endtag"
Truth table0%0/3
A or B
A= $tok->{"type"} eq "starttag"B= $tok->{"type"} eq "endtag"
ABresult
1X1
011
000
882 || $tok->{type} eq 'endtag')
883 { # Tag
884 0 ▶ last if $tok->{content} =~ m|$endtag|i;
Branch: 1 branch
BranchTrueFalseTotal
if $tok->{"content"} =~ /$endtag/i 0 0 0
885 }
886 55 $i++;
887 }
888 # assert: there is a name, or there is no name to be found
889 56 ▶ if (!$name)
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 56 56
890 {
891 # make up a name
892 0 $name = $self->make_anchor_name(content=>"TOC",
893 anchors=>\%anchors);
894 }
895 56 ▶ if (!$adone && $args{use_id})
Branch: 1 branch
BranchTrueFalseTotal
if (not $adone and $args{"use_id"}) 8 48 56
Condition100%3/3
not $adone and $args{"use_id"}
!ll&&!rl&&r
1478
MC/DC100%2/2
not $adone and $args{"use_id"}
not $adone $args{"use_id"}
Truth table100%3/3
not(A) and B
A= not $adoneB= $args{"use_id"}
ABresult
0X0
100
111
896 {
897 8 ▶ if (!$name_in_anchor)
Branch: 1 branch
BranchTrueFalseTotal
if (not $name_in_anchor) { } 8 0 8
898 {
899 8 $anchors{lc $name} = $name;
900 # add the ID
901 8 $sig_tok->{content} .= " id='$name'";
902 8 push @newhtml, $hp->execute($sig_tok);
903 8 $adone = 1;
904 }
905 else
906 {
907 # we have an already-named anchor, so don't add an ID
908 0 push @newhtml, $hp->execute($sig_tok);
909 }
910 }
911
912 56 ▶ while (@tree) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tree) 122 0 122
913 122 $tok = shift @tree;
914 122 $next_tok = $tree[0];
915 # Text
916 122 ▶ if ($tok->{type} eq 'text') {
Branch: 3 branches
BranchTrueFalseTotal
if ($tok->{'type'} eq 'text') { } 56 66 122
elsif (not $adone and $tok->{'type'} eq 'starttag' and $tok->{'content'} =~ /^A/i) { } 5 61 66
elsif ($tok->{'type'} eq 'starttag' or $tok->{'type'} eq 'endtag') { } 61 0 61
917 56 ▶ if (!$adone && $tok->{content} !~ /^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $adone || $tok->{'content'} =~ /^\s*$/) { } 42 14 56
Condition66%2/3
$adone || $tok->{'content'} =~ /^\s*$/
l!l&&r!l&&!r
14042
MC/DC50%1/2
$adone || $tok->{'content'} =~ /^\s*$/
$adone $tok->{'content'} =~ /^\s*$/
Truth table66%2/3
A || B
A= $adoneB= $tok->{'content'} =~ /^\s*$/
ABresult
1X1
011
000
918 42 $anchors{lc $name} = $name;
919 # replace the text with an anchor containing the text
920 42 push(@newhtml, qq|<a name="$name">$tok->{content}</a>|);
921 42 $adone = 1;
922 } else {
923 14 push @newhtml, $hp->execute($tok);
924 }
925 # Anchor
926 ▶ } elsif (!$adone && $tok->{type} eq 'starttag'
Condition66%4/6
not $adone and $tok->{"type"} eq "starttag" and $tok->{"content"} =~ /^A/i
!ll&&!rl&&r
6105
not $adone and $tok->{"type"} eq "starttag"
!ll&&!rl&&r
6105
MC/DC33%1/3
not $adone and $tok->{"type"} eq "starttag" and $tok->{"content"} =~ /^A/i
not $adone $tok->{"type"} eq "starttag" $tok->{"content"} =~ /^A/i
Truth table50%2/4
not(A) and B and C
A= not $adoneB= $tok->{"type"} eq "starttag"C= $tok->{"content"} =~ /^A/i
ABCresult
0XX0
10X0
1100
1111
927 && $tok->{content} =~ /^A/i)
928 {
929 # is there an existing NAME anchor?
930 5 ▶ if ($name_in_anchor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name_in_anchor) { } 5 0 5
931 5 $anchors{lc $name} = $name;
932 5 push @newhtml, $hp->execute($tok);
933 } else {
934 # add the current name anchor
935 0 $tmp = $hp->execute($tok);
936 0 $tmp =~ s/^(<A)(.*)$/$1 name="$name" $2/i;
937 0 push @newhtml, $tmp;
938 0 $anchors{lc $name} = $name;
939 }
940 5 $adone = 1;
941 ▶ } elsif ($tok->{type} eq 'starttag'
Condition33%1/3
$tok->{"type"} eq "starttag" or $tok->{"type"} eq "endtag"
l!l&&r!l&&!r
0610
MC/DC0%0/2
$tok->{"type"} eq "starttag" or $tok->{"type"} eq "endtag"
$tok->{"type"} eq "starttag" $tok->{"type"} eq "endtag"
Truth table33%1/3
A or B
A= $tok->{"type"} eq "starttag"B= $tok->{"type"} eq "endtag"
ABresult
1X1
011
000
942 || $tok->{type} eq 'endtag')
943 { # Tag
944 61 push @newhtml, $hp->execute($tok);
945 61 ▶ last if $tok->{content} =~ m|$endtag|i;
Branch: 1 branch
BranchTrueFalseTotal
if $tok->{"content"} =~ /$endtag/i 56 5 61
946 }
947 else {
948 0 push @newhtml, $hp->execute($tok);
949 }
950 }
951 }
952 14 my $out = join('', @newhtml);
953  
954 14 return $out;
955 } # make_anchors
956  
957 =head2 make_toc_list
958  
959 my @toc_list = $toc->make_toc_list(input=>$html,
960 labels=>\%labels,
961 notoc_match=>$notoc_match,
962 toc_entry=>\%toc_entry,
963 toc_end=>\%toc_end,
964 filename=>$filename);
965  
966 Makes a list of lists which represents the structure and content
967 of (a portion of) the ToC from one file.
968 Also updates a list of labels for the ToC entries.
969  
970 =cut
971  
972 19 ▶sub make_toc_list ($%) {
Subroutine make_toc_list: called CC=41 SCAR=41.8
Pod documented
973 19 my $self = shift;
974 my %args = (
975 input=>'',
976 filename=>'',
977 labels=>undef,
978 notoc_match=>$self->{notoc_match},
979 toc_entry=>$self->{toc_entry},
980 toc_end=>$self->{toc_end},
981 inline=>$self->{inline},
982 debug=>$self->{debug},
983 toc_before=>$self->{toc_before},
984 toc_after=>$self->{toc_after},
985 textonly=>$self->{textonly},
986 ignore_sole_first=>$self->{ignore_sole_first},
987 ignore_only_one=>$self->{ignore_only_one},
988 @_
989 19 );
990 19 my $html_str = $args{input};
991 19 my $infile = $args{filename};
992 19 my $labels = $args{labels};
993  
994 19 my $toc_str = "";
995 19 my @toc = ();
996 19 my @list_of_paths = ();
997 19 my %level_count = ();
998  
999 # parse the HTML
1000 19 my $hp = new HTML::SimpleParse();
1001 19 $hp->text($html_str);
1002 19 $hp->parse();
1003  
1004 19 my $noli;
1005 my $prevnoli;
1006 19 my $before = "";
1007 19 my $after = "";
1008 19 my $tag;
1009 my $endtag;
1010 19 my $level = 0;
1011 19 my $levelopen;
1012 my $tmp;
1013 19 my $content;
1014 19 my $adone = 0;
1015 19 my $name = "NOTOC"; # if no anchor is found...
1016 19 my $is_title;
1017 19 my $found_title = 0;
1018 19 my $notoc = $args{notoc_match};
1019 # go through the HTML
1020 19 my $tok;
1021 19 my @tree = $hp->tree();
1022 19 ▶ while (@tree) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tree) 1690 19 1709
1023 1690 $tok = shift @tree;
1024 1690 $level = 0;
1025 1690 $is_title = 0;
1026 1690 $tag = '';
1027 1690 ▶ if ($tok->{type} eq 'starttag')
Branch: 1 branch
BranchTrueFalseTotal
if ($tok->{"type"} eq "starttag") 582 1108 1690
1028 {
1029 # check if tag included in TOC
1030 582 foreach my $key (keys %{$args{toc_entry}}) {
1031 1253 ▶ if ($tok->{content} =~ /^$key/i
Branch: 1 branch
BranchTrueFalseTotal
if ($tok->{"content"} =~ /^$key/i and not $notoc && $tok->{"content"} =~ /$notoc/) 77 1176 1253
Condition83%5/6
$tok->{"content"} =~ /^$key/i and not $notoc && $tok->{"content"} =~ /$notoc/
!ll&&!rl&&r
1171577
$notoc && $tok->{'content'} =~ /$notoc/
!ll&&!rl&&r
0775
MC/DC66%2/3
$tok->{"content"} =~ /^$key/i and not $notoc && $tok->{"content"} =~ /$notoc/
$tok->{"content"} =~ /^$key/i $notoc $tok->{'content'} =~ /$notoc/
Truth table75%3/4
A and not(B && C)
A= $tok->{"content"} =~ /^$key/iB= $notocC= $tok->{'content'} =~ /$notoc/
ABCresult
0XX0
1110
10X1
1101
1032 && (!$notoc
1033 || $tok->{content} !~ /$notoc/)) {
1034 77 $tag = $key;
1035 77 ▶ if ($args{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"debug"}) 0 77 77
1036 0 print STDERR "============\n";
1037 0 print STDERR "key = $key ";
1038 0 print STDERR "tok->content = '", $tok->{content}, "' ";
1039 0 print STDERR "tag = $tag";
1040 0 print STDERR "\n============\n";
1041 }
1042 # level of significant element
1043 77 $level = abs($args{toc_entry}->{$key});
1044 # no <li> used in ToC listing
1045 77 $noli = $args{toc_entry}->{$key} < 0;
1046 # End tag of significant element
1047 77 $endtag = $args{toc_end}->{$key};
1048 77 ▶ if (defined $args{toc_before}->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'toc_before'}{$key}) { } 0 77 77
1049 0 $before = $args{toc_before}->{$key};
1050 } else {
1051 77 $before = "";
1052 }
1053 77 ▶ if (defined $args{toc_after}->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'toc_after'}{$key}) { } 0 77 77
1054 0 $after = $args{toc_after}->{$key};
1055 } else {
1056 77 $after = "";
1057 }
1058 }
1059 }
1060 }
1061 1690 ▶ if (!$level) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($level) 1613 77 1690
1062 1613 next;
1063 }
1064 77 ▶ if ($args{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"debug"}) 0 77 77
1065 0 print STDERR "Chosen tag:$tag\n";
1066 }
1067 # assert: we are at a Significant tag
1068  
1069 # get A element or ID from document
1070 # This assumes that there is one there
1071 77 $content = '';
1072 77 $adone = 0;
1073 77 ▶ if ($tag =~ /title/i) { # TITLE tag is a special case
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /title/i) 0 77 77
1074 0 ▶ if ($found_title) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found_title) { } 0 0 0
1075 # don't need to find a title again, we found it
1076 0 next;
1077 } else {
1078 0 $is_title = 1; $adone = 1;
1079 0 $found_title = 1;
1080 }
1081 }
1082 77 ▶ if ($args{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"debug"}) 0 77 77
1083 0 print STDERR "is_title:$is_title\n";
1084 }
1085 # check for an ID before we skip this tag
1086 77 ▶ if ($tok->{content} =~ /ID\s*=\s*(['"])/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tok->{"content"} =~ /ID\s*=\s*(['"])/i) 8 69 77
1087 8 my $q = $1;
1088 8 ($name) = $tok->{content} =~ m/ID\s*=\s*$q([^$q]*)$q/i;
1089 8 $adone = 1;
1090 }
1091 77 ▶ while (@tree) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tree) 342 0 342
1092 342 $tok = shift @tree;
1093 # Text
1094 342 ▶ if ($tok->{type} eq 'text') {
Branch: 3 branches
BranchTrueFalseTotal
if ($tok->{'type'} eq 'text') { } 103 239 342
elsif (not $adone and $tok->{'type'} eq 'starttag' and $tok->{'content'} =~ /^A/i) { } 69 170 239
elsif ($tok->{'type'} eq 'starttag' or $tok->{'type'} eq 'endtag') { } 170 0 170
1095 103 $content .= $tok->{content};
1096 103 ▶ if ($args{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"debug"}) 0 103 103
1097 0 print STDERR "tok-content = ", $tok->{content}, "\n";
1098 0 print STDERR "content = $content\n";
1099 }
1100 # Anchor
1101 ▶ } elsif (!$adone && $tok->{type} eq 'starttag'
Condition66%4/6
not $adone and $tok->{"type"} eq "starttag" and $tok->{"content"} =~ /^A/i
!ll&&!rl&&r
170069
not $adone and $tok->{"type"} eq "starttag"
!ll&&!rl&&r
170069
MC/DC33%1/3
not $adone and $tok->{"type"} eq "starttag" and $tok->{"content"} =~ /^A/i
not $adone $tok->{"type"} eq "starttag" $tok->{"content"} =~ /^A/i
Truth table50%2/4
not(A) and B and C
A= not $adoneB= $tok->{"type"} eq "starttag"C= $tok->{"content"} =~ /^A/i
ABCresult
0XX0
10X0
1100
1111
1102 && $tok->{content} =~ /^A/i)
1103 {
1104 69 ▶ if ($tok->{content} =~ /NAME\s*=\s*(['"])/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tok->{"content"} =~ /NAME\s*=\s*(['"])/i) 69 0 69
1105 69 my $q = $1;
1106 69 ($name) = $tok->{content} =~ m/NAME\s*=\s*$q([^$q]*)$q/i;
1107 69 $adone = 1;
1108 }
1109 ▶ } elsif ($tok->{type} eq 'starttag'
Condition66%2/3
$tok->{"type"} eq "starttag" or $tok->{"type"} eq "endtag"
l!l&&r!l&&!r
161540
MC/DC0%0/2
$tok->{"type"} eq "starttag" or $tok->{"type"} eq "endtag"
$tok->{"type"} eq "starttag" $tok->{"type"} eq "endtag"
Truth table66%2/3
A or B
A= $tok->{"type"} eq "starttag"B= $tok->{"type"} eq "endtag"
ABresult
1X1
011
000
1110 || $tok->{type} eq 'endtag')
1111 { # Tag
1112 170 ▶ if ($args{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"debug"}) 0 170 170
1113 print STDERR "file = ", $infile,
1114 " tag = $tag, endtag = '$endtag",
1115 "' tok-type = ", $tok->{type},
1116 0 " tok-content = '", $tok->{content}, "'\n";
1117 }
1118 170 ▶ last if $tok->{content} =~ m#$endtag#i;
Branch: 1 branch
BranchTrueFalseTotal
if $tok->{"content"} =~ /$endtag/i 77 93 170
1119 $content .= $hp->execute($tok)
1120 unless $args{textonly}
1121 93 ▶ || $tok->{content} =~ m#/?(hr|p|a|img)#i;
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"textonly"} or $tok->{"content"} =~ m[/?(hr|p|a|img)]i 0 93 93
Condition33%1/3
$args{"textonly"} or $tok->{"content"} =~ m[/?(hr|p|a|img)]i
l!l&&r!l&&!r
0930
MC/DC0%0/2
$args{"textonly"} or $tok->{"content"} =~ m[/?(hr|p|a|img)]i
$args{"textonly"} $tok->{"content"} =~ m[/?(hr|p|a|img)]i
Truth table33%1/3
A or B
A= $args{"textonly"}B= $tok->{"content"} =~ m[/?(hr|p|a|img)]i
ABresult
1X1
011
000
1122 }
1123  
1124 }
1125 77 ▶ if ($args{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"debug"}) 0 77 77
1126 0 print STDERR "Chosen content:'$content'\n";
1127 }
1128  
1129 77 ▶ if ($content =~ /^\s*$/) { # Check for empty content
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /^\s*$/) { } 0 77 77
1130 0 warn "Warning: A $tag in $infile has no content; $tag skipped\n";
1131 0 next;
1132 } else {
1133 77 $content =~ s/^\s+//; # Strip beginning whitespace
1134 77 $content =~ s/\s+$//; # Strip end whitespace
1135 77 $content = $before . $content . $after;
1136 }
1137 # figure out the anchor link needed
1138 77 my $link = '';
1139 77 ▶ if ($args{inline} and $args{first_file} eq $infile)
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'inline'} and $args{'first_file'} eq $infile) { } 19 58 77
Condition66%2/3
$args{"inline"} and $args{"first_file"} eq $infile
!ll&&!rl&&r
58019
MC/DC50%1/2
$args{"inline"} and $args{"first_file"} eq $infile
$args{"inline"} $args{"first_file"} eq $infile
Truth table66%2/3
A and B
A= $args{"inline"}B= $args{"first_file"} eq $infile
ABresult
0X0
100
111
1140 {
1141 19 ▶ $link = (!$is_title ? qq|#$name| : '');
Branch: 1 branch
BranchTrueFalseTotal
!$is_title ? : 19 0 19
1142 }
1143 else
1144 {
1145 58 ▶ $link .= join('',
Branch: 1 branch
BranchTrueFalseTotal
!$is_title ? : 58 0 58
1146 qq|$infile|,
1147 !$is_title ? qq|#$name| : '');
1148 }
1149 # Assert: we know the info about this TOC entry
1150 77 push @list_of_paths, {
1151 level=>$level,
1152 path=>$link,
1153 };
1154 77 $labels->{$link} = $content;
1155 77 $level_count{$level}++;
1156  
1157 77 $name = 'NOTOC';
1158 77 $prevnoli = $noli;
1159 } # while tree
1160  
1161 # If we want to ignore the first H1 if there's only one of them
1162 # if the first item is a level-0 item
1163 # and there is only one of them
1164 # then remove it and readjust levels
1165 19 ▶ if ($args{ignore_sole_first}
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'ignore_sole_first'} and $level_count{'1'} == 1 and $list_of_paths[0]{'level'} == 1) { } 1 18 19
elsif ($args{'ignore_only_one'} and @list_of_paths == 1) { } 1 34 35
Condition66%4/6
$args{"ignore_sole_first"} and $level_count{1} == 1 and $list_of_paths[0]{"level"} == 1
!ll&&!rl&&r
1801
$args{"ignore_sole_first"} and $level_count{1} == 1
!ll&&!rl&&r
1801
MC/DC33%1/3
$args{"ignore_sole_first"} and $level_count{1} == 1 and $list_of_paths[0]{"level"} == 1
$args{"ignore_sole_first"} $level_count{1} == 1 $list_of_paths[0]{"level"} == 1
Truth table50%2/4
A and B and C
A= $args{"ignore_sole_first"}B= $level_count{1} == 1C= $list_of_paths[0]{"level"} == 1
ABCresult
0XX0
10X0
1100
1111
1166 and $level_count{"1"} == 1
1167 and $list_of_paths[0]->{level} == 1)
1168 {
1169 1 shift @list_of_paths;
1170 1 ▶ for (my $i = 0; $i < @list_of_paths; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @list_of_paths) 1 1 2
1171 {
1172 1 $list_of_paths[$i]->{level}--;
1173 }
1174 }
1175 ▶ elsif ($args{ignore_only_one}
Condition66%2/3
$args{"ignore_only_one"} and @list_of_paths == 1
!ll&&!rl&&r
1701
MC/DC50%1/2
$args{"ignore_only_one"} and @list_of_paths == 1
$args{"ignore_only_one"} @list_of_paths == 1
Truth table66%2/3
A and B
A= $args{"ignore_only_one"}B= @list_of_paths == 1
ABresult
0X0
100
111
1176 and @list_of_paths == 1)
1177 {
1178 1 return ();
1179 }
1180  
1181 18 my @list_of_lists = ();
1182 18 @list_of_lists = $self->build_lol(
1183 paths=>\@list_of_paths);
1184  
1185 18 return @list_of_lists;
1186 } # make_toc_list
1187  
1188 =head2 build_lol
1189  
1190 Build a list of lists of paths, given a list
1191 of hashes with info about paths.
1192  
1193 =cut
1194 40 ▶sub build_lol {
Subroutine build_lol: called CC=13 SCAR=35.6
Pod documented
1195 40 my $self = shift;
1196 40 my %args = (
1197 paths=>undef,
1198 depth=>1,
1199 prepend_list=>undef,
1200 append_list=>undef,
1201 @_
1202 );
1203 40 my $paths_ref = $args{paths};
1204 40 my $depth = $args{depth};
1205  
1206 40 my @list_of_lists = ();
1207 40 ▶ while (@{$paths_ref})
Branch: 1 branch
BranchTrueFalseTotal
if (@{$paths_ref;}) 106 31 137
1208 {
1209 106 my $toc_entry = $paths_ref->[0];
1210 106 my $path_depth = $toc_entry->{level};
1211 106 my $path = $toc_entry->{path};
1212 106 ▶ if ($path_depth == $depth)
Branch: 3 branches
BranchTrueFalseTotal
if ($path_depth == $depth) { } 75 31 106
elsif ($path_depth > $depth) { } 22 9 31
elsif ($path_depth < $depth) { } 9 0 9
1213 {
1214 75 shift @{$paths_ref}; # use this path
1215 75 push @list_of_lists, $path;
1216 }
1217 elsif ($path_depth > $depth)
1218 {
1219 22 push @list_of_lists, [$self->build_lol(
1220 %args,
1221 prepend_list=>undef,
1222 append_list=>undef,
1223 paths=>$paths_ref,
1224 depth=>$path_depth,
1225 )];
1226 }
1227 elsif ($path_depth < $depth)
1228 {
1229 9 return @list_of_lists;
1230 }
1231 }
1232 # prepend the given list to the top level
1233 0 ▶ if (defined $args{prepend_list} and @{$args{prepend_list}})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"prepend_list"} and @{$args{"prepend_list"};}) 0 31 31
Condition33%1/3
defined $args{"prepend_list"} and @{$args{"prepend_list"};}
!ll&&!rl&&r
3100
MC/DC0%0/2
defined $args{"prepend_list"} and @{$args{"prepend_list"};}
defined $args{"prepend_list"} @{$args{"prepend_list"};}
Truth table33%1/3
A and B
A= defined $args{"prepend_list"}B= @{$args{"prepend_list"};}
ABresult
0X0
100
111
1234 {
1235 # if the list of lists is a single item which is a list
1236 # then add the extra list to that item
1237 0 ▶ if ($#list_of_lists == 0
Branch: 1 branch
BranchTrueFalseTotal
if ($#list_of_lists == 0 and ref $list_of_lists[0] eq 'ARRAY') { } 0 0 0
Condition0%0/3
$#list_of_lists == 0 and ref $list_of_lists[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$#list_of_lists == 0 and ref $list_of_lists[0] eq "ARRAY"
$#list_of_lists == 0 ref $list_of_lists[0] eq "ARRAY"
Truth table0%0/3
A and B
A= $#list_of_lists == 0B= ref $list_of_lists[0] eq "ARRAY"
ABresult
0X0
100
111
1238 and ref($list_of_lists[0]) eq "ARRAY")
1239 {
1240 0 unshift @{$list_of_lists[0]}, @{$args{prepend_list}};
1241 }
1242 else
1243 {
1244 0 unshift @list_of_lists, @{$args{prepend_list}};
1245 }
1246 }
1247 # append the given list to the top level
1248 0 ▶ if (defined $args{append_list} and @{$args{append_list}})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"append_list"} and @{$args{"append_list"};}) 0 31 31
Condition33%1/3
defined $args{"append_list"} and @{$args{"append_list"};}
!ll&&!rl&&r
3100
MC/DC0%0/2
defined $args{"append_list"} and @{$args{"append_list"};}
defined $args{"append_list"} @{$args{"append_list"};}
Truth table33%1/3
A and B
A= defined $args{"append_list"}B= @{$args{"append_list"};}
ABresult
0X0
100
111
1249 {
1250 # if the list of lists is a single item which is a list
1251 # then add the extra list to that item
1252 0 ▶ if ($#list_of_lists == 0
Branch: 1 branch
BranchTrueFalseTotal
if ($#list_of_lists == 0 and ref $list_of_lists[0] eq 'ARRAY') { } 0 0 0
Condition0%0/3
$#list_of_lists == 0 and ref $list_of_lists[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$#list_of_lists == 0 and ref $list_of_lists[0] eq "ARRAY"
$#list_of_lists == 0 ref $list_of_lists[0] eq "ARRAY"
Truth table0%0/3
A and B
A= $#list_of_lists == 0B= ref $list_of_lists[0] eq "ARRAY"
ABresult
0X0
100
111
1253 and ref($list_of_lists[0]) eq "ARRAY")
1254 {
1255 0 push @{$list_of_lists[0]}, @{$args{append_list}};
1256 }
1257 else
1258 {
1259 0 push @list_of_lists, @{$args{append_list}};
1260 }
1261 }
1262 31 return @list_of_lists;
1263 } # build_lol
1264  
1265 =head2 output_toc
1266  
1267 $self->output_toc(toc=>$toc_str,
1268 input=>\@input,
1269 filenames=>\@filenames);
1270  
1271 Put the output (whether to file, STDOUT or string).
1272 The "output" in this case could be the ToC, the modified
1273 (anchors added) HTML, or both.
1274  
1275 =cut
1276 31 ▶sub output_toc ($%) {
Subroutine output_toc: called CC=57 SCAR=60.3
Pod documented
1277 31 my $self = shift;
1278 my %args = (
1279 toc=>'',
1280 input=>undef,
1281 filenames=>undef,
1282 bak=>$self->{bak},
1283 useorg=>$self->{useorg},
1284 inline=>$self->{inline},
1285 overwrite=>$self->{overwrite},
1286 to_string=>$self->{to_string},
1287 header=>$self->{header},
1288 footer=>$self->{footer},
1289 toc_only=>$self->{toc_only},
1290 title=>$self->{title},
1291 toclabel=>$self->{toclabel},
1292 outfile=>$self->{outfile},
1293 debug=>$self->{debug},
1294 quiet=>$self->{quiet},
1295 @_
1296 31 );
1297  
1298 #
1299 # Output to the files if we were making anchors
1300 #
1301 31 ▶ if ($args{make_anchors}
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"make_anchors"} and not $args{"to_string"} and $args{"overwrite"}) 0 31 31
Condition83%5/6
$args{"make_anchors"} and not $args{"to_string"} and $args{"overwrite"}
!ll&&!rl&&r
21100
$args{"make_anchors"} and not $args{"to_string"}
!ll&&!rl&&r
17410
MC/DC0%0/3
$args{"make_anchors"} and not $args{"to_string"} and $args{"overwrite"}
$args{"make_anchors"} not $args{"to_string"} $args{"overwrite"}
Truth table75%3/4
A and not(B) and C
A= $args{"make_anchors"}B= not $args{"to_string"}C= $args{"overwrite"}
ABCresult
0XX0
10X0
1100
1111
1302 && !$args{to_string}
1303 && $args{overwrite})
1304 {
1305 0 my $ofh;
1306 # start from 1 if we're going to be inlining the toc
1307 # in the first file and not to an output file
1308 my $start_from = (($args{make_toc}
1309 && $args{inline}
1310 && !$args{outfile})
1311 0 ▶ ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$args{'make_toc'} && $args{'inline'} && !$args{'outfile'} ? : 0 0 0
Condition0%0/6
$args{"make_toc"} and $args{"inline"} and not $args{"outfile"}
!ll&&!rl&&r
000
$args{"make_toc"} and $args{"inline"}
!ll&&!rl&&r
000
MC/DC0%0/3
$args{"make_toc"} and $args{"inline"} and not $args{"outfile"}
$args{"make_toc"} $args{"inline"} not $args{"outfile"}
Truth table0%0/4
A and B and not(C)
A= $args{"make_toc"}B= $args{"inline"}C= not $args{"outfile"}
ABCresult
0XX0
10X0
1100
1111
1312 0 ▶ for (my $i=$start_from; $i < @{$args{filenames}}; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$args{"filenames"};}) 0 0 0
1313 {
1314 0 my $filename = $args{filenames}->[$i];
1315 0 my $bakfile = $filename . "." . $args{bak};
1316 0 ▶ if ($args{bak}
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"bak"} and not $args{"useorg"} && -e $bakfile) 0 0 0
Condition0%0/6
$args{"bak"} and not $args{"useorg"} && -e $bakfile
!ll&&!rl&&r
000
$args{'useorg'} && -e $bakfile
!ll&&!rl&&r
000
MC/DC0%0/3
$args{"bak"} and not $args{"useorg"} && -e $bakfile
$args{"bak"} $args{'useorg'} -e $bakfile
Truth table0%0/4
A and not(B && C)
A= $args{"bak"}B= $args{'useorg'}C= -e $bakfile
ABCresult
0XX0
1110
10X1
1101
1317 && !($args{useorg} && -e $bakfile))
1318 {
1319 # copy the file to a backup
1320 print STDERR "Backing up ", $filename, " to ",
1321 $bakfile, "\n"
1322 0 ▶ unless $args{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"quiet"} 0 0 0
1323 0 cp($filename, $bakfile);
1324 }
1325 0 ▶ open($ofh, "> $filename")
Branch: 1 branch
BranchTrueFalseTotal
open $ofh, "> $filename" or die "Error: unable to open ", $filename, ": $!\n" 0 0 0
1326 || die "Error: unable to open ", $filename, ": $!\n";
1327 print STDERR "Overwriting ToC to ", $filename, "\n"
1328 0 ▶ unless $args{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"quiet"} 0 0 0
1329 0 print $ofh $args{input}->[$i];
1330 0 close($ofh);
1331 }
1332 }
1333  
1334 #
1335 # Construct and output the ToC
1336 #
1337 31 my $output = '';
1338 31 ▶ if ($args{make_toc})
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'make_toc'}) { } 19 12 31
elsif ($args{'make_anchors'} and !$args{'overwrite'} || $args{'to_string'}) { } 11 2 13
1339 {
1340 19 ▶ if ($args{toc})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'toc'}) { } 18 1 19
1341 {
1342 18 my @toc = ();
1343 # put the header at the start of the ToC if there is one
1344 18 ▶ if ($args{header}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'header'}) { } 0 18 18
elsif (not $args{'toc_only'} || $args{'inline'}) { } 14 4 18
1345 0 ▶ if (-f $args{header})
Branch: 1 branch
BranchTrueFalseTotal
if (-f $args{'header'}) { } 0 0 0
1346 {
1347 open(HEADER, $args{header})
1348 0 ▶ || die "Error: unable to open ", $args{header}, ": $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open HEADER, $args{"header"} or die "Error: unable to open ", $args{"header"}, ": $!\n" 0 0 0
1349 0 push @toc, <HEADER>;
1350 0 close (HEADER);
1351 }
1352 else # not a file
1353 {
1354 0 push @toc, $args{header};
1355 }
1356 }
1357 # if we are outputing a standalone page,
1358 # then make sure it can stand
1359 ▶ elsif (!$args{toc_only}
Condition66%2/3
$args{'toc_only'} || $args{'inline'}
l!l&&r!l&&!r
0414
MC/DC50%1/2
$args{'toc_only'} || $args{'inline'}
$args{'toc_only'} $args{'inline'}
Truth table66%2/3
A || B
A= $args{'toc_only'}B= $args{'inline'}
ABresult
1X1
011
000
1360 && !$args{inline}) {
1361  
1362 14 push @toc, qq|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML//EN">\n|,
1363 "<html>\n",
1364 "<head>\n";
1365 14 ▶ push @toc, "<title>", $args{title}, "</title>\n" if $args{title};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"title"} 14 0 14
1366 14 push @toc, "</head>\n",
1367 "<body>\n";
1368 }
1369  
1370 # start the ToC with the ToC label
1371 18 ▶ if ($args{toclabel}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"toclabel"}) 17 1 18
1372 17 push @toc, $args{toclabel};
1373 }
1374  
1375 # and the actual ToC
1376 18 push @toc, "\n", $args{toc}, "\n";
1377  
1378 # add the footer, if there is one
1379 18 ▶ if ($args{footer}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'footer'}) { } 0 18 18
elsif (not $args{'toc_only'} || $args{'inline'}) { } 14 4 18
1380 0 ▶ if (-f $args{footer})
Branch: 1 branch
BranchTrueFalseTotal
if (-f $args{'footer'}) { } 0 0 0
1381 {
1382 open(FOOTER, $args{footer})
1383 0 ▶ || die "Error: unable to open ", $args{footer}, ": $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open FOOTER, $args{"footer"} or die "Error: unable to open ", $args{"footer"}, ": $!\n" 0 0 0
1384 0 push @toc, <FOOTER>;
1385 0 close (FOOTER);
1386 }
1387 else
1388 {
1389 0 push @toc, $args{footer};
1390 }
1391 }
1392 # if we are outputing a standalone page,
1393 # then make sure it can stand
1394 ▶ elsif (!$args{toc_only}
Condition66%2/3
$args{'toc_only'} || $args{'inline'}
l!l&&r!l&&!r
0414
MC/DC50%1/2
$args{'toc_only'} || $args{'inline'}
$args{'toc_only'} $args{'inline'}
Truth table66%2/3
A || B
A= $args{'toc_only'}B= $args{'inline'}
ABresult
1X1
011
000
1395 && !$args{inline}) {
1396  
1397 14 push @toc, "</body>\n", "</html>\n";
1398 }
1399  
1400 18 $output = join '', @toc;
1401 }
1402 else
1403 {
1404 1 $output = "\n";
1405 }
1406 }
1407 ▶ elsif ($args{make_anchors} && (!$args{overwrite} || $args{to_string}))
Condition50%3/6
$args{"make_anchors"} and !$args{"overwrite"} || $args{"to_string"}
!ll&&!rl&&r
1011
not $args{"overwrite"} or $args{"to_string"}
l!l&&r!l&&!r
1100
MC/DC33%1/3
$args{"make_anchors"} and !$args{"overwrite"} || $args{"to_string"}
$args{"make_anchors"} not $args{"overwrite"} $args{"to_string"}
Truth table50%2/4
A and not(B) or C
A= $args{"make_anchors"}B= not $args{"overwrite"}C= $args{"to_string"}
ABCresult
0XX0
1000
11X1
1011
1408 {
1409 # if we're just making anchors, and we aren't overwriting
1410 # the original file, we need to output it
1411 11 $output = $args{input}->[0];
1412 }
1413  
1414 31 ▶ if ($output)
Branch: 1 branch
BranchTrueFalseTotal
if ($output) 30 1 31
1415 {
1416 #
1417 # Sent the outfile to its final destination
1418 #
1419 30 my $file_needs_closing = 0;
1420 30 my $ofh;
1421 30 ▶ if ($args{to_string})
Branch: 3 branches
BranchTrueFalseTotal
if ($args{'to_string'}) { } 8 22 30
elsif ($args{'outfile'} and $args{'outfile'} ne '-') { } 20 2 22
elsif (not $args{'overwrite'}) { } 0 2 2
1422 {
1423 8 $ofh = undef;
1424 }
1425 ▶ elsif ($args{outfile} && $args{outfile} ne "-") {
Condition100%3/3
$args{"outfile"} and $args{"outfile"} ne "-"
!ll&&!rl&&r
1120
MC/DC100%2/2
$args{"outfile"} and $args{"outfile"} ne "-"
$args{"outfile"} $args{"outfile"} ne "-"
Truth table100%3/3
A and B
A= $args{"outfile"}B= $args{"outfile"} ne "-"
ABresult
0X0
100
111
1426 open($ofh, "> " . $args{outfile})
1427 20 ▶ || die "Error: unable to open ", $args{outfile}, ": $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $ofh, '> ' . $args{"outfile"} or die "Error: unable to open ", $args{"outfile"}, ": $!\n" 20 0 20
1428 20 $file_needs_closing = 1;
1429 }
1430 elsif (!$args{overwrite}) {
1431 0 $ofh = *STDOUT;
1432 0 $file_needs_closing = 0;
1433 }
1434 30 ▶ if ($args{inline}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'inline'}) { } 5 25 30
1435 # create the modified version of the first set of input
1436 5 my $first_file = $args{filenames}->[0];
1437 5 my $bakfile = $first_file . "." . $args{bak};
1438 $output = $self->put_toc_inline(%args,
1439 toc_str=>$output,
1440 in_string=>$args{input}->[0],
1441 5 filename=>$args{filenames}->[0],
1442 );
1443  
1444 5 ▶ if ($args{to_string})
Branch: 4 branches
BranchTrueFalseTotal
if ($args{'to_string'}) { } 3 2 5
elsif ($args{'overwrite'}) { } 2 0 2
elsif ($args{'outfile'} and $args{'outfile'} ne '-') { } 0 0 0
elsif ($args{'outfile'}) { } 0 0 0
1445 {
1446 # just send to string, don't print anything
1447 3 ▶ if ($args{debug})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"debug"}) 0 3 3
1448 {
1449 0 print STDERR "======== to_string output_toc ========\n";
1450 0 print STDERR $output;
1451 0 print STDERR "========----------------------========\n";
1452 }
1453 }
1454 elsif ($args{overwrite}) {
1455 2 ▶ if ($args{bak}
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"bak"} and not $args{"useorg"} && -e $bakfile) 2 0 2
Condition33%2/6
$args{"bak"} and not $args{"useorg"} && -e $bakfile
!ll&&!rl&&r
002
$args{'useorg'} && -e $bakfile
!ll&&!rl&&r
200
MC/DC0%0/3
$args{"bak"} and not $args{"useorg"} && -e $bakfile
$args{"bak"} $args{'useorg'} -e $bakfile
Truth table25%1/4
A and not(B && C)
A= $args{"bak"}B= $args{'useorg'}C= -e $bakfile
ABCresult
0XX0
1110
10X1
1101
1456 && !($args{useorg} && -e $bakfile))
1457 {
1458 # copy the file to a backup
1459 print STDERR "Backing up ", $first_file, " to ",
1460 $bakfile, "\n"
1461 2 ▶ unless $args{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"quiet"} 0 2 2
1462 2 cp($first_file, $bakfile);
1463 }
1464 2 ▶ open($ofh, "> $first_file")
Branch: 1 branch
BranchTrueFalseTotal
open $ofh, "> $first_file" or die "Error: unable to open ", $first_file, ": $!\n" 2 0 2
1465 || die "Error: unable to open ", $first_file, ": $!\n";
1466 2 $file_needs_closing = 1;
1467 print STDERR "Overwriting ToC to ", $first_file, "\n"
1468 2 ▶ unless $args{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"quiet"} 0 2 2
1469 2 print $ofh $output;
1470 }
1471 ▶ elsif ($args{outfile}
Condition0%0/3
$args{"outfile"} and $args{"outfile"} ne "-"
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"outfile"} and $args{"outfile"} ne "-"
$args{"outfile"} $args{"outfile"} ne "-"
Truth table0%0/3
A and B
A= $args{"outfile"}B= $args{"outfile"} ne "-"
ABresult
0X0
100
111
1472 && $args{outfile} ne "-") {
1473 print STDERR "Writing Inline ToC to ", $args{outfile}, "\n"
1474 0 ▶ unless $args{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"quiet"} 0 0 0
1475 0 print $ofh $output;
1476 }
1477 elsif ($args{outfile})
1478 {
1479 0 print $ofh $output;
1480 }
1481 } else {
1482 25 ▶ if ($args{to_string})
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'to_string'}) { } 5 20 25
elsif ($args{'outfile'} and $args{'outfile'} ne '-') { } 20 0 20
1483 {
1484 # just send to string, don't print anything
1485 }
1486 ▶ elsif ($args{outfile} && $args{outfile} ne "-") {
Condition33%1/3
$args{"outfile"} and $args{"outfile"} ne "-"
!ll&&!rl&&r
0020
MC/DC0%0/2
$args{"outfile"} and $args{"outfile"} ne "-"
$args{"outfile"} $args{"outfile"} ne "-"
Truth table33%1/3
A and B
A= $args{"outfile"}B= $args{"outfile"} ne "-"
ABresult
0X0
100
111
1487 print STDERR "Writing ToC to ", $args{outfile}, "\n"
1488 20 ▶ unless $args{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"quiet"} 0 20 20
1489 20 print $ofh $output;
1490 }
1491 else
1492 {
1493 0 print $ofh $output;
1494 }
1495 }
1496 30 ▶ if ($file_needs_closing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_needs_closing) 22 8 30
1497 22 close($ofh);
1498 }
1499 }
1500  
1501 31 ▶ if ($args{to_string})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'to_string'}) { } 8 23 31
1502 {
1503 8 return $output;
1504 }
1505 else
1506 {
1507 23 return 1;
1508 }
1509 } # output_toc
1510  
1511 =head2 put_toc_inline
1512  
1513 my $newhtml = $toc->put_toc_inline(toc_str=>$toc_str,
1514 filename=>$filename, in_string=>$in_string);
1515  
1516 Puts the given toc_str into the given input string;
1517 returns a string.
1518  
1519 =cut
1520  
1521 5 ▶sub put_toc_inline ($) {
Subroutine put_toc_inline: called CC=10 SCAR=23.3
Pod documented
1522 5 my $self = shift;
1523 my %args = (
1524 toc_str=>'',
1525 filename=>'',
1526 in_string=>'',
1527 toc_tag=>$self->{toc_tag},
1528 toc_tag_replace=>$self->{toc_tag_replace},
1529 @_
1530 5 );
1531 5 my $toc_str = $args{toc_str};
1532 5 my $infile = $args{filename};
1533  
1534 5 my $html_str = "";
1535  
1536 5 ▶ if ($args{in_string}) # use input string, not file
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'in_string'}) { } 5 0 5
1537 {
1538 5 $html_str = $args{in_string};
1539 }
1540 else
1541 {
1542 0 local $/;
1543 0 ▶ open (FILE, $infile) ||
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $infile or die "Error: unable to open ", $infile, ": $!\n" 0 0 0
1544 die "Error: unable to open ", $infile, ": $!\n";
1545  
1546 0 $html_str = <FILE>;
1547 0 close (FILE);
1548 }
1549  
1550  
1551 # parse the file
1552 5 my $hp = new HTML::SimpleParse();
1553 5 $hp->text($html_str);
1554 5 $hp->parse();
1555  
1556 5 my $toc_tag = $args{toc_tag};
1557 5 my @newhtml = ();
1558  
1559 5 my $toc_done = 0;
1560 # go through the HTML
1561 5 my $tok;
1562 my $i;
1563 5 my @tree = $hp->tree();
1564 5 ▶ while (@tree) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tree) 580 5 585
1565 580 $tok = shift @tree;
1566 # look for the ToC tag in tags or comments
1567 580 ▶ if ($tok->{type} eq 'starttag'
Branch: 1 branch
BranchTrueFalseTotal
if ($tok->{'type'} eq 'starttag' or $tok->{'type'} eq 'endtag' or $tok->{'type'} eq 'comment') { } 315 265 580
Condition100%6/6
$tok->{"type"} eq "starttag" or $tok->{"type"} eq "endtag" or $tok->{"type"} eq "comment"
l!l&&r!l&&!r
30312265
$tok->{"type"} eq "starttag" or $tok->{"type"} eq "endtag"
l!l&&r!l&&!r
167136277
MC/DC100%3/3
$tok->{"type"} eq "starttag" or $tok->{"type"} eq "endtag" or $tok->{"type"} eq "comment"
$tok->{"type"} eq "starttag" $tok->{"type"} eq "endtag" $tok->{"type"} eq "comment"
Truth table100%4/4
A or B or C
A= $tok->{"type"} eq "starttag"B= $tok->{"type"} eq "endtag"C= $tok->{"type"} eq "comment"
ABCresult
1XX1
01X1
0011
0000
1568 || $tok->{type} eq 'endtag'
1569 || $tok->{type} eq 'comment')
1570 {
1571 315 ▶ if (!$toc_done
Branch: 1 branch
BranchTrueFalseTotal
if (not $toc_done and $tok->{'content'} =~ /$toc_tag/i) { } 5 310 315
Condition100%3/3
not $toc_done and $tok->{"content"} =~ /$toc_tag/i
!ll&&!rl&&r
256545
MC/DC100%2/2
not $toc_done and $tok->{"content"} =~ /$toc_tag/i
not $toc_done $tok->{"content"} =~ /$toc_tag/i
Truth table100%3/3
not(A) and B
A= not $toc_doneB= $tok->{"content"} =~ /$toc_tag/i
ABresult
0X0
100
111
1572 && $tok->{content} =~ m|$toc_tag|i) {
1573 # some tags need to be preserved, with the ToC put after,
1574 # while others need to be replaced
1575 5 ▶ if (!$args{toc_tag_replace}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"toc_tag_replace"}) 4 1 5
1576 4 push @newhtml, $hp->execute($tok);
1577 }
1578 # put the ToC in
1579 5 push @newhtml, $toc_str;
1580 5 $toc_done = 1;
1581 }
1582 else {
1583 310 push @newhtml, $hp->execute($tok);
1584 }
1585 }
1586 else
1587 {
1588 265 push @newhtml, $hp->execute($tok);
1589 265 next;
1590 }
1591 }
1592  
1593 5 return join('', @newhtml);
1594 }
1595  
1596 =head2 cp
1597  
1598 cp($src, $dst);
1599  
1600 Copies file $src to $dst.
1601 Used for making backups of files.
1602  
1603 =cut
1604  
1605 2 ▶sub cp ($$) {
Subroutine cp: called CC=3 SCAR=11.2
Pod documented
1606 2 my($src, $dst) = @_;
1607 2 ▶ open (SRC, $src) ||
Branch: 1 branch
BranchTrueFalseTotal
open SRC, $src or die "Error: unable to open ", $src, ": $!\n" 2 0 2
1608 die "Error: unable to open ", $src, ": $!\n";
1609 2 ▶ open (DST, "> $dst") ||
Branch: 1 branch
BranchTrueFalseTotal
open DST, "> $dst" or die "Error: unable to open ", $dst, ": $!\n" 2 0 2
1610 die "Error: unable to open ", $dst, ": $!\n";
1611 2 print DST <SRC>;
1612 2 close(SRC);
1613 2 close(DST);
1614 }
1615  
1616 8 1;
1617  
1618 =head1 FILE FORMATS
1619  
1620 =head2 Formatting the ToC
1621  
1622 The B<toc_entry> and other related options give you control on how the
1623 ToC entries may look, but there are other options to affect the final
1624 appearance of the ToC file created.
1625  
1626 With the B<header> option, the contents of the given file (or string)
1627 will be prepended before the generated ToC. This allows you to have
1628 introductory text, or any other text, before the ToC.
1629  
1630 =over
1631  
1632 =item Note:
1633  
1634 If you use the B<header> option, make sure the file specified
1635 contains the opening HTML tag, the HEAD element (containing the
1636 TITLE element), and the opening BODY tag. However, these
1637 tags/elements should not be in the header file if the B<inline>
1638 option is used. See L</Inlining the ToC> for information on what
1639 the header file should contain for inlining the ToC.
1640  
1641 =back
1642  
1643 With the B<toclabel> option, the contents of the given string will be
1644 prepended before the generated ToC (but after any text taken from a
1645 B<header> file).
1646  
1647 With the B<footer> option, the contents of the file will be appended
1648 after the generated ToC.
1649  
1650 =over
1651  
1652 =item Note:
1653  
1654 If you use the B<footer>, make sure it includes the closing BODY
1655 and HTML tags (unless, of course, you are using the B<inline> option).
1656  
1657 =back
1658  
1659 If the B<header> option is not specified, the appropriate starting
1660 HTML markup will be added, unless the B<toc_only> option is specified.
1661 If the B<footer> option is not specified, the appropriate closing
1662 HTML markup will be added, unless the B<toc_only> option is specified.
1663  
1664 If you do not want/need to deal with header, and footer, files, then
1665 you are allowed to specify the title, B<title> option, of the ToC file;
1666 and it allows you to specify a heading, or label, to put before ToC
1667 entries' list, the B<toclabel> option. Both options have default values.
1668  
1669 If you do not want HTML page tags to be supplied, and just want
1670 the ToC itself, then specify the B<toc_only> option.
1671 If there are no B<header> or B<footer> files, then this will simply
1672 output the contents of B<toclabel> and the ToC itself.
1673  
1674 =head2 Inlining the ToC
1675  
1676 The ability to incorporate the ToC directly into an HTML document
1677 is supported via the B<inline> option.
1678  
1679 Inlining will be done on the first file in the list of files processed,
1680 and will only be done if that file contains an opening tag matching the
1681 B<toc_tag> value.
1682  
1683 If B<overwrite> is true, then the first file in the list will be
1684 overwritten, with the generated ToC inserted at the appropriate spot.
1685 Otherwise a modified version of the first file is output to either STDOUT
1686 or to the output file defined by the B<outfile> option.
1687  
1688 The options B<toc_tag> and B<toc_tag_replace> are used to determine where
1689 and how the ToC is inserted into the output.
1690  
1691 B<Example 1>
1692  
1693 $toc->generate_toc(inline=>1,
1694 toc_tag => 'BODY',
1695 toc_tag_replace => 0,
1696 ...
1697 );
1698  
1699 This will put the generated ToC after the BODY tag of the first file.
1700 If the B<header> option is specified, then the contents of the specified
1701 file are inserted after the BODY tag. If the B<toclabel> option is not
1702 empty, then the text specified by the B<toclabel> option is inserted.
1703 Then the ToC is inserted, and finally, if the B<footer> option is
1704 specified, it inserts the footer. Then the rest of the input file
1705 follows as it was before.
1706  
1707 B<Example 2>
1708  
1709 $toc->generate_toc(inline=>1,
1710 toc_tag => '!--toc--',
1711 toc_tag_replace => 1,
1712 ...
1713 );
1714  
1715 This will put the generated ToC after the first comment of the form
1716 <!--toc-->, and that comment will be replaced by the ToC
1717 (in the order
1718 B<header>
1719 B<toclabel>
1720 ToC
1721 B<footer>)
1722 followed by the rest of the input file.
1723  
1724 =over
1725  
1726 =item Note:
1727  
1728 The header file should not contain the beginning HTML tag
1729 and HEAD element since the HTML file being processed should
1730 already contain these tags/elements.
1731  
1732 =back
1733  
1734 =head1 NOTES
1735  
1736 =over
1737  
1738 =item *
1739  
1740 HTML::GenToc is smart enough to detect anchors inside significant
1741 elements. If the anchor defines the NAME attribute, HTML::GenToc uses
1742 the value. Else, it adds its own NAME attribute to the anchor.
1743 If B<use_id> is true, then it likewise checks for and uses IDs.
1744  
1745 =item *
1746  
1747 The TITLE element is treated specially if specified in the B<toc_entry>
1748 option. It is illegal to insert anchors (A) into TITLE elements.
1749 Therefore, HTML::GenToc will actually link to the filename itself
1750 instead of the TITLE element of the document.
1751  
1752 =item *
1753  
1754 HTML::GenToc will ignore a significant element if it does not contain
1755 any non-whitespace characters. A warning message is generated if
1756 such a condition exists.
1757  
1758 =item *
1759  
1760 If you have a sequence of significant elements that change in a slightly
1761 disordered fashion, such as H1 -> H3 -> H2 or even H2 -> H1, though
1762 HTML::GenToc deals with this to create a list which is still good HTML, if
1763 you are using an ordered list to that depth, then you will get strange
1764 numbering, as an extra list element will have been inserted to nest the
1765 elements at the correct level.
1766  
1767 For example (H2 -> H1 with ol_num_levels=1):
1768  
1769 1.
1770 * My H2 Header
1771 2. My H1 Header
1772  
1773 For example (H1 -> H3 -> H2 with ol_num_levels=0 and H3 also being
1774 significant):
1775  
1776 1. My H1 Header
1777 1.
1778 1. My H3 Header
1779 2. My H2 Header
1780 2. My Second H1 Header
1781  
1782 In cases such as this it may be better not to use the B<ol> option.
1783  
1784 =back
1785  
1786 =head1 CAVEATS
1787  
1788 =over
1789  
1790 =item *
1791  
1792 Version 3.10 (and above) generates more verbose (SEO-friendly) anchors
1793 than prior versions. Thus anchors generated with earlier versions will
1794 not match version 3.10 anchors.
1795  
1796 =item *
1797  
1798 Version 3.00 (and above) of HTML::GenToc is not compatible with
1799 Version 2.x of HTML::GenToc. It is now designed to do everything
1800 in one pass, and has dropped certain options: the B<infile> option
1801 is no longer used (it has been replaced with the B<input> option);
1802 the B<toc_file> option no longer exists; use the B<outfile> option
1803 instead; the B<tocmap> option is no longer supported. Also the old
1804 array-parsing of arguments is no longer supported. There is no longer
1805 a B<generate_anchors> method; everything is done with B<generate_toc>.
1806  
1807 It now generates lower-case tags rather than upper-case ones.
1808  
1809 =item *
1810  
1811 HTML::GenToc is not very efficient (memory and speed), and can be
1812 slow for large documents.
1813  
1814 =item *
1815  
1816 Now that generation of anchors and of the ToC are done in one pass,
1817 even more memory is used than was the case before. This is more notable
1818 when processing multiple files, since all files are read into memory
1819 before processing them.
1820  
1821 =item *
1822  
1823 Invalid markup will be generated if a significant element is
1824 contained inside of an anchor. For example:
1825  
1826 <a name="foo"><h1>The FOO command</h1></a>
1827  
1828 will be converted to (if H1 is a significant element),
1829  
1830 <a name="foo"><h1><a name="The">The</a> FOO command</h1></a>
1831  
1832 which is illegal since anchors cannot be nested.
1833  
1834 It is better style to put anchor statements within the element to
1835 be anchored. For example, the following is preferred:
1836  
1837 <h1><a name="foo">The FOO command</a></h1>
1838  
1839 HTML::GenToc will detect the "foo" name and use it.
1840  
1841 =item *
1842  
1843 name attributes without quotes are not recognized.
1844  
1845 =back
1846  
1847 =head1 BUGS
1848  
1849 Tell me about them.
1850  
1851 =head1 REQUIRES
1852  
1853 The installation of this module requires C<Module::Build>. The module
1854 depends on C<HTML::SimpleParse>, C<HTML::Entities> and C<HTML::LinkList> and uses
1855 C<Data::Dumper> for debugging purposes. The hypertoc script depends on
1856 C<Getopt::Long>, C<Getopt::ArgvFile> and C<Pod::Usage>. Testing of this
1857 distribution depends on C<Test::More>.
1858  
1859 =head1 INSTALLATION
1860  
1861 To install this module, run the following commands:
1862  
1863 perl Build.PL
1864 ./Build
1865 ./Build test
1866 ./Build install
1867  
1868 Or, if you're on a platform (like DOS or Windows) that doesn't like the
1869 "./" notation, you can do this:
1870  
1871 perl Build.PL
1872 perl Build
1873 perl Build test
1874 perl Build install
1875  
1876 In order to install somewhere other than the default, such as
1877 in a directory under your home directory, like "/home/fred/perl"
1878 go
1879  
1880 perl Build.PL --install_base /home/fred/perl
1881  
1882 as the first step instead.
1883  
1884 This will install the files underneath /home/fred/perl.
1885  
1886 You will then need to make sure that you alter the PERL5LIB variable to
1887 find the modules, and the PATH variable to find the script.
1888  
1889 Therefore you will need to change:
1890 your path, to include /home/fred/perl/script (where the script will be)
1891  
1892 PATH=/home/fred/perl/script:${PATH}
1893  
1894 the PERL5LIB variable to add /home/fred/perl/lib
1895  
1896 PERL5LIB=/home/fred/perl/lib:${PERL5LIB}
1897  
1898 =head1 SEE ALSO
1899  
1900 perl(1)
1901 htmltoc(1)
1902 hypertoc(1)
1903  
1904 =head1 AUTHOR
1905  
1906 Kathryn Andersen (RUBYKAT) http://www.katspace.org/tools/hypertoc/
1907  
1908 Based on htmltoc by Earl Hood ehood AT medusa.acs.uci.edu
1909  
1910 Contributions by Dan Dascalescu, <http://dandascalescu.com>
1911  
1912 =head1 COPYRIGHT
1913  
1914 Copyright (C) 1994-1997 Earl Hood, ehood AT medusa.acs.uci.edu
1915 Copyright (C) 2002-2008 Kathryn Andersen
1916  
1917 This program is free software; you can redistribute it and/or modify
1918 it under the terms of the GNU General Public License as published by
1919 the Free Software Foundation; either version 2 of the License, or
1920 (at your option) any later version.
1921  
1922 This program is distributed in the hope that it will be useful,
1923 but WITHOUT ANY WARRANTY; without even the implied warranty of
1924 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1925 GNU General Public License for more details.
1926  
1927 You should have received a copy of the GNU General Public License
1928 along with this program; if not, write to the Free Software
1929 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1930  
1931 =cut
↑ Summary scripts/hypertoc »