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.
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 "buffer_funcs.h"
19 #include "BufferParams.h"
23 #include "OutputParams.h"
24 #include "Paragraph.h"
25 #include "ParagraphList.h"
26 #include "ParagraphParameters.h"
29 #include "TextClass.h"
31 #include "support/convert.h"
32 #include "support/debug.h"
33 #include "support/lassert.h"
34 #include "support/lstrings.h"
35 #include "support/textutils.h"
39 // Uncomment to activate debugging code.
40 // #define XHTML_DEBUG
43 using namespace lyx::support;
48 docstring fontToHtmlTag(xml::FontTypes type)
51 case xml::FontTypes::FT_EMPH:
52 return from_utf8("em");
53 case xml::FontTypes::FT_BOLD:
54 return from_utf8("b");
55 case xml::FontTypes::FT_NOUN:
56 return from_utf8("dfn");
57 case xml::FontTypes::FT_UBAR:
58 case xml::FontTypes::FT_WAVE:
59 case xml::FontTypes::FT_DBAR:
60 return from_utf8("u");
61 case xml::FontTypes::FT_SOUT:
62 case xml::FontTypes::FT_XOUT:
63 return from_utf8("del");
64 case xml::FontTypes::FT_ITALIC:
65 return from_utf8("i");
66 case xml::FontTypes::FT_UPRIGHT:
67 case xml::FontTypes::FT_SLANTED:
68 case xml::FontTypes::FT_SMALLCAPS:
69 case xml::FontTypes::FT_ROMAN:
70 case xml::FontTypes::FT_SANS:
71 case xml::FontTypes::FT_TYPE:
72 case xml::FontTypes::FT_SIZE_TINY:
73 case xml::FontTypes::FT_SIZE_SCRIPT:
74 case xml::FontTypes::FT_SIZE_FOOTNOTE:
75 case xml::FontTypes::FT_SIZE_SMALL:
76 case xml::FontTypes::FT_SIZE_NORMAL:
77 case xml::FontTypes::FT_SIZE_LARGE:
78 case xml::FontTypes::FT_SIZE_LARGER:
79 case xml::FontTypes::FT_SIZE_LARGEST:
80 case xml::FontTypes::FT_SIZE_HUGE:
81 case xml::FontTypes::FT_SIZE_HUGER:
82 case xml::FontTypes::FT_SIZE_INCREASE:
83 case xml::FontTypes::FT_SIZE_DECREASE:
84 return from_utf8("span");
91 docstring fontToHtmlAttribute(xml::FontTypes type)
94 case xml::FontTypes::FT_EMPH:
95 case xml::FontTypes::FT_BOLD:
96 return from_ascii("");
97 case xml::FontTypes::FT_NOUN:
98 return from_ascii("class='lyxnoun'");
99 case xml::FontTypes::FT_UBAR:
100 return from_ascii("");
101 case xml::FontTypes::FT_DBAR:
102 return from_ascii("class='dline'");
103 case xml::FontTypes::FT_XOUT:
104 case xml::FontTypes::FT_SOUT:
105 return from_ascii("class='strikeout'");
106 case xml::FontTypes::FT_WAVE:
107 return from_ascii("class='wline'");
108 case xml::FontTypes::FT_ITALIC:
109 return from_ascii("");
110 case xml::FontTypes::FT_UPRIGHT:
111 return from_ascii("style='font-style:normal;'");
112 case xml::FontTypes::FT_SLANTED:
113 return from_ascii("style='font-style:oblique;'");
114 case xml::FontTypes::FT_SMALLCAPS:
115 return from_ascii("style='font-variant:small-caps;'");
116 case xml::FontTypes::FT_ROMAN:
117 return from_ascii("style='font-family:serif;'");
118 case xml::FontTypes::FT_SANS:
119 return from_ascii("style='font-family:sans-serif;'");
120 case xml::FontTypes::FT_TYPE:
121 return from_ascii("style='font-family:monospace;'");
122 case xml::FontTypes::FT_SIZE_TINY:
123 case xml::FontTypes::FT_SIZE_SCRIPT:
124 case xml::FontTypes::FT_SIZE_FOOTNOTE:
125 return from_ascii("style='font-size:x-small;'");
126 case xml::FontTypes::FT_SIZE_SMALL:
127 return from_ascii("style='font-size:small;'");
128 case xml::FontTypes::FT_SIZE_NORMAL:
129 return from_ascii("style='font-size:normal;'");
130 case xml::FontTypes::FT_SIZE_LARGE:
131 return from_ascii("style='font-size:large;'");
132 case xml::FontTypes::FT_SIZE_LARGER:
133 case xml::FontTypes::FT_SIZE_LARGEST:
134 return from_ascii("style='font-size:x-large;'");
135 case xml::FontTypes::FT_SIZE_HUGE:
136 case xml::FontTypes::FT_SIZE_HUGER:
137 return from_ascii("style='font-size:xx-large;'");
138 case xml::FontTypes::FT_SIZE_INCREASE:
139 return from_ascii("style='font-size:larger;'");
140 case xml::FontTypes::FT_SIZE_DECREASE:
141 return from_ascii("style='font-size:smaller;'");
144 return from_ascii("");
148 xml::FontTag xhtmlStartFontTag(xml::FontTypes type)
150 return xml::FontTag(fontToHtmlTag(type), fontToHtmlAttribute(type), type);
154 xml::EndFontTag xhtmlEndFontTag(xml::FontTypes type)
156 return xml::EndFontTag(fontToHtmlTag(type), type);
161 // convenience functions
163 inline void openParTag(XMLStream & xs, Layout const & lay,
164 std::string parlabel)
166 xs << xml::ParTag(lay.htmltag(), lay.htmlattr(), parlabel);
170 void openParTag(XMLStream & xs, Layout const & lay,
171 ParagraphParameters const & params,
172 std::string parlabel)
174 // FIXME Are there other things we should handle here?
175 string const align = alignmentToCSS(params.align());
177 openParTag(xs, lay, parlabel);
180 string attrs = lay.htmlattr() + " style='text-align: " + align + ";'";
181 xs << xml::ParTag(lay.htmltag(), attrs, parlabel);
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 while (bpit < epit) {
589 ParagraphList::const_iterator par = paragraphs.iterator_at(bpit);
590 if (par->params().startOfAppendix()) {
591 // We want to reset the counter corresponding to toplevel sectioning
593 buf.masterBuffer()->params().documentClass().getTOCLayout();
594 docstring const cnt = lay.counter;
597 buf.masterBuffer()->params().documentClass().counters();
601 Layout const & style = par->layout();
602 ParagraphList::const_iterator const lastpar = par;
603 ParagraphList::const_iterator send;
605 switch (style.latextype) {
606 case LATEX_COMMAND: {
607 // The files with which we are working never have more than
608 // one paragraph in a command structure.
610 // if (ourparams.html_in_par)
611 // fix it so we don't get sections inside standard, e.g.
612 // note that we may then need to make runparams not const, so we
613 // can communicate that back.
614 // FIXME Maybe this fix should be in the routines themselves, in case
615 // they are called from elsewhere.
616 makeCommand(buf, xs, ourparams, text, par);
620 case LATEX_ENVIRONMENT:
621 case LATEX_LIST_ENVIRONMENT:
622 case LATEX_ITEM_ENVIRONMENT: {
623 // FIXME Same fix here.
624 send = findEndOfEnvironment(par, pend);
625 par = makeEnvironment(buf, xs, ourparams, text, par, send);
628 case LATEX_BIB_ENVIRONMENT: {
629 // FIXME Same fix here.
630 send = findEndOfEnvironment(par, pend);
631 par = makeBibliography(buf, xs, ourparams, text, par, send);
634 case LATEX_PARAGRAPH:
635 send = findLastParagraph(par, pend);
636 par = makeParagraphs(buf, xs, ourparams, text, par, send);
639 bpit += distance(lastpar, par);
644 string alignmentToCSS(LyXAlignment align)
647 case LYX_ALIGN_BLOCK:
648 // we are NOT going to use text-align: justify!!
651 case LYX_ALIGN_RIGHT:
653 case LYX_ALIGN_CENTER: