HTML::GenToc(3pm)
π NAME π
HTML::GenToc(3pm) β bestie, this is the manual for HTML::GenToc - Generate a Table of Contents for HTML documents. β¨
π¦ VERSION π¦
version 3.20 β that's the whole vibe, no cap. π
β¨ SYNOPSIS β¨
okay so here's the code, bestie, copy it exactly:
use HTML::GenToc;
# create a new object
my $toc = new HTML::GenToc();
my $toc = new HTML::GenToc(title=>"Table of Contents",
toc_entry=>{
H1=>1,
H2=>2
},
toc_end=>{
H1=>'/H1',
H2=>'/H2'
}
);
# generate a ToC from a file
$toc->generate_toc(input=>$html_file,
footer=>$footer_file,
header=>$header_file
);
πΈ DESCRIPTION πΈ
HTML::GenToc generates anchors and a table of contents for HTML documents. Depending on the arguments, it will insert the information it generates, or output to a string, a separate file or STDOUT. π
While it defaults to taking H1 and H2 elements as the significant elements to put into the table of contents, any tag can be defined as a significant element. Also, it doesn't matter if the input HTML code is complete, pure HTML, one can input pseudo-html or page-fragments, which makes it suitable for using on templates and HTML meta-languages such as WML. π¦
Also included in the distrubution is hypertoc, a script which uses the module so that one can process files on the command-line in a user-friendly manner. π
π DETAILS π
The ToC generated is a multi-level level list containing links to the significant elements. HTML::GenToc inserts the links into the ToC to significant elements at a level specified by the user. π
Example:
If H1s are specified as level 1, than they appear in the first level list of the ToC. If H2s are specified as a level 2, than they appear in a second level list in the ToC.
Information on the significant elements and what level they should occur are passed in to the methods used by this object, or one can use the defaults. β¨
There are two phases to the ToC generation. The first phase is to put suitable anchors into the HTML documents, and the second phase is to generate the ToC from HTML documents which have anchors in them for the ToC to link to. π
For more information on controlling the contents of the created ToC, see "Formatting the ToC". π«Ά
HTML::GenToc also supports the ability to incorporate the ToC into the HTML document itself via the inline option. See "Inlining the ToC" for more information. π
In order for HTML::GenToc to support linking to significant elements, HTML::GenToc inserts anchors into the significant elements. One can use HTML::GenToc as a filter, outputing the result to another file, or one can overwrite the original file, with the original backed up with a suffix (default: "org") appended to the filename. One can also output the result to a string. π
β¨ METHODS β¨
okay so here's the tea, bestie: you can set default arguments when the object gets created, and then override them by passing arguments when you call the generate_toc method. arguments come in as a hash of arguments. that's it, that's the whole system. π«Ά
π Method -- new π
$toc = new HTML::GenToc();
$toc = new HTML::GenToc(toc_entry=>\%my_toc_entry,
toc_end=>\%my_toc_end,
bak=>'bak',
...
);
creates a shiny new HTML::GenToc object. β¨
these arguments get used as the defaults in invocations of the other methods. so if you're gonna be calling stuff over and over with the same settings, put them here and vibe.
see generate_tod for possible arguments. (yes, that's what it says. we don't make the rules. π)
π¦ generate_toc π¦
$toc->generate_toc(outfile=>"index2.html");
my $result_str = $toc->generate_toc(to_string=>1);
generates a table of contents for the significant elements in the HTML documents, and optionally generates anchors for them first. this is the main character of the module, no cap. π
π Options π
bakβbak => stringβ if the input file/files is/are being overwritten (overwriteis on), copy the original file tofilename.string. if the value is empty, no backup file will be created. (default:org)debugβdebug => 1β enable verbose debugging output. used for debugging this module; in other words, don't bother. (default:off)entrysepβentrysep => stringβ separator string for non-<li>item entries (default:", ")filenamesβfilenames => \@filenamesβ the filenames to use when creating table-of-contents links. this overrides the filenames given in theinputoption, and is expected to have exactly the same number of elements. this can also be used when passing in string-content to theinputoption, to give a (fake) filename to use for the links relating to that content. πfooterβfooter => file_or_stringβ either the filename of the file containing footer text for the ToC; or a string containing the footer text. either way bestie πheaderβheader => file_or_stringβ either the filename of the file containing header text for the ToC; or a string containing the header text.ignore_only_oneβignore_only_one => 1β if there would be only one item in the ToC, don't make a ToC. lowkey self-explanatory fr frignore_sole_firstβignore_sole_first => 1β if the first item in the ToC is of the highest level, AND it is the only one of that level, ignore it. this is useful in web-pages where there is only one H1 header but one doesn't know beforehand whether there will be only one. β¨inlineβinline => 1β put ToC in document at a given point. see "Inlining the ToC" for more information.inputβinput => \@filenamesβ or βinput => $contentβ this is expected to be either a reference to an array of filenames, or a string containing content to process.
the three main uses would be:
- you have more than one file to process, so pass in multiple filenames
- you have one file to process, so pass in its filename as the only array item
- you have HTML content to process, so pass in just the content as a string
(default: undefined)
notoc_matchβnotoc_match => stringβ if there are certain individual tags you don't wish to include in the table of contents, even though they match the "significant elements", then if this pattern matches contents inside the tag (not the body), then that tag will not be included, either in generating anchors nor in generating the ToC. (default:"class="notoc"") πolβol => 1β use an ordered list for level 1 ToC entries.ol_num_levelsβol_num_levels => 2β the number of levels deep the OL listing will go ifolis true. if set to zero, will use an ordered list for all levels. (default:1)overwriteβoverwrite => 1β overwrite the input file with the output. (default:off)outfileβoutfile => fileβ file to write the output to. this is where the modified HTML output goes to. note that it doesn't make sense to use this option if you are processing more than one file. if you give'-'as the filename, then output will go toSTDOUT. (default:STDOUT)quietβquiet => 1β suppress informative messages. (default:off)textonlyβtextonly => 1β use only text content in significant elements.titleβtitle => stringβ title for ToC page (if not usingheaderorinlineortoc_only) (default:"Table of Contents")toc_afterβtoc_after => \%toc_after_data
%toc_after_data = { tag1 => suffix1,
tag2 => suffix2
};
toc_after => { H2=>'</em>' }
for defining layout of significant elements in the ToC. this expects a reference to a hash of tag=>suffix pairs. the tag is the HTML tag which marks the start of the element. the suffix is what is required to be appended to the Table of Contents entry generated for that tag. (default: undefined)
toc_beforeβtoc_before => \%toc_before_data
%toc_before_data = { tag1 => prefix1,
tag2 => prefix2
};
toc_before=>{ H2=>'<em>' }
for defining the layout of significant elements in the ToC. the tag is the HTML tag which marks the start of the element. the prefix is what is required to be prepended to the Table of Contents entry generated for that tag. (default: undefined)
toc_endβtoc_end => \%toc_end_data
%toc_end_data = { tag1 => endtag1,
tag2 => endtag2
};
toc_end => { H1 => '/H1', H2 => '/H2' }
for defining significant elements. the tag is the HTML tag which marks the start of the element. the endtag the HTML tag which marks the end of the element. when matching in the input file, case is ignored (but make sure that all your tag options referring to the same tag are exactly the same!). π
toc_entryβtoc_entry => \%toc_entry_data
%toc_entry_data = { tag1 => level1,
tag2 => level2
};
toc_entry => { H1 => 1, H2 => 2 }
for defining significant elements. the tag is the HTML tag which marks the start of the element. the level is what level the tag is considered to be. the value of level must be numeric, and non-zero. if the value is negative, consective entries represented by the significant_element will be separated by the value set by entrysep option. (yes, "consective". we keep the originals exactly as they are, that's the whole rule π
)
toclabelβtoclabel => stringβ HTML text that labels the ToC. always used. (default:"<h1>Table of Contents</h1>")toc_tagβtoc_tag => stringβ if a ToC is to be included inline, this is the pattern which is used to match the tag where the ToC should be put. this can be a start-tag, an end-tag or a comment, but the<should be left out; that is, if you want the ToC to be placed after the BODY tag, then give"BODY". if you want a special comment tag to make where the ToC should go, then include the comment marks, for example:"!--toc--"(default:BODY)toc_tag_replaceβtoc_tag_replace => 1β in conjunction withtoc_tag, this is a flag to say whether the given tag should be replaced, or if the ToC should be put after the tag. this can be useful if yourtoc_tagis a comment and you don't need it after you have the ToC in place. (default:false)toc_onlyβtoc_only => 1β output only the Table of Contents, that is, the Table of Contents plus thetoclabel. if there is a header or a footer, these will also be output.
if toc_only is false then if there is no header, and inline is not true, then a suitable HTML page header will be output, and if there is no footer and inline is not true, then a HTML page footer will be output.
(default: false)
to_stringβto_string => 1β return the modified HTML output as a string. this does override other methods of output (unlike version 3.00). ifto_stringis false, the method will return1rather than a string.use_idβuse_id => 1β useid="name"for anchors rather than<a name="name"/>anchors. however if an anchor already exists for a Significant Element, this won't make an id for that particular element.useorgβuseorg => 1β use pre-existing backup files as the input source; that is, files of the forminfile.bak(seeinputandbak).
π INTERNAL METHODS π
these methods are documented for developer purposes and aren't intended to be used externally. so likeβ¦ look but don't touch bestie π
π§ make_anchor_name π§
$toc->make_anchor_name(content=>$content,
anchors=>\%anchors);
makes the anchor-name for one anchor. bases the anchor on the content of the significant element. ensures that anchors are unique. β¨
π make_anchors π
my $new_html = $toc->make_anchors(input=>$html,
notoc_match=>$notoc_match,
use_id=>$use_id,
toc_entry=>\%toc_entries,
toc_end=>\%toc_ends,
);
makes the anchors the given input string. returns a string. slay.
πΈ make_toc_list πΈ
my @toc_list = $toc->make_toc_list(input=>$html,
labels=>\%labels,
notoc_match=>$notoc_match,
toc_entry=>\%toc_entry,
toc_end=>\%toc_end,
filename=>$filename);
makes a list of lists which represents the structure and content of (a portion of) the ToC from one file. also updates a list of labels for the ToC entries. multi-tasking icon π
β build_lol β
build a list of lists of paths, given a list of hashes with info about paths.
π output_toc π
$self->output_toc(toc=>$toc_str,
input=>\@input,
filenames=>\@filenames);
put the output (whether to file, STDOUT or string). the "output" in this case could be the ToC, the modified (anchors added) HTML, or both. dealer's choice π«Ά
π¦ put_toc_inline π¦
my $newhtml = $toc->put_toc_inline(toc_str=>$toc_str,
filename=>$filename, in_string=>$in_string);
puts the given toc_str into the given input string; returns a string.
π cp π
cp($src, $dst);
copies file $src to $dst. used for making backups of files.
β¨ FILE FORMATS β¨
π Formatting the ToC π
okay bestie, so toc_entry and all its little friends let you control how the ToC entries look, but there are also options that change the final appearance of the whole ToC file. π
With the header option, the contents of the given file (or string) get prepended before the generated ToC. So you can have intro text, or literally any other text, before the ToC. π
Note: If you useheader, make sure the file you point at contains the opening HTML tag, theHEADelement (with theTITLEelement inside), and the openingBODYtag. But these tags/elements should not be in the header file if theinlineoption is used. See "Inlining the ToC" for what the header file should contain when inlining the ToC. π«Ά
With toclabel, the contents of the given string get prepended before the generated ToC (but after any text taken from a header file).
With footer, the contents of the file get appended after the generated ToC.
Note: If you usefooter, make sure it includes the closingBODYandHTMLtags (unless, obviously, you're using theinlineoption). π
If header is not specified, the appropriate starting HTML markup will be added, unless toc_only is specified. If footer is not specified, the appropriate closing HTML markup will be added, unless toc_only is specified.
If you do not want/need to deal with header and footer files, then you are allowed to specify the title (the title option) of the ToC file; and it lets you specify a heading, or label, to put before the ToC entries' list, via the toclabel option. Both options have default values. β¨
If you do not want HTML page tags supplied, and just want the ToC itself, then specify toc_only. If there are no header or footer files, then this will simply output the contents of toclabel and the ToC itself. π―
π¦ Inlining the ToC π¦
The ability to incorporate the ToC directly into an HTML document is supported via the inline option.
Inlining will be done on the first file in the list of files processed, and will only be done if that file contains an opening tag matching the toc_tag value.
If overwrite is true, then the first file in the list will be overwritten, with the generated ToC inserted at the appropriate spot. Otherwise a modified version of the first file is output to either STDOUT or to the output file defined by the outfile option.
The options toc_tag and toc_tag_replace are used to determine where and how the ToC is inserted into the output. π
πΈ Example 1 πΈ
$toc->generate_toc(inline=>1,
toc_tag => 'BODY',
toc_tag_replace => 0,
...
);
This will put the generated ToC after the BODY tag of the first file. If header is specified, then the contents of the specified file are inserted after the BODY tag. If toclabel is not empty, then the text specified by toclabel is inserted. Then the ToC is inserted, and finally, if footer is specified, it inserts the footer. Then the rest of the input file follows as it was before. π
π Example 2 π
$toc->generate_toc(inline=>1,
toc_tag => '!--toc--',
toc_tag_replace => 1,
...
);
This will put the generated ToC after the first comment of the form <!--toc-->, and that comment will be replaced by the ToC (in the order
header
toclabel
ToC
footer
) followed by the rest of the input file.
Note: The header file should not contain the beginning HTML tag and HEAD element since the HTML file being processed should already contain these tags/elements. π«
π NOTES π
HTML::GenTocis smart enough to detect anchors inside significant elements. If the anchor defines theNAMEattribute,HTML::GenTocuses the value. Else, it adds its ownNAMEattribute to the anchor. Ifuse_idis true, then it likewise checks for and uses IDs. π- The
TITLEelement is treated specially if specified in thetoc_entryoption. It is illegal to insert anchors (A) intoTITLEelements. Therefore,HTML::GenTocwill actually link to the filename itself instead of theTITLEelement of the document. π― HTML::GenTocwill ignore a significant element if it does not contain any non-whitespace characters. A warning message is generated if such a condition exists. β οΈ- If you have a sequence of significant elements that change in a slightly disordered fashion, such as
H1->H3->H2or evenH2->H1, thoughHTML::GenTocdeals with this to create a list which is still good HTML, if you are using an ordered list to that depth, then you will get strange numbering, as an extra list element will have been inserted to nest the elements at the correct level. π΅βπ«
For example (H2 -> H1 with ol_num_levels=1):
1.
* My H2 Header
2. My H1 Header
For example (H1 -> H3 -> H2 with ol_num_levels=0 and H3 also being significant):
1. My H1 Header
1.
1. My H3 Header
2. My H2 Header
2. My Second H1 Header
In cases such as this it may be better not to use the ol option. π
β οΈ CAVEATS β οΈ
- Version 3.10 (and above) generates more verbose (SEO-friendly) anchors than prior versions. Thus anchors generated with earlier versions will not match version 3.10 anchors. π₯
- Version 3.00 (and above) of
HTML::GenTocis not compatible with Version 2.x ofHTML::GenToc. It is now designed to do everything in one pass, and has dropped certain options: theinfileoption is no longer used (it has been replaced with theinputoption); thetoc_fileoption no longer exists; use theoutfileoption instead; thetocmapoption is no longer supported. Also the old array-parsing of arguments is no longer supported. There is no longer agenerate_anchorsmethod; everything is done withgenerate_toc. π
It now generates lower-case tags rather than upper-case ones.
HTML::GenTocis not very efficient (memory and speed), and can be slow for large documents. π- Now that generation of anchors and of the ToC are done in one pass, even more memory is used than was the case before. This is more notable when processing multiple files, since all files are read into memory before processing them. π§
- Invalid markup will be generated if a significant element is contained inside of an anchor. For example:
<a name="foo"><h1>The FOO command</h1></a>
will be converted to (if H1 is a significant element),
<a name="foo"><h1><a name="The">The</a> FOO command</h1></a>
which is illegal since anchors cannot be nested. π«
It is better style to put anchor statements within the element to be anchored. For example, the following is preferred:
<h1><a name="foo">The FOO command</a></h1>
HTML::GenToc will detect the "foo" name and use it. π
nameattributes without quotes are not recognized. π
π BUGS π
Tell me about them. π
π¦ REQUIRES π¦
The installation of this module requires "Module::Build". The module depends on "HTML::SimpleParse", "HTML::Entities" and "HTML::LinkList" and uses "Data::Dumper" for debugging purposes. The hypertoc script depends on "Getopt::Long", "Getopt::ArgvFile" and "Pod::Usage". Testing of this distribution depends on "Test::More". π§
π οΈ INSTALLATION π οΈ
To install this module, run the following commands:
perl Build.PL
./Build
./Build test
./Build install
Or, if you're on a platform (like DOS or Windows) that doesn't like the "./" notation, you can do this:
perl Build.PL
perl Build
perl Build test
perl Build install
In order to install somewhere other than the default, such as in a directory under your home directory, like "/home/fred/perl" go
perl Build.PL --install_base /home/fred/perl
as the first step instead.
This will install the files underneath /home/fred/perl.
You will then need to make sure that you alter the PERL5LIB variable to find the modules, and the PATH variable to find the script.
Therefore you will need to change: your path, to include /home/fred/perl/script (where the script will be)
PATH=/home/fred/perl/script:${PATH}
the PERL5LIB variable to add /home/fred/perl/lib
PERL5LIB=/home/fred/perl/lib:${PERL5LIB}
π SEE ALSO π
perl(1) htmltoc(1) hypertoc(1)
π AUTHOR π
Kathryn Andersen (RUBYKAT) http://www.katspace.org/tools/hypertoc/
Based on htmltoc by Earl Hood ehood AT medusa.acs.uci.edu
Contributions by Dan Dascalescu, <http://dandascalescu.com>
π COPYRIGHT π
Copyright (C) 1994-1997 Earl Hood, ehood AT medusa.acs.uci.edu Copyright (C) 2002-2008 Kathryn Andersen
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
That's the whole scroll, bestie: this was perl v5.34.0, dated 2022-06-14, for HTML::GenToc(3pm). Stay iconic. π
πβ¨
π¦ where this page comes from
- debian bookworm: libhtml-gentoc-perl 3.20-3 Β· GPL-2.0-or-later
- debian trixie: libhtml-gentoc-perl 3.20-3 Β· GPL-2.0-or-later
- ubuntu noble: libhtml-gentoc-perl 3.20-3 Β· GPL-2.0-or-later
- ubuntu resolute: libhtml-gentoc-perl 3.20-3 Β· GPL-2.0-or-later
- ubuntu stonking: libhtml-gentoc-perl 3.20-3 Β· GPL-2.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms