Exporting a notebook as XHTML allows me to override the default HTML tags that are written for section-header-like cells. For example, a notebook cell whose type is "Section", containing text "The Larch" would, by default, be written as the following XHTML:
<p class="Section">The Larch</p>
Now, a section heading might logically be considered to deserve an h4
tag in the output XHTML document (it is alt-4, after all), so I could Export
using ConversionRules
:
Export["mywebpage.xhtml", myNotebook, "XHTMLMathML",
"ConversionRules" -> { "Section" -> {"<h4 class=\"Section\">", "</h4>"} } ]
and then the output XHTML would read
<h4 class="Section">The Larch</h4>
(Aside: I left the class in the h4
tag just in case. It allows some CSS flexibility, and I also might have different header types that are logically the same kind of h-tag. For example, a "Title" and a "Subtitle" are both logically h1
's because they both label the entire document.)
In order to eventually make a nice clickable table of contents, I would like each header-type cell's XHTML to also include an <a id="_something-unique_">
tag, so that the document's XHTML might read:
<h2 class="Chapter"><a id="header-cell-000016">Trees</a></h2>
<h3 class="Subchapter"><a id="header-cell-000017">Interesting Trees</a></h3>
<h4 class="Section"><a id="header-cell-000018">The Larch</a></h4>
<h4 class="Section"><a id="header-cell-000019">The Cedar</a></h4>
I tried including a counter in the ConversionRules
, something like ToString[headerCounter++]
, but it was only evaluated once per rule, so all Chapters were 000002 and all Sections were 000004, and so on.
What should I do to make all these <a>
tags have unique id
s? Is there some combination of Hold
s and Evaluate
or something that will work?
I know I could do it by postprocessing the XHTML, perhaps with XSL or just with Mathematica, but I'm hoping for a more elegant solution.
"Section" :>
instead? $\endgroup$