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"
33 #include "support/regex.h"
41 using namespace lyx::support;
47 std::string fontToDocBookTag(xml::FontTypes type)
50 case xml::FontTypes::FT_EMPH:
51 case xml::FontTypes::FT_BOLD:
53 case xml::FontTypes::FT_NOUN:
55 case xml::FontTypes::FT_UBAR:
56 case xml::FontTypes::FT_WAVE:
57 case xml::FontTypes::FT_DBAR:
58 case xml::FontTypes::FT_SOUT:
59 case xml::FontTypes::FT_XOUT:
60 case xml::FontTypes::FT_ITALIC:
61 case xml::FontTypes::FT_UPRIGHT:
62 case xml::FontTypes::FT_SLANTED:
63 case xml::FontTypes::FT_SMALLCAPS:
64 case xml::FontTypes::FT_ROMAN:
65 case xml::FontTypes::FT_SANS:
67 case xml::FontTypes::FT_TYPE:
69 case xml::FontTypes::FT_SIZE_TINY:
70 case xml::FontTypes::FT_SIZE_SCRIPT:
71 case xml::FontTypes::FT_SIZE_FOOTNOTE:
72 case xml::FontTypes::FT_SIZE_SMALL:
73 case xml::FontTypes::FT_SIZE_NORMAL:
74 case xml::FontTypes::FT_SIZE_LARGE:
75 case xml::FontTypes::FT_SIZE_LARGER:
76 case xml::FontTypes::FT_SIZE_LARGEST:
77 case xml::FontTypes::FT_SIZE_HUGE:
78 case xml::FontTypes::FT_SIZE_HUGER:
79 case xml::FontTypes::FT_SIZE_INCREASE:
80 case xml::FontTypes::FT_SIZE_DECREASE:
88 string fontToRole(xml::FontTypes type)
90 // Specific fonts are achieved with roles. The only common ones are "" for basic emphasis,
91 // and "bold"/"strong" for bold. With some specific options, other roles are copied into
92 // HTML output (via the DocBook XSLT sheets); otherwise, if not recognised, they are just ignored.
93 // Hence, it is not a problem to have many roles by default here.
94 // See https://www.sourceware.org/ml/docbook/2003-05/msg00269.html
96 case xml::FontTypes::FT_ITALIC:
97 case xml::FontTypes::FT_EMPH:
99 case xml::FontTypes::FT_BOLD:
101 case xml::FontTypes::FT_NOUN: // Outputs a <person>
102 case xml::FontTypes::FT_TYPE: // Outputs a <code>
104 case xml::FontTypes::FT_UBAR:
107 // All other roles are non-standard for DocBook.
109 case xml::FontTypes::FT_WAVE:
111 case xml::FontTypes::FT_DBAR:
113 case xml::FontTypes::FT_SOUT:
115 case xml::FontTypes::FT_XOUT:
117 case xml::FontTypes::FT_UPRIGHT:
119 case xml::FontTypes::FT_SLANTED:
121 case xml::FontTypes::FT_SMALLCAPS:
123 case xml::FontTypes::FT_ROMAN:
125 case xml::FontTypes::FT_SANS:
127 case xml::FontTypes::FT_SIZE_TINY:
129 case xml::FontTypes::FT_SIZE_SCRIPT:
130 return "size_script";
131 case xml::FontTypes::FT_SIZE_FOOTNOTE:
132 return "size_footnote";
133 case xml::FontTypes::FT_SIZE_SMALL:
135 case xml::FontTypes::FT_SIZE_NORMAL:
136 return "size_normal";
137 case xml::FontTypes::FT_SIZE_LARGE:
139 case xml::FontTypes::FT_SIZE_LARGER:
140 return "size_larger";
141 case xml::FontTypes::FT_SIZE_LARGEST:
142 return "size_largest";
143 case xml::FontTypes::FT_SIZE_HUGE:
145 case xml::FontTypes::FT_SIZE_HUGER:
147 case xml::FontTypes::FT_SIZE_INCREASE:
148 return "size_increase";
149 case xml::FontTypes::FT_SIZE_DECREASE:
150 return "size_decrease";
157 string fontToAttribute(xml::FontTypes type) {
158 // If there is a role (i.e. nonstandard use of a tag), output the attribute. Otherwise, the sheer tag is sufficient
160 string role = fontToRole(type);
162 return "role='" + role + "'";
169 xml::FontTag docbookStartFontTag(xml::FontTypes type)
171 return xml::FontTag(from_utf8(fontToDocBookTag(type)), from_utf8(fontToAttribute(type)), type);
175 xml::EndFontTag docbookEndFontTag(xml::FontTypes type)
177 return xml::EndFontTag(from_utf8(fontToDocBookTag(type)), type);
181 // Convenience functions to open and close tags. First, very low-level ones to ensure a consistent new-line behaviour.
185 // Contents of the block.
190 // <paratag>Contents of the paragraph.</paratag>
193 // Content before<inlinetag>Contents of the paragraph.</inlinetag>Content after
195 void openInlineTag(XMLStream & xs, const std::string & tag, const std::string & attr)
197 xs << xml::StartTag(tag, attr);
201 void closeInlineTag(XMLStream & xs, const std::string & tag)
203 xs << xml::EndTag(tag);
207 void openParTag(XMLStream & xs, const std::string & tag, const std::string & attr)
209 if (!xs.isLastTagCR())
211 xs << xml::StartTag(tag, attr);
215 void closeParTag(XMLStream & xs, const std::string & tag)
217 xs << xml::EndTag(tag);
222 void openBlockTag(XMLStream & xs, const std::string & tag, const std::string & attr)
224 if (!xs.isLastTagCR())
226 xs << xml::StartTag(tag, attr);
231 void closeBlockTag(XMLStream & xs, const std::string & tag)
233 if (!xs.isLastTagCR())
235 xs << xml::EndTag(tag);
240 void openTag(XMLStream & xs, const std::string & tag, const std::string & attr, const std::string & tagtype)
242 if (tag.empty() || tag == "NONE")
245 if (tag == "para" || tagtype == "paragraph") // Special case for <para>: always considered as a paragraph.
246 openParTag(xs, tag, attr);
247 else if (tagtype == "block")
248 openBlockTag(xs, tag, attr);
249 else if (tagtype == "inline")
250 openInlineTag(xs, tag, attr);
252 xs.writeError("Unrecognised tag type '" + tagtype + "' for '" + tag + " " + attr + "'");
256 void closeTag(XMLStream & xs, const std::string & tag, const std::string & tagtype)
258 if (tag.empty() || tag == "NONE")
261 if (tag == "para" || tagtype == "paragraph") // Special case for <para>: always considered as a paragraph.
262 closeParTag(xs, tag);
263 else if (tagtype == "block")
264 closeBlockTag(xs, tag);
265 else if (tagtype == "inline")
266 closeInlineTag(xs, tag);
268 xs.writeError("Unrecognised tag type '" + tagtype + "' for '" + tag + "'");
272 // Higher-level convenience functions.
274 void openParTag(XMLStream & xs, const Paragraph * par, const Paragraph * prevpar)
276 Layout const & lay = par->layout();
281 // When should the wrapper be opened here? Only if the previous paragraph has the SAME wrapper tag
282 // (usually, they won't have the same layout) and the CURRENT one allows merging.
283 // The main use case is author information in several paragraphs: if the name of the author is the
284 // first paragraph of an author, then merging with the previous tag does not make sense. Say the
285 // next paragraph is the affiliation, then it should be output in the same <author> tag (different
286 // layout, same wrapper tag).
287 bool openWrapper = lay.docbookwrappertag() != "NONE";
288 if (prevpar != nullptr) {
289 Layout const & prevlay = prevpar->layout();
290 if (prevlay.docbookwrappertag() != "NONE") {
291 openWrapper = prevlay.docbookwrappertag() == lay.docbookwrappertag()
292 && !lay.docbookwrappermergewithprevious();
298 openTag(xs, lay.docbookwrappertag(), lay.docbookwrapperattr(), lay.docbookwrappertagtype());
300 const string & tag = lay.docbooktag();
302 auto xmltag = xml::ParTag(tag, lay.docbookattr());
303 if (!xs.isTagOpen(xmltag, 1)) // Don't nest a paragraph directly in a paragraph.
304 // TODO: required or not?
305 // TODO: avoid creating a ParTag object just for this query...
306 openTag(xs, lay.docbooktag(), lay.docbookattr(), lay.docbooktagtype());
309 openTag(xs, lay.docbookitemtag(), lay.docbookitemattr(), lay.docbookitemtagtype());
310 openTag(xs, lay.docbookiteminnertag(), lay.docbookiteminnerattr(), lay.docbookiteminnertagtype());
314 void closeParTag(XMLStream & xs, Paragraph const * par, Paragraph const * nextpar)
319 // See comment in openParTag.
320 Layout const & lay = par->layout();
321 bool closeWrapper = lay.docbookwrappertag() != "NONE";
322 if (nextpar != nullptr) {
323 Layout const & nextlay = nextpar->layout();
324 if (nextlay.docbookwrappertag() != "NONE") {
325 closeWrapper = nextlay.docbookwrappertag() == lay.docbookwrappertag()
326 && !nextlay.docbookwrappermergewithprevious();
331 closeTag(xs, lay.docbookiteminnertag(), lay.docbookiteminnertagtype());
332 closeTag(xs, lay.docbookitemtag(), lay.docbookitemtagtype());
333 closeTag(xs, lay.docbooktag(), lay.docbooktagtype());
335 closeTag(xs, lay.docbookwrappertag(), lay.docbookwrappertagtype());
339 void openLabelTag(XMLStream & xs, Layout const & lay) // Mostly for definition lists.
341 openTag(xs, lay.docbookitemlabeltag(), lay.docbookitemlabelattr(), lay.docbookitemlabeltagtype());
345 void closeLabelTag(XMLStream & xs, Layout const & lay)
347 closeTag(xs, lay.docbookitemlabeltag(), lay.docbookitemlabeltagtype());
351 void openItemTag(XMLStream & xs, Layout const & lay)
353 openTag(xs, lay.docbookitemtag(), lay.docbookitemattr(), lay.docbookitemtagtype());
357 void closeItemTag(XMLStream & xs, Layout const & lay)
359 closeTag(xs, lay.docbookitemtag(), lay.docbookitemtagtype());
367 OutputParams const &,
368 ParagraphList::const_iterator);
371 void makeParagraphBibliography(
374 OutputParams const & runparams,
376 ParagraphList::const_iterator const & par)
378 // If this is the first paragraph in a bibliography, open the bibliography tag.
379 auto pbegin_before = text.paragraphs().getParagraphBefore(par);
380 if (pbegin_before->layout().latextype != LATEX_BIB_ENVIRONMENT) {
381 xs << xml::StartTag("bibliography");
385 // Start the precooked bibliography entry. This is very much like opening a paragraph tag.
386 // Don't forget the citation ID!
388 for (auto i = 0; i < par->size(); ++i) {
389 Inset const *ip = par->getInset(0);
390 if (ip != nullptr && ip->lyxCode() == BIBITEM_CODE) {
391 const auto * bibitem = dynamic_cast<const InsetBibitem*>(par->getInset(i));
392 attr = from_utf8("xml:id='") + bibitem->getParam("key") + from_utf8("'");
396 xs << xml::StartTag(from_utf8("bibliomixed"), attr);
398 // Generate the entry.
399 auto const begin = text.paragraphs().begin();
400 par->simpleDocBookOnePar(buf, xs, runparams, text.outerFont(std::distance(begin, par)), true, true, 0);
402 // End the precooked bibliography entry.
403 xs << xml::EndTag("bibliomixed");
406 // If this is the last paragraph in a bibliography, close the bibliography tag.
407 auto const end = text.paragraphs().end();
408 bool endBibliography = par == end;
409 if (!endBibliography) {
412 endBibliography = par->layout().latextype != LATEX_BIB_ENVIRONMENT;
415 if (endBibliography) {
416 xs << xml::EndTag("bibliography");
425 OutputParams const & runparams,
427 ParagraphList::const_iterator const & par)
429 auto const begin = text.paragraphs().begin();
430 auto const end = text.paragraphs().end();
431 auto prevpar = text.paragraphs().getParagraphBefore(par);
433 // We want to open the paragraph tag if:
434 // (i) the current layout permits multiple paragraphs
435 // (ii) we are either not already inside a paragraph (HTMLIsBlock) OR
436 // we are, but this is not the first paragraph
438 // But there is also a special case, and we first see whether we are in it.
439 // We do not want to open the paragraph tag if this paragraph contains
440 // only one item, and that item is "inline", i.e., not HTMLIsBlock (such
441 // as a branch). On the other hand, if that single item has a font change
442 // applied to it, then we still do need to open the paragraph.
444 // Obviously, this is very fragile. The main reason we need to do this is
445 // because of branches, e.g., a branch that contains an entire new section.
446 // We do not really want to wrap that whole thing in a <div>...</div>.
447 bool special_case = false;
448 Inset const *specinset = par->size() == 1 ? par->getInset(0) : nullptr;
449 if (specinset && !specinset->getLayout().htmlisblock()) { // TODO: Convert htmlisblock to a DocBook parameter?
450 Layout const &style = par->layout();
451 FontInfo const first_font = style.labeltype == LABEL_MANUAL ?
452 style.labelfont : style.font;
453 FontInfo const our_font =
454 par->getFont(buf.masterBuffer()->params(), 0,
455 text.outerFont(std::distance(begin, par))).fontInfo();
457 if (first_font == our_font)
461 // Plain layouts must be ignored.
462 if (!special_case && buf.params().documentClass().isPlainLayout(par->layout()) && !runparams.docbook_force_pars)
464 // TODO: Could get rid of this with a DocBook equivalent to htmlisblock?
465 if (!special_case && par->size() == 1 && par->getInset(0)) {
466 Inset const * firstInset = par->getInset(0);
468 // Floats cannot be in paragraphs.
469 special_case = to_utf8(firstInset->layoutName()).substr(0, 6) == "Float:";
471 // Bibliographies cannot be in paragraphs.
472 if (!special_case && firstInset->asInsetCommand())
473 special_case = firstInset->asInsetCommand()->params().getCmdName() == "bibtex";
475 // Equations do not deserve their own paragraph (DocBook allows them outside paragraphs).
476 if (!special_case && firstInset->asInsetMath())
479 // ERTs are in comments, not paragraphs.
480 if (!special_case && firstInset->lyxCode() == lyx::ERT_CODE)
483 // Listings should not get into their own paragraph.
484 if (!special_case && firstInset->lyxCode() == lyx::LISTINGS_CODE)
488 bool const open_par = runparams.docbook_make_pars
489 && !runparams.docbook_in_par
492 // We want to issue the closing tag if either:
493 // (i) We opened it, and either docbook_in_par is false,
494 // or we're not in the last paragraph, anyway.
495 // (ii) We didn't open it and docbook_in_par is true,
496 // but we are in the first par, and there is a next par.
499 bool const close_par = open_par && (!runparams.docbook_in_par);
501 // Determine if this paragraph has some real content. Things like new pages are not caught
502 // by Paragraph::empty(), even though they do not generate anything useful in DocBook.
503 odocstringstream os2;
505 par->simpleDocBookOnePar(buf, xs2, runparams, text.outerFont(distance(begin, par)), open_par, close_par, 0);
507 docstring cleaned = os2.str();
508 static const lyx::regex reg("[ \\r\\n]*");
509 cleaned = from_utf8(lyx::regex_replace(to_utf8(cleaned), reg, string("")));
511 if (!cleaned.empty()) {
513 openParTag(xs, &*par, prevpar);
515 xs << XMLStream::ESCAPE_NONE << os2.str();
518 closeParTag(xs, &*par, (nextpar != end) ? &*nextpar : nullptr);
523 void makeEnvironment(
526 OutputParams const &runparams,
528 ParagraphList::const_iterator const & par)
530 auto const end = text.paragraphs().end();
532 // Output the opening tag for this environment, but only if it has not been previously opened (condition
533 // implemented in openParTag).
534 auto prevpar = text.paragraphs().getParagraphBefore(par);
535 openParTag(xs, &*par, prevpar); // TODO: switch in layout for par/block?
537 // Generate the contents of this environment. There is a special case if this is like some environment.
538 Layout const & style = par->layout();
539 if (style.latextype == LATEX_COMMAND) {
540 // Nothing to do (otherwise, infinite loops).
541 } else if (style.latextype == LATEX_ENVIRONMENT ||
542 style.latextype == LATEX_LIST_ENVIRONMENT ||
543 style.latextype == LATEX_ITEM_ENVIRONMENT) {
544 // Open a wrapper tag if needed.
545 if (style.docbookitemwrappertag() != "NONE") {
546 xs << xml::StartTag(style.docbookitemwrappertag(), style.docbookitemwrapperattr());
550 // Generate the label, if need be. If it is taken from the text, sep != 0 and corresponds to the first
551 // character after the label.
553 if (style.labeltype != LABEL_NO_LABEL && style.docbookitemlabeltag() != "NONE") {
554 // At least one condition must be met:
555 // - this environment is not a list
556 // - if this is a list, the label must not be manual (i.e. it must be taken from the layout)
557 if (style.latextype != LATEX_LIST_ENVIRONMENT || style.labeltype != LABEL_MANUAL) {
558 // Usual cases: maybe there is something specified at the layout level. Highly unlikely, though.
559 docstring const lbl = par->params().labelString();
564 openLabelTag(xs, style);
566 closeLabelTag(xs, style);
569 // Only variablelist gets here (or similar items defined as an extension in the layout).
570 openLabelTag(xs, style);
571 sep = par->firstWordDocBook(xs, runparams);
572 closeLabelTag(xs, style);
576 // Maybe the item is completely empty, i.e. if the first word ends at the end of the current paragraph
577 // AND if the next paragraph doesn't have the same depth (if there is such a paragraph).
578 // Common case: there is only the first word on the line, but there is a nested list instead
580 bool emptyItem = false;
581 if (sep == par->size()) { // If the separator is already at the end of this paragraph...
584 if (next_par == text.paragraphs().end()) // There is no next paragraph.
586 else // There is a next paragraph: check depth.
587 emptyItem = par->params().depth() >= next_par->params().depth();
591 // Avoid having an empty item, this is not valid DocBook. A single character is enough to force
592 // generation of a full <para>.
593 // TODO: this always worked only by magic...
596 // Generate the rest of the paragraph, if need be.
597 par->simpleDocBookOnePar(buf, xs, runparams, text.outerFont(std::distance(text.paragraphs().begin(), par)),
601 makeAny(text, buf, xs, runparams, par);
604 // Close the environment.
607 closeParTag(xs, &*par, (nextpar != end) ? &*nextpar : nullptr); // TODO: switch in layout for par/block?
614 OutputParams const & runparams,
616 ParagraphList::const_iterator const & par)
618 // Unlike XHTML, no need for labels, as they are handled by DocBook tags.
619 auto const begin = text.paragraphs().begin();
620 auto const end = text.paragraphs().end();
624 // Generate this command.
625 auto prevpar = text.paragraphs().getParagraphBefore(par);
626 openParTag(xs, &*par, prevpar);
628 par->simpleDocBookOnePar(buf, xs, runparams,
629 text.outerFont(distance(begin, par)));
631 closeParTag(xs, &*par, (nextpar != end) ? &*nextpar : nullptr);
639 OutputParams const &ourparams,
640 ParagraphList::const_iterator par)
642 switch (par->layout().latextype) {
644 makeCommand(buf, xs, ourparams, text, par);
646 case LATEX_ENVIRONMENT:
647 case LATEX_LIST_ENVIRONMENT:
648 case LATEX_ITEM_ENVIRONMENT:
649 makeEnvironment(buf, xs, ourparams, text, par);
651 case LATEX_PARAGRAPH:
652 makeParagraph(buf, xs, ourparams, text, par);
654 case LATEX_BIB_ENVIRONMENT:
655 makeParagraphBibliography(buf, xs, ourparams, text, par);
660 } // end anonymous namespace
663 using DocBookDocumentSectioning = tuple<bool, pit_type>;
666 struct DocBookInfoTag
668 const set<pit_type> shouldBeInInfo;
669 const set<pit_type> mustBeInInfo;
670 const set<pit_type> abstract;
674 DocBookInfoTag(const set<pit_type> & shouldBeInInfo, const set<pit_type> & mustBeInInfo,
675 const set<pit_type> & abstract, pit_type bpit, pit_type epit) :
676 shouldBeInInfo(shouldBeInInfo), mustBeInInfo(mustBeInInfo), abstract(abstract),
677 bpit(bpit), epit(epit) {}
681 DocBookDocumentSectioning hasDocumentSectioning(ParagraphList const ¶graphs, pit_type bpit, pit_type const epit) {
682 bool documentHasSections = false;
684 while (bpit < epit) {
685 Layout const &style = paragraphs[bpit].layout();
686 documentHasSections |= style.category() == from_utf8("Sectioning");
688 if (documentHasSections)
692 // Paragraphs before the first section: [ runparams.par_begin ; eppit )
694 return make_tuple(documentHasSections, bpit);
698 bool hasOnlyNotes(Paragraph const & par)
700 // Precondition: the paragraph is not empty. Otherwise, the function will always return true...
701 for (int i = 0; i < par.size(); ++i)
702 // If you find something that is not an inset (like actual text) or an inset that is not a note,
704 if (!par.isInset(i) || !dynamic_cast<InsetNote *>(par.insetList().get(i)))
710 DocBookInfoTag getParagraphsWithInfo(ParagraphList const ¶graphs, pit_type bpit, pit_type const epit) {
711 set<pit_type> shouldBeInInfo;
712 set<pit_type> mustBeInInfo;
713 set<pit_type> abstract;
715 // Find the first non empty paragraph by mutating bpit.
716 while (bpit < epit) {
717 Paragraph const &par = paragraphs[bpit];
718 if (par.empty() || hasOnlyNotes(par))
724 // Find the last info-like paragraph.
725 pit_type cpit = bpit;
726 bool hasAbstractLayout = false;
727 while (cpit < epit) {
728 // Skip paragraphs only containing one note.
729 Paragraph const & par = paragraphs[cpit];
730 if (hasOnlyNotes(par)) {
735 if (par.layout().docbookabstract())
736 hasAbstractLayout = true;
738 // Based on layout information, store this paragraph in one set: should be in <info>, must be.
739 Layout const &style = par.layout();
741 if (style.docbookininfo() == "always") {
742 mustBeInInfo.emplace(cpit);
743 } else if (style.docbookininfo() == "maybe") {
744 shouldBeInInfo.emplace(cpit);
746 // Hypothesis: the <info> parts should be grouped together near the beginning bpit.
747 // There may be notes in between, but nothing else.
752 // Now, cpit points to the last paragraph that has things that could go in <info>.
753 // bpit is the beginning of the <info> part.
755 // Go once again through the list of paragraphs to find the abstract. If there is an abstract
756 // layout, only consider it. Otherwise, an abstract is just a sequence of paragraphs with text.
757 if (hasAbstractLayout) {
759 while (pit < cpit) { // Don't overshoot the <info> part.
760 if (paragraphs[pit].layout().docbookabstract())
761 abstract.emplace(pit);
765 pit_type lastAbstract = epit + 1; // A nonsensical value.
766 docstring lastAbstractLayout;
769 while (pit < cpit) { // Don't overshoot the <info> part.
770 const Paragraph & par = paragraphs.at(pit);
771 if (!par.insetList().empty()) {
772 for (const auto &i : par.insetList()) {
773 if (i.inset->getText(0) != nullptr) {
774 if (lastAbstract == epit + 1) {
775 // First paragraph that matches the heuristic definition of abstract.
777 lastAbstractLayout = par.layout().name();
778 } else if (pit > lastAbstract + 1 || par.layout().name() != lastAbstractLayout) {
779 // This is either too far from the last abstract paragraph or doesn't
780 // have the right layout name, BUT there has already been an abstract
781 // in this document: done with detecting the abstract.
782 goto done; // Easier to get out of two nested loops.
785 abstract.emplace(pit);
795 return DocBookInfoTag(shouldBeInInfo, mustBeInInfo, abstract, bpit, cpit);
799 void outputDocBookInfo(
803 OutputParams const & runparams,
804 ParagraphList const & paragraphs,
805 DocBookInfoTag const & info)
807 // Perform an additional check on the abstract. Sometimes, there are many paragraphs that should go
808 // into the abstract, but none generates actual content. Thus, first generate to a temporary stream,
809 // then only create the <abstract> tag if these paragraphs generate some content.
810 // This check must be performed *before* a decision on whether or not to output <info> is made.
811 bool hasAbstract = !info.abstract.empty();
814 // Generate the abstract XML into a string before further checks.
815 odocstringstream os2;
818 auto bpit = *std::min_element(info.abstract.begin(), info.abstract.end());
819 auto epit = 1 + *std::max_element(info.abstract.begin(), info.abstract.end());
820 // info.abstract is inclusive, epit is exclusive, hence +1 for looping.
822 while (bpit < epit) {
823 makeAny(text, buf, xs2, runparams, paragraphs.iterator_at(bpit));
828 // Actually output the abstract if there is something to do. Don't count line feeds or spaces in this,
829 // even though they must be properly output if there is some abstract.
830 abstract = os2.str();
831 static const lyx::regex reg("[ \\r\\n]*");
832 docstring abstractContent = from_utf8(lyx::regex_replace(to_utf8(abstract), reg, string("")));
834 // Nothing? Then there is no abstract!
835 if (abstractContent.empty())
839 // The abstract must go in <info>. Otherwise, decide whether to open <info> based on the layouts.
840 bool needInfo = !info.mustBeInInfo.empty() || hasAbstract;
842 // Start the <info> tag if required.
844 xs.startDivision(false);
845 xs << xml::StartTag("info");
849 // Output the elements that should go in <info>, before and after the abstract.
850 for (auto pit : info.shouldBeInInfo) { // Typically, the title: these elements are so important and ubiquitous
851 // that mandating a wrapper like <info> would repel users. Thus, generate them first.
852 makeAny(text, buf, xs, runparams, paragraphs.iterator_at(pit));
854 for (auto pit : info.mustBeInInfo) {
855 if (info.abstract.find(pit) == info.abstract.end()) // The abstract must be in info, but is dealt with after.
856 makeAny(text, buf, xs, runparams, paragraphs.iterator_at(pit));
859 // Always output the abstract as the last item of the <info>, as it requires special treatment (especially if
860 // it contains several paragraphs that are empty).
862 // string tag = paragraphs[*info.abstract.begin()].layout().docbookforceabstracttag();
863 // if (tag == "NONE")
866 // xs << xml::StartTag(tag);
868 xs << XMLStream::ESCAPE_NONE << abstract;
869 // xs << xml::EndTag(tag);
873 // End the <info> tag if it was started.
875 xs << xml::EndTag("info");
882 void docbookFirstParagraphs(
886 OutputParams const &runparams,
889 // Handle the beginning of the document, supposing it has sections.
890 // Major role: output the first <info> tag.
892 ParagraphList const ¶graphs = text.paragraphs();
893 pit_type bpit = runparams.par_begin;
894 DocBookInfoTag info = getParagraphsWithInfo(paragraphs, bpit, epit);
895 outputDocBookInfo(text, buf, xs, runparams, paragraphs, info);
899 void docbookSimpleAllParagraphs(
903 OutputParams const & runparams)
905 // Handle the given text, supposing it has no sections (i.e. a "simple" text). The input may vary in length
906 // between a single paragraph to a whole document.
908 // First, the <info> tag.
909 ParagraphList const ¶graphs = text.paragraphs();
910 pit_type bpit = runparams.par_begin;
911 pit_type const epit = runparams.par_end;
912 DocBookInfoTag info = getParagraphsWithInfo(paragraphs, bpit, epit);
913 outputDocBookInfo(text, buf, xs, runparams, paragraphs, info);
915 // Then, the content. It starts where the <info> ends.
917 while (bpit < epit) {
918 auto par = paragraphs.iterator_at(bpit);
919 if (!hasOnlyNotes(*par))
920 makeAny(text, buf, xs, runparams, par);
926 void docbookParagraphs(Text const &text,
929 OutputParams const &runparams) {
930 ParagraphList const ¶graphs = text.paragraphs();
931 if (runparams.par_begin == runparams.par_end) {
932 runparams.par_begin = 0;
933 runparams.par_end = paragraphs.size();
935 pit_type bpit = runparams.par_begin;
936 pit_type const epit = runparams.par_end;
939 xs << XMLStream::ESCAPE_NONE << "<!-- DocBook output error! -->\n";
943 std::stack<std::pair<int, string>> headerLevels; // Used to determine when to open/close sections: store the depth
944 // of the section and the tag that was used to open it.
946 // Detect whether the document contains sections. If there are no sections, there can be no automatically
947 // discovered abstract.
948 bool documentHasSections;
950 tie(documentHasSections, eppit) = hasDocumentSectioning(paragraphs, bpit, epit);
952 if (documentHasSections) {
953 docbookFirstParagraphs(text, buf, xs, runparams, eppit);
956 docbookSimpleAllParagraphs(text, buf, xs, runparams);
960 bool currentlyInAppendix = false;
962 while (bpit < epit) {
963 OutputParams ourparams = runparams;
965 auto par = paragraphs.iterator_at(bpit);
966 if (par->params().startOfAppendix())
967 currentlyInAppendix = true;
968 Layout const &style = par->layout();
969 ParagraphList::const_iterator const lastStartedPar = par;
970 ParagraphList::const_iterator send;
972 if (hasOnlyNotes(*par)) {
977 // Think about adding <section> and/or </section>s.
978 const bool isLayoutSectioning = style.category() == from_utf8("Sectioning");
979 if (isLayoutSectioning) {
980 int level = style.toclevel;
982 // Need to close a previous section if it has the same level or a higher one (close <section> if opening a <h2>
983 // after a <h2>, <h3>, <h4>, <h5> or <h6>). More examples:
984 // - current: h2; back: h1; do not close any <section>
985 // - current: h1; back: h2; close two <section> (first the <h2>, then the <h1>, so a new <h1> can come)
986 while (!headerLevels.empty() && level <= headerLevels.top().first) {
987 int stackLevel = headerLevels.top().first;
988 docstring stackTag = from_utf8("</" + headerLevels.top().second + ">");
991 // Output the tag only if it corresponds to a legit section.
992 if (stackLevel != Layout::NOT_IN_TOC)
993 xs << XMLStream::ESCAPE_NONE << stackTag << xml::CR();
996 // Open the new section: first push it onto the stack, then output it in DocBook.
997 string sectionTag = (currentlyInAppendix && style.docbooksectiontag() == "chapter") ?
998 "appendix" : style.docbooksectiontag();
999 headerLevels.push(std::make_pair(level, sectionTag));
1001 // Some sectioning-like elements should not be output (such as FrontMatter).
1002 if (level != Layout::NOT_IN_TOC) {
1003 // Look for a label in the title, i.e. a InsetLabel as a child.
1004 docstring id = docstring();
1005 for (pos_type i = 0; i < par->size(); ++i) {
1006 Inset const *inset = par->getInset(i);
1008 if (auto label = dynamic_cast<InsetLabel const *>(inset)) {
1009 // Generate the attributes for the section if need be.
1010 id += "xml:id=\"" + xml::cleanID(label->screenLabel()) + "\"";
1012 // Don't output the ID as a DocBook <anchor>.
1013 ourparams.docbook_anchors_to_ignore.emplace(label->screenLabel());
1015 // Cannot have multiple IDs per tag.
1021 // Write the open tag for this section.
1022 docstring tag = from_utf8("<" + sectionTag);
1024 tag += from_utf8(" ") + id;
1025 tag += from_utf8(">");
1026 xs << XMLStream::ESCAPE_NONE << tag;
1031 // Close all sections before the bibliography.
1032 // 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)?
1033 auto insetsLength = distance(par->insetList().begin(), par->insetList().end());
1034 if (insetsLength > 0) {
1035 Inset const *firstInset = par->getInset(0);
1036 if (firstInset && dynamic_cast<InsetBibtex const *>(firstInset)) {
1037 while (!headerLevels.empty()) {
1038 int level = headerLevels.top().first;
1039 docstring tag = from_utf8("</" + headerLevels.top().second + ">");
1042 // Output the tag only if it corresponds to a legit section.
1043 if (level != Layout::NOT_IN_TOC) {
1044 xs << XMLStream::ESCAPE_NONE << tag;
1051 // Generate this paragraph.
1052 makeAny(text, buf, xs, ourparams, par);
1056 // If need be, close <section>s, but only at the end of the document (otherwise, dealt with at the beginning
1058 while (!headerLevels.empty() && headerLevels.top().first > Layout::NOT_IN_TOC) {
1059 docstring tag = from_utf8("</" + headerLevels.top().second + ">");
1061 xs << XMLStream::ESCAPE_NONE << tag;