HXTOC(1)

πŸ“¦
version 8.8 Β· 2 versions
, in alpine 3.24, arch rolling, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0

✨ HXTOC(1) ✨

okay so bestie, welcome to hxtoc, one of the little legends from the HTML-XML-utils collection πŸ’– it's the tool that slides a whole table of contents into your HTML like it's decorating your bedroom wall 🌸


πŸŽ€ NAME πŸŽ€

hxtoc β€” insert a table of contents in an HTML file. yes. that's the whole job. and it ate πŸ’…


🌈 SYNOPSIS 🌈

hxtoc [ -x ] [ -l low ] [ -h high ] [ -t ] [ -d ] [ -c class ] [ -f ] [
file-or-URL ]

πŸ’– DESCRIPTION πŸ’–

so here's the vibe, bestie: hxtoc reads an HTML file and goes to work. It inserts the missing ID attributes in all H1 to H6 elements that fall between the levels -l and -h β€” unless the option -d is in effect, in which case scroll down and read the -d bit, okay? πŸ‘€

Unless the option -t is given, it also inserts A elements with NAME attributes, because old browsers literally do not recognize ID attributes as target anchors. ancestors said no πŸ’€ so we give them the old-school version too.

The output gets written to stdout. classic.

Now β€” if there's a comment that looks like this:

<!--toc-->

or a pair of comments like this:

<!--begin-toc-->
...
<!--end-toc-->

then that comment, or the pair with everything in between, gets replaced by a table of contents. and what is that TOC made of? a list (UL) of links to all the headers in the document. it's giving navigation, it's giving main character energy πŸ‘‘

The text of the headers gets copied into this table of contents, and yes β€” including any inline markup. The exceptions? ID attributes, DFN tags, and SPAN tags with a CLASS of "index" are omitted. (but the elements' content is still copied, don't worry 🫢)

The copied text can optionally be "flattened" first β€” see option -f below.

And one more thing, because it matters: if a header has a CLASS attribute whose value (or one of whose values) is the keyword "no-toc", then that header will not appear in the table of contents. it said "not today" and honestly, respect πŸ™ˆ


✨ OPTIONS ✨

The following options are supported:

  • -x β€” Use XML conventions: empty elements are written with a slash at the end, like <IMG />. slay formatting πŸ’…
  • -l low β€” Sets the lowest numbered header to appear in the table of content. Default is 1 (i.e., H1).
  • -h high β€” Sets the highest numbered header to appear in the table of content. Default is unlimited. no ceiling, no cap, fr fr πŸ”₯
  • -t β€” Normally, hxtoc adds both ID attributes and empty A elements with a NAME attribute and CLASS="bctarget", so that older browsers that do no understand ID will still find the target. With this option, the A elements will not be generated.
  • -c class β€” The generated UL elements in the table of contents will have a CLASS attribute with the value class. The default is "toc".
  • -d β€” Tries to use sectioning elements as targets in the table of contents instead of H1 to H6. A sectioning elements is a DIV, SECTION, ARTICLE, ASIDE or NAV element that contains at least one heading element (H1 to H6) or HGROUP. The sectioning element will be given an ID if it doesn't have one yet. With this option, the level of any H1 to H6 that is the first heading of a sectioning element is not determined by its name, but by the nesting depth of the sectioning elements. (Any H1 to H6 that are not the first heading of a sectioning element still have their level implied by their name.)
  • -f β€” Flatten the text of the table of contents. Without -f, the contents of header elements are copied to the table of contents almost unchanged, i.e., including any child elements and their attributes (except for ID attributes, DFN elements and certain SPAN elements, as explained above). With -f, the contents are flattened instead: All child elements are removed and only their contents are copied to the table of contents. Additionally elements with an ALT attribute, such as IMG, are replaced by the contents of the ALT attribute. Exception: BDO tags are copied unchanged and elements with a DIR attribute are replaced by a SPAN with that DIR attribute. (BDO and DIR may occur in languages written right-to-left.) 🌍

πŸ¦‹ OPERANDS πŸ¦‹

The following operand is supported:

  • file-or-URL β€” The name or URL of an HTML file. If absent, standard input is read instead. no file? no problem, just pipe it in 🫢

πŸ‘‘ DIAGNOSTICS πŸ‘‘

The following exit values are returned:

  • 0 β€” Successful completion. we love a clean exit, iconic ⭐
  • > 0 β€” An error occurred in the parsing of the HTML file. hxtoc will try to correct the error and produce output anyway. it's a fixer, it's a healer 🧚

🌸 SEE ALSO 🌸

asc2xml(1), hxnormalize(1), hxnum(1), xml2asc(1)

go read the whole friend group, they're all so good πŸ’œ


πŸ’€ BUGS πŸ’€

The error recovery for incorrect HTML is primitive. ngl bestie, it tries its best but it's not a miracle worker 😭


okay that's the whole page!! version 7.x, dated 10 Jul 2011 β€” yes she's an elder but she still serves πŸ’–βœ¨ bye bestie, go build that table of contents πŸŽ€