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")
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 // Higher-level convenience functions.
260 void openParTag(XMLStream & xs, const Paragraph * par, const Paragraph * prevpar)
262 Layout const & lay = par->layout();
267 // When should the wrapper be opened here? Only if the previous paragraph has the SAME wrapper tag
268 // (usually, they won't have the same layout) and the CURRENT one allows merging.
269 // The main use case is author information in several paragraphs: if the name of the author is the
270 // first paragraph of an author, then merging with the previous tag does not make sense. Say the
271 // next paragraph is the affiliation, then it should be output in the same <author> tag (different
272 // layout, same wrapper tag).
273 bool openWrapper = lay.docbookwrappertag() != "NONE";
274 if (prevpar != nullptr) {
275 Layout const & prevlay = prevpar->layout();
276 if (prevlay.docbookwrappertag() != "NONE") {
277 openWrapper = prevlay.docbookwrappertag() == lay.docbookwrappertag()
278 && !lay.docbookwrappermergewithprevious();
284 openTag(xs, lay.docbookwrappertag(), lay.docbookwrapperattr(), lay.docbookwrappertagtype());
286 const string & tag = lay.docbooktag();
288 auto xmltag = xml::ParTag(tag, lay.docbookattr());
289 if (!xs.isTagOpen(xmltag, 1)) // Don't nest a paragraph directly in a paragraph.
290 // TODO: required or not?
291 // TODO: avoid creating a ParTag object just for this query...
292 openTag(xs, lay.docbooktag(), lay.docbookattr(), lay.docbooktagtype());
295 openTag(xs, lay.docbookitemtag(), lay.docbookitemattr(), lay.docbookitemtagtype());
296 openTag(xs, lay.docbookiteminnertag(), lay.docbookiteminnerattr(), lay.docbookiteminnertagtype());
300 void closeParTag(XMLStream & xs, Paragraph const * par, Paragraph const * nextpar)
305 // See comment in openParTag.
306 Layout const & lay = par->layout();
307 bool closeWrapper = lay.docbookwrappertag() != "NONE";
308 if (nextpar != nullptr) {
309 Layout const & nextlay = nextpar->layout();
310 if (nextlay.docbookwrappertag() != "NONE") {
311 if (nextpar->getDepth() == par->getDepth()) {
312 // Same depth: the basic condition applies.
313 closeWrapper = nextlay.docbookwrappertag() == lay.docbookwrappertag()
314 && !nextlay.docbookwrappermergewithprevious();
315 } else if (nextpar->getDepth() > par->getDepth()) {
316 // The next paragraph is deeper: no need to close the wrapper, only to open it (cf. openParTag).
319 // This paragraph is deeper than the next one: close the wrapper,
320 // disregarding docbookwrappermergewithprevious.
321 // Hypothesis: nextlay.docbookwrappertag() == lay.docbookwrappertag(). TODO: THIS IS WRONG! Loop back until a layout with the right depth is found?
322 closeWrapper = 1L + (long long) par->getDepth() - (long long) nextpar->getDepth(); // > 0, as nextpar->getDepth() < par->getDepth()
325 if (nextpar->getDepth() == par->getDepth()) {
326 // This is not wrapped: this must be the rest of the item, still within the wrapper.
328 } else if (nextpar->getDepth() > par->getDepth()) {
329 // The next paragraph is deeper: no need to close the wrapper, only to open it (cf. openParTag).
332 // This paragraph is deeper than the next one: close the wrapper,
333 // disregarding docbookwrappermergewithprevious.
334 // Hypothesis: nextlay.docbookwrappertag() == lay.docbookwrappertag(). TODO: THIS IS WRONG! Loop back until a layout with the right depth is found?
335 closeWrapper = 1L + (long long) par->getDepth() - (long long) nextpar->getDepth(); // > 0, as nextpar->getDepth() < par->getDepth()
341 closeTag(xs, lay.docbookiteminnertag(), lay.docbookiteminnertagtype());
342 closeTag(xs, lay.docbookitemtag(), lay.docbookitemtagtype());
343 closeTag(xs, lay.docbooktag(), lay.docbooktagtype());
345 closeTag(xs, lay.docbookwrappertag(), lay.docbookwrappertagtype());
349 void openLabelTag(XMLStream & xs, Layout const & lay) // Mostly for definition lists.
351 openTag(xs, lay.docbookitemlabeltag(), lay.docbookitemlabelattr(), lay.docbookitemlabeltagtype());
355 void closeLabelTag(XMLStream & xs, Layout const & lay)
357 closeTag(xs, lay.docbookitemlabeltag(), lay.docbookitemlabeltagtype());
361 void openItemTag(XMLStream & xs, Layout const & lay)
363 openTag(xs, lay.docbookitemtag(), lay.docbookitemattr(), lay.docbookitemtagtype());
367 void closeItemTag(XMLStream & xs, Layout const & lay)
369 closeTag(xs, lay.docbookitemtag(), lay.docbookitemtagtype());
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();
421 bool endBibliography = par == end;
422 if (!endBibliography) {
425 endBibliography = par->layout().latextype != LATEX_BIB_ENVIRONMENT;
428 if (endBibliography) {
429 xs << xml::EndTag("bibliography");
439 OutputParams const & runparams,
440 ParagraphList::const_iterator const & par)
442 auto const begin = text.paragraphs().begin();
443 auto const end = text.paragraphs().end();
444 auto prevpar = text.paragraphs().getParagraphBefore(par);
446 // We want to open the paragraph tag if:
447 // (i) the current layout permits multiple paragraphs
448 // (ii) we are either not already inside a paragraph (HTMLIsBlock) OR
449 // we are, but this is not the first paragraph
451 // But there is also a special case, and we first see whether we are in it.
452 // We do not want to open the paragraph tag if this paragraph contains
453 // only one item, and that item is "inline", i.e., not HTMLIsBlock (such
454 // as a branch). On the other hand, if that single item has a font change
455 // applied to it, then we still do need to open the paragraph.
457 // Obviously, this is very fragile. The main reason we need to do this is
458 // because of branches, e.g., a branch that contains an entire new section.
459 // We do not really want to wrap that whole thing in a <div>...</div>.
460 bool special_case = false;
461 Inset const *specinset = par->size() == 1 ? par->getInset(0) : nullptr;
462 if (specinset && !specinset->getLayout().htmlisblock()) { // TODO: Convert htmlisblock to a DocBook parameter?
463 Layout const &style = par->layout();
464 FontInfo const first_font = style.labeltype == LABEL_MANUAL ?
465 style.labelfont : style.font;
466 FontInfo const our_font =
467 par->getFont(buf.masterBuffer()->params(), 0,
468 text.outerFont(std::distance(begin, par))).fontInfo();
470 if (first_font == our_font)
474 // Plain layouts must be ignored.
475 if (!special_case && buf.params().documentClass().isPlainLayout(par->layout()) && !runparams.docbook_force_pars)
477 // TODO: Could get rid of this with a DocBook equivalent to htmlisblock?
478 if (!special_case && par->size() == 1 && par->getInset(0)) {
479 Inset const * firstInset = par->getInset(0);
481 // Floats cannot be in paragraphs.
482 special_case = to_utf8(firstInset->layoutName()).substr(0, 6) == "Float:";
484 // Bibliographies cannot be in paragraphs.
485 if (!special_case && firstInset->asInsetCommand())
486 special_case = firstInset->asInsetCommand()->params().getCmdName() == "bibtex";
488 // Equations do not deserve their own paragraph (DocBook allows them outside paragraphs).
489 if (!special_case && firstInset->asInsetMath())
492 // ERTs are in comments, not paragraphs.
493 if (!special_case && firstInset->lyxCode() == lyx::ERT_CODE)
496 // Listings should not get into their own paragraph.
497 if (!special_case && firstInset->lyxCode() == lyx::LISTINGS_CODE)
501 bool const open_par = runparams.docbook_make_pars
502 && !runparams.docbook_in_par
505 // We want to issue the closing tag if either:
506 // (i) We opened it, and either docbook_in_par is false,
507 // or we're not in the last paragraph, anyway.
508 // (ii) We didn't open it and docbook_in_par is true,
509 // but we are in the first par, and there is a next par.
512 bool const close_par = open_par && (!runparams.docbook_in_par);
514 // Determine if this paragraph has some real content. Things like new pages are not caught
515 // by Paragraph::empty(), even though they do not generate anything useful in DocBook.
516 // Thus, remove all spaces (including new lines: \r, \n) before checking for emptiness.
517 // std::all_of allows doing this check without having to copy the string.
518 // Open and close tags around each contained paragraph.
519 auto pars = par->simpleDocBookOnePar(buf, runparams, text.outerFont(distance(begin, par)), 0);
520 for (auto & parXML : pars) {
521 if (!std::all_of(parXML.begin(), parXML.end(), ::isspace)) {
523 openParTag(xs, &*par, prevpar);
525 xs << XMLStream::ESCAPE_NONE << parXML;
528 closeParTag(xs, &*par, (nextpar != end) ? &*nextpar : nullptr);
534 void makeEnvironment(Text const &text,
537 OutputParams const &runparams,
538 ParagraphList::const_iterator const & par)
540 // TODO: simplify me!
541 auto const end = text.paragraphs().end();
543 // Output the opening tag for this environment, but only if it has not been previously opened (condition
544 // implemented in openParTag).
545 auto prevpar = text.paragraphs().getParagraphBefore(par);
546 openParTag(xs, &*par, prevpar); // TODO: switch in layout for par/block?
548 // Generate the contents of this environment. There is a special case if this is like some environment.
549 Layout const & style = par->layout();
550 if (style.latextype == LATEX_COMMAND) {
551 // Nothing to do (otherwise, infinite loops).
552 } else if (style.latextype == LATEX_ENVIRONMENT) {
553 // Open a wrapper tag if needed.
554 if (style.docbookitemwrappertag() != "NONE")
555 openTag(xs, style.docbookitemwrappertag(), style.docbookitemwrapperattr(), style.docbookitemwrappertagtype());
557 // Generate the label, if need be. If it is taken from the text, sep != 0 and corresponds to the first
558 // character after the label.
560 if (style.labeltype != LABEL_NO_LABEL && style.docbookitemlabeltag() != "NONE") {
561 // At least one condition must be met:
562 // - this environment is not a list
563 // - if this is a list, the label must not be manual (i.e. it must be taken from the layout)
564 if (style.latextype != LATEX_LIST_ENVIRONMENT || style.labeltype != LABEL_MANUAL) {
565 // Usual cases: maybe there is something specified at the layout level. Highly unlikely, though.
566 docstring const lbl = par->params().labelString();
571 openLabelTag(xs, style);
573 closeLabelTag(xs, style);
576 // Only variablelist gets here (or similar items defined as an extension in the layout).
577 openLabelTag(xs, style);
578 sep = par->firstWordDocBook(xs, runparams);
579 closeLabelTag(xs, style);
583 // Maybe the item is completely empty, i.e. if the first word ends at the end of the current paragraph
584 // AND if the next paragraph doesn't have the same depth (if there is such a paragraph).
585 // Common case: there is only the first word on the line, but there is a nested list instead
587 bool emptyItem = false;
588 if (sep == par->size()) { // If the separator is already at the end of this paragraph...
591 if (next_par == text.paragraphs().end()) // There is no next paragraph.
593 else // There is a next paragraph: check depth.
594 emptyItem = par->params().depth() >= next_par->params().depth();
598 // Avoid having an empty item, this is not valid DocBook. A single character is enough to force
599 // generation of a full <para>.
600 // TODO: this always worked only by magic...
603 // Generate the rest of the paragraph, if need be. Open as many inner tags as necessary.
604 auto pars = par->simpleDocBookOnePar(buf, runparams, text.outerFont(std::distance(text.paragraphs().begin(), par)), sep);
605 auto p = pars.begin();
607 xs << XMLStream::ESCAPE_NONE << *p;
609 if (p != pars.end()) {
610 closeTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnertagtype());
611 openTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnerattr(), par->layout().docbookiteminnertagtype());
617 makeAny(text, buf, xs, runparams, par);
620 // Close the environment.
623 closeParTag(xs, &*par, (nextpar != end) ? &*nextpar : nullptr); // TODO: switch in layout for par/block?
627 ParagraphList::const_iterator findEndOfEnvironment(
628 ParagraphList::const_iterator const & pstart,
629 ParagraphList::const_iterator const & pend)
631 // Copy-paste from XHTML. Should be factored out at some point...
633 ParagraphList::const_iterator p = pstart;
634 Layout const & bstyle = p->layout();
635 size_t const depth = p->params().depth();
636 for (++p; p != pend; ++p) {
637 Layout const & style = p->layout();
638 // It shouldn't happen that e.g. a section command occurs inside
639 // a quotation environment, at a higher depth, but as of 6/2009,
640 // it can happen. We pretend that it's just at lowest depth.
641 if (style.latextype == LATEX_COMMAND)
644 // If depth is down, we're done
645 if (p->params().depth() < depth)
648 // If depth is up, we're not done
649 if (p->params().depth() > depth)
652 // FIXME I am not sure about the first check.
653 // Surely we *could* have different layouts that count as
654 // LATEX_PARAGRAPH, right?
655 if (style.latextype == LATEX_PARAGRAPH || style != bstyle)
662 ParagraphList::const_iterator makeListEnvironment(Text const &text,
665 OutputParams const &runparams,
666 ParagraphList::const_iterator const & par)
667 >>>>>>> be6480e59c... DocBook: same refactoring for docbookSimpleAllParagraphs.
669 auto const end = text.paragraphs().end();
671 // Output the opening tag for this environment, but only if it has not been previously opened (condition
672 // implemented in openParTag).
673 auto prevpar = text.paragraphs().getParagraphBefore(par);
674 openParTag(xs, &*par, prevpar); // TODO: switch in layout for par/block?
676 // Generate the contents of this environment. There is a special case if this is like some environment.
677 Layout const & style = par->layout();
678 if (style.latextype == LATEX_COMMAND) {
679 // Nothing to do (otherwise, infinite loops).
680 } else if (style.latextype == LATEX_ENVIRONMENT ||
681 style.latextype == LATEX_LIST_ENVIRONMENT ||
682 style.latextype == LATEX_ITEM_ENVIRONMENT) {
683 // Open a wrapper tag if needed.
684 if (style.docbookitemwrappertag() != "NONE")
685 openTag(xs, style.docbookitemwrappertag(), style.docbookitemwrapperattr(), style.docbookitemwrappertagtype());
687 // Generate the label, if need be. If it is taken from the text, sep != 0 and corresponds to the first
688 // character after the label.
690 if (style.labeltype != LABEL_NO_LABEL && style.docbookitemlabeltag() != "NONE") {
691 // At least one condition must be met:
692 // - this environment is not a list
693 // - if this is a list, the label must not be manual (i.e. it must be taken from the layout)
694 if (style.latextype != LATEX_LIST_ENVIRONMENT || style.labeltype != LABEL_MANUAL) {
695 // Usual cases: maybe there is something specified at the layout level. Highly unlikely, though.
696 docstring const lbl = par->params().labelString();
701 openLabelTag(xs, style);
703 closeLabelTag(xs, style);
706 // Only variablelist gets here (or similar items defined as an extension in the layout).
707 openLabelTag(xs, style);
708 sep = par->firstWordDocBook(xs, runparams);
709 closeLabelTag(xs, style);
713 // Maybe the item is completely empty, i.e. if the first word ends at the end of the current paragraph
714 // AND if the next paragraph doesn't have the same depth (if there is such a paragraph).
715 // Common case: there is only the first word on the line, but there is a nested list instead
717 bool emptyItem = false;
718 if (sep == par->size()) { // If the separator is already at the end of this paragraph...
721 if (next_par == text.paragraphs().end()) // There is no next paragraph.
723 else // There is a next paragraph: check depth.
724 emptyItem = par->params().depth() >= next_par->params().depth();
728 // Avoid having an empty item, this is not valid DocBook. A single character is enough to force
729 // generation of a full <para>.
730 // TODO: this always worked only by magic...
733 // Generate the rest of the paragraph, if need be. Open as many inner tags as necessary.
734 auto pars = par->simpleDocBookOnePar(buf, runparams, text.outerFont(std::distance(text.paragraphs().begin(), par)), sep);
735 auto p = pars.begin();
737 xs << XMLStream::ESCAPE_NONE << *p;
739 if (p != pars.end()) {
740 closeTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnertagtype());
741 openTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnerattr(), par->layout().docbookiteminnertagtype());
747 makeAny(text, buf, xs, runparams, par);
750 // Close the environment.
753 closeParTag(xs, &*par, (nextpar != end) ? &*nextpar : nullptr); // TODO: switch in layout for par/block?
761 OutputParams const & runparams,
762 ParagraphList::const_iterator const & par)
764 // Unlike XHTML, no need for labels, as they are handled by DocBook tags.
765 auto const begin = text.paragraphs().begin();
766 auto const end = text.paragraphs().end();
770 // Generate this command.
771 auto prevpar = text.paragraphs().getParagraphBefore(par);
772 openParTag(xs, &*par, prevpar);
774 auto pars = par->simpleDocBookOnePar(buf, runparams,text.outerFont(distance(begin, par)));
775 for (auto & parXML : pars)
776 // TODO: decide what to do with openParTag/closeParTag in new lines.
777 xs << XMLStream::ESCAPE_NONE << parXML;
779 closeParTag(xs, &*par, (nextpar != end) ? &*nextpar : nullptr);
783 ParagraphList::const_iterator makeAny(Text const &text,
786 OutputParams const &runparams,
787 ParagraphList::const_iterator par)
789 switch (par->layout().latextype) {
791 makeCommand(text, buf, xs, runparams, par);
793 case LATEX_ENVIRONMENT:
794 case LATEX_LIST_ENVIRONMENT:
795 case LATEX_ITEM_ENVIRONMENT:
796 makeEnvironment(buf, xs, ourparams, text, par);
798 case LATEX_PARAGRAPH:
799 makeParagraph(text, buf, xs, runparams, par);
801 case LATEX_BIB_ENVIRONMENT:
802 makeBibliography(text, buf, xs, runparams, par);
808 using DocBookDocumentSectioning = tuple<bool, pit_type>;
811 struct DocBookInfoTag
813 const set<pit_type> shouldBeInInfo;
814 const set<pit_type> mustBeInInfo;
815 const set<pit_type> abstract;
819 DocBookInfoTag(const set<pit_type> & shouldBeInInfo, const set<pit_type> & mustBeInInfo,
820 const set<pit_type> & abstract, pit_type bpit, pit_type epit) :
821 shouldBeInInfo(shouldBeInInfo), mustBeInInfo(mustBeInInfo), abstract(abstract),
822 bpit(bpit), epit(epit) {}
826 DocBookDocumentSectioning hasDocumentSectioning(ParagraphList const ¶graphs, pit_type bpit, pit_type const epit) {
827 bool documentHasSections = false;
829 while (bpit < epit) {
830 Layout const &style = paragraphs[bpit].layout();
831 documentHasSections |= style.category() == from_utf8("Sectioning");
833 if (documentHasSections)
837 // Paragraphs before the first section: [ runparams.par_begin ; eppit )
839 return make_tuple(documentHasSections, bpit);
843 bool hasOnlyNotes(Paragraph const & par)
845 // Precondition: the paragraph is not empty. Otherwise, the function will always return true...
846 for (int i = 0; i < par.size(); ++i)
847 // If you find something that is not an inset (like actual text) or an inset that is not a note,
849 if (!par.isInset(i) || !dynamic_cast<InsetNote *>(par.insetList().get(i)))
855 DocBookInfoTag getParagraphsWithInfo(ParagraphList const ¶graphs, pit_type bpit, pit_type const epit) {
856 set<pit_type> shouldBeInInfo;
857 set<pit_type> mustBeInInfo;
858 set<pit_type> abstract;
860 // Find the first non empty paragraph by mutating bpit.
861 while (bpit < epit) {
862 Paragraph const &par = paragraphs[bpit];
863 if (par.empty() || hasOnlyNotes(par))
869 // Find the last info-like paragraph.
870 pit_type cpit = bpit;
871 bool hasAbstractLayout = false;
872 while (cpit < epit) {
873 // Skip paragraphs only containing one note.
874 Paragraph const & par = paragraphs[cpit];
875 if (hasOnlyNotes(par)) {
880 if (par.layout().docbookabstract())
881 hasAbstractLayout = true;
883 // Based on layout information, store this paragraph in one set: should be in <info>, must be.
884 Layout const &style = par.layout();
886 if (style.docbookininfo() == "always") {
887 mustBeInInfo.emplace(cpit);
888 } else if (style.docbookininfo() == "maybe") {
889 shouldBeInInfo.emplace(cpit);
891 // Hypothesis: the <info> parts should be grouped together near the beginning bpit.
892 // There may be notes in between, but nothing else.
897 // Now, cpit points to the last paragraph that has things that could go in <info>.
898 // bpit is the beginning of the <info> part.
900 // Go once again through the list of paragraphs to find the abstract. If there is an abstract
901 // layout, only consider it. Otherwise, an abstract is just a sequence of paragraphs with text.
902 if (hasAbstractLayout) {
904 while (pit < cpit) { // Don't overshoot the <info> part.
905 if (paragraphs[pit].layout().docbookabstract())
906 abstract.emplace(pit);
910 pit_type lastAbstract = epit + 1; // A nonsensical value.
911 docstring lastAbstractLayout;
914 while (pit < cpit) { // Don't overshoot the <info> part.
915 const Paragraph & par = paragraphs.at(pit);
916 if (!par.insetList().empty()) {
917 for (const auto &i : par.insetList()) {
918 if (i.inset->getText(0) != nullptr) {
919 if (lastAbstract == epit + 1) {
920 // First paragraph that matches the heuristic definition of abstract.
922 lastAbstractLayout = par.layout().name();
923 } else if (pit > lastAbstract + 1 || par.layout().name() != lastAbstractLayout) {
924 // This is either too far from the last abstract paragraph or doesn't
925 // have the right layout name, BUT there has already been an abstract
926 // in this document: done with detecting the abstract.
927 goto done; // Easier to get out of two nested loops.
930 abstract.emplace(pit);
940 return DocBookInfoTag(shouldBeInInfo, mustBeInInfo, abstract, bpit, cpit);
943 } // end anonymous namespace
946 xml::FontTag docbookStartFontTag(xml::FontTypes type)
948 return xml::FontTag(from_utf8(fontToDocBookTag(type)), from_utf8(fontToAttribute(type)), type);
952 xml::EndFontTag docbookEndFontTag(xml::FontTypes type)
954 return xml::EndFontTag(from_utf8(fontToDocBookTag(type)), type);
958 void outputDocBookInfo(
962 OutputParams const & runparams,
963 ParagraphList const & paragraphs,
964 DocBookInfoTag const & info)
966 // Perform an additional check on the abstract. Sometimes, there are many paragraphs that should go
967 // into the abstract, but none generates actual content. Thus, first generate to a temporary stream,
968 // then only create the <abstract> tag if these paragraphs generate some content.
969 // This check must be performed *before* a decision on whether or not to output <info> is made.
970 bool hasAbstract = !info.abstract.empty();
973 // Generate the abstract XML into a string before further checks.
974 odocstringstream os2;
977 auto bpit = *std::min_element(info.abstract.begin(), info.abstract.end());
978 auto epit = 1 + *std::max_element(info.abstract.begin(), info.abstract.end());
979 // info.abstract is inclusive, epit is exclusive, hence +1 for looping.
981 while (bpit < epit) {
982 makeAny(text, buf, xs2, runparams, paragraphs.iterator_at(bpit));
987 // Actually output the abstract if there is something to do. Don't count line feeds or spaces in this,
988 // even though they must be properly output if there is some abstract.
989 abstract = os2.str();
990 docstring cleaned = abstract;
991 cleaned.erase(std::remove_if(cleaned.begin(), cleaned.end(), ::isspace), cleaned.end());
993 // Nothing? Then there is no abstract!
998 // The abstract must go in <info>. Otherwise, decide whether to open <info> based on the layouts.
999 bool needInfo = !info.mustBeInInfo.empty() || hasAbstract;
1001 // Start the <info> tag if required.
1003 xs.startDivision(false);
1004 xs << xml::StartTag("info");
1008 // Output the elements that should go in <info>, before and after the abstract.
1009 for (auto pit : info.shouldBeInInfo) { // Typically, the title: these elements are so important and ubiquitous
1010 // that mandating a wrapper like <info> would repel users. Thus, generate them first.
1011 makeAny(text, buf, xs, runparams, paragraphs.iterator_at(pit));
1013 for (auto pit : info.mustBeInInfo) {
1014 if (info.abstract.find(pit) == info.abstract.end()) // The abstract must be in info, but is dealt with after.
1015 makeAny(text, buf, xs, runparams, paragraphs.iterator_at(pit));
1018 // Always output the abstract as the last item of the <info>, as it requires special treatment (especially if
1019 // it contains several paragraphs that are empty).
1021 // string tag = paragraphs[*info.abstract.begin()].layout().docbookforceabstracttag();
1022 // if (tag == "NONE")
1023 // tag = "abstract";
1025 // xs << xml::StartTag(tag);
1027 xs << XMLStream::ESCAPE_NONE << abstract;
1028 // xs << xml::EndTag(tag);
1032 // End the <info> tag if it was started.
1034 xs << xml::EndTag("info");
1041 void docbookFirstParagraphs(
1045 OutputParams const &runparams,
1048 // Handle the beginning of the document, supposing it has sections.
1049 // Major role: output the first <info> tag.
1051 ParagraphList const ¶graphs = text.paragraphs();
1052 pit_type bpit = runparams.par_begin;
1053 DocBookInfoTag info = getParagraphsWithInfo(paragraphs, bpit, epit);
1054 outputDocBookInfo(text, buf, xs, runparams, paragraphs, info);
1058 void docbookSimpleAllParagraphs(
1062 OutputParams const & runparams)
1064 // Handle the given text, supposing it has no sections (i.e. a "simple" text). The input may vary in length
1065 // between a single paragraph to a whole document.
1067 // First, the <info> tag.
1068 ParagraphList const ¶graphs = text.paragraphs();
1069 pit_type bpit = runparams.par_begin;
1070 pit_type const epit = runparams.par_end;
1071 DocBookInfoTag info = getParagraphsWithInfo(paragraphs, bpit, epit);
1072 outputDocBookInfo(text, buf, xs, runparams, paragraphs, info);
1074 // Then, the content. It starts where the <info> ends.
1075 auto par = text.paragraphs().iterator_at(info.epit);
1076 auto end = text.paragraphs().iterator_at(epit);
1077 while (par != end) {
1078 if (!hasOnlyNotes(*par))
1079 par = makeAny(text, buf, xs, runparams, par);
1086 void docbookParagraphs(Text const &text,
1089 OutputParams const &runparams) {
1090 ParagraphList const ¶graphs = text.paragraphs();
1091 if (runparams.par_begin == runparams.par_end) {
1092 runparams.par_begin = 0;
1093 runparams.par_end = paragraphs.size();
1095 pit_type bpit = runparams.par_begin;
1096 pit_type const epit = runparams.par_end;
1097 LASSERT(bpit < epit,
1099 xs << XMLStream::ESCAPE_NONE << "<!-- DocBook output error! -->\n";
1103 std::stack<std::pair<int, string>> headerLevels; // Used to determine when to open/close sections: store the depth
1104 // of the section and the tag that was used to open it.
1106 // Detect whether the document contains sections. If there are no sections, there can be no automatically
1107 // discovered abstract.
1108 bool documentHasSections;
1110 tie(documentHasSections, eppit) = hasDocumentSectioning(paragraphs, bpit, epit);
1112 if (documentHasSections) {
1113 docbookFirstParagraphs(text, buf, xs, runparams, eppit);
1116 docbookSimpleAllParagraphs(text, buf, xs, runparams);
1120 bool currentlyInAppendix = false;
1122 auto par = text.paragraphs().iterator_at(bpit);
1123 auto end = text.paragraphs().iterator_at(epit);
1124 while (par != end) {
1125 OutputParams ourparams = runparams;
1127 if (par->params().startOfAppendix())
1128 currentlyInAppendix = true;
1129 if (hasOnlyNotes(*par)) {
1134 Layout const &style = par->layout();
1136 // Think about adding <section> and/or </section>s.
1137 const bool isLayoutSectioning = style.category() == from_utf8("Sectioning");
1138 if (isLayoutSectioning) {
1139 int level = style.toclevel;
1141 // Need to close a previous section if it has the same level or a higher one (close <section> if opening a <h2>
1142 // after a <h2>, <h3>, <h4>, <h5> or <h6>). More examples:
1143 // - current: h2; back: h1; do not close any <section>
1144 // - current: h1; back: h2; close two <section> (first the <h2>, then the <h1>, so a new <h1> can come)
1145 while (!headerLevels.empty() && level <= headerLevels.top().first) {
1146 int stackLevel = headerLevels.top().first;
1147 docstring stackTag = from_utf8("</" + headerLevels.top().second + ">");
1150 // Output the tag only if it corresponds to a legit section.
1151 if (stackLevel != Layout::NOT_IN_TOC)
1152 xs << XMLStream::ESCAPE_NONE << stackTag << xml::CR();
1155 // Open the new section: first push it onto the stack, then output it in DocBook.
1156 string sectionTag = (currentlyInAppendix && style.docbooksectiontag() == "chapter") ?
1157 "appendix" : style.docbooksectiontag();
1158 headerLevels.push(std::make_pair(level, sectionTag));
1160 // Some sectioning-like elements should not be output (such as FrontMatter).
1161 if (level != Layout::NOT_IN_TOC) {
1162 // Look for a label in the title, i.e. a InsetLabel as a child.
1163 docstring id = docstring();
1164 for (pos_type i = 0; i < par->size(); ++i) {
1165 Inset const *inset = par->getInset(i);
1167 if (auto label = dynamic_cast<InsetLabel const *>(inset)) {
1168 // Generate the attributes for the section if need be.
1169 id += "xml:id=\"" + xml::cleanID(label->screenLabel()) + "\"";
1171 // Don't output the ID as a DocBook <anchor>.
1172 ourparams.docbook_anchors_to_ignore.emplace(label->screenLabel());
1174 // Cannot have multiple IDs per tag.
1180 // Write the open tag for this section.
1181 docstring tag = from_utf8("<" + sectionTag);
1183 tag += from_utf8(" ") + id;
1184 tag += from_utf8(">");
1185 xs << XMLStream::ESCAPE_NONE << tag;
1190 // Close all sections before the bibliography.
1191 // 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)?
1192 auto insetsLength = distance(par->insetList().begin(), par->insetList().end());
1193 if (insetsLength > 0) {
1194 Inset const *firstInset = par->getInset(0);
1195 if (firstInset && dynamic_cast<InsetBibtex const *>(firstInset)) {
1196 while (!headerLevels.empty()) {
1197 int level = headerLevels.top().first;
1198 docstring tag = from_utf8("</" + headerLevels.top().second + ">");
1201 // Output the tag only if it corresponds to a legit section.
1202 if (level != Layout::NOT_IN_TOC) {
1203 xs << XMLStream::ESCAPE_NONE << tag;
1210 // Generate this paragraph.
1211 makeAny(text, buf, xs, ourparams, par);
1214 // If need be, close <section>s, but only at the end of the document (otherwise, dealt with at the beginning
1216 while (!headerLevels.empty() && headerLevels.top().first > Layout::NOT_IN_TOC) {
1217 docstring tag = from_utf8("</" + headerLevels.top().second + ">");
1219 xs << XMLStream::ESCAPE_NONE << tag;