2 * \file output_xhtml.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Richard Kimberly Heck
8 * This code is based upon output_docbook.cpp
10 * Full author contact details are available in file CREDITS.
15 #include "output_xhtml.h"
18 #include "BufferParams.h"
22 #include "LayoutEnums.h"
23 #include "Paragraph.h"
24 #include "ParagraphList.h"
25 #include "ParagraphParameters.h"
28 #include "TextClass.h"
30 #include "support/lassert.h"
35 // Uncomment to activate debugging code.
36 // #define XHTML_DEBUG
39 using namespace lyx::support;
44 docstring fontToHtmlTag(xml::FontTypes type)
47 case xml::FontTypes::FT_EMPH:
48 return from_utf8("em");
49 case xml::FontTypes::FT_BOLD:
50 return from_utf8("b");
51 case xml::FontTypes::FT_NOUN:
52 return from_utf8("dfn");
53 case xml::FontTypes::FT_UBAR:
54 case xml::FontTypes::FT_WAVE:
55 case xml::FontTypes::FT_DBAR:
56 return from_utf8("u");
57 case xml::FontTypes::FT_SOUT:
58 case xml::FontTypes::FT_XOUT:
59 return from_utf8("del");
60 case xml::FontTypes::FT_ITALIC:
61 return from_utf8("i");
62 case xml::FontTypes::FT_UPRIGHT:
63 case xml::FontTypes::FT_SLANTED:
64 case xml::FontTypes::FT_SMALLCAPS:
65 case xml::FontTypes::FT_ROMAN:
66 case xml::FontTypes::FT_SANS:
67 case xml::FontTypes::FT_TYPE:
68 case xml::FontTypes::FT_SIZE_TINY:
69 case xml::FontTypes::FT_SIZE_SCRIPT:
70 case xml::FontTypes::FT_SIZE_FOOTNOTE:
71 case xml::FontTypes::FT_SIZE_SMALL:
72 case xml::FontTypes::FT_SIZE_NORMAL:
73 case xml::FontTypes::FT_SIZE_LARGE:
74 case xml::FontTypes::FT_SIZE_LARGER:
75 case xml::FontTypes::FT_SIZE_LARGEST:
76 case xml::FontTypes::FT_SIZE_HUGE:
77 case xml::FontTypes::FT_SIZE_HUGER:
78 case xml::FontTypes::FT_SIZE_INCREASE:
79 case xml::FontTypes::FT_SIZE_DECREASE:
80 return from_utf8("span");
87 docstring fontToHtmlAttribute(xml::FontTypes type)
90 case xml::FontTypes::FT_EMPH:
91 case xml::FontTypes::FT_BOLD:
92 return from_ascii("");
93 case xml::FontTypes::FT_NOUN:
94 return from_ascii("class='lyxnoun'");
95 case xml::FontTypes::FT_UBAR:
96 return from_ascii("");
97 case xml::FontTypes::FT_DBAR:
98 return from_ascii("class='dline'");
99 case xml::FontTypes::FT_XOUT:
100 case xml::FontTypes::FT_SOUT:
101 return from_ascii("class='strikeout'");
102 case xml::FontTypes::FT_WAVE:
103 return from_ascii("class='wline'");
104 case xml::FontTypes::FT_ITALIC:
105 return from_ascii("");
106 case xml::FontTypes::FT_UPRIGHT:
107 return from_ascii("style='font-style:normal;'");
108 case xml::FontTypes::FT_SLANTED:
109 return from_ascii("style='font-style:oblique;'");
110 case xml::FontTypes::FT_SMALLCAPS:
111 return from_ascii("style='font-variant:small-caps;'");
112 case xml::FontTypes::FT_ROMAN:
113 return from_ascii("style='font-family:serif;'");
114 case xml::FontTypes::FT_SANS:
115 return from_ascii("style='font-family:sans-serif;'");
116 case xml::FontTypes::FT_TYPE:
117 return from_ascii("style='font-family:monospace;'");
118 case xml::FontTypes::FT_SIZE_TINY:
119 case xml::FontTypes::FT_SIZE_SCRIPT:
120 case xml::FontTypes::FT_SIZE_FOOTNOTE:
121 return from_ascii("style='font-size:x-small;'");
122 case xml::FontTypes::FT_SIZE_SMALL:
123 return from_ascii("style='font-size:small;'");
124 case xml::FontTypes::FT_SIZE_NORMAL:
125 return from_ascii("style='font-size:normal;'");
126 case xml::FontTypes::FT_SIZE_LARGE:
127 return from_ascii("style='font-size:large;'");
128 case xml::FontTypes::FT_SIZE_LARGER:
129 case xml::FontTypes::FT_SIZE_LARGEST:
130 return from_ascii("style='font-size:x-large;'");
131 case xml::FontTypes::FT_SIZE_HUGE:
132 case xml::FontTypes::FT_SIZE_HUGER:
133 return from_ascii("style='font-size:xx-large;'");
134 case xml::FontTypes::FT_SIZE_INCREASE:
135 return from_ascii("style='font-size:larger;'");
136 case xml::FontTypes::FT_SIZE_DECREASE:
137 return from_ascii("style='font-size:smaller;'");
140 return from_ascii("");
144 xml::FontTag xhtmlStartFontTag(xml::FontTypes type)
146 return xml::FontTag(fontToHtmlTag(type), fontToHtmlAttribute(type), type);
150 xml::EndFontTag xhtmlEndFontTag(xml::FontTypes type)
152 return xml::EndFontTag(fontToHtmlTag(type), type);
157 // convenience functions
159 inline void openParTag(XMLStream & xs, Layout const & lay,
160 std::string const & parlabel)
162 string attrs = lay.htmlGetAttrString();
163 if (!parlabel.empty())
164 attrs += " id='" + parlabel + "'";
165 xs << xml::ParTag(lay.htmltag(), attrs);
169 void openParTag(XMLStream & xs, Layout const & lay,
170 std::string const & cssclass,
171 std::string const & parlabel) {
172 string attrs = "class='" + cssclass + "'";
173 if (!parlabel.empty())
174 attrs += " id='" + parlabel + "'";
175 xs << xml::ParTag(lay.htmltag(), attrs);
178 void openParTag(XMLStream & xs, Layout const & lay,
179 ParagraphParameters const & params,
180 std::string const & parlabel)
182 // FIXME Are there other things we should handle here?
183 string const align = alignmentToCSS(params.align());
185 openParTag(xs, lay, parlabel);
188 string attrs = lay.htmlGetAttrString() + " style='text-align: " + align + ";'";
189 if (!parlabel.empty())
190 attrs += " id='" + parlabel + "'";
191 xs << xml::ParTag(lay.htmltag(), attrs);
195 inline void closeTag(XMLStream & xs, Layout const & lay)
197 xs << xml::EndTag(lay.htmltag());
201 inline void openLabelTag(XMLStream & xs, Layout const & lay)
203 xs << xml::StartTag(lay.htmllabeltag(), lay.htmllabelattr());
207 inline void closeLabelTag(XMLStream & xs, Layout const & lay)
209 xs << xml::EndTag(lay.htmllabeltag());
213 inline void openItemTag(XMLStream & xs, Layout const & lay)
215 if (lay.htmlitemtag() != "NONE") {
216 xs << xml::StartTag(lay.htmlitemtag(), lay.htmlitemattr(), true);
221 void openItemTag(XMLStream & xs, Layout const & lay,
222 ParagraphParameters const & params)
224 if (lay.htmlitemtag() != "NONE") {
225 // FIXME Are there other things we should handle here?
226 string const align = alignmentToCSS(params.align());
228 openItemTag(xs, lay);
231 string attrs = lay.htmlGetAttrString() + " style='text-align: " + align + ";'";
232 xs << xml::StartTag(lay.htmlitemtag(), attrs);
237 inline void closeItemTag(XMLStream & xs, Layout const & lay)
239 if (lay.htmlitemtag() != "NONE") {
240 xs << xml::EndTag(lay.htmlitemtag());
244 // end of convenience functions
246 ParagraphList::const_iterator findLastParagraph(
247 ParagraphList::const_iterator p,
248 ParagraphList::const_iterator const & pend)
250 for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
257 ParagraphList::const_iterator findEndOfEnvironment(
258 ParagraphList::const_iterator const & pstart,
259 ParagraphList::const_iterator const & pend)
261 ParagraphList::const_iterator p = pstart;
262 Layout const & bstyle = p->layout();
263 size_t const depth = p->params().depth();
264 for (++p; p != pend; ++p) {
265 Layout const & style = p->layout();
266 // It shouldn't happen that e.g. a section command occurs inside
267 // a quotation environment, at a higher depth, but as of 6/2009,
268 // it can happen. We pretend that it's just at lowest depth.
269 if (style.latextype == LATEX_COMMAND)
272 // If depth is down, we're done
273 if (p->params().depth() < depth)
276 // If depth is up, we're not done
277 if (p->params().depth() > depth)
280 // FIXME I am not sure about the first check.
281 // Surely we *could* have different layouts that count as
282 // LATEX_PARAGRAPH, right?
283 if (style.latextype == LATEX_PARAGRAPH || style != bstyle)
290 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
292 OutputParams const & runparams,
294 ParagraphList::const_iterator const & pbegin,
295 ParagraphList::const_iterator const & pend)
297 ParagraphList::const_iterator const begin = text.paragraphs().begin();
298 ParagraphList::const_iterator par = pbegin;
299 for (; par != pend; ++par) {
300 Layout const & lay = par->layout();
301 if (!lay.counter.empty())
302 buf.masterBuffer()->params().
303 documentClass().counters().step(lay.counter, OutputUpdate);
305 // FIXME We should see if there's a label to be output and
306 // do something with it.
310 // We want to open the paragraph tag if:
311 // (i) the current layout permits multiple paragraphs
312 // (ii) we are either not already inside a paragraph (HTMLIsBlock) OR
313 // we are, but this is not the first paragraph
315 // But there is also a special case, and we first see whether we are in it.
316 // We do not want to open the paragraph tag if this paragraph contains
317 // only one item, and that item is "inline", i.e., not HTMLIsBlock (such
318 // as a branch). On the other hand, if that single item has a font change
319 // applied to it, then we still do need to open the paragraph.
321 // Obviously, this is very fragile. The main reason we need to do this is
322 // because of branches, e.g., a branch that contains an entire new section.
323 // We do not really want to wrap that whole thing in a <div>...</div>.
324 bool special_case = false;
325 Inset const * specinset = par->size() == 1 ? par->getInset(0) : nullptr;
326 if (specinset && !specinset->getLayout().htmlisblock()) {
327 Layout const & style = par->layout();
328 FontInfo const first_font = style.labeltype == LABEL_MANUAL ?
329 style.labelfont : style.font;
330 FontInfo const our_font =
331 par->getFont(buf.masterBuffer()->params(), 0,
332 text.outerFont(distance(begin, par))).fontInfo();
333 if (first_font == our_font)
337 bool const open_par = runparams.html_make_pars
338 && (!runparams.html_in_par || par != pbegin)
341 // We want to issue the closing tag if either:
342 // (i) We opened it, and either html_in_par is false,
343 // or we're not in the last paragraph, anyway.
344 // (ii) We didn't open it and html_in_par is true,
345 // but we are in the first par, and there is a next par.
346 ParagraphList::const_iterator nextpar = par;
348 bool const close_par =
349 (open_par && (!runparams.html_in_par || nextpar != pend))
350 || (!open_par && runparams.html_in_par && par == pbegin && nextpar != pend);
353 // We do not issue the paragraph id if we are doing
354 // this for the TOC (or some similar purpose)
355 openParTag(xs, lay, par->params(),
356 runparams.for_toc ? "" : par->magicLabel());
359 docstring const deferred = par->simpleLyXHTMLOnePar(buf, xs,
360 runparams, text.outerFont(distance(begin, par)),
361 open_par, close_par);
368 if (!deferred.empty()) {
369 xs << XMLStream::ESCAPE_NONE << deferred << xml::CR();
376 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
378 OutputParams const & runparams,
380 ParagraphList::const_iterator const & pbegin,
381 ParagraphList::const_iterator const & pend)
384 // Use TextClass::htmlTOCLayout() to figure out how we should look.
385 xs << xml::StartTag("h2", "class='bibliography'")
386 << pbegin->layout().labelstring(false)
389 << xml::StartTag("div", "class='bibliography'")
391 makeParagraphs(buf, xs, runparams, text, pbegin, pend);
392 xs << xml::EndTag("div");
397 bool isNormalEnv(Layout const & lay)
399 return lay.latextype == LATEX_ENVIRONMENT
400 || lay.latextype == LATEX_BIB_ENVIRONMENT;
404 ParagraphList::const_iterator makeEnvironment(Buffer const & buf,
406 OutputParams const & runparams,
408 ParagraphList::const_iterator const & pbegin,
409 ParagraphList::const_iterator const & pend)
411 ParagraphList::const_iterator const begin = text.paragraphs().begin();
412 ParagraphList::const_iterator par = pbegin;
413 Layout const & bstyle = par->layout();
414 depth_type const origdepth = pbegin->params().depth();
416 // open tag for this environment
417 if ((bstyle.labeltype == LABEL_ENUMERATE || bstyle.labeltype == LABEL_ITEMIZE)
418 && bstyle.htmlclass().empty()) {
419 // In this case, we have to calculate the CSS class ourselves, each time
421 // FIXME We assume in these cases that the standard counters are being used.
422 // (We also do not deal with 'resume' counters, though I'm not sure that can
423 // be done at all in HTML.)
425 // Code adapated from Buffer::Impl::setLabel
426 bool const isenum = bstyle.labeltype == LABEL_ENUMERATE;
427 docstring enumcounter = bstyle.counter.empty() ?
428 ( isenum ? from_ascii("enum") : from_ascii("lyxitem") ) :
430 switch (par->itemdepth) {
444 // not a valid enumdepth...
447 openParTag(xs, bstyle,
448 string( isenum ? "lyxenum" : "lyxitem" ) + " "
449 + to_utf8(enumcounter), pbegin->magicLabel());
452 openParTag(xs, bstyle, pbegin->magicLabel());
455 // we will on occasion need to remember a layout from before.
456 Layout const * lastlay = nullptr;
458 while (par != pend) {
459 Layout const & style = par->layout();
460 // the counter only gets stepped if we're in some kind of list,
461 // or if it's the first time through.
462 // note that enum, etc, are handled automatically.
463 // FIXME There may be a bug here about user defined enumeration
464 // types. If so, then we'll need to take the counter and add "i",
465 // "ii", etc, as with enum.
466 Counters & cnts = buf.masterBuffer()->params().documentClass().counters();
467 docstring const & cntr = style.counter;
468 if (!style.counter.empty()
469 && (par == pbegin || !isNormalEnv(style))
470 && cnts.hasCounter(cntr)
472 cnts.step(cntr, OutputUpdate);
473 ParagraphList::const_iterator send;
475 switch (style.latextype) {
476 case LATEX_ENVIRONMENT:
477 case LATEX_LIST_ENVIRONMENT:
478 case LATEX_ITEM_ENVIRONMENT: {
479 // There are two possibilities in this case.
480 // One is that we are still in the environment in which we
481 // started---which we will be if the depth is the same.
482 if (par->params().depth() == origdepth) {
483 LATTEST(bstyle == style);
484 if (lastlay != nullptr) {
485 closeItemTag(xs, *lastlay);
489 // this will be positive, if we want to skip the
490 // initial word (if it's been taken for the label).
492 bool const labelfirst = style.htmllabelfirst();
494 openItemTag(xs, style, par->params());
497 if (style.labeltype != LABEL_NO_LABEL &&
498 style.htmllabeltag() != "NONE") {
499 if (isNormalEnv(style)) {
500 // in this case, we print the label only for the first
501 // paragraph (as in a theorem).
503 docstring const lbl =
504 pbegin->params().labelString();
506 openLabelTag(xs, style);
508 closeLabelTag(xs, style);
512 } else { // some kind of list
513 if (style.labeltype == LABEL_MANUAL) {
514 openLabelTag(xs, style);
515 sep = par->firstWordLyXHTML(xs, runparams);
516 closeLabelTag(xs, style);
520 docstring const & ls = par->params().labelString();
522 openLabelTag(xs, style);
524 closeLabelTag(xs, style);
529 } // end label output
532 openItemTag(xs, style, par->params());
534 docstring deferred = par->simpleLyXHTMLOnePar(buf, xs, runparams,
535 text.outerFont(distance(begin, par)), true, true, sep);
536 xs << XMLStream::ESCAPE_NONE << deferred;
539 // We may not want to close the tag yet, in particular:
540 // If we're not at the end...
542 // and are doing items...
543 && !isNormalEnv(style)
544 // and if the depth has changed...
545 && par->params().depth() != origdepth) {
546 // then we'll save this layout for later, and close it when
547 // we get another item.
550 closeItemTag(xs, style);
553 // The other possibility is that the depth has increased, in which
554 // case we need to recurse.
556 send = findEndOfEnvironment(par, pend);
557 par = makeEnvironment(buf, xs, runparams, text, par, send);
561 case LATEX_PARAGRAPH:
562 send = findLastParagraph(par, pend);
563 par = makeParagraphs(buf, xs, runparams, text, par, send);
566 case LATEX_BIB_ENVIRONMENT:
569 par = makeParagraphs(buf, xs, runparams, text, par, send);
578 if (lastlay != nullptr)
579 closeItemTag(xs, *lastlay);
580 closeTag(xs, bstyle);
586 void makeCommand(Buffer const & buf,
588 OutputParams const & runparams,
590 ParagraphList::const_iterator const & pbegin)
592 Layout const & style = pbegin->layout();
593 if (!style.counter.empty())
594 buf.masterBuffer()->params().
595 documentClass().counters().step(style.counter, OutputUpdate);
597 bool const make_parid = !runparams.for_toc && runparams.html_make_pars;
599 openParTag(xs, style, pbegin->params(),
600 make_parid ? pbegin->magicLabel() : "");
602 // Label around sectioning number:
603 // FIXME Probably need to account for LABEL_MANUAL
604 // FIXME Probably also need now to account for labels ABOVE and CENTERED.
605 if (style.labeltype != LABEL_NO_LABEL) {
606 openLabelTag(xs, style);
607 xs << pbegin->params().labelString();
608 closeLabelTag(xs, style);
609 // Otherwise the label might run together with the text
610 xs << from_ascii(" ");
613 ParagraphList::const_iterator const begin = text.paragraphs().begin();
614 pbegin->simpleLyXHTMLOnePar(buf, xs, runparams,
615 text.outerFont(distance(begin, pbegin)));
620 } // end anonymous namespace
623 void xhtmlParagraphs(Text const & text,
626 OutputParams const & runparams)
628 ParagraphList const & paragraphs = text.paragraphs();
629 if (runparams.par_begin == runparams.par_end) {
630 runparams.par_begin = 0;
631 runparams.par_end = paragraphs.size();
633 pit_type bpit = runparams.par_begin;
634 pit_type const epit = runparams.par_end;
636 { xs << XMLStream::ESCAPE_NONE << "<!-- XHTML output error! -->\n"; return; });
638 OutputParams ourparams = runparams;
639 ParagraphList::const_iterator const pend =
640 (epit == (int) paragraphs.size()) ?
641 paragraphs.end() : paragraphs.iterator_at(epit);
642 std::stack<int> headerLevels;
644 while (bpit < epit) {
645 ParagraphList::const_iterator par = paragraphs.iterator_at(bpit);
646 if (par->params().startOfAppendix()) {
647 // We want to reset the counter corresponding to toplevel sectioning
649 buf.masterBuffer()->params().documentClass().getTOCLayout();
650 docstring const cnt = lay.counter;
653 buf.masterBuffer()->params().documentClass().counters();
657 Layout const & style = par->layout();
658 ParagraphList::const_iterator const lastpar = par;
659 ParagraphList::const_iterator send;
661 // Think about adding <section> and/or </section>s.
662 // Document title is not in Sectioning, but rather in FrontMatter, so that it does not need to be taken
664 if (style.category() == from_utf8("Sectioning")) {
665 int level = style.toclevel;
667 // Need to close a previous section if it has the same level or a higher one (close <section> if opening a
668 // <h2> after a <h2>, <h3>, <h4>, <h5> or <h6>). More examples:
669 // - current: h2; back: h1; do not close any <section>
670 // - current: h1; back: h2; close two <section> (first the <h2>, then the <h1>, so a new <h1> can come)
671 while (!headerLevels.empty() && level <= headerLevels.top()) {
672 // Output the tag only if it corresponds to a legit section.
673 int stackLevel = headerLevels.top();
674 if (stackLevel != Layout::NOT_IN_TOC) {
675 xs << xml::EndTag("section");
681 // Open the new section: first push it onto the stack, then output it in XHTML.
682 headerLevels.push(level);
683 // Some sectioning-like elements should not be output (such as FrontMatter).
684 if (level != Layout::NOT_IN_TOC ) {
685 xs << xml::StartTag("section");
690 switch (style.latextype) {
691 case LATEX_COMMAND: {
692 // The files with which we are working never have more than
693 // one paragraph in a command structure.
695 // if (ourparams.html_in_par)
696 // fix it so we don't get sections inside standard, e.g.
697 // note that we may then need to make runparams not const, so we
698 // can communicate that back.
699 // FIXME Maybe this fix should be in the routines themselves, in case
700 // they are called from elsewhere.
701 makeCommand(buf, xs, ourparams, text, par);
705 case LATEX_ENVIRONMENT:
706 case LATEX_LIST_ENVIRONMENT:
707 case LATEX_ITEM_ENVIRONMENT: {
708 // FIXME Same fix here.
709 send = findEndOfEnvironment(par, pend);
710 par = makeEnvironment(buf, xs, ourparams, text, par, send);
713 case LATEX_BIB_ENVIRONMENT: {
714 // FIXME Same fix here.
715 send = findEndOfEnvironment(par, pend);
716 par = makeBibliography(buf, xs, ourparams, text, par, send);
719 case LATEX_PARAGRAPH:
720 send = findLastParagraph(par, pend);
721 par = makeParagraphs(buf, xs, ourparams, text, par, send);
724 bpit += distance(lastpar, par);
727 // If need be, close <section>s, but only at the end of the document (otherwise, dealt with at the beginning
729 while (!headerLevels.empty() && headerLevels.top() != Layout::NOT_IN_TOC) {
731 xs << xml::EndTag("section");
737 string alignmentToCSS(LyXAlignment align)
740 case LYX_ALIGN_BLOCK:
741 // we are NOT going to use text-align: justify!!
744 case LYX_ALIGN_RIGHT:
746 case LYX_ALIGN_CENTER: