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 "Paragraph.h"
23 #include "ParagraphList.h"
24 #include "ParagraphParameters.h"
27 #include "TextClass.h"
29 #include "support/lassert.h"
34 // Uncomment to activate debugging code.
35 // #define XHTML_DEBUG
38 using namespace lyx::support;
43 docstring fontToHtmlTag(xml::FontTypes type)
46 case xml::FontTypes::FT_EMPH:
47 return from_utf8("em");
48 case xml::FontTypes::FT_BOLD:
49 return from_utf8("b");
50 case xml::FontTypes::FT_NOUN:
51 return from_utf8("dfn");
52 case xml::FontTypes::FT_UBAR:
53 case xml::FontTypes::FT_WAVE:
54 case xml::FontTypes::FT_DBAR:
55 return from_utf8("u");
56 case xml::FontTypes::FT_SOUT:
57 case xml::FontTypes::FT_XOUT:
58 return from_utf8("del");
59 case xml::FontTypes::FT_ITALIC:
60 return from_utf8("i");
61 case xml::FontTypes::FT_UPRIGHT:
62 case xml::FontTypes::FT_SLANTED:
63 case xml::FontTypes::FT_SMALLCAPS:
64 case xml::FontTypes::FT_ROMAN:
65 case xml::FontTypes::FT_SANS:
66 case xml::FontTypes::FT_TYPE:
67 case xml::FontTypes::FT_SIZE_TINY:
68 case xml::FontTypes::FT_SIZE_SCRIPT:
69 case xml::FontTypes::FT_SIZE_FOOTNOTE:
70 case xml::FontTypes::FT_SIZE_SMALL:
71 case xml::FontTypes::FT_SIZE_NORMAL:
72 case xml::FontTypes::FT_SIZE_LARGE:
73 case xml::FontTypes::FT_SIZE_LARGER:
74 case xml::FontTypes::FT_SIZE_LARGEST:
75 case xml::FontTypes::FT_SIZE_HUGE:
76 case xml::FontTypes::FT_SIZE_HUGER:
77 case xml::FontTypes::FT_SIZE_INCREASE:
78 case xml::FontTypes::FT_SIZE_DECREASE:
79 return from_utf8("span");
86 docstring fontToHtmlAttribute(xml::FontTypes type)
89 case xml::FontTypes::FT_EMPH:
90 case xml::FontTypes::FT_BOLD:
91 return from_ascii("");
92 case xml::FontTypes::FT_NOUN:
93 return from_ascii("class='lyxnoun'");
94 case xml::FontTypes::FT_UBAR:
95 return from_ascii("");
96 case xml::FontTypes::FT_DBAR:
97 return from_ascii("class='dline'");
98 case xml::FontTypes::FT_XOUT:
99 case xml::FontTypes::FT_SOUT:
100 return from_ascii("class='strikeout'");
101 case xml::FontTypes::FT_WAVE:
102 return from_ascii("class='wline'");
103 case xml::FontTypes::FT_ITALIC:
104 return from_ascii("");
105 case xml::FontTypes::FT_UPRIGHT:
106 return from_ascii("style='font-style:normal;'");
107 case xml::FontTypes::FT_SLANTED:
108 return from_ascii("style='font-style:oblique;'");
109 case xml::FontTypes::FT_SMALLCAPS:
110 return from_ascii("style='font-variant:small-caps;'");
111 case xml::FontTypes::FT_ROMAN:
112 return from_ascii("style='font-family:serif;'");
113 case xml::FontTypes::FT_SANS:
114 return from_ascii("style='font-family:sans-serif;'");
115 case xml::FontTypes::FT_TYPE:
116 return from_ascii("style='font-family:monospace;'");
117 case xml::FontTypes::FT_SIZE_TINY:
118 case xml::FontTypes::FT_SIZE_SCRIPT:
119 case xml::FontTypes::FT_SIZE_FOOTNOTE:
120 return from_ascii("style='font-size:x-small;'");
121 case xml::FontTypes::FT_SIZE_SMALL:
122 return from_ascii("style='font-size:small;'");
123 case xml::FontTypes::FT_SIZE_NORMAL:
124 return from_ascii("style='font-size:normal;'");
125 case xml::FontTypes::FT_SIZE_LARGE:
126 return from_ascii("style='font-size:large;'");
127 case xml::FontTypes::FT_SIZE_LARGER:
128 case xml::FontTypes::FT_SIZE_LARGEST:
129 return from_ascii("style='font-size:x-large;'");
130 case xml::FontTypes::FT_SIZE_HUGE:
131 case xml::FontTypes::FT_SIZE_HUGER:
132 return from_ascii("style='font-size:xx-large;'");
133 case xml::FontTypes::FT_SIZE_INCREASE:
134 return from_ascii("style='font-size:larger;'");
135 case xml::FontTypes::FT_SIZE_DECREASE:
136 return from_ascii("style='font-size:smaller;'");
139 return from_ascii("");
143 xml::FontTag xhtmlStartFontTag(xml::FontTypes type)
145 return xml::FontTag(fontToHtmlTag(type), fontToHtmlAttribute(type), type);
149 xml::EndFontTag xhtmlEndFontTag(xml::FontTypes type)
151 return xml::EndFontTag(fontToHtmlTag(type), type);
156 // convenience functions
158 inline void openParTag(XMLStream & xs, Layout const & lay,
159 const std::string & parlabel)
161 string attrs = lay.htmlattr();
162 if (!parlabel.empty())
163 attrs += " id='" + parlabel + "'";
164 xs << xml::ParTag(lay.htmltag(), attrs);
168 void openParTag(XMLStream & xs, Layout const & lay,
169 ParagraphParameters const & params,
170 const std::string & parlabel)
172 // FIXME Are there other things we should handle here?
173 string const align = alignmentToCSS(params.align());
175 openParTag(xs, lay, parlabel);
178 string attrs = lay.htmlattr() + " style='text-align: " + align + ";'";
179 if (!parlabel.empty())
180 attrs += " id='" + parlabel + "'";
181 xs << xml::ParTag(lay.htmltag(), attrs);
185 inline void closeTag(XMLStream & xs, Layout const & lay)
187 xs << xml::EndTag(lay.htmltag());
191 inline void openLabelTag(XMLStream & xs, Layout const & lay)
193 xs << xml::StartTag(lay.htmllabeltag(), lay.htmllabelattr());
197 inline void closeLabelTag(XMLStream & xs, Layout const & lay)
199 xs << xml::EndTag(lay.htmllabeltag());
203 inline void openItemTag(XMLStream & xs, Layout const & lay)
205 xs << xml::StartTag(lay.htmlitemtag(), lay.htmlitemattr(), true);
209 void openItemTag(XMLStream & xs, Layout const & lay,
210 ParagraphParameters const & params)
212 // FIXME Are there other things we should handle here?
213 string const align = alignmentToCSS(params.align());
215 openItemTag(xs, lay);
218 string attrs = lay.htmlattr() + " style='text-align: " + align + ";'";
219 xs << xml::StartTag(lay.htmlitemtag(), attrs);
223 inline void closeItemTag(XMLStream & xs, Layout const & lay)
225 xs << xml::EndTag(lay.htmlitemtag());
228 // end of convenience functions
230 ParagraphList::const_iterator findLastParagraph(
231 ParagraphList::const_iterator p,
232 ParagraphList::const_iterator const & pend)
234 for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
241 ParagraphList::const_iterator findEndOfEnvironment(
242 ParagraphList::const_iterator const & pstart,
243 ParagraphList::const_iterator const & pend)
245 ParagraphList::const_iterator p = pstart;
246 Layout const & bstyle = p->layout();
247 size_t const depth = p->params().depth();
248 for (++p; p != pend; ++p) {
249 Layout const & style = p->layout();
250 // It shouldn't happen that e.g. a section command occurs inside
251 // a quotation environment, at a higher depth, but as of 6/2009,
252 // it can happen. We pretend that it's just at lowest depth.
253 if (style.latextype == LATEX_COMMAND)
256 // If depth is down, we're done
257 if (p->params().depth() < depth)
260 // If depth is up, we're not done
261 if (p->params().depth() > depth)
264 // FIXME I am not sure about the first check.
265 // Surely we *could* have different layouts that count as
266 // LATEX_PARAGRAPH, right?
267 if (style.latextype == LATEX_PARAGRAPH || style != bstyle)
274 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
276 OutputParams const & runparams,
278 ParagraphList::const_iterator const & pbegin,
279 ParagraphList::const_iterator const & pend)
281 ParagraphList::const_iterator const begin = text.paragraphs().begin();
282 ParagraphList::const_iterator par = pbegin;
283 for (; par != pend; ++par) {
284 Layout const & lay = par->layout();
285 if (!lay.counter.empty())
286 buf.masterBuffer()->params().
287 documentClass().counters().step(lay.counter, OutputUpdate);
289 // FIXME We should see if there's a label to be output and
290 // do something with it.
294 // We want to open the paragraph tag if:
295 // (i) the current layout permits multiple paragraphs
296 // (ii) we are either not already inside a paragraph (HTMLIsBlock) OR
297 // we are, but this is not the first paragraph
299 // But there is also a special case, and we first see whether we are in it.
300 // We do not want to open the paragraph tag if this paragraph contains
301 // only one item, and that item is "inline", i.e., not HTMLIsBlock (such
302 // as a branch). On the other hand, if that single item has a font change
303 // applied to it, then we still do need to open the paragraph.
305 // Obviously, this is very fragile. The main reason we need to do this is
306 // because of branches, e.g., a branch that contains an entire new section.
307 // We do not really want to wrap that whole thing in a <div>...</div>.
308 bool special_case = false;
309 Inset const * specinset = par->size() == 1 ? par->getInset(0) : nullptr;
310 if (specinset && !specinset->getLayout().htmlisblock()) {
311 Layout const & style = par->layout();
312 FontInfo const first_font = style.labeltype == LABEL_MANUAL ?
313 style.labelfont : style.font;
314 FontInfo const our_font =
315 par->getFont(buf.masterBuffer()->params(), 0,
316 text.outerFont(distance(begin, par))).fontInfo();
317 if (first_font == our_font)
321 bool const open_par = runparams.html_make_pars
322 && (!runparams.html_in_par || par != pbegin)
325 // We want to issue the closing tag if either:
326 // (i) We opened it, and either html_in_par is false,
327 // or we're not in the last paragraph, anyway.
328 // (ii) We didn't open it and html_in_par is true,
329 // but we are in the first par, and there is a next par.
330 ParagraphList::const_iterator nextpar = par;
332 bool const close_par =
333 (open_par && (!runparams.html_in_par || nextpar != pend))
334 || (!open_par && runparams.html_in_par && par == pbegin && nextpar != pend);
337 // We do not issue the paragraph id if we are doing
338 // this for the TOC (or some similar purpose)
339 openParTag(xs, lay, par->params(),
340 runparams.for_toc ? "" : par->magicLabel());
343 docstring const deferred = par->simpleLyXHTMLOnePar(buf, xs,
344 runparams, text.outerFont(distance(begin, par)),
345 open_par, close_par);
352 if (!deferred.empty()) {
353 xs << XMLStream::ESCAPE_NONE << deferred << xml::CR();
360 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
362 OutputParams const & runparams,
364 ParagraphList::const_iterator const & pbegin,
365 ParagraphList::const_iterator const & pend)
368 // Use TextClass::htmlTOCLayout() to figure out how we should look.
369 xs << xml::StartTag("h2", "class='bibliography'")
370 << pbegin->layout().labelstring(false)
373 << xml::StartTag("div", "class='bibliography'")
375 makeParagraphs(buf, xs, runparams, text, pbegin, pend);
376 xs << xml::EndTag("div");
381 bool isNormalEnv(Layout const & lay)
383 return lay.latextype == LATEX_ENVIRONMENT
384 || lay.latextype == LATEX_BIB_ENVIRONMENT;
388 ParagraphList::const_iterator makeEnvironment(Buffer const & buf,
390 OutputParams const & runparams,
392 ParagraphList::const_iterator const & pbegin,
393 ParagraphList::const_iterator const & pend)
395 ParagraphList::const_iterator const begin = text.paragraphs().begin();
396 ParagraphList::const_iterator par = pbegin;
397 Layout const & bstyle = par->layout();
398 depth_type const origdepth = pbegin->params().depth();
400 // open tag for this environment
401 openParTag(xs, bstyle, pbegin->magicLabel());
404 // we will on occasion need to remember a layout from before.
405 Layout const * lastlay = nullptr;
407 while (par != pend) {
408 Layout const & style = par->layout();
409 // the counter only gets stepped if we're in some kind of list,
410 // or if it's the first time through.
411 // note that enum, etc, are handled automatically.
412 // FIXME There may be a bug here about user defined enumeration
413 // types. If so, then we'll need to take the counter and add "i",
414 // "ii", etc, as with enum.
415 Counters & cnts = buf.masterBuffer()->params().documentClass().counters();
416 docstring const & cntr = style.counter;
417 if (!style.counter.empty()
418 && (par == pbegin || !isNormalEnv(style))
419 && cnts.hasCounter(cntr)
421 cnts.step(cntr, OutputUpdate);
422 ParagraphList::const_iterator send;
424 switch (style.latextype) {
425 case LATEX_ENVIRONMENT:
426 case LATEX_LIST_ENVIRONMENT:
427 case LATEX_ITEM_ENVIRONMENT: {
428 // There are two possibilities in this case.
429 // One is that we are still in the environment in which we
430 // started---which we will be if the depth is the same.
431 if (par->params().depth() == origdepth) {
432 LATTEST(bstyle == style);
433 if (lastlay != nullptr) {
434 closeItemTag(xs, *lastlay);
438 // this will be positive, if we want to skip the
439 // initial word (if it's been taken for the label).
441 bool const labelfirst = style.htmllabelfirst();
443 openItemTag(xs, style, par->params());
446 if (style.labeltype != LABEL_NO_LABEL &&
447 style.htmllabeltag() != "NONE") {
448 if (isNormalEnv(style)) {
449 // in this case, we print the label only for the first
450 // paragraph (as in a theorem).
452 docstring const lbl =
453 pbegin->params().labelString();
455 openLabelTag(xs, style);
457 closeLabelTag(xs, style);
461 } else { // some kind of list
462 if (style.labeltype == LABEL_MANUAL) {
463 openLabelTag(xs, style);
464 sep = par->firstWordLyXHTML(xs, runparams);
465 closeLabelTag(xs, style);
469 openLabelTag(xs, style);
470 xs << par->params().labelString();
471 closeLabelTag(xs, style);
475 } // end label output
478 openItemTag(xs, style, par->params());
480 docstring deferred = par->simpleLyXHTMLOnePar(buf, xs, runparams,
481 text.outerFont(distance(begin, par)), true, true, sep);
482 xs << XMLStream::ESCAPE_NONE << deferred;
485 // We may not want to close the tag yet, in particular:
486 // If we're not at the end...
488 // and are doing items...
489 && !isNormalEnv(style)
490 // and if the depth has changed...
491 && par->params().depth() != origdepth) {
492 // then we'll save this layout for later, and close it when
493 // we get another item.
496 closeItemTag(xs, style);
499 // The other possibility is that the depth has increased, in which
500 // case we need to recurse.
502 send = findEndOfEnvironment(par, pend);
503 par = makeEnvironment(buf, xs, runparams, text, par, send);
507 case LATEX_PARAGRAPH:
508 send = findLastParagraph(par, pend);
509 par = makeParagraphs(buf, xs, runparams, text, par, send);
512 case LATEX_BIB_ENVIRONMENT:
515 par = makeParagraphs(buf, xs, runparams, text, par, send);
524 if (lastlay != nullptr)
525 closeItemTag(xs, *lastlay);
526 closeTag(xs, bstyle);
532 void makeCommand(Buffer const & buf,
534 OutputParams const & runparams,
536 ParagraphList::const_iterator const & pbegin)
538 Layout const & style = pbegin->layout();
539 if (!style.counter.empty())
540 buf.masterBuffer()->params().
541 documentClass().counters().step(style.counter, OutputUpdate);
543 bool const make_parid = !runparams.for_toc && runparams.html_make_pars;
545 openParTag(xs, style, pbegin->params(),
546 make_parid ? pbegin->magicLabel() : "");
548 // Label around sectioning number:
549 // FIXME Probably need to account for LABEL_MANUAL
550 // FIXME Probably also need now to account for labels ABOVE and CENTERED.
551 if (style.labeltype != LABEL_NO_LABEL) {
552 openLabelTag(xs, style);
553 xs << pbegin->params().labelString();
554 closeLabelTag(xs, style);
555 // Otherwise the label might run together with the text
556 xs << from_ascii(" ");
559 ParagraphList::const_iterator const begin = text.paragraphs().begin();
560 pbegin->simpleLyXHTMLOnePar(buf, xs, runparams,
561 text.outerFont(distance(begin, pbegin)));
566 } // end anonymous namespace
569 void xhtmlParagraphs(Text const & text,
572 OutputParams const & runparams)
574 ParagraphList const & paragraphs = text.paragraphs();
575 if (runparams.par_begin == runparams.par_end) {
576 runparams.par_begin = 0;
577 runparams.par_end = paragraphs.size();
579 pit_type bpit = runparams.par_begin;
580 pit_type const epit = runparams.par_end;
582 { xs << XMLStream::ESCAPE_NONE << "<!-- XHTML output error! -->\n"; return; });
584 OutputParams ourparams = runparams;
585 ParagraphList::const_iterator const pend =
586 (epit == (int) paragraphs.size()) ?
587 paragraphs.end() : paragraphs.iterator_at(epit);
588 std::stack<int> headerLevels;
590 while (bpit < epit) {
591 ParagraphList::const_iterator par = paragraphs.iterator_at(bpit);
592 if (par->params().startOfAppendix()) {
593 // We want to reset the counter corresponding to toplevel sectioning
595 buf.masterBuffer()->params().documentClass().getTOCLayout();
596 docstring const cnt = lay.counter;
599 buf.masterBuffer()->params().documentClass().counters();
603 Layout const & style = par->layout();
604 ParagraphList::const_iterator const lastpar = par;
605 ParagraphList::const_iterator send;
607 // Think about adding <section> and/or </section>s.
608 // Document title is not in Sectioning, but rather in FrontMatter, so that it does not need to be taken
610 if (style.category() == from_utf8("Sectioning")) {
611 int level = style.toclevel;
613 // Need to close a previous section if it has the same level or a higher one (close <section> if opening a
614 // <h2> after a <h2>, <h3>, <h4>, <h5> or <h6>). More examples:
615 // - current: h2; back: h1; do not close any <section>
616 // - current: h1; back: h2; close two <section> (first the <h2>, then the <h1>, so a new <h1> can come)
617 while (!headerLevels.empty() && level <= headerLevels.top()) {
618 // Output the tag only if it corresponds to a legit section.
619 int stackLevel = headerLevels.top();
620 if (stackLevel != Layout::NOT_IN_TOC) {
621 xs << xml::EndTag("section");
627 // Open the new section: first push it onto the stack, then output it in XHTML.
628 headerLevels.push(level);
629 // Some sectioning-like elements should not be output (such as FrontMatter).
630 if (level != Layout::NOT_IN_TOC ) {
631 xs << xml::StartTag("section");
636 switch (style.latextype) {
637 case LATEX_COMMAND: {
638 // The files with which we are working never have more than
639 // one paragraph in a command structure.
641 // if (ourparams.html_in_par)
642 // fix it so we don't get sections inside standard, e.g.
643 // note that we may then need to make runparams not const, so we
644 // can communicate that back.
645 // FIXME Maybe this fix should be in the routines themselves, in case
646 // they are called from elsewhere.
647 makeCommand(buf, xs, ourparams, text, par);
651 case LATEX_ENVIRONMENT:
652 case LATEX_LIST_ENVIRONMENT:
653 case LATEX_ITEM_ENVIRONMENT: {
654 // FIXME Same fix here.
655 send = findEndOfEnvironment(par, pend);
656 par = makeEnvironment(buf, xs, ourparams, text, par, send);
659 case LATEX_BIB_ENVIRONMENT: {
660 // FIXME Same fix here.
661 send = findEndOfEnvironment(par, pend);
662 par = makeBibliography(buf, xs, ourparams, text, par, send);
665 case LATEX_PARAGRAPH:
666 send = findLastParagraph(par, pend);
667 par = makeParagraphs(buf, xs, ourparams, text, par, send);
670 bpit += distance(lastpar, par);
673 // If need be, close <section>s, but only at the end of the document (otherwise, dealt with at the beginning
675 while (!headerLevels.empty() && headerLevels.top() != Layout::NOT_IN_TOC) {
677 xs << xml::EndTag("section");
683 string alignmentToCSS(LyXAlignment align)
686 case LYX_ALIGN_BLOCK:
687 // we are NOT going to use text-align: justify!!
690 case LYX_ALIGN_RIGHT:
692 case LYX_ALIGN_CENTER: