3 HTML::Toc - Generate, insert and update HTML Table of Contents.
7 Generate, insert and update HTML Table of Contents.
11 The HTML::Toc consists out of the following packages:
18 HTML::Toc is the object which will eventually hold the Table of Contents. HTML::TocGenerator does the actual generation of the ToC. HTML::TocInsertor handles the insertion of the ToC in the source. HTML::TocUpdator takes care of updating previously inserted ToCs.
20 HTML::Parser is the base object of HTML::TocGenerator, HTML::TocInsertor and HTML::TocUpdator. Each of these objects uses its predecessor as its ancestor, as shown in the UML diagram underneath:
22 +---------------------+
24 +---------------------+
25 +---------------------+
28 +----------+----------+
31 +----------+----------+ <<uses>> +-----------+
32 | HTML::TocGenerator + - - - - - -+ HTML::Toc |
33 +---------------------+ +-----------+
34 +---------------------+ +-----------+
35 | +extend() | | +clear() |
36 | +extendFromFile() | | +format() |
37 | +generate() | +-----+-----+
38 | +generateFromFile() | :
39 +----------+----------+ :
42 +----------+----------+ <<uses>> :
43 | HTML::TocInsertor + - - - - - - - - -+
44 +---------------------+ :
45 +---------------------+ :
47 | +insertIntoFile() | :
48 +----------+----------+ :
51 +----------+----------+ <<uses>> :
52 | HTML::TocUpdator + - - - - - - - - -+
53 +---------------------+
54 +---------------------+
59 +---------------------+
61 When generating a ToC you'll have to decide which object you want to use:
64 for generating a ToC without inserting the ToC into the source
66 for generating a ToC and inserting the ToC into the source
68 for generating and inserting a ToC, removing any previously
71 Thus in tabular view, each object is capable of:
73 generating inserting updating
74 ---------------------------------
81 The code underneath will generate a ToC of the HTML headings C<<h1>>..C<<h6>> from a file C<index.htm>:
84 use HTML::TocGenerator;
86 my $toc = HTML::Toc->new();
87 my $tocGenerator = HTML::TocGenerator->new();
89 $tocGenerator->generateFromFile($toc, 'index.htm');
92 For example, with C<index.htm> containing:
103 <!-- Table of Contents generated by Perl - HTML::Toc -->
105 <li><a href=#h-1>Chapter</a>
107 <!-- End of generated Table of Contents -->
111 This code will generate a ToC of HTML headings C<<h1>>..C<<h6>> of file C<index.htm>, and insert the ToC after the C<<body>> tag at the same time:
114 use HTML::TocInsertor;
116 my $toc = HTML::Toc->new();
117 my $tocInsertor = HTML::TocInsertor->new();
119 $tocInsertor->insertIntoFile($toc, 'index.htm');
121 For example, with C<index.htm> containing:
133 <!-- Table of Contents generated by Perl - HTML::Toc -->
135 <li><a href=#h-1>Chapter</a>
137 <!-- End of generated Table of Contents -->
139 <a name=h-1><h1>Chapter</h1></a>
143 If you're planning to update the inserted ToC, you'd better use C<TocUpdator> to insert the ToC. C<TocUpdator> marks the inserted ToC elements with update tokens. These update tokens allow C<TocUpdator> to identify and remove the ToC elements during a future update session. This code uses C<TocUpdator> instead of C<TocInsertor>:
146 use HTML::TocUpdator;
148 my $toc = HTML::Toc->new();
149 my $tocUpdator = HTML::TocUpdator->new();
151 $tocUpdator->insertIntoFile($toc, 'index.htm');
153 When applying the code above on 'index.htm':
163 the output will contain additional update tokens:
167 <!-- #BeginTocAnchorNameBegin -->
168 <!-- #EndTocAnchorNameBegin -->
169 <!-- #BeginTocAnchorNameEnd -->
170 <!-- #EndTocAnchorNameEnd -->
172 around the inserted ToC elements:
175 <body><!-- #BeginToc-->
176 <!-- Table of Contents generated by Perl - HTML::Toc -->
178 <li><a href=#h-1> Chapter </a>
180 <!-- End of generated Table of Contents -->
182 <!-- #BeginTocAnchorNameBegin --><a name=h-1><!-- #EndTocAnchorNameBegin --><h1>
184 </h1><!-- #BeginTocAnchorNameEnd --></a><!-- #EndTocAnchorNameEnd -->
188 Instead of C<HTML::TocUpdator::insertIntoFile> you can also use C<HTML::TocUpdator::updateFile()>. C<HTML::TocUpdator::updateFile()> will also insert the ToC, whether there is a ToC already inserted or not.
192 This code will generate a ToC of HTML headings C<<h1>>..C<<h6>> of file C<indexToc.htm>, and insert or update the ToC after the C<<body>> tag at the same time:
195 use HTML::TocUpdator;
197 my $toc = HTML::Toc->new();
198 my $tocUpdator = HTML::TocUpdator->new();
200 $tocUpdator->updateFile($toc, 'indexToc.htm');
202 For example, with C<indexToc.htm> containing:
205 <body><!-- #BeginToc -->
208 <!-- #BeginTocAnchorNameBegin -->bar<!-- #EndTocAnchorNameBegin --><h1>
210 </h1><!-- #BeginTocAnchorNameEnd -->foo<!-- #EndTocAnchorNameEnd -->
217 <body><!-- #BeginToc -->
218 <!-- Table of Contents generated by Perl - HTML::Toc -->
220 <li><a href=#h-1> Chapter </a>
222 <!-- End of generated Table of Contents -->
224 <!-- #BeginTocAnchorNameBegin --><a name=h-1><!-- #EndTocAnchorNameBegin --><h1>
226 </h1><!-- #BeginTocAnchorNameEnd --></a><!-- #EndTocAnchorNameEnd -->
230 All text between the update tokens will be replaced. So be warned: all manual changes made to text between update tokens will be removed unrecoverable after calling C<HTML::TocUpdator::update()> or C<HTML::TocUpdator::updateFile()>.
234 The ToC isn't generated all at once. There are two stages involved: generating and formatting. Generating the ToC actually means storing a preliminary ToC in C<HTML::Toc-E<gt>{_toc}>. This preliminary, tokenized ToC has to be turned into something useful by calling C<HTML::Toc-E<gt>format()>. For an example, see paragraph 'L<Generating|"generating">'.
238 The ToC generation can be modified in a variety of ways. The following paragraphs each explain a single modification. An example of most of the modifications can be found in the C<manualTest.t> test file. Within this test, a manual containing:
252 =head2 Using attribute value as ToC text
254 Normally, the ToC will be made of text between specified ToC tokens. It's also possible to use the attribute value of a token as a ToC text. This can be done by specifying the attribute marked with an L<attributeToTocToken|"attributeToTocToken"> within the L<tokenBegin|"tokenBegin"> token. For example, suppose you want to generate a ToC of the C<alt> attributes of the following image tokens:
257 <img src=test1.gif alt="First picture">
258 <img src=test2.gif alt="Second picture">
261 This would be the code:
264 use HTML::TocInsertor;
266 my $toc = HTML::Toc->new();
267 my $tocInsertor = HTML::TocInsertor->new();
271 'groupId' => 'image',
272 'tokenBegin' => '<img alt=@>'
275 $tocInsertor->insertIntoFile($toc, $filename);
277 and the output will be:
280 <!-- Table of Contents generated by Perl - HTML::Toc -->
282 <li><a href=#image-1>First picture</a>
283 <li><a href=#image-2>Second picture</a>
285 <!-- End of generated Table of Contents -->
287 <a name=image-1><img src=test1.gif alt="First picture"></a>
288 <a name=image-2><img src=test2.gif alt="Second picture"></a>
291 =head2 Generate single ToC of multiple files
293 Besides generating a ToC of a single file, it's also possible to generate a single ToC of multiple files. This can be done by specifying either an array of files as the file argument and/or by extending an existing ToC.
295 =head3 Specify an array of files
297 For example, suppose you want to generate a ToC of both C<doc1.htm>:
300 <h1>Chapter of document 1</h1>
306 <h1>Chapter of document 2</h1>
309 Here's the code to do so by specifying an array of files:
312 use HTML::TocGenerator;
314 my $toc = HTML::Toc->new();
315 my $tocGenerator = HTML::TocGenerator->new();
317 $toc->setOptions({'doLinkToFile' => 1});
318 $tocGenerator->generateFromFile($toc, ['doc1.htm', 'doc2.htm']);
319 print $toc->format();
321 And the output will be:
324 <!-- Table of Contents generated by Perl - HTML::Toc -->
326 <li><a href=doc1.htm#h-1>Chapter of document 1</a>
327 <li><a href=doc2.htm#h-2>Chapter of document 2</a>
329 <!-- End of generated Table of Contents -->
331 =head3 Extend an existing ToC
333 It's also possible to extend an existing ToC. For example, suppose we want the generate a ToC of file C<doc1.htm>:
336 <h1>Chapter of document 1</h1>
339 and extend this ToC with text from C<doc2.htm>:
342 <h1>Chapter of document 2</h1>
345 Here's the code to do so:
348 use HTML::TocGenerator;
350 my $toc = HTML::Toc->new();
351 my $tocGenerator = HTML::TocGenerator->new();
353 $toc->setOptions({'doLinkToFile' => 1});
354 $tocGenerator->generateFromFile($toc, 'doc1.htm');
355 $tocGenerator->extendFromFile($toc, 'doc2.htm');
356 print $toc->format();
358 And the output will be:
361 <!-- Table of Contents generated by Perl - HTML::Toc -->
363 <li><a href=doc1.htm#h-1>Chapter of document 1</a>
364 <li><a href=doc2.htm#h-2>Chapter of document 2</a>
366 <!-- End of generated Table of Contents -->
368 =head2 Generate multiple ToCs
370 It's possible to generate multiple ToCs at once by specifying a C<HTML::Toc> object array as the ToC argument. For example, suppose you want to generate a default ToC of HTML headings <h1>..<h6> as well as a ToC of the C<alt> image attributes of the following text:
374 <img src=test1.gif alt="First picture" id=image_001>
375 <h2>Paragraph One</h2>
376 <img src=test2.gif alt="Second picture" id=image_002>
379 Here's how you would do so:
382 use HTML::TocInsertor;
384 my $toc1 = HTML::Toc->new();
385 my $toc2 = HTML::Toc->new();
386 my $tocInsertor = HTML::TocInsertor->new();
390 'groupId' => 'image',
391 'tokenBegin' => '<img alt=@>'
394 $tocInsertor->insertIntoFile([$toc1, $toc2], $filename);
396 And the output will be:
399 <!-- Table of Contents generated by Perl - HTML::Toc -->
401 <li><a href=#h-1>Header One</a>
403 <li><a href=#h-1.1>Paragraph One</a>
406 <!-- End of generated Table of Contents -->
408 <!-- Table of Contents generated by Perl - HTML::Toc -->
410 <li><a href=#image-1>First picture</a>
411 <li><a href=#image-2>Second picture</a>
413 <!-- End of generated Table of Contents -->
415 <a name=h-1><h1>Header One</h1></a>
416 <a name=image-1><img src=test1.gif alt="First picture"></a>
417 <a name=h-1.1><h2>Paragraph One</h2></a>
418 <a name=image-2><img src=test2.gif alt="Second picture"></a>
421 =head2 Generate multiple groups in one ToC
423 You may want to generate a ToC consisting of multiple ToC groups.
425 =head3 Specify an additional 'Appendix' group
427 Suppose you want to generate a ToC with one group for the normal headings, and one group for the appendix headings, using this source file:
432 <h3>Subparagraph</h3>
434 <h1 class=appendix>Appendix Chapter</h1>
435 <h2 class=appendix>Appendix Paragraph</h2>
438 With the code underneath:
441 use HTML::TocInsertor;
443 my $toc = HTML::Toc->new();
444 my $tocInsertor = HTML::TocInsertor->new();
448 'tokenBegin' => '<h1 class=-appendix>'
450 'tokenBegin' => '<h2 class=-appendix>',
453 'groupId' => 'appendix',
454 'tokenBegin' => '<h1 class=appendix>',
456 'groupId' => 'appendix',
457 'tokenBegin' => '<h2 class=appendix>',
461 $tocInsertor->insertIntoFile($toc, $filename);
466 <!-- Table of Contents generated by Perl - HTML::Toc -->
468 <li><a href=#h-1>Chapter</a>
470 <li><a href=#h-1.1>Paragraph</a>
472 <li><a href=#h-2>Chapter</a>
475 <li><a href=#appendix-1>Appendix Chapter</a>
477 <li><a href=#appendix-1.1>Appendix Paragraph</a>
480 <!-- End of generated Table of Contents -->
482 <a name=h-1><h1>Chapter</h1></a>
483 <a name=h-1.1><h2>Paragraph</h2></a>
484 <h3>Subparagraph</h3>
485 <a name=h-2><h1>Chapter</h1></a>
486 <a name=appendix-1><h1 class=appendix>Appendix Chapter</h1></a>
487 <a name=appendix-1.1><h2 class=appendix>Appendix Paragraph</h2></a>
490 =head3 Specify an additional 'Part' group
492 Suppose you want to generate a ToC of a document which is divided in multiple parts like this file underneath:
495 <h1 class=part>First Part</h1>
498 <h1 class=part>Second Part</h1>
503 With the code underneath:
506 use HTML::TocInsertor;
508 my $toc = HTML::Toc->new();
509 my $tocInsertor = HTML::TocInsertor->new();
512 'doNumberToken' => 1,
514 'tokenBegin' => '<h1 class=-part>'
516 'tokenBegin' => '<h2 class=-part>',
520 'tokenBegin' => '<h1 class=part>',
522 'numberingStyle' => 'upper-alpha'
525 $tocInsertor->insertIntoFile($toc, $filename);
530 <!-- Table of Contents generated by Perl - HTML::Toc -->
532 <li><a href=#part-A>First Part</a>
535 <li><a href=#h-1>Chapter</a>
537 <li><a href=#h-1.1>Paragraph</a>
541 <li><a href=#part-B>Second Part</a>
544 <li><a href=#h-2>Chapter</a>
546 <li><a href=#h-2.1>Paragraph</a>
549 <!-- End of generated Table of Contents -->
551 <a name=part-A><h1 class=part>A First Part</h1></a>
552 <a name=h-1><h1>1 Chapter</h1></a>
553 <a name=h-1.1><h2>1.1 Paragraph</h2></a>
554 <a name=part-B><h1 class=part>B Second Part</h1></a>
555 <a name=h-2><h1>2 Chapter</h1></a>
556 <a name=h-2.1><h2>2.1 Paragraph</h2></a>
559 =head2 Number ToC entries
561 By default, the generated ToC will list its entries unnumbered. If you want to number the ToC entries, two options are available. Either you can specify a numbered list by modifying L<templateLevelBegin|"templateLevelBegin"> and L<templateLevelEnd|"templateLevelEnd">. Or when the ToC isn't a simple numbered list, you can use the numbers generated by HTML::TocGenerator.
563 =head3 Specify numbered list
565 By modifying L<templateLevelBegin|"templateLevelBegin"> and L<templateLevelEnd|"templateLevelEnd"> you can specify a numbered ToC:
568 use HTML::TocGenerator;
570 my $toc = HTML::Toc->new();
571 my $tocGenerator = HTML::TocGenerator->new();
574 'templateLevelBegin' => '"<ol>\n"',
575 'templateLevelEnd' => '"</ol>\n"',
577 $tocGenerator->generateFromFile($toc, 'index.htm');
578 print $toc->format();
580 For instance with the original file containing:
587 The formatted ToC now will contain C<ol> instead of C<ul> tags:
589 <!-- Table of Contents generated by Perl - HTML::Toc -->
591 <li><a href=#h-1>Chapter</a>
593 <li><a href=#h-1.1>Paragraph</a>
596 <!-- End of generated Table of Contents -->
598 See also: L<Using CSS for ToC formatting|"Using CSS for ToC formatting">.
600 =head3 Use generated numbers
602 Instead of using the HTML ordered list (OL), it's also possible to use the generated numbers to number to ToC nodes. This can be done by modifying L<templateLevel|"templateLevel">:
605 use HTML::TocGenerator;
607 my $toc = HTML::Toc->new();
608 my $tocGenerator = HTML::TocGenerator->new();
611 'templateLevel' => '"<li>$node $text\n"',
613 $tocGenerator->generateFromFile($toc, 'index.htm');
614 print $toc->format();
616 For instance with the original file containing:
623 The formatted ToC now will have the node numbers hard-coded:
625 <!-- Table of Contents generated by Perl - HTML::Toc -->
627 <li>1 <a href=#h-1>Chapter</a>
629 <li>1.1 <a href=#h-1.1>Paragraph</a>
632 <!-- End of generated Table of Contents -->
634 See also: L<Using CSS for ToC formatting|"Using CSS for ToC formatting">.
636 =head2 Using CSS for ToC formatting
638 Suppose you want to display a ToC with upper-alpha numbered appendix headings. To accomplish this, you can specify a CSS style within the source document:
642 <style type="text/css">
643 ol.toc_appendix1 { list-style-type: upper-alpha }
647 <h1 class=appendix>Appendix</h1>
648 <h2 class=appendix>Appendix Paragraph</h2>
649 <h1 class=appendix>Appendix</h1>
650 <h2 class=appendix>Appendix Paragraph</h2>
656 my $toc = new HTML::Toc;
657 my $tocInsertor = new HTML::TocInsertor;
660 'templateLevelBegin' => '"<ol class=toc_$groupId$level>\n"',
661 'templateLevelEnd' => '"</ol>\n"',
662 'doNumberToken' => 1,
664 'groupId' => 'appendix',
665 'tokenBegin' => '<h1>',
666 'numberingStyle' => 'upper-alpha'
668 'groupId' => 'appendix',
669 'tokenBegin' => '<h2>',
673 $tocInsertor->insertIntoFile($toc, $filename);
675 Which whill result in the following output:
679 <style type="text/css">
680 ol.toc_appendix1 { list-style-type: upper-alpha }
684 <!-- Table of Contents generated by Perl - HTML::Toc -->
685 <ol class=toc_appendix1>
686 <li><a href=#appendix-A>Appendix</a>
687 <ol class=toc_appendix2>
688 <li><a href=#appendix-A.1>Appendix Paragraph</a>
690 <li><a href=#appendix-B>Appendix</a>
691 <ol class=toc_appendix2>
692 <li><a href=#appendix-B.1>Appendix Paragraph</a>
695 <!-- End of generated Table of Contents -->
697 <a name=appendix-A><h1>A Appendix</h1></a>
698 <a name=appendix-A.1><h2>A.1 Appendix Paragraph</h2></a>
699 <a name=appendix-B><h1>B Appendix</h1></a>
700 <a name=appendix-B.1><h2>B.1 Appendix Paragraph</h2></a>
704 =head2 Creating site map
706 Suppose you want to generate a table of contents of the E<lt>titleE<gt> tags of the files in the following directory structure:
710 . index.htm, <title>Main</title>
711 |- SubDir1 index.htm, <title>Sub1</title>
712 | |- SubSubDir1 index.htm, <title>SubSub1</title>
714 |- SubDir2 index.htm, <title>Sub2</title>
715 | |- SubSubDir1 index.htm, <title>SubSub1</title>
716 | |- SubSubDir2 index.htm, <title>SubSub2</title>
718 |- SubDir3 index.htm, <title>Sub3</title>
720 By specifying 'fileSpec' which determine how many slashes (/) each file may contain for a specific level:
723 use HTML::TocGenerator;
726 my $toc = HTML::Toc->new;
727 my $tocGenerator = HTML::TocGenerator->new;
731 # Add file to 'fileList' if extension matches '.htm'
732 push (@fileList, $File::Find::name) if (m/\.htm$/);
737 'templateAnchorName' => '""',
738 'templateAnchorHref' => '"<a href=$file"."#".$groupId.$level.">"',
739 'doLinkTocToToken' => 1,
743 'tokenBegin' => '<title>',
744 'tokenEnd' => '</title>',
745 'fileSpec' => '\./[^/]+$'
749 'tokenBegin' => '<title>',
750 'tokenEnd' => '</title>',
751 'fileSpec' => '\./[^/]+?/[^/]+$'
755 'tokenBegin' => '<title>',
756 'tokenEnd' => '</title>',
757 'fileSpec' => '\./[^/]+?/[^/]+?/[^/]+$'
761 # Traverse directory structure
762 find({wanted => \&wanted, no_chdir => 1}, '.');
763 # Generate ToC of case-insensitively sorted file list
764 $tocGenerator->extendFromFile(
765 $toc, [sort {uc($a) cmp uc($b)} @fileList]
767 print $toc->format();
769 the following ToC will be generated:
771 <!-- Table of Contents generated by Perl - HTML::Toc -->
773 <li><a href=./index.htm#>Main</a>
775 <li><a href=./SubDir1/index.htm#>Sub1</a>
777 <li><a href=./SubDir1/SubSubDir1/index.htm#>SubSub1</a>
779 <li><a href=./SubDir2/index.htm#>Sub2</a>
781 <li><a href=./SubDir2/SubSubDir1/index.htm#>SubSub1</a>
782 <li><a href=./SubDir2/SubSubDir2/index.htm#>SubSub2</a>
784 <li><a href=./SubDir3/index.htm#>Sub3</a>
787 <!-- End of generated Table of Contents -->
791 =head2 HTML::Toc::clear()
793 syntax: $toc->clear()
798 =head2 HTML::Toc::format()
800 syntax: $scalar = $toc->format()
801 returns: Formatted ToC.
803 Format tokenized ToC.
805 =head2 HTML::TocGenerator::extend()
807 syntax: $tocGenerator->extend($toc, $string [, $options])
808 args: - $toc: (reference to array of) HTML::Toc object(s) to extend
809 - $string: string to retrieve ToC from
810 - $options: hash reference containing generator options.
812 Extend ToC from specified string. For generator options, see L<HTML::TocGenerator Options|"HTML::TocGenerator Options">
814 =head2 HTML::TocGenerator::extendFromFile()
816 syntax: $tocGenerator->extendFromFile($toc, $filename [, $options])
817 args: - $toc: (reference to array of) HTML::Toc object(s) to extend
818 - $filename: (reference to array of) file(s) to extend ToC from
819 - $options: hash reference containing generator options.
821 Extend ToC from specified file. For generator options, see L<HTML::TocGenerator Options|"HTML::TocGenerator Options">. For an example, see L<Extend an existing ToC>.
823 =head2 HTML::TocGenerator::generate()
825 syntax: $tocGenerator->generate($toc, $string [, $options])
826 args: - $toc: (reference to array of) HTML::Toc object(s) to generate
827 - $string: string to retrieve ToC from
828 - $options: hash reference containing generator options.
830 Generate ToC from specified string. Before generating, the ToC will be cleared. For extending an existing ToC, use the L<HTML::TocGenerator::extend()|"HTML::TocGenerator::extend()"> method. For generator options, see L<HTML::TocGenerator Options|"HTML::TocGenerator Options">.
832 =head2 HTML::TocGenerator::generateFromFile()
834 syntax: $tocGenerator->generateFromFile($toc, $filename [, $options])
835 args: - $toc: (reference to array of) HTML::Toc object(s) to
837 - $filename: (reference to array of) file(s) to generate ToC from
838 - $options: hash reference containing generator options.
840 Generate ToC from specified file. Before generating, the ToC will be cleared. For extending an extisting ToC, use the L<HTML::TocGenerator::extendFromFile()|"HTML::TocGenerator::extendFromFile()"> method. For generator options, see L<HTML::TocGenerator Options|"HTML::TocGenerator Options">.
842 =head2 HTML::TocInsertor::insert()
844 syntax: $tocInsertor->insert($toc, $string [, $options])
845 args: - $toc: (reference to array of) HTML::Toc object(s) to insert
846 - $string: string to insert ToC in
847 - $options: hash reference containing insertor options.
849 Insert ToC into specified string. For insertor options, see L<HTML::TocInsertor Options|"HTML::TocInsertor Options">.
851 =head2 HTML::TocInsertor::insertIntoFile()
853 syntax: $tocInsertor->insertIntoFile($toc, $filename [, $options])
854 args: - $toc: (reference to array of) HTML::Toc object(s) to insert
855 - $filename: (reference to array of) file(s) to insert ToC in
856 - $options: hash reference containing insertor options.
858 Insert ToC into specified file. For insertor options, see L<HTML::TocInsertor Options|"HTML::TocInsertor Options">.
860 =head2 HTML::TocUpdator::insert()
862 syntax: $tocUpdator->insert($toc, $string [, $options])
863 args: - $toc: (reference to array of) HTML::Toc object(s) to insert
864 - $string: string to insert ToC in
865 - $options: hash reference containing updator options.
867 Insert ToC into specified string. Differs from L<HTML::TocInsertor::insert()|"HTML::TocInsertor::insert()"> in that inserted text will be surrounded with update tokens in order for C<HTML::TocUpdator> to be able to update this text the next time an update is issued. For updator options, see L<HTML::TocUpdator Options|"HTML::TocUpdator Options">.
869 =head2 HTML::TocUpdator::insertIntoFile()
871 syntax: $tocUpdator->insertIntoFile($toc, $filename [, $options])
872 args: - $toc: (reference to array of) HTML::Toc object(s) to insert
873 - $filename: (reference to array of) file(s) to insert ToC in
874 - $options: hash reference containing updator options.
876 Insert ToC into specified file. Differs from L<HTML::TocInsertor::insert()|"HTML::TocInsertor::insert()"> in that inserted text will be surrounded with update tokens in order for C<HTML::TocUpdator> to be able to update this text the next time an update is issued. For updator options, see L<HTML::TocUpdator Options|"HTML::TocUpdator Options">.
878 =head2 HTML::TocUpdator::update()
880 syntax: $tocUpdator->update($toc, $string [, $options])
881 args: - $toc: (reference to array of) HTML::Toc object(s) to insert
882 - $string: string to update ToC in
883 - $options: hash reference containing updator options.
885 Update ToC within specified string. For updator options, see L<HTML::TocUpdator Options|"HTML::TocUpdator Options">.
887 =head2 HTML::TocUpdator::updateFile()
889 syntax: $tocUpdator->updateFile($toc, $filename [, $options])
890 args: - $toc: (reference to array of) HTML::Toc object(s) to insert
891 - $filename: (reference to array of) file(s) to update ToC in
892 - $options: hash reference containing updator options.
894 Update ToC of specified file. For updator options, see L<HTML::TocUpdator Options|"HTML::TocUpdator Options">.
896 =head1 Parser Options
898 When generating a ToC, additional options may be specified which influence the way the ToCs are generated using either C<TocGenerator>, C<TocInsertor> or C<TocUpdator>. The options must be specified as a hash reference. For example:
900 $tocGenerator->generateFromFile($toc, $filename, {doUseGroupsGlobal => 1});
902 Available options are:
906 =item L<doGenerateToc|"doGenerateToc">
908 =item L<doUseGroupsGlobal|"doUseGroupsGlobal">
910 =item L<output|"output">
912 =item L<outputFile|"outputFile">
920 applicable to: TocInsertor, TocUpdator
922 True (1) if ToC must be generated. False (0) if ToC must be inserted only.
924 =head2 doUseGroupsGlobal
928 applicable to: TocGenerator, TocInsertor, TocUpdator
930 True (1) if group levels must be used globally accross ToCs. False (0) if not. This option only makes sense when an array of ToCs is specified. For example, suppose you want to generate two ToCs, one ToC for '<h1>' tokens and one ToC for '<h2>' tokens, of the file 'index.htm':
935 Using the default setting of 'doUseGroupsGlobal' => 0:
938 use HTML::TocGenerator;
940 my $toc1 = HTML::Toc->new();
941 my $toc2 = HTML::Toc->new();
942 my $tocGenerator = HTML::TocGenerator->new();
947 'tokenToToc' => [{'tokenBegin' => '<h1>'}]
952 'tokenToToc' => [{'tokenBegin' => '<h2>'}]
954 $tocGenerator->generateFromFile([$toc1, $toc2], 'index.htm');
955 print $toc1->format() . "\n\n" . $toc2->format();
960 <li><a href=#h-1>Chapter</a>
964 <li><a href=#h-1>Paragraph</a>
967 Each ToC will use its own numbering scheme. Now if 'C<doUseGroupsGlobal = 1>' is specified:
969 $tocGenerator->generateFromFile(
970 [$toc1, $toc2], 'index.htm', {'doUseGroupsGlobal' => 1}
976 <li><a href=#h-1>Chapter</a>
980 <li><a href=#h-2>Paragraph</a>
983 using a global numbering scheme for all ToCs.
987 syntax: reference to scalar
989 applicable to: TocInsertor, TocUpdator
991 Reference to scalar where the output must be stored in.
997 applicable to: TocInsertor, TocUpdator
999 Filename to write output to. If no filename is specified, output will be written to standard output.
1001 =head1 HTML::Toc Options
1003 The C<HTML::Toc> options can be grouped in the following categories:
1007 =item L<Generate options|"Generate options">
1009 =item L<Insert options|"Insert options">
1011 =item L<Update options|"Update options">
1013 =item L<Format options|"Format options">
1017 The ToC options must be specified using the 'setOptions' method. For example:
1019 my $toc = new HTML::Toc;
1022 'doNumberToken' => 1,
1023 'footer' => '<!-- End Of ToC -->'
1026 'tokenBegin' => '<h1>',
1027 'numberingStyle' => 'lower-alpha'
1031 =head2 Generate options
1039 =item L<tokenToToc|"tokenToToc">
1043 =item L<doNumberToken|"doNumberToken">
1045 =item L<fileSpec|"fileSpec">
1047 =item L<groupId|"groupId">
1049 =item L<level|"level">
1051 =item L<tokenBegin|"tokenBegin">
1053 =item L<tokenEnd|"tokenEnd">
1055 =item L<numberingStyle|"numberingStyle">
1059 =item L<groupToToc|"groupToToc">
1061 =item L<levelToToc|"levelToToc">
1065 =item Numbering tokens
1069 =item L<doNumberToken|"doNumberToken">
1071 =item L<numberingStyle|"numberingStyle">
1073 =item L<templateTokenNumber|"templateTokenNumber">
1081 =item L<attributeToExcludeToken|"attributeToExcludeToken">
1083 =item L<attributeToTocToken|"attributeToTocToken">
1085 =item L<groupToToc|"groupToToc">
1087 =item L<levelToToc|"levelToToc">
1091 =item Linking ToC to tokens
1095 =item L<doLinkToToken|"doLinkToToken">
1097 =item L<doLinkToFile|"doLinkToFile">
1099 =item L<doLinkToId|"doLinkToId">
1101 =item L<templateAnchorName|"templateAnchorName">
1103 =item L<templateAnchorHrefBegin|"templateAnchorHrefBegin">
1105 =item L<templateAnchorHrefEnd|"templateAnchorHrefEnd">
1107 =item L<templateAnchorNameBegin|"templateAnchorNameBegin">
1109 =item L<templateAnchorNameEnd|"templateAnchorNameEnd">
1115 =head2 Insert options
1119 =item L<insertionPoint|"insertionPoint">
1123 =head2 Update options
1127 =item L<tokenUpdateBeginAnchorName|"tokenUpdateBeginAnchorName">
1129 =item L<tokenUpdateEndAnchorName|"tokenUpdateEndAnchorName">
1131 =item L<tokenUpdateBeginToc|"tokenUpdateBeginToc">
1133 =item L<tokenUpdateEndToc|"tokenUpdateEndToc">
1135 =item L<tokenUpdateBeginNumber|"tokenUpdateBeginNumber">
1137 =item L<tokenUpdateEndNumber|"tokenUpdateEndNumber">
1141 =head2 Format options
1145 =item L<doSingleStepLevel|"doSingleStepLevel">
1147 =item L<doNestGroup|"doNestGroup">
1149 =item L<footer|"footer">
1151 =item L<groupToToc|"groupToToc">
1153 =item L<header|"header">
1155 =item L<levelIndent|"levelIndent">
1157 =item L<levelToToc|"levelToToc">
1159 =item L<templateLevelBegin|"templateLevelBegin">
1161 =item L<templateLevelEnd|"templateLevelEnd">
1165 =head1 HTML::Toc Options Reference
1167 =head2 attributeToExcludeToken
1172 Token which marks an attribute value in a L<tokenBegin|"tokenBegin"> or L<insertionPoint|"insertionPoint"> token as an attribute value a token should not have to be marked as a ToC token. See also: L<Using attribute value as ToC entry|"Using attribute value as ToC text">.
1174 =head2 attributeToTocToken
1179 Token which marks an attribute in a L<tokenBegin|"tokenBegin"> token as an attribute which must be used as ToC text. See also: L<Using attribute value as ToC entry|"Using attribute value as ToC text">.
1181 =head2 doLinkToToken
1186 True (1) if ToC must be linked to tokens, False (0) if not. Note that 'HTML::TocInsertor' must be used to do the actual insertion of the anchor name within the source data.
1193 True (1) if ToC must be linked to file, False (0) if not. In effect only when L<doLinkToToken|"doLinkToToken"> equals True (1) and L<templateAnchorHrefBegin|"templateAnchorHrefBegin"> isn't specified.
1200 True (1) if ToC must be linked to tokens by using token ids. False (0) if ToC must be linked to tokens by using anchor names.
1207 True (1) if groups must be nested in the formatted ToC, False (0) if not. In effect only when multiple groups are specified within the L<tokenToToc|"tokenToToc"> setting. For an example, see L<Generate multiple groups in one ToC|"Generate multiple groups in one ToC">.
1209 =head2 doNumberToken
1214 True (1) if tokens which are used for the ToC generation must be numbered. This option may be specified both as a global ToC option or within a L<tokenToToc|"tokenToToc"> group. When specified within a C<tokenToToc> option, the C<doNumberToken> applies to that group only. For an example, see L<Specify an additional 'Part' group|"Specify an additional 'Part' group">.
1216 =head2 doSingleStepLevel
1221 True (1) if levels of a formatted ToC must advance one level at a time. For example, when generating a ToC of a file with a missing '<h2>':
1226 By default, an empty indentation level will be inserted in the ToC:
1228 <!-- Table of Contents generated by Perl - HTML::Toc -->
1230 <li><a href=#h-1>Header 1</a>
1233 <li><a href=#h-1.0.1>Header 3</a>
1237 <!-- End of generated Table of Contents -->
1241 $toc->setOptions({'doSingleStepLevel' => 0});
1243 the ToC will not have an indentation level inserted for level 2:
1245 <!-- Table of Contents generated by Perl - HTML::Toc -->
1247 <li><a href=#h-1>Header 1</a>
1249 <li><a href=#h-1.0.1>Header 3</a>
1252 <!-- End of generated Table of Contents -->
1259 Specifies which files should match the current level. Valid only if L<doLinkToFile|"doLinkToFile"> equals 1. For an example, see L<Site map|"Site map">.
1264 default: "\n<!-- End of generated Table of Contents -->\n"
1266 String to output at end of ToC.
1273 Sets the group id attribute of a tokenGroup. With this attribute it's possible to divide the ToC into multiple groups. Each group has its own numbering scheme. For example, to generate a ToC of both normal headings and 'appendix' headings, specify the following ToC settings:
1277 'tokenBegin' => '<h1 class=-appendix>'
1279 'groupId' => 'appendix',
1280 'tokenBegin' => '<h1 class=appendix>'
1289 Determines which groups to use for generating the ToC. For example, to create a ToC for groups [a-b] only, specify:
1291 'groupToToc => '[a-b]'
1293 This option is evaluated during both ToC generation and ToC formatting. This enables you to generate a ToC of all groups, but - after generating - format only specified groups:
1295 $toc->setOptions({'groupToToc' => '.*'});
1296 $tocGenerator->generateToc($toc, ...);
1297 # Get ToC of all groups
1298 $fullToc = $toc->format();
1299 # Get ToC of 'appendix' group only
1300 $toc->setOptions({'groupToToc' => 'appendix'});
1301 $appendixToc = $toc->format();
1306 default: "\n<!-- Table of Contents generated by Perl - HTML::Toc -->\n"
1308 String to output at begin of ToC.
1311 =head2 insertionPoint
1313 syntax: [<before|after|replace>] <token>
1314 default: 'after <body>'
1315 token: <[/]tag{ attribute=[-|@]<regexp>}> |
1317 <declaration regexp> |
1320 Determines the point within the source, where the ToC should be inserted. When specifying a start tag as the insertion point token, attributes to be included may be specified as well. Note that the attribute value must be specified as a regular expression. For example, to specify the C<<h1 class=header>> tag as insertion point:
1322 '<h1 class=^header$>'
1324 Examples of valid 'insertionPoint' tokens are:
1330 'ToC will be placed here'
1332 It is also possible to specify attributes to exclude, by prefixing the value with an L<attributeToExcludeToken|"attributeToExcludeToken">, default a minus sign (-). For example, to specify the C<<h1>> tag as insertion point, excluding all C<<h1 class=header>> tags:
1334 '<h1 class=-^header$>'
1336 See also L<tokenBegin|"tokenBegin">.
1343 Number which identifies at which level the tokengroup should be incorporated into the ToC. See also: L<tokenToToc|"tokenToToc">.
1350 Sets the number of spaces each level will be indented, when formatting the ToC.
1357 Determines which group levels to use for generating the ToC. For example, to create a ToC for levels 1-2 only, specify:
1359 'levelToToc => '[1-2]'
1361 This option is evaluated during both ToC generation and ToC formatting. This enables you to generate a ToC of all levels, but - after generating - retrieve only specified levels:
1363 $toc->setOptions({'levelToToc' => '.*'});
1364 $tocGenerator->generateToc($toc, ...);
1365 # Get ToC of all levels
1366 $fullToc = $toc->getToc();
1367 # Get ToC of level 1 only
1368 $toc->setOptions({'levelToToc' => '1'});
1369 $level1Toc = $toc->getToc();
1371 =head2 numberingStyle
1373 syntax: [decimal|lower-alpha|upper-alpha|lower-roman|upper-roman]}
1376 Determines which numbering style to use for a token group when L<doLinkToToken|"doLinkToToken"> is set to True (1). When specified as a main ToC option, the setting will be the default for all groups. When specified within a tokengroup, this setting will override any default for that particular tokengroup, e.g.:
1379 'doNumberToken' => 1,
1382 'tokenBegin' => '<h1>',
1383 'numberingStyle' => 'lower-alpha'
1387 If C<roman> style is specified, be sure to have the Roman module installed, available from L<http://www.perl.com/CPAN/modules/by-module/Roman>.
1389 =head2 templateAnchorName
1391 syntax: <expression|function reference>
1392 default: '$groupId."-".$node'
1394 Anchor name to use when L<doLinkToToken|"doLinkToToken"> is set to True (1). The anchor name is passed to both L<templateAnchorHrefBegin|"templateAnchorHrefBegin"> and L<templateAnchorNameBegin|"templateAnchorNameBegin">. The template may be specified as either an expression or a function reference. The expression may contain the following variables:
1401 If C<templateAnchorHrefBegin> is a function reference to a function returning the anchor, like in:
1403 $toc->setOptions({'templateAnchorName' => \&assembleAnchorName});
1405 the function will be called with the following arguments:
1407 $anchorName = assembleAnchorName($file, $groupId, $level, $node);
1409 =head2 templateAnchorHrefBegin
1411 syntax: <expression|function reference>
1412 default: '"<a href=#$anchorName>"' or
1413 '"<a href=$file#$anchorName>"',
1414 depending on 'doLinkToFile' being 0 or 1 respectively.
1416 Anchor reference begin token to use when L<doLinkToToken|"doLinkToToken"> is set to True (1). The template may be specified as either an expression or a function reference. The expression may contain the following variables:
1424 If C<templateAnchorHrefBegin> is a function reference to a function returning the anchor, like in:
1426 $toc->setOptions({'templateAnchorHrefBegin' => \&assembleAnchorHrefBegin});
1428 the function will be called with the following arguments:
1430 $anchorHrefBegin = &assembleAnchorHrefBegin(
1431 $file, $groupId, $level, $node, $anchorName
1434 See also: L<templateAnchorName|"templateAnchorName">, L<templateAnchorHrefEnd|"templateAnchorHrefEnd">.
1436 =head2 templateAnchorHrefEnd
1438 syntax: <expression|function reference>
1441 Anchor reference end token to use when L<doLinkToToken|"doLinkToToken"> is set to True (1). The template may be specified as either an expression or a function reference. If L<templateAnchorHrefEnd|"templateAnchorHrefEnd"> is a function reference to a function returning the anchor end, like in:
1443 $toc->setOptions({'templateAnchorHrefEnd' => \&assembleAnchorHrefEnd});
1445 the function will be called without arguments:
1447 $anchorHrefEnd = &assembleAnchorHrefEnd;
1449 See also: L<templateAnchorHrefBegin|"templateAnchorHrefBegin">.
1451 =head2 templateAnchorNameBegin
1453 syntax: <expression|function reference>
1454 default: '"<a name=$anchorName>"'
1456 Anchor name begin token to use when L<doLinkToToken|"doLinkToToken"> is set to True (1). The template may be specified as either an expression or a function reference. The expression may contain the following variables:
1464 If C<templateAnchorNameBegin> is a function reference to a function returning the anchor name, like in:
1466 $toc->setOptions({'templateAnchorNameBegin' => \&assembleAnchorNameBegin});
1468 the function will be called with the following arguments:
1470 $anchorNameBegin = assembleAnchorNameBegin(
1471 $file, $groupId, $level, $node, $anchorName
1474 See also: L<templateAnchorName|"templateAnchorName">, L<templateAnchorNameEnd|"templateAnchorNameEnd">.
1476 =head2 templateAnchorNameEnd
1478 syntax: <expression|function reference>
1481 Anchor name end token to use when L<doLinkToToken|"doLinkToToken"> is set to True (1). The template may be specified as either an expression or a function reference. If L<templateAnchorNameEnd|"templateAnchorNameEnd"> is a function reference to a function returning the anchor end, like in:
1483 $toc->setOptions({'templateAnchorNameEnd' => \&assembleAnchorNameEnd});
1485 the function will be called without arguments:
1487 $anchorNameEnd = &assembleAnchorNameEnd;
1489 See also: L<templateAnchorNameBegin|"templateAnchorNameBegin">.
1491 =head2 templateLevel
1493 syntax: <expression|function reference>
1494 default: '"<li>$text\n"'
1496 Expression to use when formatting a ToC node. The template may be specified as either an expression or a function reference. The expression may contain the following variables:
1504 If C<templateLevel> is a function reference to a function returning the ToC node, like in:
1506 $toc->setOptions({'templateLevel' => \&AssembleTocNode});
1508 the function will be called with the following arguments:
1510 $tocNode = &AssembleTocNode(
1511 $level, $groupId, $node, $sequenceNr, $text
1514 =head2 templateLevelBegin
1516 syntax: <expression>
1519 Expression to use when formatting begin of ToC level. See L<templateLevel|"templateLevel"> for list of available variables to use within the expression. For example, to give each ToC level a class name to use with Cascading Style Sheets (CSS), use the expression:
1521 '"<ul class=toc_$groupId$level>\n"'
1523 which will result in each ToC group being given a class name:
1529 For an example, see L<Using CSS for ToC formatting|"Using CSS for ToC formatting">.
1531 =head2 templateLevelEnd
1533 syntax: <expression>
1536 Expression to use when formatting end of ToC level. See L<templateLevel|"templateLevel"> for a list of available variables to use within the expression. The default expression is:
1540 For an example, see L<Using CSS for ToC formatting|"Using CSS for ToC formatting">.
1542 =head2 templateTokenNumber
1544 syntax: <expression|function reference>
1545 default: '"$node "'
1547 Token number to use when L<doNumberToken|"doNumberToken"> equals True (1). The template may be specified as either an expression or a function reference. The expression has access to the following variables:
1556 If C<templateTokenNumber> is a function reference to a function returning the token number, like in:
1558 $toc->setOptions({'templateTokenNumber' => \&assembleTokenNumber});
1560 the function will be called with the following arguments:
1562 $number = &assembleTokenNumber(
1563 $node, $groupId, $file, $groupLevel, $level, $toc
1570 token: <[/]tag{ attribute=[-|@]<regexp>}> |
1572 <declaration regexp> |
1575 This scalar defines the token that will trigger text to be put into the ToC. Any start tag, end tag, comment, declaration or text string can be specified. Examples of valid 'tokenBegin' tokens are:
1579 '<!-- Start ToC entry -->'
1580 '<!Start ToC entry>'
1583 When specifying a start tag, attributes to be included may be specified as well. Note that the attribute value is used as a regular expression. For example, to specify the C<<h1 class=header>> tag as tokenBegin:
1585 '<h1 class=^header$>'
1587 It is also possible to specify attributes to exclude, by prefixing the value with an L<attributeToExcludeToken|"attributeToExcludeToken">, default a minus sign (-). For example, to specify the C<<h1>> tag as tokenBegin, excluding all C<<h1 class=header>> tags:
1589 '<h1 class=-^header$>'
1591 Also, you can specify here an attribute value which has to be used as ToC text, by prefixing the value with an L<attributeToTocToken|"">, default an at sign (@). For example, to use the class value as ToC text:
1595 See L<Generate multiple ToCs|"Generate multiple ToCs"> for an elaborated example using the C<attributeToTocToken> to generate a ToC of image C<alt> attribute values.
1597 See also: L<tokenEnd|"tokenEnd">, L<tokenToToc|"tokenToToc">.
1602 default: empty string ('') or end tag counterpart of 'tokenBegin' if
1603 'tokenBegin' is a start tag
1605 The 'tokenEnd' definition applies to the same rules as L<tokenBegin|"tokenBegin">.
1607 See also: L<tokenBegin|"tokenBegin">, L<tokenToToc|"tokenToToc">.
1611 syntax: [{array of hashrefs}]
1614 'tokenBegin' => '<h1>'
1617 'tokenBegin' => '<h2>'
1620 'tokenBegin' => '<h3>'
1623 'tokenBegin' => '<h4>'
1626 'tokenBegin' => '<h5>'
1629 'tokenBegin' => '<h6>'
1632 This hash define the tokens that must act as ToC entries. Each tokengroup may contain a L<groupId|"groupId">, L<level|"level">, L<numberingStyle|"numberingStyle">, L<tokenBegin|"tokenBegin"> and L<tokenEnd|"tokenEnd"> identifier.
1634 =head2 tokenUpdateBeginAnchorName
1637 default: '<!-- #BeginTocAnchorNameBegin -->';
1639 This token marks the begin of an anchor name, inserted by C<HTML::TocInsertor>. This option is used by C<HTML::TocUpdator>.
1641 =head2 tokenUpdateEndAnchorName
1644 default: '<!-- #EndTocAnchorName -->';
1646 This option is used by C<HTML::TocUpdator>, to mark the end of an inserted anchor name.
1648 =head2 tokenUpdateBeginNumber
1651 default: '<!-- #BeginTocNumber -->';
1653 This option is used by C<HTML::TocUpdator>, to mark the begin of an inserted number.
1655 =head2 tokenUpdateEndNumber
1658 default: '<!-- #EndTocAnchorName -->';
1660 This option is used by C<HTML::TocUpdator>, to mark the end of an inserted number.
1662 =head2 tokenUpdateBeginToc
1665 default: '<!-- #BeginToc -->';
1667 This option is used by C<HTML::TocUpdator>, to mark the begin of an inserted ToC.
1669 =head2 tokenUpdateEndToc
1672 default: '<!-- #EndToc -->';
1674 This option is used by C<HTML::TocUpdator>, to mark the end of an inserted ToC.
1680 In order for the test files to run on Cygwin without errors, the 'UNIX' default text file type has to be selected during the Cygwin setup.
1681 When extracting the tar.gz file with WinZip the 'TAR file smart CR/LF conversion' has to be turned off via {Options|Configuration...|Miscellaneous} in order for the files 'toc.pod' and './manualTest/manualTest1.htm' to be left in UNIX format.
1683 =head2 Nested anchors
1685 HTML::Toc can only link to existing anchors if these anchors are placed outside of the ToC tokens. Otherwise a warning will be given. For example, generating a L<linked|"doLinkToToken"> ToC of C<E<lt>h1E<gt>> tokens of the following text:
1687 <a name=foo><h1>Header</h1></a>
1689 will go all right, whereas:
1691 <h1><a name=foo>Header</a></h1>
1693 will yield the warning:
1695 warning (1): Nested anchor '<a name=foo>' within anchor '<a name=h-1>'.
1697 since anchor names aren't allowed to be nested according to the HTML 4.01 specification.
1701 Freddy Vulto E<lt>L<"fvu@fvu.myweb.nl">E<gt>
1705 Copyright (c) 2001 Freddy Vulto. All rights reserved.
1707 This library is free software; you can redistribute it and/or
1708 modify it under the same terms as Perl itself.