+ // Think about adding <section> and/or </section>s.
+ if (style.category() == from_utf8("Sectioning")) {
+ // Need to close a previous section if it has the same level or a higher one (close <section> if opening a
+ // <h2> after a <h2>, <h3>, <h4>, <h5>, or <h6>). More examples:
+ // - current: h2; back: h1; do not close any <section>
+ // - current: h1; back: h2; close two <section> (first the <h2>, then the <h1>, so a new <h1> can come)
+ // The level (h1, h2, etc.) corresponds to style.toclevel.
+ while (! headerLevels.empty() && style.toclevel <= headerLevels.top()) {
+ headerLevels.pop();
+ xs << xml::EndTag("section");
+ xs << xml::CR();
+ }
+
+ // Open the new one.
+ headerLevels.push(style.toclevel);
+ if (style.toclevel > 1) { // <h1> is the document title.
+ xs << xml::StartTag("section");
+ xs << xml::CR();
+ }
+ }
+