Debian Codegraph @

README

raw
1 NAME
2 HTML::GenToc - Generate a Table of Contents for HTML documents.
3
4 VERSION
5 version 3.20
6
7 SYNOPSIS
8 use HTML::GenToc;
9
10 # create a new object
11 my $toc = new HTML::GenToc();
12
13 my $toc = new HTML::GenToc(title=>"Table of Contents",
14 toc_entry=>{
15 H1=>1,
16 H2=>2
17 },
18 toc_end=>{
19 H1=>'/H1',
20 H2=>'/H2'
21 }
22 );
23
24 # generate a ToC from a file
25 $toc->generate_toc(input=>$html_file,
26 footer=>$footer_file,
27 header=>$header_file
28 );
29
30 DESCRIPTION
31 HTML::GenToc generates anchors and a table of contents for HTML
32 documents. Depending on the arguments, it will insert the information it
33 generates, or output to a string, a separate file or STDOUT.
34
35 While it defaults to taking H1 and H2 elements as the significant
36 elements to put into the table of contents, any tag can be defined as a
37 significant element. Also, it doesn't matter if the input HTML code is
38 complete, pure HTML, one can input pseudo-html or page-fragments, which
39 makes it suitable for using on templates and HTML meta-languages such as
40 WML.
41
42 Also included in the distrubution is hypertoc, a script which uses the
43 module so that one can process files on the command-line in a
44 user-friendly manner.
45
46 DETAILS
47 The ToC generated is a multi-level level list containing links to the
48 significant elements. HTML::GenToc inserts the links into the ToC to
49 significant elements at a level specified by the user.
50
51 Example:
52
53 If H1s are specified as level 1, than they appear in the first level
54 list of the ToC. If H2s are specified as a level 2, than they appear in
55 a second level list in the ToC.
56
57 Information on the significant elements and what level they should occur
58 are passed in to the methods used by this object, or one can use the
59 defaults.
60
61 There are two phases to the ToC generation. The first phase is to put
62 suitable anchors into the HTML documents, and the second phase is to
63 generate the ToC from HTML documents which have anchors in them for the
64 ToC to link to.
65
66 For more information on controlling the contents of the created ToC, see
67 "Formatting the ToC".
68
69 HTML::GenToc also supports the ability to incorporate the ToC into the
70 HTML document itself via the inline option. See "Inlining the ToC" for
71 more information.
72
73 In order for HTML::GenToc to support linking to significant elements,
74 HTML::GenToc inserts anchors into the significant elements. One can use
75 HTML::GenToc as a filter, outputing the result to another file, or one
76 can overwrite the original file, with the original backed up with a
77 suffix (default: "org") appended to the filename. One can also output
78 the result to a string.
79
80 METHODS
81 Default arguments can be set when the object is created, and overridden
82 by setting arguments when the generate_toc method is called. Arguments
83 are given as a hash of arguments.
84
85 Method -- new
86 $toc = new HTML::GenToc();
87
88 $toc = new HTML::GenToc(toc_entry=>\%my_toc_entry,
89 toc_end=>\%my_toc_end,
90 bak=>'bak',
91 ...
92 );
93
94 Creates a new HTML::GenToc object.
95
96 These arguments will be used as defaults in invocations of other
97 methods.
98
99 See generate_tod for possible arguments.
100
101 generate_toc
102 $toc->generate_toc(outfile=>"index2.html");
103
104 my $result_str = $toc->generate_toc(to_string=>1);
105
106 Generates a table of contents for the significant elements in the HTML
107 documents, optionally generating anchors for them first.
108
109 Options
110
111 bak bak => *string*
112
113 If the input file/files is/are being overwritten (overwrite is on),
114 copy the original file to "*filename*.*string*". If the value is
115 empty, no backup file will be created. (default:org)
116
117 debug
118 debug => 1
119
120 Enable verbose debugging output. Used for debugging this module; in
121 other words, don't bother. (default:off)
122
123 entrysep
124 entrysep => *string*
125
126 Separator string for non-<li> item entries (default: ", ")
127
128 filenames
129 filenames => \@filenames
130
131 The filenames to use when creating table-of-contents links. This
132 overrides the filenames given in the input option, and is expected
133 to have exactly the same number of elements. This can also be used
134 when passing in string-content to the input option, to give a (fake)
135 filename to use for the links relating to that content.
136
137 footer
138 footer => *file_or_string*
139
140 Either the filename of the file containing footer text for ToC; or a
141 string containing the footer text.
142
143 header
144 header => *file_or_string*
145
146 Either the filename of the file containing header text for ToC; or a
147 string containing the header text.
148
149 ignore_only_one
150 ignore_only_one => 1
151
152 If there would be only one item in the ToC, don't make a ToC.
153
154 ignore_sole_first
155 ignore_sole_first => 1
156
157 If the first item in the ToC is of the highest level, AND it is the
158 only one of that level, ignore it. This is useful in web-pages where
159 there is only one H1 header but one doesn't know beforehand whether
160 there will be only one.
161
162 inline
163 inline => 1
164
165 Put ToC in document at a given point. See "Inlining the ToC" for
166 more information.
167
168 input
169 input => \@filenames
170
171 input => $content
172
173 This is expected to be either a reference to an array of filenames,
174 or a string containing content to process.
175
176 The three main uses would be:
177
178 (a) you have more than one file to process, so pass in multiple
179 filenames
180
181 (b) you have one file to process, so pass in its filename as the
182 only array item
183
184 (c) you have HTML content to process, so pass in just the content as
185 a string
186
187 (default:undefined)
188
189 notoc_match
190 notoc_match => *string*
191
192 If there are certain individual tags you don't wish to include in
193 the table of contents, even though they match the "significant
194 elements", then if this pattern matches contents inside the tag (not
195 the body), then that tag will not be included, either in generating
196 anchors nor in generating the ToC. (default: "class="notoc"")
197
198 ol ol => 1
199
200 Use an ordered list for level 1 ToC entries.
201
202 ol_num_levels
203 ol_num_levels => 2
204
205 The number of levels deep the OL listing will go if ol is true. If
206 set to zero, will use an ordered list for all levels. (default:1)
207
208 overwrite
209 overwrite => 1
210
211 Overwrite the input file with the output. (default:off)
212
213 outfile
214 outfile => *file*
215
216 File to write the output to. This is where the modified HTML output
217 goes to. Note that it doesn't make sense to use this option if you
218 are processing more than one file. If you give '-' as the filename,
219 then output will go to STDOUT. (default: STDOUT)
220
221 quiet
222 quiet => 1
223
224 Suppress informative messages. (default: off)
225
226 textonly
227 textonly => 1
228
229 Use only text content in significant elements.
230
231 title
232 title => *string*
233
234 Title for ToC page (if not using header or inline or toc_only)
235 (default: "Table of Contents")
236
237 toc_after
238 toc_after => \%toc_after_data
239
240 %toc_after_data = { *tag1* => *suffix1*, *tag2* => *suffix2* };
241
242 toc_after => { H2=>'</em>' }
243
244 For defining layout of significant elements in the ToC.
245
246 This expects a reference to a hash of tag=>suffix pairs.
247
248 The *tag* is the HTML tag which marks the start of the element. The
249 *suffix* is what is required to be appended to the Table of Contents
250 entry generated for that tag.
251
252 (default: undefined)
253
254 toc_before
255 toc_before => \%toc_before_data
256
257 %toc_before_data = { *tag1* => *prefix1*, *tag2* => *prefix2* };
258
259 toc_before=>{ H2=>'<em>' }
260
261 For defining the layout of significant elements in the ToC. The
262 *tag* is the HTML tag which marks the start of the element. The
263 *prefix* is what is required to be prepended to the Table of
264 Contents entry generated for that tag.
265
266 (default: undefined)
267
268 toc_end
269 toc_end => \%toc_end_data
270
271 %toc_end_data = { *tag1* => *endtag1*, *tag2* => *endtag2* };
272
273 toc_end => { H1 => '/H1', H2 => '/H2' }
274
275 For defining significant elements. The *tag* is the HTML tag which
276 marks the start of the element. The *endtag* the HTML tag which
277 marks the end of the element. When matching in the input file, case
278 is ignored (but make sure that all your *tag* options referring to
279 the same tag are exactly the same!).
280
281 toc_entry
282 toc_entry => \%toc_entry_data
283
284 %toc_entry_data = { *tag1* => *level1*, *tag2* => *level2* };
285
286 toc_entry => { H1 => 1, H2 => 2 }
287
288 For defining significant elements. The *tag* is the HTML tag which
289 marks the start of the element. The *level* is what level the tag is
290 considered to be. The value of *level* must be numeric, and
291 non-zero. If the value is negative, consective entries represented
292 by the significant_element will be separated by the value set by
293 entrysep option.
294
295 toclabel
296 toclabel => *string*
297
298 HTML text that labels the ToC. Always used. (default: "<h1>Table of
299 Contents</h1>")
300
301 toc_tag
302 toc_tag => *string*
303
304 If a ToC is to be included inline, this is the pattern which is used
305 to match the tag where the ToC should be put. This can be a
306 start-tag, an end-tag or a comment, but the < should be left out;
307 that is, if you want the ToC to be placed after the BODY tag, then
308 give "BODY". If you want a special comment tag to make where the ToC
309 should go, then include the comment marks, for example: "!--toc--"
310 (default:BODY)
311
312 toc_tag_replace
313 toc_tag_replace => 1
314
315 In conjunction with toc_tag, this is a flag to say whether the given
316 tag should be replaced, or if the ToC should be put after the tag.
317 This can be useful if your toc_tag is a comment and you don't need
318 it after you have the ToC in place. (default:false)
319
320 toc_only
321 toc_only => 1
322
323 Output only the Table of Contents, that is, the Table of Contents
324 plus the toclabel. If there is a header or a footer, these will also
325 be output.
326
327 If toc_only is false then if there is no header, and inline is not
328 true, then a suitable HTML page header will be output, and if there
329 is no footer and inline is not true, then a HTML page footer will be
330 output.
331
332 (default:false)
333
334 to_string
335 to_string => 1
336
337 Return the modified HTML output as a string. This *does* override
338 other methods of output (unlike version 3.00). If *to_string* is
339 false, the method will return 1 rather than a string.
340
341 use_id
342 use_id => 1
343
344 Use id="*name*" for anchors rather than <a name="*name*"/> anchors.
345 However if an anchor already exists for a Significant Element, this
346 won't make an id for that particular element.
347
348 useorg
349 useorg => 1
350
351 Use pre-existing backup files as the input source; that is, files of
352 the form *infile*.*bak* (see input and bak).
353
354 INTERNAL METHODS
355 These methods are documented for developer purposes and aren't intended
356 to be used externally.
357
358 make_anchor_name
359 $toc->make_anchor_name(content=>$content,
360 anchors=>\%anchors);
361
362 Makes the anchor-name for one anchor. Bases the anchor on the content of
363 the significant element. Ensures that anchors are unique.
364
365 make_anchors
366 my $new_html = $toc->make_anchors(input=>$html,
367 notoc_match=>$notoc_match,
368 use_id=>$use_id,
369 toc_entry=>\%toc_entries,
370 toc_end=>\%toc_ends,
371 );
372
373 Makes the anchors the given input string. Returns a string.
374
375 make_toc_list
376 my @toc_list = $toc->make_toc_list(input=>$html,
377 labels=>\%labels,
378 notoc_match=>$notoc_match,
379 toc_entry=>\%toc_entry,
380 toc_end=>\%toc_end,
381 filename=>$filename);
382
383 Makes a list of lists which represents the structure and content of (a
384 portion of) the ToC from one file. Also updates a list of labels for the
385 ToC entries.
386
387 build_lol
388 Build a list of lists of paths, given a list of hashes with info about
389 paths.
390
391 output_toc
392 $self->output_toc(toc=>$toc_str,
393 input=>\@input,
394 filenames=>\@filenames);
395
396 Put the output (whether to file, STDOUT or string). The "output" in this
397 case could be the ToC, the modified (anchors added) HTML, or both.
398
399 put_toc_inline
400 my $newhtml = $toc->put_toc_inline(toc_str=>$toc_str,
401 filename=>$filename, in_string=>$in_string);
402
403 Puts the given toc_str into the given input string; returns a string.
404
405 cp
406 cp($src, $dst);
407
408 Copies file $src to $dst. Used for making backups of files.
409
410 FILE FORMATS
411 Formatting the ToC
412 The toc_entry and other related options give you control on how the ToC
413 entries may look, but there are other options to affect the final
414 appearance of the ToC file created.
415
416 With the header option, the contents of the given file (or string) will
417 be prepended before the generated ToC. This allows you to have
418 introductory text, or any other text, before the ToC.
419
420 Note:
421 If you use the header option, make sure the file specified contains
422 the opening HTML tag, the HEAD element (containing the TITLE
423 element), and the opening BODY tag. However, these tags/elements
424 should not be in the header file if the inline option is used. See
425 "Inlining the ToC" for information on what the header file should
426 contain for inlining the ToC.
427
428 With the toclabel option, the contents of the given string will be
429 prepended before the generated ToC (but after any text taken from a
430 header file).
431
432 With the footer option, the contents of the file will be appended after
433 the generated ToC.
434
435 Note:
436 If you use the footer, make sure it includes the closing BODY and
437 HTML tags (unless, of course, you are using the inline option).
438
439 If the header option is not specified, the appropriate starting HTML
440 markup will be added, unless the toc_only option is specified. If the
441 footer option is not specified, the appropriate closing HTML markup will
442 be added, unless the toc_only option is specified.
443
444 If you do not want/need to deal with header, and footer, files, then you
445 are allowed to specify the title, title option, of the ToC file; and it
446 allows you to specify a heading, or label, to put before ToC entries'
447 list, the toclabel option. Both options have default values.
448
449 If you do not want HTML page tags to be supplied, and just want the ToC
450 itself, then specify the toc_only option. If there are no header or
451 footer files, then this will simply output the contents of toclabel and
452 the ToC itself.
453
454 Inlining the ToC
455 The ability to incorporate the ToC directly into an HTML document is
456 supported via the inline option.
457
458 Inlining will be done on the first file in the list of files processed,
459 and will only be done if that file contains an opening tag matching the
460 toc_tag value.
461
462 If overwrite is true, then the first file in the list will be
463 overwritten, with the generated ToC inserted at the appropriate spot.
464 Otherwise a modified version of the first file is output to either
465 STDOUT or to the output file defined by the outfile option.
466
467 The options toc_tag and toc_tag_replace are used to determine where and
468 how the ToC is inserted into the output.
469
470 Example 1
471
472 $toc->generate_toc(inline=>1,
473 toc_tag => 'BODY',
474 toc_tag_replace => 0,
475 ...
476 );
477
478 This will put the generated ToC after the BODY tag of the first file. If
479 the header option is specified, then the contents of the specified file
480 are inserted after the BODY tag. If the toclabel option is not empty,
481 then the text specified by the toclabel option is inserted. Then the ToC
482 is inserted, and finally, if the footer option is specified, it inserts
483 the footer. Then the rest of the input file follows as it was before.
484
485 Example 2
486
487 $toc->generate_toc(inline=>1,
488 toc_tag => '!--toc--',
489 toc_tag_replace => 1,
490 ...
491 );
492
493 This will put the generated ToC after the first comment of the form
494 <!--toc-->, and that comment will be replaced by the ToC (in the order
495 header toclabel ToC footer) followed by the rest of the input file.
496
497 Note:
498 The header file should not contain the beginning HTML tag and HEAD
499 element since the HTML file being processed should already contain
500 these tags/elements.
501
502 NOTES
503 * HTML::GenToc is smart enough to detect anchors inside significant
504 elements. If the anchor defines the NAME attribute, HTML::GenToc
505 uses the value. Else, it adds its own NAME attribute to the anchor.
506 If use_id is true, then it likewise checks for and uses IDs.
507
508 * The TITLE element is treated specially if specified in the toc_entry
509 option. It is illegal to insert anchors (A) into TITLE elements.
510 Therefore, HTML::GenToc will actually link to the filename itself
511 instead of the TITLE element of the document.
512
513 * HTML::GenToc will ignore a significant element if it does not
514 contain any non-whitespace characters. A warning message is
515 generated if such a condition exists.
516
517 * If you have a sequence of significant elements that change in a
518 slightly disordered fashion, such as H1 -> H3 -> H2 or even H2 ->
519 H1, though HTML::GenToc deals with this to create a list which is
520 still good HTML, if you are using an ordered list to that depth,
521 then you will get strange numbering, as an extra list element will
522 have been inserted to nest the elements at the correct level.
523
524 For example (H2 -> H1 with ol_num_levels=1):
525
526 1.
527 * My H2 Header
528 2. My H1 Header
529
530 For example (H1 -> H3 -> H2 with ol_num_levels=0 and H3 also being
531 significant):
532
533 1. My H1 Header
534 1.
535 1. My H3 Header
536 2. My H2 Header
537 2. My Second H1 Header
538
539 In cases such as this it may be better not to use the ol option.
540
541 CAVEATS
542 * Version 3.10 (and above) generates more verbose (SEO-friendly)
543 anchors than prior versions. Thus anchors generated with earlier
544 versions will not match version 3.10 anchors.
545
546 * Version 3.00 (and above) of HTML::GenToc is not compatible with
547 Version 2.x of HTML::GenToc. It is now designed to do everything in
548 one pass, and has dropped certain options: the infile option is no
549 longer used (it has been replaced with the input option); the
550 toc_file option no longer exists; use the outfile option instead;
551 the tocmap option is no longer supported. Also the old array-parsing
552 of arguments is no longer supported. There is no longer a
553 generate_anchors method; everything is done with generate_toc.
554
555 It now generates lower-case tags rather than upper-case ones.
556
557 * HTML::GenToc is not very efficient (memory and speed), and can be
558 slow for large documents.
559
560 * Now that generation of anchors and of the ToC are done in one pass,
561 even more memory is used than was the case before. This is more
562 notable when processing multiple files, since all files are read
563 into memory before processing them.
564
565 * Invalid markup will be generated if a significant element is
566 contained inside of an anchor. For example:
567
568 <a name="foo"><h1>The FOO command</h1></a>
569
570 will be converted to (if H1 is a significant element),
571
572 <a name="foo"><h1><a name="The">The</a> FOO command</h1></a>
573
574 which is illegal since anchors cannot be nested.
575
576 It is better style to put anchor statements within the element to be
577 anchored. For example, the following is preferred:
578
579 <h1><a name="foo">The FOO command</a></h1>
580
581 HTML::GenToc will detect the "foo" name and use it.
582
583 * name attributes without quotes are not recognized.
584
585 BUGS
586 Tell me about them.
587
588 REQUIRES
589 The installation of this module requires "Module::Build". The module
590 depends on "HTML::SimpleParse", "HTML::Entities" and "HTML::LinkList"
591 and uses "Data::Dumper" for debugging purposes. The hypertoc script
592 depends on "Getopt::Long", "Getopt::ArgvFile" and "Pod::Usage". Testing
593 of this distribution depends on "Test::More".
594
595 INSTALLATION
596 To install this module, run the following commands:
597
598 perl Build.PL
599 ./Build
600 ./Build test
601 ./Build install
602
603 Or, if you're on a platform (like DOS or Windows) that doesn't like the
604 "./" notation, you can do this:
605
606 perl Build.PL
607 perl Build
608 perl Build test
609 perl Build install
610
611 In order to install somewhere other than the default, such as in a
612 directory under your home directory, like "/home/fred/perl" go
613
614 perl Build.PL --install_base /home/fred/perl
615
616 as the first step instead.
617
618 This will install the files underneath /home/fred/perl.
619
620 You will then need to make sure that you alter the PERL5LIB variable to
621 find the modules, and the PATH variable to find the script.
622
623 Therefore you will need to change: your path, to include
624 /home/fred/perl/script (where the script will be)
625
626 PATH=/home/fred/perl/script:${PATH}
627
628 the PERL5LIB variable to add /home/fred/perl/lib
629
630 PERL5LIB=/home/fred/perl/lib:${PERL5LIB}
631
632 SEE ALSO
633 perl(1) htmltoc(1) hypertoc(1)
634
635 AUTHOR
636 Kathryn Andersen (RUBYKAT) http://www.katspace.org/tools/hypertoc/
637
638 Based on htmltoc by Earl Hood ehood AT medusa.acs.uci.edu
639
640 Contributions by Dan Dascalescu, <http://dandascalescu.com>
641
642 COPYRIGHT
643 Copyright (C) 1994-1997 Earl Hood, ehood AT medusa.acs.uci.edu Copyright
644 (C) 2002-2008 Kathryn Andersen
645
646 This program is free software; you can redistribute it and/or modify it
647 under the terms of the GNU General Public License as published by the
648 Free Software Foundation; either version 2 of the License, or (at your
649 option) any later version.
650
651 This program is distributed in the hope that it will be useful, but
652 WITHOUT ANY WARRANTY; without even the implied warranty of
653 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
654 Public License for more details.
655
656 You should have received a copy of the GNU General Public License along
657 with this program; if not, write to the Free Software Foundation, Inc.,
658 675 Mass Ave, Cambridge, MA 02139, USA.
659