]> git.lyx.org Git - lyx.git/commitdiff
XHTML: fix generation of many useless </section>.
authorThibaut Cuvelier <tcuvelier@lyx.org>
Thu, 10 Sep 2020 22:44:49 +0000 (00:44 +0200)
committerThibaut Cuvelier <tcuvelier@lyx.org>
Sat, 19 Sep 2020 18:43:40 +0000 (20:43 +0200)
src/output_xhtml.cpp

index 7d9a69bd6ae6369627f7b734968f0f7069b4dab1..f8cd53e47867cfba71920ac9af059632b34af482 100644 (file)
@@ -611,20 +611,26 @@ void xhtmlParagraphs(Text const & text,
 
                // Think about adding <section> and/or </section>s.
                if (style.category() == from_utf8("Sectioning")) {
 
                // Think about adding <section> and/or </section>s.
                if (style.category() == from_utf8("Sectioning")) {
+                       int level = style.toclevel;
+
                        // Need to close a previous section if it has the same level or a higher one (close <section> if opening a
                        // 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:
+                       // <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)
                        //   - 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()) {
+                       while (!headerLevels.empty() && level <= headerLevels.top()) {
+                               // Output the tag only if it corresponds to a legit section.
+                               int stackLevel = headerLevels.top();
+                               if (stackLevel != Layout::NOT_IN_TOC && level > 1) { // <h1> is the document title.
+                                       xs << xml::EndTag("section");
+                                       xs << xml::CR();
+                               }
                                headerLevels.pop();
                                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.
+                       // Open the new section: first push it onto the stack, then output it in XHTML.
+                       headerLevels.push(level);
+                       // Some sectioning-like elements should not be output (such as FrontMatter).
+                       if (level != Layout::NOT_IN_TOC && level > 1) { // <h1> is the document title.
                                xs << xml::StartTag("section");
                                xs << xml::CR();
                        }
                                xs << xml::StartTag("section");
                                xs << xml::CR();
                        }
@@ -669,9 +675,11 @@ void xhtmlParagraphs(Text const & text,
 
        // If need be, close <section>s, but only at the end of the document (otherwise, dealt with at the beginning
        // of the loop).
 
        // If need be, close <section>s, but only at the end of the document (otherwise, dealt with at the beginning
        // of the loop).
-       while (! headerLevels.empty() && headerLevels.top() > 1) {
+       while (!headerLevels.empty() && headerLevels.top() > Layout::NOT_IN_TOC) {
+               docstring tag = from_utf8("</section>");
                headerLevels.pop();
                headerLevels.pop();
-               xs << xml::EndTag("section") << xml::CR();
+               xs << XMLStream::ESCAPE_NONE << tag;
+               xs << xml::CR();
        }
 }
 
        }
 }