* 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 "LayoutEnums.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
namespace lyx {
-docstring const & fontToHtmlTag(xml::FontTypes type)
+docstring fontToHtmlTag(xml::FontTypes type)
{
switch(type) {
case xml::FontTypes::FT_EMPH:
}
-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);
}
// convenience functions
inline void openParTag(XMLStream & xs, Layout const & lay,
- std::string parlabel)
+ std::string const & parlabel)
{
- xs << xml::ParTag(lay.htmltag(), lay.htmlattr(), parlabel);
+ string attrs = lay.htmlGetAttrString();
+ if (!parlabel.empty())
+ attrs += " id='" + parlabel + "'";
+ xs << xml::ParTag(lay.htmltag(), attrs);
}
+void openParTag(XMLStream & xs, Layout const & lay,
+ std::string const & cssclass,
+ std::string const & parlabel) {
+ string attrs = "class='" + cssclass + "'";
+ 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)
+ std::string const & parlabel)
{
// FIXME Are there other things we should handle here?
string const align = alignmentToCSS(params.align());
openParTag(xs, lay, parlabel);
return;
}
- string attrs = lay.htmlattr() + " style='text-align: " + align + ";'";
- xs << xml::ParTag(lay.htmltag(), attrs, parlabel);
+ string attrs = lay.htmlGetAttrString() + " style='text-align: " + align + ";'";
+ if (!parlabel.empty())
+ attrs += " id='" + parlabel + "'";
+ xs << xml::ParTag(lay.htmltag(), attrs);
}
}
-inline void openItemTag(XMLStream & xs, Layout const & lay)
+inline void openItemTag(XMLStream & xs, Layout const & lay,
+ std::string const & parlabel)
{
- xs << xml::StartTag(lay.htmlitemtag(), lay.htmlitemattr(), true);
+ if (lay.htmlitemtag() != "NONE") {
+ string attrs = lay.htmlitemattr();
+ if (!parlabel.empty())
+ attrs += " id='" + parlabel + "'";
+ xs << xml::StartTag(lay.htmlitemtag(), attrs, true);
+ }
}
void openItemTag(XMLStream & xs, Layout const & lay,
- ParagraphParameters const & params)
+ ParagraphParameters const & params,
+ std::string const & parlabel)
{
- // FIXME Are there other things we should handle here?
- string const align = alignmentToCSS(params.align());
- if (align.empty()) {
- openItemTag(xs, lay);
- return;
+ if (lay.htmlitemtag() != "NONE") {
+ // FIXME Are there other things we should handle here?
+ string const align = alignmentToCSS(params.align());
+ if (align.empty()) {
+ openItemTag(xs, lay, parlabel);
+ return;
+ }
+ string attrs = lay.htmlGetAttrString() + " style='text-align: " + align + ";'";
+ if (!parlabel.empty())
+ attrs += " id='" + parlabel + "'";
+ xs << xml::StartTag(lay.htmlitemtag(), attrs);
}
- string attrs = lay.htmlattr() + " style='text-align: " + align + ";'";
- xs << xml::StartTag(lay.htmlitemtag(), attrs);
}
inline void closeItemTag(XMLStream & xs, Layout const & lay)
{
- xs << xml::EndTag(lay.htmlitemtag());
+ if (lay.htmlitemtag() != "NONE") {
+ xs << xml::EndTag(lay.htmlitemtag());
+ }
}
// end of convenience functions
ParagraphList::const_iterator const begin = text.paragraphs().begin();
ParagraphList::const_iterator par = pbegin;
Layout const & bstyle = par->layout();
- depth_type const origdepth = pbegin->params().depth();
+ depth_type const origdepth = par->params().depth();
+ string const parId = bstyle.htmlitemtag().empty() ? par->magicLabel() : "";
// open tag for this environment
- openParTag(xs, bstyle, pbegin->magicLabel());
+ if ((bstyle.labeltype == LABEL_ENUMERATE || bstyle.labeltype == LABEL_ITEMIZE)
+ && bstyle.htmlclass().empty()) {
+ // In this case, we have to calculate the CSS class ourselves, each time
+ // through
+ // FIXME We assume in these cases that the standard counters are being used.
+ // (We also do not deal with 'resume' counters, though I'm not sure that can
+ // be done at all in HTML.)
+
+ // Code adapted from Buffer::Impl::setLabel
+ bool const isenum = bstyle.labeltype == LABEL_ENUMERATE;
+ docstring enumcounter = bstyle.counter.empty() ?
+ ( isenum ? from_ascii("enum") : from_ascii("lyxitem") ) :
+ bstyle.counter;
+ switch (par->itemdepth) {
+ case 2:
+ enumcounter += 'i';
+ // fall through
+ case 1:
+ enumcounter += 'i';
+ // fall through
+ case 0:
+ enumcounter += 'i';
+ break;
+ case 3:
+ enumcounter += "iv";
+ break;
+ default:
+ // not a valid enumdepth...
+ break;
+ }
+ const string cssClass = string(isenum ? "lyxenum" : "lyxitem") + " "
+ + to_utf8(enumcounter);
+ openParTag(xs, bstyle, cssClass, parId);
+ }
+ else
+ openParTag(xs, bstyle, parId);
xs << xml::CR();
// we will on occasion need to remember a layout from before.
if (!style.counter.empty()
&& (par == pbegin || !isNormalEnv(style))
&& cnts.hasCounter(cntr)
- )
+ )
cnts.step(cntr, OutputUpdate);
ParagraphList::const_iterator send;
pos_type sep = 0;
bool const labelfirst = style.htmllabelfirst();
if (!labelfirst)
- openItemTag(xs, style, par->params());
+ openItemTag(xs, style, par->params(), par->magicLabel());
// label output
if (style.labeltype != LABEL_NO_LABEL &&
xs << xml::CR();
}
else {
- openLabelTag(xs, style);
- xs << par->params().labelString();
- closeLabelTag(xs, style);
- xs << xml::CR();
+ docstring const & ls = par->params().labelString();
+ if (!ls.empty()) {
+ openLabelTag(xs, style);
+ xs << ls;
+ closeLabelTag(xs, style);
+ xs << xml::CR();
+ }
}
}
} // end label output
if (labelfirst)
- openItemTag(xs, style, par->params());
+ openItemTag(xs, style, par->params(), par->magicLabel());
docstring deferred = par->simpleLyXHTMLOnePar(buf, xs, runparams,
text.outerFont(distance(begin, par)), true, true, sep);
buf.masterBuffer()->params().
documentClass().counters().step(style.counter, OutputUpdate);
- bool const make_parid = !runparams.for_toc && runparams.html_make_pars;
-
+ bool const make_parid = !runparams.for_toc && runparams.html_make_pars && style.itemtag().empty();
openParTag(xs, style, pbegin->params(),
make_parid ? pbegin->magicLabel() : "");
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()) {
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
}
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();
+ }
}