]> git.lyx.org Git - features.git/blobdiff - src/output_xhtml.cpp
Add button to stop processing
[features.git] / src / output_xhtml.cpp
index 428e6c7f72608186c6728d84a0dc4d6aa0acc4d8..f1bcfb879b3cfba5b579fdde04d05b65bc9c41a8 100644 (file)
@@ -3,7 +3,7 @@
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
- * \author Richard Heck
+ * \author Richard Kimberly Heck
  *
  * This code is based upon output_docbook.cpp
  *
 #include "output_xhtml.h"
 
 #include "Buffer.h"
-#include "buffer_funcs.h"
 #include "BufferParams.h"
 #include "Counters.h"
 #include "Font.h"
 #include "Layout.h"
-#include "OutputParams.h"
 #include "Paragraph.h"
 #include "ParagraphList.h"
 #include "ParagraphParameters.h"
 #include "Text.h"
 #include "TextClass.h"
 
-#include "support/convert.h"
-#include "support/debug.h"
 #include "support/lassert.h"
-#include "support/lstrings.h"
-#include "support/textutils.h"
 
-#include <vector>
+#include <stack>
+#include <iostream>
 
 // Uncomment to activate debugging code.
 // #define XHTML_DEBUG
@@ -161,15 +156,18 @@ namespace {
 // convenience functions
 
 inline void openParTag(XMLStream & xs, Layout const & lay,
-                       std::string parlabel)
+                       const std::string & parlabel)
 {
-       xs << xml::ParTag(lay.htmltag(), lay.htmlattr(), parlabel);
+       string attrs = lay.htmlattr();
+       if (!parlabel.empty())
+               attrs += " id='" + parlabel + "'";
+       xs << xml::ParTag(lay.htmltag(), attrs);
 }
 
 
 void openParTag(XMLStream & xs, Layout const & lay,
                 ParagraphParameters const & params,
-                std::string parlabel)
+                const std::string & parlabel)
 {
        // FIXME Are there other things we should handle here?
        string const align = alignmentToCSS(params.align());
@@ -178,7 +176,9 @@ void openParTag(XMLStream & xs, Layout const & lay,
                return;
        }
        string attrs = lay.htmlattr() + " style='text-align: " + align + ";'";
-       xs << xml::ParTag(lay.htmltag(), attrs, parlabel);
+       if (!parlabel.empty())
+               attrs += " id='" + parlabel + "'";
+       xs << xml::ParTag(lay.htmltag(), attrs);
 }
 
 
@@ -585,6 +585,8 @@ void xhtmlParagraphs(Text const & text,
        ParagraphList::const_iterator const pend =
                (epit == (int) paragraphs.size()) ?
                        paragraphs.end() : paragraphs.iterator_at(epit);
+       std::stack<int> headerLevels;
+
        while (bpit < epit) {
                ParagraphList::const_iterator par = paragraphs.iterator_at(bpit);
                if (par->params().startOfAppendix()) {
@@ -602,6 +604,35 @@ void xhtmlParagraphs(Text const & text,
                ParagraphList::const_iterator const lastpar = par;
                ParagraphList::const_iterator send;
 
+               // Think about adding <section> and/or </section>s.
+               // Document title is not in Sectioning, but rather in FrontMatter, so that it does not need to be taken
+               // into account.
+               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
+                       // <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)
+                       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) {
+                                       xs << xml::EndTag("section");
+                                       xs << xml::CR();
+                               }
+                               headerLevels.pop();
+                       }
+
+                       // 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 ) {
+                               xs << xml::StartTag("section");
+                               xs << xml::CR();
+                       }
+               }
+
                switch (style.latextype) {
                case LATEX_COMMAND: {
                        // The files with which we are working never have more than
@@ -638,6 +669,14 @@ void xhtmlParagraphs(Text const & text,
                }
                bpit += distance(lastpar, par);
        }
+
+       // 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() != Layout::NOT_IN_TOC) {
+               headerLevels.pop();
+               xs << xml::EndTag("section");
+               xs << xml::CR();
+       }
 }