2 * \file output_docbook.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
14 #include "output_docbook.h"
17 #include "buffer_funcs.h"
18 #include "BufferParams.h"
20 #include "InsetList.h"
21 #include "Paragraph.h"
22 #include "ParagraphList.h"
23 #include "ParagraphParameters.h"
26 #include "TextClass.h"
28 #include "insets/InsetBibtex.h"
29 #include "insets/InsetBibitem.h"
30 #include "insets/InsetLabel.h"
31 #include "mathed/InsetMath.h"
32 #include "insets/InsetNote.h"
34 #include "support/lassert.h"
35 #include "support/textutils.h"
43 using namespace lyx::support;
49 std::string fontToDocBookTag(xml::FontTypes type)
52 case xml::FontTypes::FT_EMPH:
53 case xml::FontTypes::FT_BOLD:
55 case xml::FontTypes::FT_NOUN:
57 case xml::FontTypes::FT_UBAR:
58 case xml::FontTypes::FT_WAVE:
59 case xml::FontTypes::FT_DBAR:
60 case xml::FontTypes::FT_SOUT:
61 case xml::FontTypes::FT_XOUT:
62 case xml::FontTypes::FT_ITALIC:
63 case xml::FontTypes::FT_UPRIGHT:
64 case xml::FontTypes::FT_SLANTED:
65 case xml::FontTypes::FT_SMALLCAPS:
66 case xml::FontTypes::FT_ROMAN:
67 case xml::FontTypes::FT_SANS:
69 case xml::FontTypes::FT_TYPE:
71 case xml::FontTypes::FT_SIZE_TINY:
72 case xml::FontTypes::FT_SIZE_SCRIPT:
73 case xml::FontTypes::FT_SIZE_FOOTNOTE:
74 case xml::FontTypes::FT_SIZE_SMALL:
75 case xml::FontTypes::FT_SIZE_NORMAL:
76 case xml::FontTypes::FT_SIZE_LARGE:
77 case xml::FontTypes::FT_SIZE_LARGER:
78 case xml::FontTypes::FT_SIZE_LARGEST:
79 case xml::FontTypes::FT_SIZE_HUGE:
80 case xml::FontTypes::FT_SIZE_HUGER:
81 case xml::FontTypes::FT_SIZE_INCREASE:
82 case xml::FontTypes::FT_SIZE_DECREASE:
90 string fontToRole(xml::FontTypes type)
92 // Specific fonts are achieved with roles. The only common ones are "" for basic emphasis,
93 // and "bold"/"strong" for bold. With some specific options, other roles are copied into
94 // HTML output (via the DocBook XSLT sheets); otherwise, if not recognised, they are just ignored.
95 // Hence, it is not a problem to have many roles by default here.
96 // See https://www.sourceware.org/ml/docbook/2003-05/msg00269.html
98 case xml::FontTypes::FT_ITALIC:
99 case xml::FontTypes::FT_EMPH:
101 case xml::FontTypes::FT_BOLD:
103 case xml::FontTypes::FT_NOUN: // Outputs a <person>
104 case xml::FontTypes::FT_TYPE: // Outputs a <code>
106 case xml::FontTypes::FT_UBAR:
109 // All other roles are non-standard for DocBook.
111 case xml::FontTypes::FT_WAVE:
113 case xml::FontTypes::FT_DBAR:
115 case xml::FontTypes::FT_SOUT:
117 case xml::FontTypes::FT_XOUT:
119 case xml::FontTypes::FT_UPRIGHT:
121 case xml::FontTypes::FT_SLANTED:
123 case xml::FontTypes::FT_SMALLCAPS:
125 case xml::FontTypes::FT_ROMAN:
127 case xml::FontTypes::FT_SANS:
129 case xml::FontTypes::FT_SIZE_TINY:
131 case xml::FontTypes::FT_SIZE_SCRIPT:
132 return "size_script";
133 case xml::FontTypes::FT_SIZE_FOOTNOTE:
134 return "size_footnote";
135 case xml::FontTypes::FT_SIZE_SMALL:
137 case xml::FontTypes::FT_SIZE_NORMAL:
138 return "size_normal";
139 case xml::FontTypes::FT_SIZE_LARGE:
141 case xml::FontTypes::FT_SIZE_LARGER:
142 return "size_larger";
143 case xml::FontTypes::FT_SIZE_LARGEST:
144 return "size_largest";
145 case xml::FontTypes::FT_SIZE_HUGE:
147 case xml::FontTypes::FT_SIZE_HUGER:
149 case xml::FontTypes::FT_SIZE_INCREASE:
150 return "size_increase";
151 case xml::FontTypes::FT_SIZE_DECREASE:
152 return "size_decrease";
159 string fontToAttribute(xml::FontTypes type) {
160 // If there is a role (i.e. nonstandard use of a tag), output the attribute. Otherwise, the sheer tag is sufficient
162 string role = fontToRole(type);
164 return "role='" + role + "'";
171 // Convenience functions to open and close tags. First, very low-level ones to ensure a consistent new-line behaviour.
175 // Contents of the block.
180 // <paratag>Contents of the paragraph.</paratag>
183 // Content before<inlinetag>Contents of the paragraph.</inlinetag>Content after
185 void openInlineTag(XMLStream & xs, const std::string & tag, const std::string & attr)
187 xs << xml::StartTag(tag, attr);
191 void closeInlineTag(XMLStream & xs, const std::string & tag)
193 xs << xml::EndTag(tag);
197 void openParTag(XMLStream & xs, const std::string & tag, const std::string & attr)
199 if (!xs.isLastTagCR())
201 xs << xml::StartTag(tag, attr);
205 void closeParTag(XMLStream & xs, const std::string & tag)
207 xs << xml::EndTag(tag);
212 void openBlockTag(XMLStream & xs, const std::string & tag, const std::string & attr)
214 if (!xs.isLastTagCR())
216 xs << xml::StartTag(tag, attr);
221 void closeBlockTag(XMLStream & xs, const std::string & tag)
223 if (!xs.isLastTagCR())
225 xs << xml::EndTag(tag);
230 void openTag(XMLStream & xs, const std::string & tag, const std::string & attr, const std::string & tagtype)
232 if (tag.empty() || tag == "NONE") // Common check to be performed elsewhere, if it was not here.
235 if (tag == "para" || tagtype == "paragraph") // Special case for <para>: always considered as a paragraph.
236 openParTag(xs, tag, attr);
237 else if (tagtype == "block")
238 openBlockTag(xs, tag, attr);
239 else if (tagtype == "inline")
240 openInlineTag(xs, tag, attr);
242 xs.writeError("Unrecognised tag type '" + tagtype + "' for '" + tag + " " + attr + "'");
246 void closeTag(XMLStream & xs, const std::string & tag, const std::string & tagtype)
248 if (tag.empty() || tag == "NONE")
251 if (tag == "para" || tagtype == "paragraph") // Special case for <para>: always considered as a paragraph.
252 closeParTag(xs, tag);
253 else if (tagtype == "block")
254 closeBlockTag(xs, tag);
255 else if (tagtype == "inline")
256 closeInlineTag(xs, tag);
258 xs.writeError("Unrecognised tag type '" + tagtype + "' for '" + tag + "'");
262 void compTag(XMLStream & xs, const std::string & tag, const std::string & attr, const std::string & tagtype)
264 if (tag.empty() || tag == "NONE")
267 // Special case for <para>: always considered as a paragraph.
268 if (tag == "para" || tagtype == "paragraph" || tagtype == "block") {
269 if (!xs.isLastTagCR())
271 xs << xml::CompTag(tag, attr);
273 } else if (tagtype == "inline") {
274 xs << xml::CompTag(tag, attr);
276 xs.writeError("Unrecognised tag type '" + tagtype + "' for '" + tag + "'");
281 // Higher-level convenience functions.
283 void openParTag(XMLStream & xs, const Paragraph * par, const Paragraph * prevpar)
288 // When should the wrapper be opened here? Only if the previous paragraph has the SAME wrapper tag
289 // (usually, they won't have the same layout) and the CURRENT one allows merging.
290 // The main use case is author information in several paragraphs: if the name of the author is the
291 // first paragraph of an author, then merging with the previous tag does not make sense. Say the
292 // next paragraph is the affiliation, then it should be output in the same <author> tag (different
293 // layout, same wrapper tag).
294 Layout const & lay = par->layout();
295 bool openWrapper = lay.docbookwrappertag() != "NONE";
296 if (prevpar != nullptr) {
297 Layout const & prevlay = prevpar->layout();
298 if (prevlay.docbookwrappertag() != "NONE") {
299 if (prevlay.docbookwrappertag() == lay.docbookwrappertag() &&
300 prevlay.docbookwrapperattr() == lay.docbookwrapperattr())
301 openWrapper = !lay.docbookwrappermergewithprevious();
309 openTag(xs, lay.docbookwrappertag(), lay.docbookwrapperattr(), lay.docbookwrappertagtype());
311 const string & tag = lay.docbooktag();
313 auto xmltag = xml::ParTag(tag, lay.docbookattr());
314 if (!xs.isTagOpen(xmltag, 1)) { // Don't nest a paragraph directly in a paragraph.
315 // TODO: required or not?
316 // TODO: avoid creating a ParTag object just for this query...
317 openTag(xs, lay.docbooktag(), lay.docbookattr(), lay.docbooktagtype());
318 openTag(xs, lay.docbookinnertag(), lay.docbookinnerattr(), lay.docbookinnertagtype());
322 openTag(xs, lay.docbookitemtag(), lay.docbookitemattr(), lay.docbookitemtagtype());
323 openTag(xs, lay.docbookiteminnertag(), lay.docbookiteminnerattr(), lay.docbookiteminnertagtype());
327 void closeParTag(XMLStream & xs, Paragraph const * par, Paragraph const * nextpar)
332 // See comment in openParTag.
333 Layout const & lay = par->layout();
334 bool closeWrapper = lay.docbookwrappertag() != "NONE";
335 if (nextpar != nullptr) {
336 Layout const & nextlay = nextpar->layout();
337 if (nextlay.docbookwrappertag() != "NONE") {
338 if (nextlay.docbookwrappertag() == lay.docbookwrappertag() &&
339 nextlay.docbookwrapperattr() == lay.docbookwrapperattr())
340 closeWrapper = !nextlay.docbookwrappermergewithprevious();
347 closeTag(xs, lay.docbookiteminnertag(), lay.docbookiteminnertagtype());
348 closeTag(xs, lay.docbookitemtag(), lay.docbookitemtagtype());
349 closeTag(xs, lay.docbookinnertag(), lay.docbookinnertagtype());
350 closeTag(xs, lay.docbooktag(), lay.docbooktagtype());
352 closeTag(xs, lay.docbookwrappertag(), lay.docbookwrappertagtype());
356 void makeBibliography(
360 OutputParams const & runparams,
361 ParagraphList::const_iterator const & par)
363 // If this is the first paragraph in a bibliography, open the bibliography tag.
364 auto const * pbegin_before = text.paragraphs().getParagraphBefore(par);
365 if (pbegin_before == nullptr || (pbegin_before && pbegin_before->layout().latextype != LATEX_BIB_ENVIRONMENT)) {
366 xs << xml::StartTag("bibliography");
370 // Start the precooked bibliography entry. This is very much like opening a paragraph tag.
371 // Don't forget the citation ID!
373 for (auto i = 0; i < par->size(); ++i) {
374 Inset const *ip = par->getInset(i);
377 if (const auto * bibitem = dynamic_cast<const InsetBibitem*>(ip)) {
378 auto id = xml::cleanID(bibitem->getParam("key"));
379 attr = from_utf8("xml:id='") + id + from_utf8("'");
383 xs << xml::StartTag(from_utf8("bibliomixed"), attr);
385 // Generate the entry. Concatenate the different parts of the paragraph if any.
386 auto const begin = text.paragraphs().begin();
387 auto pars = par->simpleDocBookOnePar(buf, runparams, text.outerFont(std::distance(begin, par)), 0);
388 for (auto & parXML : pars)
389 xs << XMLStream::ESCAPE_NONE << parXML;
391 // End the precooked bibliography entry.
392 xs << xml::EndTag("bibliomixed");
395 // If this is the last paragraph in a bibliography, close the bibliography tag.
396 auto const end = text.paragraphs().end();
399 bool endBibliography = nextpar == end || nextpar->layout().latextype != LATEX_BIB_ENVIRONMENT;
401 if (endBibliography) {
402 xs << xml::EndTag("bibliography");
412 OutputParams const & runparams,
413 ParagraphList::const_iterator const & par)
415 // If this kind of layout should be ignored, already leave.
416 if (par->layout().docbooktag() == "IGNORE")
420 auto const begin = text.paragraphs().begin();
421 auto const end = text.paragraphs().end();
422 auto prevpar = text.paragraphs().getParagraphBefore(par);
424 // We want to open the paragraph tag if:
425 // (i) the current layout permits multiple paragraphs
426 // (ii) we are either not already inside a paragraph (HTMLIsBlock) OR
427 // we are, but this is not the first paragraph
429 // But there is also a special case, and we first see whether we are in it.
430 // We do not want to open the paragraph tag if this paragraph contains
431 // only one item, and that item is "inline", i.e., not HTMLIsBlock (such
432 // as a branch). On the other hand, if that single item has a font change
433 // applied to it, then we still do need to open the paragraph.
435 // Obviously, this is very fragile. The main reason we need to do this is
436 // because of branches, e.g., a branch that contains an entire new section.
437 // We do not really want to wrap that whole thing in a <div>...</div>.
438 bool special_case = false;
439 Inset const *specinset = par->size() == 1 ? par->getInset(0) : nullptr;
440 if (specinset && !specinset->getLayout().htmlisblock()) { // TODO: Convert htmlisblock to a DocBook parameter?
441 Layout const &style = par->layout();
442 FontInfo const first_font = style.labeltype == LABEL_MANUAL ?
443 style.labelfont : style.font;
444 FontInfo const our_font =
445 par->getFont(buf.masterBuffer()->params(), 0,
446 text.outerFont(std::distance(begin, par))).fontInfo();
448 if (first_font == our_font)
452 size_t nInsets = std::distance(par->insetList().begin(), par->insetList().end());
453 auto parSize = (size_t) par->size();
455 // If this LyX code does not produce any output, it can be safely ignored in the following checks: if this thing
456 // is present in the paragraph, it has no impact on the definition of the special case (i.e. whether or not
457 // a <para> tag should be output).
458 auto isLyxCodeToIgnore = [](InsetCode x) { return x == TOC_CODE || x == NOTE_CODE; };
460 // TODO: if a paragraph *only* contains floats, listings, bibliographies, etc., should this be considered as a
461 // special case? If so, the code could be largely simplifies (all the calls to all_of, basically) and optimised
462 // at the compilation stage.
464 // Plain layouts must be ignored.
465 special_case |= buf.params().documentClass().isPlainLayout(par->layout()) && !runparams.docbook_force_pars;
466 // Equations do not deserve their own paragraph (DocBook allows them outside paragraphs).
467 // Exception: any case that generates an <inlineequation> must still get a paragraph to be valid.
468 special_case |= nInsets == parSize && std::all_of(par->insetList().begin(), par->insetList().end(), [](InsetList::Element inset) {
469 return inset.inset && inset.inset->asInsetMath() && inset.inset->asInsetMath()->getType() != hullSimple;
471 // Tables do not deserve their own paragraphs (DocBook allows them outside paragraphs).
472 special_case |= nInsets == parSize && std::all_of(par->insetList().begin(), par->insetList().end(), [isLyxCodeToIgnore](InsetList::Element inset) {
473 return inset.inset->lyxCode() == TABULAR_CODE || isLyxCodeToIgnore(inset.inset->lyxCode());
475 // Floats cannot be in paragraphs.
476 special_case |= nInsets == parSize && std::all_of(par->insetList().begin(), par->insetList().end(), [isLyxCodeToIgnore](InsetList::Element inset) {
477 return inset.inset->lyxCode() == FLOAT_CODE || isLyxCodeToIgnore(inset.inset->lyxCode());
479 // Bibliographies cannot be in paragraphs. Bibitems should still be handled as paragraphs, though
480 // (see makeParagraphBibliography).
481 special_case |= nInsets == parSize && std::all_of(par->insetList().begin(), par->insetList().end(), [isLyxCodeToIgnore](InsetList::Element inset) {
482 return inset.inset->lyxCode() == BIBTEX_CODE || isLyxCodeToIgnore(inset.inset->lyxCode());
484 // ERTs are in comments, not paragraphs.
485 special_case |= nInsets == parSize && std::all_of(par->insetList().begin(), par->insetList().end(), [isLyxCodeToIgnore](InsetList::Element inset) {
486 return inset.inset->lyxCode() == ERT_CODE || isLyxCodeToIgnore(inset.inset->lyxCode());
488 // Listings should not get into their own paragraph.
489 special_case |= nInsets == parSize && std::all_of(par->insetList().begin(), par->insetList().end(), [isLyxCodeToIgnore](InsetList::Element inset) {
490 return inset.inset->lyxCode() == LISTINGS_CODE || isLyxCodeToIgnore(inset.inset->lyxCode());
492 // Boxes cannot get into their own paragraph.
493 special_case |= nInsets == parSize && std::all_of(par->insetList().begin(), par->insetList().end(), [isLyxCodeToIgnore](InsetList::Element inset) {
494 return inset.inset->lyxCode() == BOX_CODE || isLyxCodeToIgnore(inset.inset->lyxCode());
496 // Includes should not have a paragraph.
497 special_case |= nInsets == parSize && std::all_of(par->insetList().begin(), par->insetList().end(), [isLyxCodeToIgnore](InsetList::Element inset) {
498 return inset.inset->lyxCode() == INCLUDE_CODE || isLyxCodeToIgnore(inset.inset->lyxCode());
500 // Glossaries should not have a paragraph.
501 special_case |= nInsets == parSize && std::all_of(par->insetList().begin(), par->insetList().end(), [isLyxCodeToIgnore](InsetList::Element inset) {
502 return inset.inset->lyxCode() == NOMENCL_PRINT_CODE || isLyxCodeToIgnore(inset.inset->lyxCode());
505 bool const open_par = runparams.docbook_make_pars
506 && !runparams.docbook_in_par
509 // We want to issue the closing tag if either:
510 // (i) We opened it, and either docbook_in_par is false,
511 // or we're not in the last paragraph, anyway.
512 // (ii) We didn't open it and docbook_in_par is true,
513 // but we are in the first par, and there is a next par.
514 bool const close_par = open_par && (!runparams.docbook_in_par);
516 // Determine if this paragraph has some real content. Things like new pages are not caught
517 // by Paragraph::empty(), even though they do not generate anything useful in DocBook.
518 // Thus, remove all spaces (including new lines: \r, \n) before checking for emptiness.
519 // std::all_of allows doing this check without having to copy the string.
520 // Open and close tags around each contained paragraph.
523 auto pars = par->simpleDocBookOnePar(buf, runparams, text.outerFont(distance(begin, par)), 0, nextpar == end, special_case);
524 for (docstring const & parXML : pars) {
525 if (!xml::isNotOnlySpace(parXML))
529 openParTag(xs, &*par, prevpar);
531 xs << XMLStream::ESCAPE_NONE << parXML;
534 closeParTag(xs, &*par, (nextpar != end) ? &*nextpar : nullptr);
539 void makeEnvironment(Text const &text,
542 OutputParams const &runparams,
543 ParagraphList::const_iterator const & par)
545 // If this kind of layout should be ignored, already leave.
546 if (par->layout().docbooktag() == "IGNORE")
550 auto const end = text.paragraphs().end();
554 // Special cases for listing-like environments provided in layouts. This is quite ad-hoc, but provides a useful
555 // default. This should not be used by too many environments (only LyX-Code right now).
556 // This would be much simpler if LyX-Code was implemented as InsetListings...
557 bool mimicListing = false;
558 bool ignoreFonts = false;
559 if (par->layout().docbooktag() == "programlisting") {
564 // Output the opening tag for this environment, but only if it has not been previously opened (condition
565 // implemented in openParTag).
566 auto prevpar = text.paragraphs().getParagraphBefore(par);
567 openParTag(xs, &*par, prevpar); // TODO: switch in layout for par/block?
569 // Generate the contents of this environment. There is a special case if this is like some environment.
570 Layout const & style = par->layout();
571 if (style.latextype == LATEX_COMMAND) {
572 // Nothing to do (otherwise, infinite loops).
573 } else if (style.latextype == LATEX_ENVIRONMENT) {
574 // Generate the paragraph, if need be.
575 auto pars = par->simpleDocBookOnePar(buf, runparams, text.outerFont(std::distance(text.paragraphs().begin(), par)), 0, false, ignoreFonts);
578 auto p = pars.begin();
579 while (p != pars.end()) {
580 openTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnerattr(), par->layout().docbookiteminnertagtype());
581 xs << XMLStream::ESCAPE_NONE << *p;
582 closeTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnertagtype());
585 // Insert a new line after each "paragraph" (i.e. line in the listing), except for the last one.
586 // Otherwise, there would one more new line in the output than in the LyX document.
591 for (auto const & p : pars) {
592 openTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnerattr(), par->layout().docbookiteminnertagtype());
593 xs << XMLStream::ESCAPE_NONE << p;
594 closeTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnertagtype());
598 makeAny(text, buf, xs, runparams, par);
601 // Close the environment.
602 closeParTag(xs, &*par, (nextpar != end) ? &*nextpar : nullptr); // TODO: switch in layout for par/block?
606 ParagraphList::const_iterator findEndOfEnvironment(
607 ParagraphList::const_iterator const & pstart,
608 ParagraphList::const_iterator const & pend)
610 // Copy-paste from XHTML. Should be factored out at some point...
611 ParagraphList::const_iterator p = pstart;
612 Layout const & bstyle = p->layout();
613 size_t const depth = p->params().depth();
614 for (++p; p != pend; ++p) {
615 Layout const & style = p->layout();
616 // It shouldn't happen that e.g. a section command occurs inside
617 // a quotation environment, at a higher depth, but as of 6/2009,
618 // it can happen. We pretend that it's just at lowest depth.
619 if (style.latextype == LATEX_COMMAND)
622 // If depth is down, we're done
623 if (p->params().depth() < depth)
626 // If depth is up, we're not done
627 if (p->params().depth() > depth)
630 // FIXME I am not sure about the first check.
631 // Surely we *could* have different layouts that count as
632 // LATEX_PARAGRAPH, right?
633 if (style.latextype == LATEX_PARAGRAPH || style != bstyle)
640 ParagraphList::const_iterator makeListEnvironment(Text const &text,
643 OutputParams const &runparams,
644 ParagraphList::const_iterator const & begin)
648 auto const end = text.paragraphs().end();
649 auto const envend = findEndOfEnvironment(par, end);
651 // If this kind of layout should be ignored, already leave.
652 if (begin->layout().docbooktag() == "IGNORE") {
658 // Output the opening tag for this environment.
659 Layout const & envstyle = par->layout();
660 openTag(xs, envstyle.docbookwrappertag(), envstyle.docbookwrapperattr(), envstyle.docbookwrappertagtype());
661 openTag(xs, envstyle.docbooktag(), envstyle.docbookattr(), envstyle.docbooktagtype());
663 // Handle the content of the list environment, item by item.
664 while (par != envend) {
665 // Skip this paragraph if it is both empty and the last one (otherwise, there may be deeper paragraphs after).
668 if (par->empty() && nextpar == envend)
671 // Open the item wrapper.
672 Layout const & style = par->layout();
673 openTag(xs, style.docbookitemwrappertag(), style.docbookitemwrapperattr(), style.docbookitemwrappertagtype());
675 // Generate the label, if need be. If it is taken from the text, sep != 0 and corresponds to the first
676 // character after the label.
678 if (style.labeltype != LABEL_NO_LABEL && style.docbookitemlabeltag() != "NONE") {
679 if (style.labeltype == LABEL_MANUAL) {
680 // Only variablelist gets here (or similar items defined as an extension in the layout).
681 openTag(xs, style.docbookitemlabeltag(), style.docbookitemlabelattr(), style.docbookitemlabeltagtype());
682 sep = 1 + par->firstWordDocBook(xs, runparams);
683 closeTag(xs, style.docbookitemlabeltag(), style.docbookitemlabeltagtype());
685 // Usual cases: maybe there is something specified at the layout level. Highly unlikely, though.
686 docstring const lbl = par->params().labelString();
689 openTag(xs, style.docbookitemlabeltag(), style.docbookitemlabelattr(), style.docbookitemlabeltagtype());
691 closeTag(xs, style.docbookitemlabeltag(), style.docbookitemlabeltagtype());
696 // Open the item (after the wrapper and the label).
697 openTag(xs, style.docbookitemtag(), style.docbookitemattr(), style.docbookitemtagtype());
699 // Generate the content of the item.
700 if (sep < par->size()) {
701 auto pars = par->simpleDocBookOnePar(buf, runparams,
702 text.outerFont(std::distance(text.paragraphs().begin(), par)), sep);
703 for (auto &p : pars) {
704 openTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnerattr(),
705 par->layout().docbookiteminnertagtype());
706 xs << XMLStream::ESCAPE_NONE << p;
707 closeTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnertagtype());
710 // DocBook doesn't like emptiness.
711 compTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnerattr(),
712 par->layout().docbookiteminnertagtype());
715 // If the next item is deeper, it must go entirely within this item (do it recursively).
716 // By construction, with findEndOfEnvironment, depth can only stay constant or increase, never decrease.
717 depth_type currentDepth = par->getDepth();
719 while (par != envend && par->getDepth() != currentDepth)
720 par = makeAny(text, buf, xs, runparams, par);
721 // Usually, this loop only makes one iteration, except in complex scenarios, like an item with a paragraph,
722 // a list, and another paragraph; or an item with two types of list (itemise then enumerate, for instance).
725 closeTag(xs, style.docbookitemtag(), style.docbookitemtagtype());
726 closeTag(xs, style.docbookitemwrappertag(), style.docbookitemwrappertagtype());
729 // Close this environment in exactly the same way as it was opened.
730 closeTag(xs, envstyle.docbooktag(), envstyle.docbooktagtype());
731 closeTag(xs, envstyle.docbookwrappertag(), envstyle.docbookwrappertagtype());
741 OutputParams const & runparams,
742 ParagraphList::const_iterator const & par)
744 // If this kind of layout should be ignored, already leave.
745 if (par->layout().docbooktag() == "IGNORE")
749 // Unlike XHTML, no need for labels, as they are handled by DocBook tags.
750 auto const begin = text.paragraphs().begin();
751 auto const end = text.paragraphs().end();
755 // Generate this command.
756 auto prevpar = text.paragraphs().getParagraphBefore(par);
757 openParTag(xs, &*par, prevpar);
759 auto pars = par->simpleDocBookOnePar(buf, runparams,text.outerFont(distance(begin, par)));
760 for (auto & parXML : pars)
761 // TODO: decide what to do with openParTag/closeParTag in new lines.
762 xs << XMLStream::ESCAPE_NONE << parXML;
764 closeParTag(xs, &*par, (nextpar != end) ? &*nextpar : nullptr);
768 bool isLayoutSectioning(Layout const & lay)
770 if (lay.docbooksection()) // Special case: some DocBook styles must be handled as sections.
772 else if (lay.category() == from_utf8("Sectioning") || lay.docbooktag() == "section") // Generic case.
773 return lay.toclevel != Layout::NOT_IN_TOC;
778 bool isLayoutSectioningOrSimilar(Layout const & lay)
780 return isLayoutSectioning(lay) || lay.docbooktag() == "bridgehead";
784 using DocBookDocumentSectioning = tuple<bool, pit_type>;
787 struct DocBookInfoTag
789 const set<pit_type> shouldBeInInfo;
790 const set<pit_type> mustBeInInfo; // With the notable exception of the abstract!
791 const set<pit_type> abstract;
792 const bool abstractLayout;
796 DocBookInfoTag(const set<pit_type> & shouldBeInInfo, const set<pit_type> & mustBeInInfo,
797 const set<pit_type> & abstract, bool abstractLayout, pit_type bpit, pit_type epit) :
798 shouldBeInInfo(shouldBeInInfo), mustBeInInfo(mustBeInInfo), abstract(abstract),
799 abstractLayout(abstractLayout), bpit(bpit), epit(epit) {}
803 DocBookDocumentSectioning hasDocumentSectioning(ParagraphList const ¶graphs, pit_type bpit, pit_type const epit) {
804 bool documentHasSections = false;
806 while (bpit < epit) {
807 Layout const &style = paragraphs[bpit].layout();
808 documentHasSections |= isLayoutSectioningOrSimilar(style);
810 if (documentHasSections)
814 // Paragraphs before the first section: [ runparams.par_begin ; eppit )
816 return make_tuple(documentHasSections, bpit);
820 bool hasOnlyNotes(Paragraph const & par)
822 // Precondition: the paragraph is not empty. Otherwise, the function will always return true...
823 for (int i = 0; i < par.size(); ++i)
824 // If you find something that is not an inset (like actual text) or an inset that is not a note,
826 if (!par.isInset(i) || par.getInset(i)->lyxCode() != NOTE_CODE)
829 // An empty paragraph may still require some output.
830 if (par.layout().docbooksection())
833 // There should be really no content here.
838 DocBookInfoTag getParagraphsWithInfo(ParagraphList const ¶graphs,
839 pit_type bpit, pit_type const epit,
840 // Typically, bpit is the beginning of the document and epit the end of the
841 // document *or* the first section.
842 bool documentHasSections,
843 bool detectUnlayoutedAbstract
844 // Whether paragraphs with no specific layout should be detected as abstracts.
845 // For inner sections, an abstract should only be detected if it has a specific
846 // layout. For others, anything that might look like an abstract should be sought.
848 set<pit_type> shouldBeInInfo;
849 set<pit_type> mustBeInInfo;
850 set<pit_type> abstractWithLayout;
851 set<pit_type> abstractNoLayout;
853 // Find the first non empty paragraph by mutating bpit.
854 while (bpit < epit) {
855 Paragraph const &par = paragraphs[bpit];
856 if (par.empty() || hasOnlyNotes(par))
862 // Traverse everything that might belong to <info>.
863 bool hasAbstractLayout = false;
864 pit_type cpit = bpit;
865 for (; cpit < epit; ++cpit) {
866 // Skip paragraphs that don't generate anything in DocBook.
867 Paragraph const & par = paragraphs[cpit];
868 Layout const &style = par.layout();
869 if (hasOnlyNotes(par))
872 // There should never be any section here, except for the first paragraph (a title can be part of <info>).
873 // (Just a sanity check: if this fails, this function could end up processing the whole document.)
874 if (cpit != bpit && isLayoutSectioningOrSimilar(par.layout())) {
875 LYXERR0("Assertion failed: section found in potential <info> paragraphs.");
879 // If this is marked as an abstract by the layout, put it in the right set.
880 if (style.docbookabstract()) {
881 hasAbstractLayout = true;
882 abstractWithLayout.emplace(cpit);
886 // Based on layout information, store this paragraph in one set: should be in <info>, must be,
887 // or abstract (either because of layout or of position).
888 if (style.docbookininfo() == "always")
889 mustBeInInfo.emplace(cpit);
890 else if (style.docbookininfo() == "maybe")
891 shouldBeInInfo.emplace(cpit);
892 else if (documentHasSections && !hasAbstractLayout && detectUnlayoutedAbstract &&
893 (style.docbooktag() == "NONE" || style.docbooktag() == "para") &&
894 style.docbookwrappertag() == "NONE")
895 // In this case, it is very likely that style.docbookininfo() == "never"! Be extra careful
896 // about anything that gets caught here.
897 abstractNoLayout.emplace(cpit);
898 else // This should definitely not be in <info>.
901 // Now, cpit points to the first paragraph that no more has things that could go in <info>.
902 // bpit is the beginning of the <info> part.
904 return DocBookInfoTag(shouldBeInInfo, mustBeInInfo,
905 hasAbstractLayout ? abstractWithLayout : abstractNoLayout,
906 hasAbstractLayout, bpit, cpit);
909 } // end anonymous namespace
912 ParagraphList::const_iterator makeAny(Text const &text,
915 OutputParams const &runparams,
916 ParagraphList::const_iterator par)
918 switch (par->layout().latextype) {
920 makeCommand(text, buf, xs, runparams, par);
922 case LATEX_ENVIRONMENT:
923 makeEnvironment(text, buf, xs, runparams, par);
925 case LATEX_LIST_ENVIRONMENT:
926 case LATEX_ITEM_ENVIRONMENT:
927 // Only case when makeAny() might consume more than one paragraph.
928 return makeListEnvironment(text, buf, xs, runparams, par);
929 case LATEX_PARAGRAPH:
930 makeParagraph(text, buf, xs, runparams, par);
932 case LATEX_BIB_ENVIRONMENT:
933 makeBibliography(text, buf, xs, runparams, par);
941 xml::FontTag docbookStartFontTag(xml::FontTypes type)
943 return xml::FontTag(from_utf8(fontToDocBookTag(type)), from_utf8(fontToAttribute(type)), type);
947 xml::EndFontTag docbookEndFontTag(xml::FontTypes type)
949 return xml::EndFontTag(from_utf8(fontToDocBookTag(type)), type);
953 void outputDocBookInfo(
957 OutputParams const & runparams,
958 ParagraphList const & paragraphs,
959 DocBookInfoTag const & info)
961 // Perform an additional check on the abstract. Sometimes, there are many paragraphs that should go
962 // into the abstract, but none generates actual content. Thus, first generate to a temporary stream,
963 // then only create the <abstract> tag if these paragraphs generate some content.
964 // This check must be performed *before* a decision on whether or not to output <info> is made.
965 bool hasAbstract = !info.abstract.empty();
968 // Generate the abstract XML into a string before further checks.
969 // Usually, makeAny only generates one paragraph at a time. However, for the specific case of lists, it might
970 // generate more than one paragraph, as indicated in the return value.
971 odocstringstream os2;
974 set<pit_type> doneParas;
975 for (auto const & p : info.abstract) {
976 if (doneParas.find(p) == doneParas.end()) {
977 auto oldPar = paragraphs.iterator_at(p);
978 auto newPar = makeAny(text, buf, xs2, runparams, oldPar);
980 // Insert the indices of all the paragraphs that were just generated (typically, one).
981 // **Make the hypothesis that, when an abstract has a list, all its items are consecutive.**
983 while (oldPar != newPar) {
984 doneParas.emplace(id);
991 // Actually output the abstract if there is something to do. Don't count line feeds or spaces in this,
992 // even though they must be properly output if there is some abstract.
993 abstract = os2.str();
994 docstring cleaned = abstract;
995 cleaned.erase(std::remove_if(cleaned.begin(), cleaned.end(), lyx::isSpace), cleaned.end());
997 // Nothing? Then there is no abstract!
1002 // The abstract must go in <info>. Otherwise, decide whether to open <info> based on the layouts.
1003 bool needInfo = !info.mustBeInInfo.empty() || hasAbstract;
1005 // Start the <info> tag if required.
1007 xs.startDivision(false);
1008 xs << xml::StartTag("info");
1012 // Output the elements that should go in <info>, before and after the abstract.
1013 for (auto pit : info.shouldBeInInfo) // Typically, the title: these elements are so important and ubiquitous
1014 // that mandating a wrapper like <info> would repel users. Thus, generate them first.
1015 makeAny(text, buf, xs, runparams, paragraphs.iterator_at(pit));
1016 for (auto pit : info.mustBeInInfo)
1017 makeAny(text, buf, xs, runparams, paragraphs.iterator_at(pit));
1019 // If there is no title, generate one (required for the document to be valid).
1020 // This code is called for the main document, for table cells, etc., so be precise in this condition.
1021 if (text.isMainText() && info.shouldBeInInfo.empty() && !runparams.inInclude) {
1022 xs << xml::StartTag("title");
1023 xs << "Untitled Document";
1024 xs << xml::EndTag("title");
1028 // Always output the abstract as the last item of the <info>, as it requires special treatment (especially if
1029 // it contains several paragraphs that are empty).
1031 if (info.abstractLayout) {
1032 xs << XMLStream::ESCAPE_NONE << abstract;
1035 string tag = paragraphs[*info.abstract.begin()].layout().docbookforceabstracttag();
1039 if (!xs.isLastTagCR())
1042 xs << xml::StartTag(tag);
1044 xs << XMLStream::ESCAPE_NONE << abstract;
1045 xs << xml::EndTag(tag);
1050 // End the <info> tag if it was started.
1052 if (!xs.isLastTagCR())
1055 xs << xml::EndTag("info");
1062 void docbookSimpleAllParagraphs(
1066 OutputParams const & runparams)
1068 // Handle the given text, supposing it has no sections (i.e. a "simple" text). The input may vary in length
1069 // between a single paragraph to a whole document.
1070 pit_type const bpit = runparams.par_begin;
1071 pit_type const epit = runparams.par_end;
1072 ParagraphList const ¶graphs = text.paragraphs();
1074 // First, the <info> tag.
1075 DocBookInfoTag info = getParagraphsWithInfo(paragraphs, bpit, epit, false, true);
1076 outputDocBookInfo(text, buf, xs, runparams, paragraphs, info);
1078 // Then, the content. It starts where the <info> ends.
1079 auto par = paragraphs.iterator_at(info.epit);
1080 auto end = paragraphs.iterator_at(epit);
1081 while (par != end) {
1082 if (!hasOnlyNotes(*par))
1083 par = makeAny(text, buf, xs, runparams, par);
1090 void docbookParagraphs(Text const &text,
1093 OutputParams const &runparams) {
1094 ParagraphList const ¶graphs = text.paragraphs();
1095 if (runparams.par_begin == runparams.par_end) {
1096 runparams.par_begin = 0;
1097 runparams.par_end = paragraphs.size();
1099 pit_type bpit = runparams.par_begin;
1100 pit_type const epit = runparams.par_end;
1101 LASSERT(bpit < epit,
1103 xs << XMLStream::ESCAPE_NONE << "<!-- DocBook output error! -->\n";
1107 std::stack<std::pair<int, string>> headerLevels; // Used to determine when to open/close sections: store the depth
1108 // of the section and the tag that was used to open it.
1110 // Detect whether the document contains sections. If there are no sections, treatment is largely simplified.
1111 // In particular, there can't be an abstract, unless it is manually marked.
1112 bool documentHasSections;
1114 tie(documentHasSections, eppit) = hasDocumentSectioning(paragraphs, bpit, epit);
1116 // Deal with "simple" documents, i.e. those without sections.
1117 if (!documentHasSections) {
1118 docbookSimpleAllParagraphs(text, buf, xs, runparams);
1122 // Output the first <info> tag (or just the title).
1123 DocBookInfoTag info = getParagraphsWithInfo(paragraphs, bpit, eppit, true, true);
1124 outputDocBookInfo(text, buf, xs, runparams, paragraphs, info);
1127 // Then, iterate through the paragraphs of this document.
1128 bool currentlyInAppendix = false;
1130 auto par = text.paragraphs().iterator_at(bpit);
1131 auto end = text.paragraphs().iterator_at(epit);
1132 while (par != end) {
1133 OutputParams ourparams = runparams;
1135 if (par->params().startOfAppendix())
1136 currentlyInAppendix = true;
1137 if (hasOnlyNotes(*par)) {
1142 Layout const &style = par->layout();
1144 // Think about adding <section> and/or </section>s.
1145 if (isLayoutSectioning(style)) {
1146 int level = style.toclevel;
1148 // Need to close a previous section if it has the same level or a higher one (close <section> if opening a
1149 // <h2> after a <h2>, <h3>, <h4>, <h5> or <h6>). More examples:
1150 // - current: h2; back: h1; do not close any <section>
1151 // - current: h1; back: h2; close two <section> (first the <h2>, then the <h1>, so a new <h1> can come)
1152 while (!headerLevels.empty() && level <= headerLevels.top().first) {
1153 // Output the tag only if it corresponds to a legit section.
1154 int stackLevel = headerLevels.top().first;
1155 if (stackLevel != Layout::NOT_IN_TOC) {
1156 xs << xml::EndTag(headerLevels.top().second);
1162 // Open the new section: first push it onto the stack, then output it in DocBook.
1163 string sectionTag = (currentlyInAppendix && style.docbooksectiontag() == "chapter") ?
1164 "appendix" : style.docbooksectiontag();
1165 headerLevels.push(std::make_pair(level, sectionTag));
1167 // Some sectioning-like elements should not be output (such as FrontMatter).
1168 if (level != Layout::NOT_IN_TOC) {
1169 // Look for a label in the title, i.e. a InsetLabel as a child.
1170 docstring id = docstring();
1171 for (pos_type i = 0; i < par->size(); ++i) {
1172 Inset const *inset = par->getInset(i);
1174 if (auto label = dynamic_cast<InsetLabel const *>(inset)) {
1175 // Generate the attributes for the section if need be.
1176 id += "xml:id=\"" + xml::cleanID(label->screenLabel()) + "\"";
1178 // Don't output the ID as a DocBook <anchor>.
1179 ourparams.docbook_anchors_to_ignore.emplace(label->screenLabel());
1181 // Cannot have multiple IDs per tag. If there is another ID inset in the document, it will
1182 // be output as a DocBook anchor.
1188 // Write the open tag for this section.
1192 xs << xml::StartTag(sectionTag, attrs);
1197 // Close all sections before the bibliography.
1198 // TODO: Only close all when the bibliography is at the end of the document? Or force to output the bibliography at the end of the document? Or don't care (as allowed by DocBook)?
1199 if (!par->insetList().empty()) {
1200 Inset const *firstInset = par->getInset(0);
1201 if (firstInset && (firstInset->lyxCode() == BIBITEM_CODE || firstInset->lyxCode() == BIBTEX_CODE)) {
1202 while (!headerLevels.empty()) {
1203 int level = headerLevels.top().first;
1204 docstring tag = from_utf8("</" + headerLevels.top().second + ">");
1207 // Output the tag only if it corresponds to a legit section.
1208 if (level != Layout::NOT_IN_TOC) {
1209 xs << XMLStream::ESCAPE_NONE << tag;
1216 // Generate the <info> tag if a section was just opened.
1217 // Some sections may require abstracts (mostly parts, in books: DocBookForceAbstractTag will not be NONE),
1218 // others can still have an abstract (it must be detected so that it can be output at the right place).
1219 // TODO: docbookforceabstracttag is a bit contrived here, but it does the job. Having another field just for this would be cleaner, but that's just for <part> and <partintro>, so it's probably not worth the effort.
1220 if (isLayoutSectioning(style)) {
1221 // This abstract may be found between the next paragraph and the next title.
1222 pit_type cpit = std::distance(text.paragraphs().begin(), par);
1223 pit_type ppit = std::get<1>(hasDocumentSectioning(paragraphs, cpit + 1L, epit));
1225 // Generate this abstract (this code corresponds to parts of outputDocBookInfo).
1226 DocBookInfoTag secInfo = getParagraphsWithInfo(paragraphs, cpit, ppit, true,
1227 style.docbookforceabstracttag() != "NONE");
1229 if (!secInfo.mustBeInInfo.empty() || !secInfo.shouldBeInInfo.empty() || !secInfo.abstract.empty()) {
1230 // Generate the <info>, if required. If DocBookForceAbstractTag != NONE, this abstract will not be in
1231 // <info>, unlike other ("standard") abstracts.
1232 bool hasStandardAbstract = !secInfo.abstract.empty() && style.docbookforceabstracttag() == "NONE";
1233 bool needInfo = !secInfo.mustBeInInfo.empty() || hasStandardAbstract;
1236 xs.startDivision(false);
1237 xs << xml::StartTag("info");
1241 // Output the elements that should go in <info>, before and after the abstract.
1242 for (auto pit : secInfo.shouldBeInInfo) // Typically, the title: these elements are so important and ubiquitous
1243 // that mandating a wrapper like <info> would repel users. Thus, generate them first.
1244 makeAny(text, buf, xs, ourparams, paragraphs.iterator_at(pit));
1245 for (auto pit : secInfo.mustBeInInfo)
1246 makeAny(text, buf, xs, ourparams, paragraphs.iterator_at(pit));
1248 // Deal with the abstract in <info> if it is standard (i.e. its tag is <abstract>).
1249 if (!secInfo.abstract.empty() && hasStandardAbstract) {
1250 if (!secInfo.abstractLayout) {
1251 xs << xml::StartTag("abstract");
1255 for (auto const &p : secInfo.abstract)
1256 makeAny(text, buf, xs, ourparams, paragraphs.iterator_at(p));
1258 if (!secInfo.abstractLayout) {
1259 xs << xml::EndTag("abstract");
1264 // End the <info> tag if it was started.
1266 if (!xs.isLastTagCR())
1269 xs << xml::EndTag("info");
1274 // Deal with the abstract outside <info> if it is not standard (i.e. its tag is layout-defined).
1275 if (!secInfo.abstract.empty() && !hasStandardAbstract) {
1276 // Assert: style.docbookforceabstracttag() != NONE.
1277 xs << xml::StartTag(style.docbookforceabstracttag());
1279 for (auto const &p : secInfo.abstract)
1280 makeAny(text, buf, xs, ourparams, paragraphs.iterator_at(p));
1281 xs << xml::EndTag(style.docbookforceabstracttag());
1285 // Skip all the text that has just been generated.
1286 par = paragraphs.iterator_at(secInfo.epit);
1288 // No <info> tag to generate, proceed as for normal paragraphs.
1289 par = makeAny(text, buf, xs, ourparams, par);
1292 // Generate this paragraph, as it has nothing special.
1293 par = makeAny(text, buf, xs, ourparams, par);
1297 // If need be, close <section>s, but only at the end of the document (otherwise, dealt with at the beginning
1299 while (!headerLevels.empty() && headerLevels.top().first > Layout::NOT_IN_TOC) {
1300 docstring tag = from_utf8("</" + headerLevels.top().second + ">");
1302 xs << XMLStream::ESCAPE_NONE << tag;