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.
15 #include "buffer_funcs.h"
16 #include "BufferParams.h"
18 #include "InsetList.h"
19 #include "Paragraph.h"
20 #include "ParagraphList.h"
21 #include "ParagraphParameters.h"
24 #include "TextClass.h"
26 #include "insets/InsetBibtex.h"
27 #include "insets/InsetBibitem.h"
28 #include "insets/InsetLabel.h"
29 #include "insets/InsetNote.h"
31 #include "support/lassert.h"
39 using namespace lyx::support;
45 std::string fontToDocBookTag(xml::FontTypes type)
48 case xml::FontTypes::FT_EMPH:
49 case xml::FontTypes::FT_BOLD:
51 case xml::FontTypes::FT_NOUN:
53 case xml::FontTypes::FT_UBAR:
54 case xml::FontTypes::FT_WAVE:
55 case xml::FontTypes::FT_DBAR:
56 case xml::FontTypes::FT_SOUT:
57 case xml::FontTypes::FT_XOUT:
58 case xml::FontTypes::FT_ITALIC:
59 case xml::FontTypes::FT_UPRIGHT:
60 case xml::FontTypes::FT_SLANTED:
61 case xml::FontTypes::FT_SMALLCAPS:
62 case xml::FontTypes::FT_ROMAN:
63 case xml::FontTypes::FT_SANS:
65 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:
86 string fontToRole(xml::FontTypes type)
88 // Specific fonts are achieved with roles. The only common ones are "" for basic emphasis,
89 // and "bold"/"strong" for bold. With some specific options, other roles are copied into
90 // HTML output (via the DocBook XSLT sheets); otherwise, if not recognised, they are just ignored.
91 // Hence, it is not a problem to have many roles by default here.
92 // See https://www.sourceware.org/ml/docbook/2003-05/msg00269.html
94 case xml::FontTypes::FT_ITALIC:
95 case xml::FontTypes::FT_EMPH:
97 case xml::FontTypes::FT_BOLD:
99 case xml::FontTypes::FT_NOUN: // Outputs a <person>
100 case xml::FontTypes::FT_TYPE: // Outputs a <code>
102 case xml::FontTypes::FT_UBAR:
105 // All other roles are non-standard for DocBook.
107 case xml::FontTypes::FT_WAVE:
109 case xml::FontTypes::FT_DBAR:
111 case xml::FontTypes::FT_SOUT:
113 case xml::FontTypes::FT_XOUT:
115 case xml::FontTypes::FT_UPRIGHT:
117 case xml::FontTypes::FT_SLANTED:
119 case xml::FontTypes::FT_SMALLCAPS:
121 case xml::FontTypes::FT_ROMAN:
123 case xml::FontTypes::FT_SANS:
125 case xml::FontTypes::FT_SIZE_TINY:
127 case xml::FontTypes::FT_SIZE_SCRIPT:
128 return "size_script";
129 case xml::FontTypes::FT_SIZE_FOOTNOTE:
130 return "size_footnote";
131 case xml::FontTypes::FT_SIZE_SMALL:
133 case xml::FontTypes::FT_SIZE_NORMAL:
134 return "size_normal";
135 case xml::FontTypes::FT_SIZE_LARGE:
137 case xml::FontTypes::FT_SIZE_LARGER:
138 return "size_larger";
139 case xml::FontTypes::FT_SIZE_LARGEST:
140 return "size_largest";
141 case xml::FontTypes::FT_SIZE_HUGE:
143 case xml::FontTypes::FT_SIZE_HUGER:
145 case xml::FontTypes::FT_SIZE_INCREASE:
146 return "size_increase";
147 case xml::FontTypes::FT_SIZE_DECREASE:
148 return "size_decrease";
155 string fontToAttribute(xml::FontTypes type) {
156 // If there is a role (i.e. nonstandard use of a tag), output the attribute. Otherwise, the sheer tag is sufficient
158 string role = fontToRole(type);
160 return "role='" + role + "'";
167 // Convenience functions to open and close tags. First, very low-level ones to ensure a consistent new-line behaviour.
171 // Contents of the block.
176 // <paratag>Contents of the paragraph.</paratag>
179 // Content before<inlinetag>Contents of the paragraph.</inlinetag>Content after
181 void openInlineTag(XMLStream & xs, const std::string & tag, const std::string & attr)
183 xs << xml::StartTag(tag, attr);
187 void closeInlineTag(XMLStream & xs, const std::string & tag)
189 xs << xml::EndTag(tag);
193 void openParTag(XMLStream & xs, const std::string & tag, const std::string & attr)
195 if (!xs.isLastTagCR())
197 xs << xml::StartTag(tag, attr);
201 void closeParTag(XMLStream & xs, const std::string & tag)
203 xs << xml::EndTag(tag);
208 void openBlockTag(XMLStream & xs, const std::string & tag, const std::string & attr)
210 if (!xs.isLastTagCR())
212 xs << xml::StartTag(tag, attr);
217 void closeBlockTag(XMLStream & xs, const std::string & tag)
219 if (!xs.isLastTagCR())
221 xs << xml::EndTag(tag);
226 void openTag(XMLStream & xs, const std::string & tag, const std::string & attr, const std::string & tagtype)
228 if (tag.empty() || tag == "NONE") // Common check to be performed elsewhere, if it was not here.
231 if (tag == "para" || tagtype == "paragraph") // Special case for <para>: always considered as a paragraph.
232 openParTag(xs, tag, attr);
233 else if (tagtype == "block")
234 openBlockTag(xs, tag, attr);
235 else if (tagtype == "inline")
236 openInlineTag(xs, tag, attr);
238 xs.writeError("Unrecognised tag type '" + tagtype + "' for '" + tag + " " + attr + "'");
242 void closeTag(XMLStream & xs, const std::string & tag, const std::string & tagtype)
244 if (tag.empty() || tag == "NONE")
247 if (tag == "para" || tagtype == "paragraph") // Special case for <para>: always considered as a paragraph.
248 closeParTag(xs, tag);
249 else if (tagtype == "block")
250 closeBlockTag(xs, tag);
251 else if (tagtype == "inline")
252 closeInlineTag(xs, tag);
254 xs.writeError("Unrecognised tag type '" + tagtype + "' for '" + tag + "'");
258 void compTag(XMLStream & xs, const std::string & tag, const std::string & attr, const std::string & tagtype)
260 if (tag.empty() || tag == "NONE")
263 // Special case for <para>: always considered as a paragraph.
264 if (tag == "para" || tagtype == "paragraph" || tagtype == "block") {
265 if (!xs.isLastTagCR())
267 xs << xml::CompTag(tag, attr);
269 } else if (tagtype == "inline") {
270 xs << xml::CompTag(tag, attr);
272 xs.writeError("Unrecognised tag type '" + tagtype + "' for '" + tag + "'");
277 // Higher-level convenience functions.
279 void openParTag(XMLStream & xs, const Paragraph * par, const Paragraph * prevpar)
281 Layout const & lay = par->layout();
286 // When should the wrapper be opened here? Only if the previous paragraph has the SAME wrapper tag
287 // (usually, they won't have the same layout) and the CURRENT one allows merging.
288 // The main use case is author information in several paragraphs: if the name of the author is the
289 // first paragraph of an author, then merging with the previous tag does not make sense. Say the
290 // next paragraph is the affiliation, then it should be output in the same <author> tag (different
291 // layout, same wrapper tag).
292 bool openWrapper = lay.docbookwrappertag() != "NONE";
293 if (prevpar != nullptr) {
294 Layout const & prevlay = prevpar->layout();
295 if (prevlay.docbookwrappertag() != "NONE") {
296 if (prevlay.docbookwrappertag() == lay.docbookwrappertag() &&
297 prevlay.docbookwrapperattr() == lay.docbookwrapperattr())
298 openWrapper = !lay.docbookwrappermergewithprevious();
306 openTag(xs, lay.docbookwrappertag(), lay.docbookwrapperattr(), lay.docbookwrappertagtype());
308 const string & tag = lay.docbooktag();
310 auto xmltag = xml::ParTag(tag, lay.docbookattr());
311 if (!xs.isTagOpen(xmltag, 1)) // Don't nest a paragraph directly in a paragraph.
312 // TODO: required or not?
313 // TODO: avoid creating a ParTag object just for this query...
314 openTag(xs, lay.docbooktag(), lay.docbookattr(), lay.docbooktagtype());
317 openTag(xs, lay.docbookitemtag(), lay.docbookitemattr(), lay.docbookitemtagtype());
318 openTag(xs, lay.docbookiteminnertag(), lay.docbookiteminnerattr(), lay.docbookiteminnertagtype());
322 void closeParTag(XMLStream & xs, Paragraph const * par, Paragraph const * nextpar)
327 // See comment in openParTag.
328 Layout const & lay = par->layout();
329 bool closeWrapper = lay.docbookwrappertag() != "NONE";
330 if (nextpar != nullptr) {
331 Layout const & nextlay = nextpar->layout();
332 if (nextlay.docbookwrappertag() != "NONE") {
333 if (nextlay.docbookwrappertag() == lay.docbookwrappertag() &&
334 nextlay.docbookwrapperattr() == lay.docbookwrapperattr())
335 closeWrapper = !nextlay.docbookwrappermergewithprevious();
342 closeTag(xs, lay.docbookiteminnertag(), lay.docbookiteminnertagtype());
343 closeTag(xs, lay.docbookitemtag(), lay.docbookitemtagtype());
344 closeTag(xs, lay.docbooktag(), lay.docbooktagtype());
346 closeTag(xs, lay.docbookwrappertag(), lay.docbookwrappertagtype());
350 void openLabelTag(XMLStream & xs, Layout const & lay) // Mostly for definition lists.
352 openTag(xs, lay.docbookitemlabeltag(), lay.docbookitemlabelattr(), lay.docbookitemlabeltagtype());
356 void closeLabelTag(XMLStream & xs, Layout const & lay)
358 closeTag(xs, lay.docbookitemlabeltag(), lay.docbookitemlabeltagtype());
362 void openItemTag(XMLStream & xs, Layout const & lay)
364 openTag(xs, lay.docbookitemtag(), lay.docbookitemattr(), lay.docbookitemtagtype());
368 void closeItemTag(XMLStream & xs, Layout const & lay)
370 closeTag(xs, lay.docbookitemtag(), lay.docbookitemtagtype());
374 ParagraphList::const_iterator makeAny(Text const &,
377 OutputParams const &,
378 ParagraphList::const_iterator);
381 void makeBibliography(
385 OutputParams const & runparams,
386 ParagraphList::const_iterator const & par)
388 // If this is the first paragraph in a bibliography, open the bibliography tag.
389 auto pbegin_before = text.paragraphs().getParagraphBefore(par);
390 if (pbegin_before->layout().latextype != LATEX_BIB_ENVIRONMENT) {
391 xs << xml::StartTag("bibliography");
395 // Start the precooked bibliography entry. This is very much like opening a paragraph tag.
396 // Don't forget the citation ID!
398 for (auto i = 0; i < par->size(); ++i) {
399 Inset const *ip = par->getInset(i);
402 if (const auto * bibitem = dynamic_cast<const InsetBibitem*>(ip)) {
403 attr = from_utf8("xml:id='") + bibitem->getParam("key") + from_utf8("'");
407 xs << xml::StartTag(from_utf8("bibliomixed"), attr);
409 // Generate the entry. Concatenate the different parts of the paragraph if any.
410 auto const begin = text.paragraphs().begin();
411 auto pars = par->simpleDocBookOnePar(buf, runparams, text.outerFont(std::distance(begin, par)), 0);
412 for (auto & parXML : pars)
413 xs << XMLStream::ESCAPE_NONE << parXML;
415 // End the precooked bibliography entry.
416 xs << xml::EndTag("bibliomixed");
419 // If this is the last paragraph in a bibliography, close the bibliography tag.
420 auto const end = text.paragraphs().end();
423 bool endBibliography = nextpar == end || nextpar->layout().latextype != LATEX_BIB_ENVIRONMENT;
425 if (endBibliography) {
426 xs << xml::EndTag("bibliography");
436 OutputParams const & runparams,
437 ParagraphList::const_iterator const & par)
439 auto const begin = text.paragraphs().begin();
440 auto const end = text.paragraphs().end();
441 auto prevpar = text.paragraphs().getParagraphBefore(par);
443 // We want to open the paragraph tag if:
444 // (i) the current layout permits multiple paragraphs
445 // (ii) we are either not already inside a paragraph (HTMLIsBlock) OR
446 // we are, but this is not the first paragraph
448 // But there is also a special case, and we first see whether we are in it.
449 // We do not want to open the paragraph tag if this paragraph contains
450 // only one item, and that item is "inline", i.e., not HTMLIsBlock (such
451 // as a branch). On the other hand, if that single item has a font change
452 // applied to it, then we still do need to open the paragraph.
454 // Obviously, this is very fragile. The main reason we need to do this is
455 // because of branches, e.g., a branch that contains an entire new section.
456 // We do not really want to wrap that whole thing in a <div>...</div>.
457 bool special_case = false;
458 Inset const *specinset = par->size() == 1 ? par->getInset(0) : nullptr;
459 if (specinset && !specinset->getLayout().htmlisblock()) { // TODO: Convert htmlisblock to a DocBook parameter?
460 Layout const &style = par->layout();
461 FontInfo const first_font = style.labeltype == LABEL_MANUAL ?
462 style.labelfont : style.font;
463 FontInfo const our_font =
464 par->getFont(buf.masterBuffer()->params(), 0,
465 text.outerFont(std::distance(begin, par))).fontInfo();
467 if (first_font == our_font)
471 size_t nInsets = std::distance(par->insetList().begin(), par->insetList().end());
473 // Plain layouts must be ignored.
474 special_case |= buf.params().documentClass().isPlainLayout(par->layout()) && !runparams.docbook_force_pars;
475 // Equations do not deserve their own paragraph (DocBook allows them outside paragraphs).
476 special_case |= nInsets == (size_t) par->size() && std::all_of(par->insetList().begin(), par->insetList().end(), [](InsetList::Element inset) {
477 return inset.inset && inset.inset->asInsetMath();
480 // TODO: Could get rid of this with a DocBook equivalent to htmlisblock? Not for all cases, unfortunately... See above for those that have been determined not to be allowable for this potential refactoring.
481 if (!special_case && par->size() == 1 && par->getInset(0)) {
482 Inset const * firstInset = par->getInset(0);
484 // Floats cannot be in paragraphs.
485 special_case = to_utf8(firstInset->layoutName()).substr(0, 6) == "Float:";
487 // Bibliographies cannot be in paragraphs.
488 if (!special_case && firstInset->asInsetCommand())
489 special_case = firstInset->asInsetCommand()->params().getCmdName() == "bibtex";
491 // ERTs are in comments, not paragraphs.
492 if (!special_case && firstInset->lyxCode() == lyx::ERT_CODE)
495 // Listings should not get into their own paragraph.
496 if (!special_case && firstInset->lyxCode() == lyx::LISTINGS_CODE)
499 // Boxes cannot get into their own paragraph.
500 if (!special_case && firstInset->lyxCode() == lyx::BOX_CODE)
504 bool const open_par = runparams.docbook_make_pars
505 && !runparams.docbook_in_par
508 // We want to issue the closing tag if either:
509 // (i) We opened it, and either docbook_in_par is false,
510 // or we're not in the last paragraph, anyway.
511 // (ii) We didn't open it and docbook_in_par is true,
512 // but we are in the first par, and there is a next par.
513 bool const close_par = open_par && (!runparams.docbook_in_par);
515 // Determine if this paragraph has some real content. Things like new pages are not caught
516 // by Paragraph::empty(), even though they do not generate anything useful in DocBook.
517 // Thus, remove all spaces (including new lines: \r, \n) before checking for emptiness.
518 // std::all_of allows doing this check without having to copy the string.
519 // Open and close tags around each contained paragraph.
522 auto pars = par->simpleDocBookOnePar(buf, runparams, text.outerFont(distance(begin, par)), 0, nextpar == end, special_case);
523 for (docstring const & parXML : pars) {
524 if (xml::isNotOnlySpace(parXML)) {
526 openParTag(xs, &*par, prevpar);
528 xs << XMLStream::ESCAPE_NONE << parXML;
531 closeParTag(xs, &*par, (nextpar != end) ? &*nextpar : nullptr);
537 void makeEnvironment(Text const &text,
540 OutputParams const &runparams,
541 ParagraphList::const_iterator const & par)
543 auto const end = text.paragraphs().end();
547 // Special cases for listing-like environments provided in layouts. This is quite ad-hoc, but provides a useful
548 // default. This should not be used by too many environments (only LyX-Code right now).
549 // This would be much simpler if LyX-Code was implemented as InsetListings...
550 bool mimicListing = false;
551 bool ignoreFonts = false;
552 if (par->layout().docbooktag() == "programlisting") {
557 // Output the opening tag for this environment, but only if it has not been previously opened (condition
558 // implemented in openParTag).
559 auto prevpar = text.paragraphs().getParagraphBefore(par);
560 openParTag(xs, &*par, prevpar); // TODO: switch in layout for par/block?
562 // Generate the contents of this environment. There is a special case if this is like some environment.
563 Layout const & style = par->layout();
564 if (style.latextype == LATEX_COMMAND) {
565 // Nothing to do (otherwise, infinite loops).
566 } else if (style.latextype == LATEX_ENVIRONMENT) {
567 // Generate the paragraph, if need be.
568 auto pars = par->simpleDocBookOnePar(buf, runparams, text.outerFont(std::distance(text.paragraphs().begin(), par)), 0, false, ignoreFonts);
571 auto p = pars.begin();
572 while (p != pars.end()) {
573 openTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnerattr(), par->layout().docbookiteminnertagtype());
574 xs << XMLStream::ESCAPE_NONE << *p;
575 closeTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnertagtype());
582 for (auto const & p : pars) {
583 openTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnerattr(), par->layout().docbookiteminnertagtype());
584 xs << XMLStream::ESCAPE_NONE << p;
585 closeTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnertagtype());
589 makeAny(text, buf, xs, runparams, par);
592 // Close the environment.
593 closeParTag(xs, &*par, (nextpar != end) ? &*nextpar : nullptr); // TODO: switch in layout for par/block?
597 ParagraphList::const_iterator findEndOfEnvironment(
598 ParagraphList::const_iterator const & pstart,
599 ParagraphList::const_iterator const & pend)
601 // Copy-paste from XHTML. Should be factored out at some point...
602 ParagraphList::const_iterator p = pstart;
603 Layout const & bstyle = p->layout();
604 size_t const depth = p->params().depth();
605 for (++p; p != pend; ++p) {
606 Layout const & style = p->layout();
607 // It shouldn't happen that e.g. a section command occurs inside
608 // a quotation environment, at a higher depth, but as of 6/2009,
609 // it can happen. We pretend that it's just at lowest depth.
610 if (style.latextype == LATEX_COMMAND)
613 // If depth is down, we're done
614 if (p->params().depth() < depth)
617 // If depth is up, we're not done
618 if (p->params().depth() > depth)
621 // FIXME I am not sure about the first check.
622 // Surely we *could* have different layouts that count as
623 // LATEX_PARAGRAPH, right?
624 if (style.latextype == LATEX_PARAGRAPH || style != bstyle)
631 ParagraphList::const_iterator makeListEnvironment(Text const &text,
634 OutputParams const &runparams,
635 ParagraphList::const_iterator const & begin)
638 auto const end = text.paragraphs().end();
639 auto const envend = findEndOfEnvironment(par, end);
641 // Output the opening tag for this environment.
642 Layout const & envstyle = par->layout();
643 openTag(xs, envstyle.docbookwrappertag(), envstyle.docbookwrapperattr(), envstyle.docbookwrappertagtype());
644 openTag(xs, envstyle.docbooktag(), envstyle.docbookattr(), envstyle.docbooktagtype());
646 // Handle the content of the list environment, item by item.
647 while (par != envend) {
648 Layout const & style = par->layout();
650 // Open the item wrapper.
651 openTag(xs, style.docbookitemwrappertag(), style.docbookitemwrapperattr(), style.docbookitemwrappertagtype());
653 // Generate the label, if need be. If it is taken from the text, sep != 0 and corresponds to the first
654 // character after the label.
656 if (style.labeltype != LABEL_NO_LABEL && style.docbookitemlabeltag() != "NONE") {
657 if (style.labeltype == LABEL_MANUAL) {
658 // Only variablelist gets here (or similar items defined as an extension in the layout).
659 openLabelTag(xs, style);
660 sep = 1 + par->firstWordDocBook(xs, runparams);
661 closeLabelTag(xs, style);
663 // Usual cases: maybe there is something specified at the layout level. Highly unlikely, though.
664 docstring const lbl = par->params().labelString();
667 openLabelTag(xs, style);
669 closeLabelTag(xs, style);
674 // Open the item (after the wrapper and the label).
675 openTag(xs, style.docbookitemtag(), style.docbookitemattr(), style.docbookitemtagtype());
677 // Generate the content of the item.
678 if (sep < par->size()) {
679 auto pars = par->simpleDocBookOnePar(buf, runparams,
680 text.outerFont(std::distance(text.paragraphs().begin(), par)), sep);
681 for (auto &p : pars) {
682 openTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnerattr(),
683 par->layout().docbookiteminnertagtype());
684 xs << XMLStream::ESCAPE_NONE << p;
685 closeTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnertagtype());
688 // DocBook doesn't like emptiness.
689 compTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnerattr(),
690 par->layout().docbookiteminnertagtype());
693 // If the next item is deeper, it must go entirely within this item (do it recursively).
694 // By construction, with findEndOfEnvironment, depth can only stay constant or increase, never decrease.
695 depth_type currentDepth = par->getDepth();
697 while (par != envend && par->getDepth() != currentDepth)
698 par = makeAny(text, buf, xs, runparams, par);
699 // Usually, this loop only makes one iteration, except in complex scenarios, like an item with a paragraph,
700 // a list, and another paragraph; or an item with two types of list (itemise then enumerate, for instance).
703 closeTag(xs, style.docbookitemtag(), style.docbookitemtagtype());
704 closeTag(xs, style.docbookitemwrappertag(), style.docbookitemwrappertagtype());
707 // Close this environment in exactly the same way as it was opened.
708 closeTag(xs, envstyle.docbooktag(), envstyle.docbooktagtype());
709 closeTag(xs, envstyle.docbookwrappertag(), envstyle.docbookwrappertagtype());
719 OutputParams const & runparams,
720 ParagraphList::const_iterator const & par)
722 // Unlike XHTML, no need for labels, as they are handled by DocBook tags.
723 auto const begin = text.paragraphs().begin();
724 auto const end = text.paragraphs().end();
728 // Generate this command.
729 auto prevpar = text.paragraphs().getParagraphBefore(par);
730 openParTag(xs, &*par, prevpar);
732 auto pars = par->simpleDocBookOnePar(buf, runparams,text.outerFont(distance(begin, par)));
733 for (auto & parXML : pars)
734 // TODO: decide what to do with openParTag/closeParTag in new lines.
735 xs << XMLStream::ESCAPE_NONE << parXML;
737 closeParTag(xs, &*par, (nextpar != end) ? &*nextpar : nullptr);
741 ParagraphList::const_iterator makeAny(Text const &text,
744 OutputParams const &runparams,
745 ParagraphList::const_iterator par)
747 switch (par->layout().latextype) {
749 makeCommand(text, buf, xs, runparams, par);
751 case LATEX_ENVIRONMENT:
752 makeEnvironment(text, buf, xs, runparams, par);
754 case LATEX_LIST_ENVIRONMENT:
755 case LATEX_ITEM_ENVIRONMENT:
756 // Only case when makeAny() might consume more than one paragraph.
757 return makeListEnvironment(text, buf, xs, runparams, par);
758 case LATEX_PARAGRAPH:
759 makeParagraph(text, buf, xs, runparams, par);
761 case LATEX_BIB_ENVIRONMENT:
762 makeBibliography(text, buf, xs, runparams, par);
770 bool isLayoutSectioning(Layout const & lay)
772 return lay.category() == from_utf8("Sectioning");
776 using DocBookDocumentSectioning = tuple<bool, pit_type>;
779 struct DocBookInfoTag
781 const set<pit_type> shouldBeInInfo;
782 const set<pit_type> mustBeInInfo; // With the notable exception of the abstract!
783 const set<pit_type> abstract;
784 const bool abstractLayout;
788 DocBookInfoTag(const set<pit_type> & shouldBeInInfo, const set<pit_type> & mustBeInInfo,
789 const set<pit_type> & abstract, bool abstractLayout, pit_type bpit, pit_type epit) :
790 shouldBeInInfo(shouldBeInInfo), mustBeInInfo(mustBeInInfo), abstract(abstract),
791 abstractLayout(abstractLayout), bpit(bpit), epit(epit) {}
795 DocBookDocumentSectioning hasDocumentSectioning(ParagraphList const ¶graphs, pit_type bpit, pit_type const epit) {
796 bool documentHasSections = false;
798 while (bpit < epit) {
799 Layout const &style = paragraphs[bpit].layout();
800 documentHasSections |= isLayoutSectioning(style);
802 if (documentHasSections)
806 // Paragraphs before the first section: [ runparams.par_begin ; eppit )
808 return make_tuple(documentHasSections, bpit);
812 bool hasOnlyNotes(Paragraph const & par)
814 // Precondition: the paragraph is not empty. Otherwise, the function will always return true...
815 for (int i = 0; i < par.size(); ++i)
816 // If you find something that is not an inset (like actual text) or an inset that is not a note,
818 if (!par.isInset(i) || par.getInset(i)->lyxCode() != NOTE_CODE)
824 DocBookInfoTag getParagraphsWithInfo(ParagraphList const ¶graphs,
825 pit_type bpit, pit_type const epit,
826 // Typically, bpit is the beginning of the document and epit the end *or* the first section.
827 bool documentHasSections) {
828 set<pit_type> shouldBeInInfo;
829 set<pit_type> mustBeInInfo;
830 set<pit_type> abstractWithLayout;
831 set<pit_type> abstractNoLayout;
833 // Find the first non empty paragraph by mutating bpit.
834 while (bpit < epit) {
835 Paragraph const &par = paragraphs[bpit];
836 if (par.empty() || hasOnlyNotes(par))
842 // Traverse everything that might belong to <info>.
843 bool hasAbstractLayout = false;
844 pit_type cpit = bpit;
845 for (; cpit < epit; ++cpit) {
846 // Skip paragraphs that don't generate anything in DocBook.
847 Paragraph const & par = paragraphs[cpit];
848 if (hasOnlyNotes(par))
851 // There should never be any section here. (Just a sanity check: if this fails, this function could end up
852 // processing the whole document.)
853 if (isLayoutSectioning(par.layout())) {
854 LYXERR0("Assertion failed: section found in potential <info> paragraphs.");
858 // If this is marked as an abstract by the layout, put it in the right set.
859 if (par.layout().docbookabstract()) {
860 hasAbstractLayout = true;
861 abstractWithLayout.emplace(cpit);
865 // Based on layout information, store this paragraph in one set: should be in <info>, must be,
866 // or abstract (either because of layout or of position).
867 Layout const &style = par.layout();
869 if (style.docbookininfo() == "always")
870 mustBeInInfo.emplace(cpit);
871 else if (style.docbookininfo() == "maybe")
872 shouldBeInInfo.emplace(cpit);
873 else if (documentHasSections && !hasAbstractLayout)
874 abstractNoLayout.emplace(cpit);
875 else // This should definitely not be in <info>.
878 // Now, cpit points to the first paragraph that no more has things that could go in <info>.
879 // bpit is the beginning of the <info> part.
881 return DocBookInfoTag(shouldBeInInfo, mustBeInInfo,
882 hasAbstractLayout ? abstractWithLayout : abstractNoLayout,
883 hasAbstractLayout, bpit, cpit);
886 } // end anonymous namespace
889 xml::FontTag docbookStartFontTag(xml::FontTypes type)
891 return xml::FontTag(from_utf8(fontToDocBookTag(type)), from_utf8(fontToAttribute(type)), type);
895 xml::EndFontTag docbookEndFontTag(xml::FontTypes type)
897 return xml::EndFontTag(from_utf8(fontToDocBookTag(type)), type);
901 void outputDocBookInfo(
905 OutputParams const & runparams,
906 ParagraphList const & paragraphs,
907 DocBookInfoTag const & info)
909 // Perform an additional check on the abstract. Sometimes, there are many paragraphs that should go
910 // into the abstract, but none generates actual content. Thus, first generate to a temporary stream,
911 // then only create the <abstract> tag if these paragraphs generate some content.
912 // This check must be performed *before* a decision on whether or not to output <info> is made.
913 bool hasAbstract = !info.abstract.empty();
916 // Generate the abstract XML into a string before further checks.
917 odocstringstream os2;
919 for (auto const & p : info.abstract)
920 makeAny(text, buf, xs2, runparams, paragraphs.iterator_at(p));
922 // Actually output the abstract if there is something to do. Don't count line feeds or spaces in this,
923 // even though they must be properly output if there is some abstract.
924 abstract = os2.str();
925 docstring cleaned = abstract;
926 cleaned.erase(std::remove_if(cleaned.begin(), cleaned.end(), ::isspace), cleaned.end());
928 // Nothing? Then there is no abstract!
933 // The abstract must go in <info>. Otherwise, decide whether to open <info> based on the layouts.
934 bool needInfo = !info.mustBeInInfo.empty() || hasAbstract;
936 // Start the <info> tag if required.
938 xs.startDivision(false);
939 xs << xml::StartTag("info");
943 // Output the elements that should go in <info>, before and after the abstract.
944 for (auto pit : info.shouldBeInInfo) // Typically, the title: these elements are so important and ubiquitous
945 // that mandating a wrapper like <info> would repel users. Thus, generate them first.
946 makeAny(text, buf, xs, runparams, paragraphs.iterator_at(pit));
947 for (auto pit : info.mustBeInInfo)
948 makeAny(text, buf, xs, runparams, paragraphs.iterator_at(pit));
950 // Always output the abstract as the last item of the <info>, as it requires special treatment (especially if
951 // it contains several paragraphs that are empty).
953 if (info.abstractLayout) {
954 xs << XMLStream::ESCAPE_NONE << abstract;
957 string tag = paragraphs[*info.abstract.begin()].layout().docbookforceabstracttag();
961 if (!xs.isLastTagCR())
964 xs << xml::StartTag(tag);
966 xs << XMLStream::ESCAPE_NONE << abstract;
967 xs << xml::EndTag(tag);
972 // End the <info> tag if it was started.
974 if (!xs.isLastTagCR())
977 xs << xml::EndTag("info");
984 void docbookSimpleAllParagraphs(
988 OutputParams const & runparams)
990 // Handle the given text, supposing it has no sections (i.e. a "simple" text). The input may vary in length
991 // between a single paragraph to a whole document.
992 pit_type const bpit = runparams.par_begin;
993 pit_type const epit = runparams.par_end;
994 ParagraphList const ¶graphs = text.paragraphs();
996 // First, the <info> tag.
997 DocBookInfoTag info = getParagraphsWithInfo(paragraphs, bpit, epit, false);
998 outputDocBookInfo(text, buf, xs, runparams, paragraphs, info);
1000 // Then, the content. It starts where the <info> ends.
1001 auto par = paragraphs.iterator_at(info.epit);
1002 auto end = paragraphs.iterator_at(epit);
1003 while (par != end) {
1004 if (!hasOnlyNotes(*par))
1005 par = makeAny(text, buf, xs, runparams, par);
1012 void docbookParagraphs(Text const &text,
1015 OutputParams const &runparams) {
1016 ParagraphList const ¶graphs = text.paragraphs();
1017 if (runparams.par_begin == runparams.par_end) {
1018 runparams.par_begin = 0;
1019 runparams.par_end = paragraphs.size();
1021 pit_type bpit = runparams.par_begin;
1022 pit_type const epit = runparams.par_end;
1023 LASSERT(bpit < epit,
1025 xs << XMLStream::ESCAPE_NONE << "<!-- DocBook output error! -->\n";
1029 std::stack<std::pair<int, string>> headerLevels; // Used to determine when to open/close sections: store the depth
1030 // of the section and the tag that was used to open it.
1032 // Detect whether the document contains sections. If there are no sections, treatment is largely simplified.
1033 // In particular, there can't be an abstract, unless it is manually marked.
1034 bool documentHasSections;
1036 tie(documentHasSections, eppit) = hasDocumentSectioning(paragraphs, bpit, epit);
1038 // Deal with "simple" documents, i.e. those without sections.
1039 if (!documentHasSections) {
1040 docbookSimpleAllParagraphs(text, buf, xs, runparams);
1044 // Output the first <info> tag (or just the title).
1045 DocBookInfoTag info = getParagraphsWithInfo(paragraphs, bpit, eppit, true);
1046 outputDocBookInfo(text, buf, xs, runparams, paragraphs, info);
1049 // Then, iterate through the paragraphs of this document.
1050 bool currentlyInAppendix = false;
1052 auto par = text.paragraphs().iterator_at(bpit);
1053 auto end = text.paragraphs().iterator_at(epit);
1054 while (par != end) {
1055 OutputParams ourparams = runparams;
1057 if (par->params().startOfAppendix())
1058 currentlyInAppendix = true;
1059 if (hasOnlyNotes(*par)) {
1064 Layout const &style = par->layout();
1066 // Think about adding <section> and/or </section>s.
1067 if (isLayoutSectioning(style)) {
1068 int level = style.toclevel;
1070 // Need to close a previous section if it has the same level or a higher one (close <section> if opening a
1071 // <h2> after a <h2>, <h3>, <h4>, <h5> or <h6>). More examples:
1072 // - current: h2; back: h1; do not close any <section>
1073 // - current: h1; back: h2; close two <section> (first the <h2>, then the <h1>, so a new <h1> can come)
1074 while (!headerLevels.empty() && level <= headerLevels.top().first) {
1075 // Output the tag only if it corresponds to a legit section.
1076 int stackLevel = headerLevels.top().first;
1077 if (stackLevel != Layout::NOT_IN_TOC) {
1078 xs << xml::EndTag(headerLevels.top().second);
1084 // Open the new section: first push it onto the stack, then output it in DocBook.
1085 string sectionTag = (currentlyInAppendix && style.docbooksectiontag() == "chapter") ?
1086 "appendix" : style.docbooksectiontag();
1087 headerLevels.push(std::make_pair(level, sectionTag));
1089 // Some sectioning-like elements should not be output (such as FrontMatter).
1090 if (level != Layout::NOT_IN_TOC) {
1091 // Look for a label in the title, i.e. a InsetLabel as a child.
1092 docstring id = docstring();
1093 for (pos_type i = 0; i < par->size(); ++i) {
1094 Inset const *inset = par->getInset(i);
1096 if (auto label = dynamic_cast<InsetLabel const *>(inset)) {
1097 // Generate the attributes for the section if need be.
1098 id += "xml:id=\"" + xml::cleanID(label->screenLabel()) + "\"";
1100 // Don't output the ID as a DocBook <anchor>.
1101 ourparams.docbook_anchors_to_ignore.emplace(label->screenLabel());
1103 // Cannot have multiple IDs per tag.
1109 // Write the open tag for this section.
1113 xs << xml::StartTag(sectionTag, attrs);
1118 // Close all sections before the bibliography.
1119 // 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)?
1120 auto insetsLength = distance(par->insetList().begin(), par->insetList().end());
1121 if (insetsLength > 0) {
1122 Inset const *firstInset = par->getInset(0);
1123 if (firstInset && (firstInset->lyxCode() == BIBITEM_CODE || firstInset->lyxCode() == BIBTEX_CODE)) {
1124 while (!headerLevels.empty()) {
1125 int level = headerLevels.top().first;
1126 docstring tag = from_utf8("</" + headerLevels.top().second + ">");
1129 // Output the tag only if it corresponds to a legit section.
1130 if (level != Layout::NOT_IN_TOC) {
1131 xs << XMLStream::ESCAPE_NONE << tag;
1138 // Generate this paragraph.
1139 par = makeAny(text, buf, xs, ourparams, par);
1141 // Some special sections may require abstracts (mostly parts, in books).
1142 // 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.
1143 if (isLayoutSectioning(style) && style.docbookforceabstracttag() != "NONE") {
1144 // This abstract may be found between the next paragraph and the next title.
1145 pit_type cpit = std::distance(text.paragraphs().begin(), par);
1146 pit_type ppit = std::get<1>(hasDocumentSectioning(paragraphs, cpit, epit));
1148 // Generate this abstract (this code corresponds to parts of outputDocBookInfo).
1149 DocBookInfoTag secInfo = getParagraphsWithInfo(paragraphs, cpit, ppit, true);
1151 if (!secInfo.abstract.empty()) {
1152 xs << xml::StartTag(style.docbookforceabstracttag());
1154 for (auto const &p : secInfo.abstract)
1155 makeAny(text, buf, xs, runparams, paragraphs.iterator_at(p));
1156 xs << xml::EndTag(style.docbookforceabstracttag());
1160 // Skip all the text that just has been generated.
1161 par = paragraphs.iterator_at(ppit);
1165 // If need be, close <section>s, but only at the end of the document (otherwise, dealt with at the beginning
1167 while (!headerLevels.empty() && headerLevels.top().first > Layout::NOT_IN_TOC) {
1168 docstring tag = from_utf8("</" + headerLevels.top().second + ">");
1170 xs << XMLStream::ESCAPE_NONE << tag;