1
$\begingroup$

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 ids? Is there some combination of Holds 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.

$\endgroup$
  • $\begingroup$ The online documentation pages do have it so there must be a way. $\endgroup$ – Szabolcs Apr 14 '15 at 20:14
  • 1
    $\begingroup$ How about using "Section" :> instead? $\endgroup$ – Taiki Apr 14 '15 at 21:15
1
$\begingroup$

Taiki's suggestion works. The "ConversionRules" is now:

"ConversionRules" ->
 { "Section" :>
   { "<h4 class=\"Section\"><a id=\"header-cell-" <>
       ToString[NumberForm[headerCount++, 6, "NumberPadding" -> {"0", "0"}]] <> "\">",
     "</a></h4>" } }

The only weird thing is that all the numbers in the output are even! The first header has number 0, the second one is 2, and so on. It appears that the rule is evaluated twice for each cell, maybe once for the opening tag and once for the closing tag. But that's OK! All I wanted was for the tag ids to be unique, and they are.

Thanks!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.