]> git.lyx.org Git - lyx.git/blobdiff - src/output_xhtml.cpp
de.po
[lyx.git] / src / output_xhtml.cpp
index dcc3f69c333c438a451452d17c60b38a1110564a..7d9a69bd6ae6369627f7b734968f0f7069b4dab1 100644 (file)
@@ -34,7 +34,7 @@
 #include "support/lstrings.h"
 #include "support/textutils.h"
 
-#include <vector>
+#include <stack>
 
 // Uncomment to activate debugging code.
 // #define XHTML_DEBUG
@@ -45,7 +45,7 @@ using namespace lyx::support;
 namespace lyx {
 
 
-docstring const & fontToHtmlTag(xml::FontTypes type)
+docstring fontToHtmlTag(xml::FontTypes type)
 {
     switch(type) {
         case xml::FontTypes::FT_EMPH:
@@ -88,70 +88,66 @@ docstring const & fontToHtmlTag(xml::FontTypes type)
 }
 
 
-namespace {
-
-string fontToAttribute(xml::FontTypes type)
+docstring fontToHtmlAttribute(xml::FontTypes type)
 {
        switch(type) {
        case xml::FontTypes::FT_EMPH:
        case xml::FontTypes::FT_BOLD:
-               return "";
+               return from_ascii("");
        case xml::FontTypes::FT_NOUN:
-               return "class='lyxnoun'";
+               return from_ascii("class='lyxnoun'");
        case xml::FontTypes::FT_UBAR:
-               return "";
+               return from_ascii("");
        case xml::FontTypes::FT_DBAR:
-               return "class='dline'";
+               return from_ascii("class='dline'");
        case xml::FontTypes::FT_XOUT:
        case xml::FontTypes::FT_SOUT:
-               return "class='strikeout'";
+               return from_ascii("class='strikeout'");
        case xml::FontTypes::FT_WAVE:
-               return "class='wline'";
+               return from_ascii("class='wline'");
        case xml::FontTypes::FT_ITALIC:
-               return "";
+               return from_ascii("");
        case xml::FontTypes::FT_UPRIGHT:
-               return "style='font-style:normal;'";
+               return from_ascii("style='font-style:normal;'");
        case xml::FontTypes::FT_SLANTED:
-               return "style='font-style:oblique;'";
+               return from_ascii("style='font-style:oblique;'");
        case xml::FontTypes::FT_SMALLCAPS:
-               return "style='font-variant:small-caps;'";
+               return from_ascii("style='font-variant:small-caps;'");
        case xml::FontTypes::FT_ROMAN:
-               return "style='font-family:serif;'";
+               return from_ascii("style='font-family:serif;'");
        case xml::FontTypes::FT_SANS:
-               return "style='font-family:sans-serif;'";
+               return from_ascii("style='font-family:sans-serif;'");
        case xml::FontTypes::FT_TYPE:
-               return "style='font-family:monospace;'";
+               return from_ascii("style='font-family:monospace;'");
        case xml::FontTypes::FT_SIZE_TINY:
        case xml::FontTypes::FT_SIZE_SCRIPT:
        case xml::FontTypes::FT_SIZE_FOOTNOTE:
-               return "style='font-size:x-small;'";
+               return from_ascii("style='font-size:x-small;'");
        case xml::FontTypes::FT_SIZE_SMALL:
-               return "style='font-size:small;'";
+               return from_ascii("style='font-size:small;'");
        case xml::FontTypes::FT_SIZE_NORMAL:
-               return "style='font-size:normal;'";
+               return from_ascii("style='font-size:normal;'");
        case xml::FontTypes::FT_SIZE_LARGE:
-               return "style='font-size:large;'";
+               return from_ascii("style='font-size:large;'");
        case xml::FontTypes::FT_SIZE_LARGER:
        case xml::FontTypes::FT_SIZE_LARGEST:
-               return "style='font-size:x-large;'";
+               return from_ascii("style='font-size:x-large;'");
        case xml::FontTypes::FT_SIZE_HUGE:
        case xml::FontTypes::FT_SIZE_HUGER:
-               return "style='font-size:xx-large;'";
+               return from_ascii("style='font-size:xx-large;'");
        case xml::FontTypes::FT_SIZE_INCREASE:
-               return "style='font-size:larger;'";
+               return from_ascii("style='font-size:larger;'");
        case xml::FontTypes::FT_SIZE_DECREASE:
-               return "style='font-size:smaller;'";
+               return from_ascii("style='font-size:smaller;'");
        }
        // kill warning
-       return "";
+       return from_ascii("");
 }
 
-} // end anonymous namespace
-
 
 xml::FontTag xhtmlStartFontTag(xml::FontTypes type)
 {
-       return xml::FontTag(fontToHtmlTag(type), from_utf8(fontToAttribute(type)), type);
+       return xml::FontTag(fontToHtmlTag(type), fontToHtmlAttribute(type), type);
 }
 
 
@@ -165,15 +161,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());
@@ -182,7 +181,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);
 }
 
 
@@ -589,6 +590,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()) {
@@ -606,6 +609,27 @@ void xhtmlParagraphs(Text const & text,
                ParagraphList::const_iterator const lastpar = par;
                ParagraphList::const_iterator send;
 
+               // 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();
+                       }
+               }
+
                switch (style.latextype) {
                case LATEX_COMMAND: {
                        // The files with which we are working never have more than
@@ -642,6 +666,13 @@ 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() > 1) {
+               headerLevels.pop();
+               xs << xml::EndTag("section") << xml::CR();
+       }
 }