]> git.lyx.org Git - features.git/blob - src/output_docbook.cpp
DocBook: specific fix for Localization_Test.lyx.
[features.git] / src / output_docbook.cpp
1 /**
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.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author José Matos
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "output_docbook.h"
15
16 #include "Buffer.h"
17 #include "buffer_funcs.h"
18 #include "BufferParams.h"
19 #include "Font.h"
20 #include "InsetList.h"
21 #include "Paragraph.h"
22 #include "ParagraphList.h"
23 #include "ParagraphParameters.h"
24 #include "xml.h"
25 #include "Text.h"
26 #include "TextClass.h"
27
28 #include "insets/InsetBibtex.h"
29 #include "insets/InsetBibitem.h"
30 #include "insets/InsetLabel.h"
31 #include "mathed/InsetMath.h"
32 #include "insets/InsetNote.h"
33
34 #include "support/lassert.h"
35 #include "support/textutils.h"
36
37 #include <stack>
38 #include <iostream>
39 #include <algorithm>
40 #include <sstream>
41
42 using namespace std;
43 using namespace lyx::support;
44
45 namespace lyx {
46
47 namespace {
48
49 std::string fontToDocBookTag(xml::FontTypes type)
50 {
51         switch (type) {
52         case xml::FontTypes::FT_EMPH:
53         case xml::FontTypes::FT_BOLD:
54                 return "emphasis";
55         case xml::FontTypes::FT_NOUN:
56                 return "personname";
57         case xml::FontTypes::FT_UBAR:
58         case xml::FontTypes::FT_WAVE:
59         case xml::FontTypes::FT_DBAR:
60         case xml::FontTypes::FT_SOUT:
61         case xml::FontTypes::FT_XOUT:
62         case xml::FontTypes::FT_ITALIC:
63         case xml::FontTypes::FT_UPRIGHT:
64         case xml::FontTypes::FT_SLANTED:
65         case xml::FontTypes::FT_SMALLCAPS:
66         case xml::FontTypes::FT_ROMAN:
67         case xml::FontTypes::FT_SANS:
68                 return "emphasis";
69         case xml::FontTypes::FT_TYPE:
70                 return "code";
71         case xml::FontTypes::FT_SIZE_TINY:
72         case xml::FontTypes::FT_SIZE_SCRIPT:
73         case xml::FontTypes::FT_SIZE_FOOTNOTE:
74         case xml::FontTypes::FT_SIZE_SMALL:
75         case xml::FontTypes::FT_SIZE_NORMAL:
76         case xml::FontTypes::FT_SIZE_LARGE:
77         case xml::FontTypes::FT_SIZE_LARGER:
78         case xml::FontTypes::FT_SIZE_LARGEST:
79         case xml::FontTypes::FT_SIZE_HUGE:
80         case xml::FontTypes::FT_SIZE_HUGER:
81         case xml::FontTypes::FT_SIZE_INCREASE:
82         case xml::FontTypes::FT_SIZE_DECREASE:
83                 return "emphasis";
84         default:
85                 return "";
86         }
87 }
88
89
90 string fontToRole(xml::FontTypes type)
91 {
92         // Specific fonts are achieved with roles. The only common ones are "" for basic emphasis,
93         // and "bold"/"strong" for bold. With some specific options, other roles are copied into
94         // HTML output (via the DocBook XSLT sheets); otherwise, if not recognised, they are just ignored.
95         // Hence, it is not a problem to have many roles by default here.
96         // See https://www.sourceware.org/ml/docbook/2003-05/msg00269.html
97         switch (type) {
98         case xml::FontTypes::FT_ITALIC:
99         case xml::FontTypes::FT_EMPH:
100                 return "";
101         case xml::FontTypes::FT_BOLD:
102                 return "bold";
103         case xml::FontTypes::FT_NOUN: // Outputs a <person>
104         case xml::FontTypes::FT_TYPE: // Outputs a <code>
105                 return "";
106         case xml::FontTypes::FT_UBAR:
107                 return "underline";
108
109         // All other roles are non-standard for DocBook.
110
111         case xml::FontTypes::FT_WAVE:
112                 return "wave";
113         case xml::FontTypes::FT_DBAR:
114                 return "dbar";
115         case xml::FontTypes::FT_SOUT:
116                 return "sout";
117         case xml::FontTypes::FT_XOUT:
118                 return "xout";
119         case xml::FontTypes::FT_UPRIGHT:
120                 return "upright";
121         case xml::FontTypes::FT_SLANTED:
122                 return "slanted";
123         case xml::FontTypes::FT_SMALLCAPS:
124                 return "smallcaps";
125         case xml::FontTypes::FT_ROMAN:
126                 return "roman";
127         case xml::FontTypes::FT_SANS:
128                 return "sans";
129         case xml::FontTypes::FT_SIZE_TINY:
130                 return "tiny";
131         case xml::FontTypes::FT_SIZE_SCRIPT:
132                 return "size_script";
133         case xml::FontTypes::FT_SIZE_FOOTNOTE:
134                 return "size_footnote";
135         case xml::FontTypes::FT_SIZE_SMALL:
136                 return "size_small";
137         case xml::FontTypes::FT_SIZE_NORMAL:
138                 return "size_normal";
139         case xml::FontTypes::FT_SIZE_LARGE:
140                 return "size_large";
141         case xml::FontTypes::FT_SIZE_LARGER:
142                 return "size_larger";
143         case xml::FontTypes::FT_SIZE_LARGEST:
144                 return "size_largest";
145         case xml::FontTypes::FT_SIZE_HUGE:
146                 return "size_huge";
147         case xml::FontTypes::FT_SIZE_HUGER:
148                 return "size_huger";
149         case xml::FontTypes::FT_SIZE_INCREASE:
150                 return "size_increase";
151         case xml::FontTypes::FT_SIZE_DECREASE:
152                 return "size_decrease";
153         default:
154                 return "";
155         }
156 }
157
158
159 string fontToAttribute(xml::FontTypes type) {
160         // If there is a role (i.e. nonstandard use of a tag), output the attribute. Otherwise, the sheer tag is sufficient
161         // for the font.
162         string role = fontToRole(type);
163         if (!role.empty()) {
164                 return "role='" + role + "'";
165         } else {
166                 return "";
167         }
168 }
169
170
171 // Convenience functions to open and close tags. First, very low-level ones to ensure a consistent new-line behaviour.
172 // Block style:
173 //        Content before
174 //        <blocktag>
175 //          Contents of the block.
176 //        </blocktag>
177 //        Content after
178 // Paragraph style:
179 //        Content before
180 //          <paratag>Contents of the paragraph.</paratag>
181 //        Content after
182 // Inline style:
183 //    Content before<inlinetag>Contents of the paragraph.</inlinetag>Content after
184
185 void openInlineTag(XMLStream & xs, const std::string & tag, const std::string & attr)
186 {
187         xs << xml::StartTag(tag, attr);
188 }
189
190
191 void closeInlineTag(XMLStream & xs, const std::string & tag)
192 {
193         xs << xml::EndTag(tag);
194 }
195
196
197 void openParTag(XMLStream & xs, const std::string & tag, const std::string & attr)
198 {
199         if (!xs.isLastTagCR())
200                 xs << xml::CR();
201         xs << xml::StartTag(tag, attr);
202 }
203
204
205 void closeParTag(XMLStream & xs, const std::string & tag)
206 {
207         xs << xml::EndTag(tag);
208         xs << xml::CR();
209 }
210
211
212 void openBlockTag(XMLStream & xs, const std::string & tag, const std::string & attr)
213 {
214         if (!xs.isLastTagCR())
215                 xs << xml::CR();
216         xs << xml::StartTag(tag, attr);
217         xs << xml::CR();
218 }
219
220
221 void closeBlockTag(XMLStream & xs, const std::string & tag)
222 {
223         if (!xs.isLastTagCR())
224                 xs << xml::CR();
225         xs << xml::EndTag(tag);
226         xs << xml::CR();
227 }
228
229
230 void openTag(XMLStream & xs, const std::string & tag, const std::string & attr, const std::string & tagtype)
231 {
232         if (tag.empty() || tag == "NONE") // Common check to be performed elsewhere, if it was not here.
233                 return;
234
235         if (tag == "para" || tagtype == "paragraph") // Special case for <para>: always considered as a paragraph.
236                 openParTag(xs, tag, attr);
237         else if (tagtype == "block")
238                 openBlockTag(xs, tag, attr);
239         else if (tagtype == "inline")
240                 openInlineTag(xs, tag, attr);
241         else
242                 xs.writeError("Unrecognised tag type '" + tagtype + "' for '" + tag + " " + attr + "'");
243 }
244
245
246 void closeTag(XMLStream & xs, const std::string & tag, const std::string & tagtype)
247 {
248         if (tag.empty() || tag == "NONE")
249                 return;
250
251         if (tag == "para" || tagtype == "paragraph") // Special case for <para>: always considered as a paragraph.
252                 closeParTag(xs, tag);
253         else if (tagtype == "block")
254                 closeBlockTag(xs, tag);
255         else if (tagtype == "inline")
256                 closeInlineTag(xs, tag);
257         else
258                 xs.writeError("Unrecognised tag type '" + tagtype + "' for '" + tag + "'");
259 }
260
261
262 void compTag(XMLStream & xs, const std::string & tag, const std::string & attr, const std::string & tagtype)
263 {
264         if (tag.empty() || tag == "NONE")
265                 return;
266
267         // Special case for <para>: always considered as a paragraph.
268         if (tag == "para" || tagtype == "paragraph" || tagtype == "block") {
269                 if (!xs.isLastTagCR())
270                         xs << xml::CR();
271                 xs << xml::CompTag(tag, attr);
272                 xs << xml::CR();
273         } else if (tagtype == "inline") {
274                 xs << xml::CompTag(tag, attr);
275         } else {
276                 xs.writeError("Unrecognised tag type '" + tagtype + "' for '" + tag + "'");
277         }
278 }
279
280
281 // Higher-level convenience functions.
282
283 void openParTag(XMLStream & xs, const Paragraph * par, const Paragraph * prevpar)
284 {
285         if (par == prevpar)
286                 prevpar = nullptr;
287
288         // When should the wrapper be opened here? Only if the previous paragraph has the SAME wrapper tag
289         // (usually, they won't have the same layout) and the CURRENT one allows merging.
290         // The main use case is author information in several paragraphs: if the name of the author is the
291         // first paragraph of an author, then merging with the previous tag does not make sense. Say the
292         // next paragraph is the affiliation, then it should be output in the same <author> tag (different
293         // layout, same wrapper tag).
294         Layout const & lay = par->layout();
295         bool openWrapper = lay.docbookwrappertag() != "NONE";
296         if (prevpar != nullptr) {
297                 Layout const & prevlay = prevpar->layout();
298                 if (prevlay.docbookwrappertag() != "NONE") {
299                         if (prevlay.docbookwrappertag() == lay.docbookwrappertag() &&
300                                         prevlay.docbookwrapperattr() == lay.docbookwrapperattr())
301                                 openWrapper = !lay.docbookwrappermergewithprevious();
302                         else
303                                 openWrapper = true;
304                 }
305         }
306
307         // Main logic.
308         if (openWrapper)
309                 openTag(xs, lay.docbookwrappertag(), lay.docbookwrapperattr(), lay.docbookwrappertagtype());
310
311         const string & tag = lay.docbooktag();
312         if (tag != "NONE") {
313                 auto xmltag = xml::ParTag(tag, lay.docbookattr());
314                 if (!xs.isTagOpen(xmltag, 1)) { // Don't nest a paragraph directly in a paragraph.
315                         // TODO: required or not?
316                         // TODO: avoid creating a ParTag object just for this query...
317                         openTag(xs, lay.docbooktag(), lay.docbookattr(), lay.docbooktagtype());
318                         openTag(xs, lay.docbookinnertag(), lay.docbookinnerattr(), lay.docbookinnertagtype());
319                 }
320         }
321
322         openTag(xs, lay.docbookitemtag(), lay.docbookitemattr(), lay.docbookitemtagtype());
323         openTag(xs, lay.docbookiteminnertag(), lay.docbookiteminnerattr(), lay.docbookiteminnertagtype());
324 }
325
326
327 void closeParTag(XMLStream & xs, Paragraph const * par, Paragraph const * nextpar)
328 {
329         if (par == nextpar)
330                 nextpar = nullptr;
331
332         // See comment in openParTag.
333         Layout const & lay = par->layout();
334         bool closeWrapper = lay.docbookwrappertag() != "NONE";
335         if (nextpar != nullptr) {
336                 Layout const & nextlay = nextpar->layout();
337                 if (nextlay.docbookwrappertag() != "NONE") {
338                         if (nextlay.docbookwrappertag() == lay.docbookwrappertag() &&
339                                         nextlay.docbookwrapperattr() == lay.docbookwrapperattr())
340                                 closeWrapper = !nextlay.docbookwrappermergewithprevious();
341                         else
342                                 closeWrapper = true;
343                 }
344         }
345
346         // Main logic.
347         closeTag(xs, lay.docbookiteminnertag(), lay.docbookiteminnertagtype());
348         closeTag(xs, lay.docbookitemtag(), lay.docbookitemtagtype());
349         closeTag(xs, lay.docbookinnertag(), lay.docbookinnertagtype());
350         closeTag(xs, lay.docbooktag(), lay.docbooktagtype());
351         if (closeWrapper)
352                 closeTag(xs, lay.docbookwrappertag(), lay.docbookwrappertagtype());
353 }
354
355
356 void makeBibliography(
357                 Text const & text,
358                 Buffer const & buf,
359                 XMLStream & xs,
360                 OutputParams const & runparams,
361                 ParagraphList::const_iterator const & par)
362 {
363         // If this is the first paragraph in a bibliography, open the bibliography tag.
364         auto const * pbegin_before = text.paragraphs().getParagraphBefore(par);
365         if (pbegin_before == nullptr || (pbegin_before && pbegin_before->layout().latextype != LATEX_BIB_ENVIRONMENT)) {
366                 xs << xml::StartTag("bibliography");
367                 xs << xml::CR();
368         }
369
370         // Start the precooked bibliography entry. This is very much like opening a paragraph tag.
371         // Don't forget the citation ID!
372         docstring attr;
373         for (auto i = 0; i < par->size(); ++i) {
374                 Inset const *ip = par->getInset(i);
375                 if (!ip)
376                         continue;
377                 if (const auto * bibitem = dynamic_cast<const InsetBibitem*>(ip)) {
378                         auto id = xml::cleanID(bibitem->getParam("key"));
379                         attr = from_utf8("xml:id='") + id + from_utf8("'");
380                         break;
381                 }
382         }
383         xs << xml::StartTag(from_utf8("bibliomixed"), attr);
384
385         // Generate the entry. Concatenate the different parts of the paragraph if any.
386         auto const begin = text.paragraphs().begin();
387         auto pars = par->simpleDocBookOnePar(buf, runparams, text.outerFont(std::distance(begin, par)), 0);
388         for (auto & parXML : pars)
389                 xs << XMLStream::ESCAPE_NONE << parXML;
390
391         // End the precooked bibliography entry.
392         xs << xml::EndTag("bibliomixed");
393         xs << xml::CR();
394
395         // If this is the last paragraph in a bibliography, close the bibliography tag.
396         auto const end = text.paragraphs().end();
397         auto nextpar = par;
398         ++nextpar;
399         bool endBibliography = nextpar == end || nextpar->layout().latextype != LATEX_BIB_ENVIRONMENT;
400
401         if (endBibliography) {
402                 xs << xml::EndTag("bibliography");
403                 xs << xml::CR();
404         }
405 }
406
407
408 void makeParagraph(
409                 Text const & text,
410                 Buffer const & buf,
411                 XMLStream & xs,
412                 OutputParams const & runparams,
413                 ParagraphList::const_iterator const & par)
414 {
415         // If this kind of layout should be ignored, already leave.
416         if (par->layout().docbooktag() == "IGNORE")
417                 return;
418
419         // Useful variables.
420         auto const begin = text.paragraphs().begin();
421         auto const end = text.paragraphs().end();
422         auto prevpar = text.paragraphs().getParagraphBefore(par);
423
424         // We want to open the paragraph tag if:
425         //   (i) the current layout permits multiple paragraphs
426         //  (ii) we are either not already inside a paragraph (HTMLIsBlock) OR
427         //         we are, but this is not the first paragraph
428         //
429         // But there is also a special case, and we first see whether we are in it.
430         // We do not want to open the paragraph tag if this paragraph contains
431         // only one item, and that item is "inline", i.e., not HTMLIsBlock (such
432         // as a branch). On the other hand, if that single item has a font change
433         // applied to it, then we still do need to open the paragraph.
434         //
435         // Obviously, this is very fragile. The main reason we need to do this is
436         // because of branches, e.g., a branch that contains an entire new section.
437         // We do not really want to wrap that whole thing in a <div>...</div>.
438         bool special_case = false;
439         Inset const *specinset = par->size() == 1 ? par->getInset(0) : nullptr;
440         if (specinset && !specinset->getLayout().htmlisblock()) { // TODO: Convert htmlisblock to a DocBook parameter?
441                 Layout const &style = par->layout();
442                 FontInfo const first_font = style.labeltype == LABEL_MANUAL ?
443                                                                         style.labelfont : style.font;
444                 FontInfo const our_font =
445                                 par->getFont(buf.masterBuffer()->params(), 0,
446                                                          text.outerFont(std::distance(begin, par))).fontInfo();
447
448                 if (first_font == our_font)
449                         special_case = true;
450         }
451
452         size_t nInsets = std::distance(par->insetList().begin(), par->insetList().end());
453         auto parSize = (size_t) par->size();
454
455         auto isLyxCodeToIgnore = [](InsetCode x) { return x == TOC_CODE; }; // If this LyX code does not produce any output,
456         // it can be safely ignored in the following checks: if this thing is present in the paragraph, it has no impact
457         // on the definition of the special case (i.e. whether or not a <para> tag should be output).
458
459         // TODO: if a paragraph *only* contains floats, listings, bibliographies, etc., should this be considered as a
460         //  special case? If so, the code could be largely simplifies (all the calls to all_of, basically) and optimised
461         //  at the compilation stage.
462
463         // Plain layouts must be ignored.
464         special_case |= buf.params().documentClass().isPlainLayout(par->layout()) && !runparams.docbook_force_pars;
465         // Equations do not deserve their own paragraph (DocBook allows them outside paragraphs).
466         // Exception: any case that generates an <inlineequation> must still get a paragraph to be valid.
467         special_case |= nInsets == parSize && std::all_of(par->insetList().begin(), par->insetList().end(), [](InsetList::Element inset) {
468                 return inset.inset && inset.inset->asInsetMath() && inset.inset->asInsetMath()->getType() != hullSimple;
469         });
470         // Tables do not deserve their own paragraphs (DocBook allows them outside paragraphs).
471         special_case |= nInsets == parSize && std::all_of(par->insetList().begin(), par->insetList().end(), [isLyxCodeToIgnore](InsetList::Element inset) {
472                 return inset.inset->lyxCode() == TABULAR_CODE || isLyxCodeToIgnore(inset.inset->lyxCode());
473         });
474         // Floats cannot be in paragraphs.
475         special_case |= nInsets == parSize && std::all_of(par->insetList().begin(), par->insetList().end(), [isLyxCodeToIgnore](InsetList::Element inset) {
476                 return inset.inset->lyxCode() == FLOAT_CODE || isLyxCodeToIgnore(inset.inset->lyxCode());
477         });
478         // Bibliographies cannot be in paragraphs. Bibitems should still be handled as paragraphs, though
479         // (see makeParagraphBibliography).
480         special_case |= nInsets == parSize && std::all_of(par->insetList().begin(), par->insetList().end(), [isLyxCodeToIgnore](InsetList::Element inset) {
481                 return inset.inset->lyxCode() == BIBTEX_CODE || isLyxCodeToIgnore(inset.inset->lyxCode());
482         });
483         // ERTs are in comments, not paragraphs.
484         special_case |= nInsets == parSize && std::all_of(par->insetList().begin(), par->insetList().end(), [isLyxCodeToIgnore](InsetList::Element inset) {
485                 return inset.inset->lyxCode() == ERT_CODE || isLyxCodeToIgnore(inset.inset->lyxCode());
486         });
487         // Listings should not get into their own paragraph.
488         special_case |= nInsets == parSize && std::all_of(par->insetList().begin(), par->insetList().end(), [isLyxCodeToIgnore](InsetList::Element inset) {
489                 return inset.inset->lyxCode() == LISTINGS_CODE || isLyxCodeToIgnore(inset.inset->lyxCode());
490         });
491         // Boxes cannot get into their own paragraph.
492         special_case |= nInsets == parSize && std::all_of(par->insetList().begin(), par->insetList().end(), [isLyxCodeToIgnore](InsetList::Element inset) {
493                 return inset.inset->lyxCode() == BOX_CODE || isLyxCodeToIgnore(inset.inset->lyxCode());
494         });
495         // Includes should not have a paragraph.
496         special_case |= nInsets == parSize && std::all_of(par->insetList().begin(), par->insetList().end(), [isLyxCodeToIgnore](InsetList::Element inset) {
497                 return inset.inset->lyxCode() == INCLUDE_CODE || isLyxCodeToIgnore(inset.inset->lyxCode());
498         });
499         // Glossaries should not have a paragraph.
500         special_case |= nInsets == parSize && std::all_of(par->insetList().begin(), par->insetList().end(), [isLyxCodeToIgnore](InsetList::Element inset) {
501                 return inset.inset->lyxCode() == NOMENCL_PRINT_CODE || isLyxCodeToIgnore(inset.inset->lyxCode());
502         });
503
504         bool const open_par = runparams.docbook_make_pars
505                                                   && !runparams.docbook_in_par
506                                                   && !special_case;
507
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);
514
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.
520         auto nextpar = par;
521         ++nextpar;
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))
525                         continue;
526
527                 if (open_par)
528                         openParTag(xs, &*par, prevpar);
529
530                 xs << XMLStream::ESCAPE_NONE << parXML;
531
532                 if (close_par)
533                         closeParTag(xs, &*par, (nextpar != end) ? &*nextpar : nullptr);
534         }
535 }
536
537
538 void makeEnvironment(Text const &text,
539                                          Buffer const &buf,
540                      XMLStream &xs,
541                      OutputParams const &runparams,
542                      ParagraphList::const_iterator const & par)
543 {
544         // If this kind of layout should be ignored, already leave.
545         if (par->layout().docbooktag() == "IGNORE")
546                 return;
547
548         // Useful variables.
549         auto const end = text.paragraphs().end();
550         auto nextpar = par;
551         ++nextpar;
552
553         // Special cases for listing-like environments provided in layouts. This is quite ad-hoc, but provides a useful
554         // default. This should not be used by too many environments (only LyX-Code right now).
555         // This would be much simpler if LyX-Code was implemented as InsetListings...
556         bool mimicListing = false;
557         bool ignoreFonts = false;
558         if (par->layout().docbooktag() == "programlisting") {
559                 mimicListing = true;
560                 ignoreFonts = true;
561         }
562
563         // Output the opening tag for this environment, but only if it has not been previously opened (condition
564         // implemented in openParTag).
565         auto prevpar = text.paragraphs().getParagraphBefore(par);
566         openParTag(xs, &*par, prevpar); // TODO: switch in layout for par/block?
567
568         // Generate the contents of this environment. There is a special case if this is like some environment.
569         Layout const & style = par->layout();
570         if (style.latextype == LATEX_COMMAND) {
571                 // Nothing to do (otherwise, infinite loops).
572         } else if (style.latextype == LATEX_ENVIRONMENT) {
573                 // Generate the paragraph, if need be.
574                 auto pars = par->simpleDocBookOnePar(buf, runparams, text.outerFont(std::distance(text.paragraphs().begin(), par)), 0, false, ignoreFonts);
575
576                 if (mimicListing) {
577                         auto p = pars.begin();
578                         while (p != pars.end()) {
579                                 openTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnerattr(), par->layout().docbookiteminnertagtype());
580                                 xs << XMLStream::ESCAPE_NONE << *p;
581                                 closeTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnertagtype());
582                                 ++p;
583
584                                 // Insert a new line after each "paragraph" (i.e. line in the listing), except for the last one.
585                                 // Otherwise, there would one more new line in the output than in the LyX document.
586                                 if (p != pars.end())
587                                         xs << xml::CR();
588                         }
589                 } else {
590                         for (auto const & p : pars) {
591                                 openTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnerattr(), par->layout().docbookiteminnertagtype());
592                                 xs << XMLStream::ESCAPE_NONE << p;
593                                 closeTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnertagtype());
594                         }
595                 }
596         } else {
597                 makeAny(text, buf, xs, runparams, par);
598         }
599
600         // Close the environment.
601         closeParTag(xs, &*par, (nextpar != end) ? &*nextpar : nullptr); // TODO: switch in layout for par/block?
602 }
603
604
605 ParagraphList::const_iterator findEndOfEnvironment(
606                 ParagraphList::const_iterator const & pstart,
607                 ParagraphList::const_iterator const & pend)
608 {
609         // Copy-paste from XHTML. Should be factored out at some point...
610         ParagraphList::const_iterator p = pstart;
611         Layout const & bstyle = p->layout();
612         size_t const depth = p->params().depth();
613         for (++p; p != pend; ++p) {
614                 Layout const & style = p->layout();
615                 // It shouldn't happen that e.g. a section command occurs inside
616                 // a quotation environment, at a higher depth, but as of 6/2009,
617                 // it can happen. We pretend that it's just at lowest depth.
618                 if (style.latextype == LATEX_COMMAND)
619                         return p;
620
621                 // If depth is down, we're done
622                 if (p->params().depth() < depth)
623                         return p;
624
625                 // If depth is up, we're not done
626                 if (p->params().depth() > depth)
627                         continue;
628
629                 // FIXME I am not sure about the first check.
630                 // Surely we *could* have different layouts that count as
631                 // LATEX_PARAGRAPH, right?
632                 if (style.latextype == LATEX_PARAGRAPH || style != bstyle)
633                         return p;
634         }
635         return pend;
636 }
637
638
639 ParagraphList::const_iterator makeListEnvironment(Text const &text,
640                                                                                                   Buffer const &buf,
641                                                           XMLStream &xs,
642                                                           OutputParams const &runparams,
643                                                           ParagraphList::const_iterator const & begin)
644 {
645         // Useful variables.
646         auto par = begin;
647         auto const end = text.paragraphs().end();
648         auto const envend = findEndOfEnvironment(par, end);
649
650         // If this kind of layout should be ignored, already leave.
651         if (begin->layout().docbooktag() == "IGNORE") {
652                 auto nextpar = par;
653                 ++nextpar;
654                 return nextpar;
655         }
656
657         // Output the opening tag for this environment.
658         Layout const & envstyle = par->layout();
659         openTag(xs, envstyle.docbookwrappertag(), envstyle.docbookwrapperattr(), envstyle.docbookwrappertagtype());
660         openTag(xs, envstyle.docbooktag(), envstyle.docbookattr(), envstyle.docbooktagtype());
661
662         // Handle the content of the list environment, item by item.
663         while (par != envend) {
664                 // Skip this paragraph if it is both empty and the last one (otherwise, there may be deeper paragraphs after).
665                 auto nextpar = par;
666                 ++nextpar;
667                 if (par->empty() && nextpar == envend)
668                         break;
669
670                 // Open the item wrapper.
671                 Layout const & style = par->layout();
672                 openTag(xs, style.docbookitemwrappertag(), style.docbookitemwrapperattr(), style.docbookitemwrappertagtype());
673
674                 // Generate the label, if need be. If it is taken from the text, sep != 0 and corresponds to the first
675                 // character after the label.
676                 pos_type sep = 0;
677                 if (style.labeltype != LABEL_NO_LABEL && style.docbookitemlabeltag() != "NONE") {
678                         if (style.labeltype == LABEL_MANUAL) {
679                                 // Only variablelist gets here (or similar items defined as an extension in the layout).
680                                 openTag(xs, style.docbookitemlabeltag(), style.docbookitemlabelattr(), style.docbookitemlabeltagtype());
681                                 sep = 1 + par->firstWordDocBook(xs, runparams);
682                                 closeTag(xs, style.docbookitemlabeltag(), style.docbookitemlabeltagtype());
683                         } else {
684                                 // Usual cases: maybe there is something specified at the layout level. Highly unlikely, though.
685                                 docstring const lbl = par->params().labelString();
686
687                                 if (!lbl.empty()) {
688                                         openTag(xs, style.docbookitemlabeltag(), style.docbookitemlabelattr(), style.docbookitemlabeltagtype());
689                                         xs << lbl;
690                                         closeTag(xs, style.docbookitemlabeltag(), style.docbookitemlabeltagtype());
691                                 }
692                         }
693                 }
694
695                 // Open the item (after the wrapper and the label).
696                 openTag(xs, style.docbookitemtag(), style.docbookitemattr(), style.docbookitemtagtype());
697
698                 // Generate the content of the item.
699                 if (sep < par->size()) {
700                         auto pars = par->simpleDocBookOnePar(buf, runparams,
701                                                              text.outerFont(std::distance(text.paragraphs().begin(), par)), sep);
702                         for (auto &p : pars) {
703                                 openTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnerattr(),
704                                         par->layout().docbookiteminnertagtype());
705                                 xs << XMLStream::ESCAPE_NONE << p;
706                                 closeTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnertagtype());
707                         }
708                 } else {
709                         // DocBook doesn't like emptiness.
710                         compTag(xs, par->layout().docbookiteminnertag(), par->layout().docbookiteminnerattr(),
711                                 par->layout().docbookiteminnertagtype());
712                 }
713
714                 // If the next item is deeper, it must go entirely within this item (do it recursively).
715                 // By construction, with findEndOfEnvironment, depth can only stay constant or increase, never decrease.
716                 depth_type currentDepth = par->getDepth();
717                 ++par;
718                 while (par != envend && par->getDepth() != currentDepth)
719                         par = makeAny(text, buf, xs, runparams, par);
720                 // Usually, this loop only makes one iteration, except in complex scenarios, like an item with a paragraph,
721                 // a list, and another paragraph; or an item with two types of list (itemise then enumerate, for instance).
722
723                 // Close the item.
724                 closeTag(xs, style.docbookitemtag(), style.docbookitemtagtype());
725                 closeTag(xs, style.docbookitemwrappertag(), style.docbookitemwrappertagtype());
726         }
727
728         // Close this environment in exactly the same way as it was opened.
729         closeTag(xs, envstyle.docbooktag(), envstyle.docbooktagtype());
730         closeTag(xs, envstyle.docbookwrappertag(), envstyle.docbookwrappertagtype());
731
732         return envend;
733 }
734
735
736 void makeCommand(
737                 Text const & text,
738                 Buffer const & buf,
739                 XMLStream & xs,
740                 OutputParams const & runparams,
741                 ParagraphList::const_iterator const & par)
742 {
743         // If this kind of layout should be ignored, already leave.
744         if (par->layout().docbooktag() == "IGNORE")
745                 return;
746
747         // Useful variables.
748         // Unlike XHTML, no need for labels, as they are handled by DocBook tags.
749         auto const begin = text.paragraphs().begin();
750         auto const end = text.paragraphs().end();
751         auto nextpar = par;
752         ++nextpar;
753
754         // Generate this command.
755         auto prevpar = text.paragraphs().getParagraphBefore(par);
756         openParTag(xs, &*par, prevpar);
757
758         auto pars = par->simpleDocBookOnePar(buf, runparams,text.outerFont(distance(begin, par)));
759         for (auto & parXML : pars)
760                 // TODO: decide what to do with openParTag/closeParTag in new lines.
761                 xs << XMLStream::ESCAPE_NONE << parXML;
762
763         closeParTag(xs, &*par, (nextpar != end) ? &*nextpar : nullptr);
764 }
765
766
767 bool isLayoutSectioning(Layout const & lay)
768 {
769         if (lay.docbooksection()) // Special case: some DocBook styles must be handled as sections.
770                 return true;
771         else if (lay.category() == from_utf8("Sectioning") || lay.docbooktag() == "section") // Generic case.
772                 return lay.toclevel != Layout::NOT_IN_TOC;
773         return false;
774 }
775
776
777 bool isLayoutSectioningOrSimilar(Layout const & lay)
778 {
779         return isLayoutSectioning(lay) || lay.docbooktag() == "bridgehead";
780 }
781
782
783 using DocBookDocumentSectioning = tuple<bool, pit_type>;
784
785
786 struct DocBookInfoTag
787 {
788         const set<pit_type> shouldBeInInfo;
789         const set<pit_type> mustBeInInfo; // With the notable exception of the abstract!
790         const set<pit_type> abstract;
791         const bool abstractLayout;
792         pit_type bpit;
793         pit_type epit;
794
795         DocBookInfoTag(const set<pit_type> & shouldBeInInfo, const set<pit_type> & mustBeInInfo,
796                                    const set<pit_type> & abstract, bool abstractLayout, pit_type bpit, pit_type epit) :
797                                    shouldBeInInfo(shouldBeInInfo), mustBeInInfo(mustBeInInfo), abstract(abstract),
798                                    abstractLayout(abstractLayout), bpit(bpit), epit(epit) {}
799 };
800
801
802 DocBookDocumentSectioning hasDocumentSectioning(ParagraphList const &paragraphs, pit_type bpit, pit_type const epit) {
803         bool documentHasSections = false;
804
805         while (bpit < epit) {
806                 Layout const &style = paragraphs[bpit].layout();
807                 documentHasSections |= isLayoutSectioningOrSimilar(style);
808
809                 if (documentHasSections)
810                         break;
811                 bpit += 1;
812         }
813         // Paragraphs before the first section: [ runparams.par_begin ; eppit )
814
815         return make_tuple(documentHasSections, bpit);
816 }
817
818
819 bool hasOnlyNotes(Paragraph const & par)
820 {
821         // Precondition: the paragraph is not empty. Otherwise, the function will always return true...
822         for (int i = 0; i < par.size(); ++i)
823                 // If you find something that is not an inset (like actual text) or an inset that is not a note,
824                 // return false.
825                 if (!par.isInset(i) || par.getInset(i)->lyxCode() != NOTE_CODE)
826                         return false;
827
828         // An empty paragraph may still require some output.
829         if (par.layout().docbooksection())
830                 return false;
831
832         // There should be really no content here.
833         return true;
834 }
835
836
837 DocBookInfoTag getParagraphsWithInfo(ParagraphList const &paragraphs,
838                                                                          pit_type bpit, pit_type const epit,
839                                                                          // Typically, bpit is the beginning of the document and epit the end of the
840                                                                          // document *or* the first section.
841                                                                          bool documentHasSections,
842                                                                          bool detectUnlayoutedAbstract
843                                                                          // Whether paragraphs with no specific layout should be detected as abstracts.
844                                                                          // For inner sections, an abstract should only be detected if it has a specific
845                                                                          // layout. For others, anything that might look like an abstract should be sought.
846                                                                          ) {
847         set<pit_type> shouldBeInInfo;
848         set<pit_type> mustBeInInfo;
849         set<pit_type> abstractWithLayout;
850         set<pit_type> abstractNoLayout;
851
852         // Find the first non empty paragraph by mutating bpit.
853         while (bpit < epit) {
854                 Paragraph const &par = paragraphs[bpit];
855                 if (par.empty() || hasOnlyNotes(par))
856                         bpit += 1;
857                 else
858                         break;
859         }
860
861         // Traverse everything that might belong to <info>.
862         bool hasAbstractLayout = false;
863         pit_type cpit = bpit;
864         for (; cpit < epit; ++cpit) {
865                 // Skip paragraphs that don't generate anything in DocBook.
866                 Paragraph const & par = paragraphs[cpit];
867                 Layout const &style = par.layout();
868                 if (hasOnlyNotes(par))
869                         continue;
870
871                 // There should never be any section here, except for the first paragraph (a title can be part of <info>).
872                 // (Just a sanity check: if this fails, this function could end up processing the whole document.)
873                 if (cpit != bpit && isLayoutSectioningOrSimilar(par.layout())) {
874                         LYXERR0("Assertion failed: section found in potential <info> paragraphs.");
875                         break;
876                 }
877
878                 // If this is marked as an abstract by the layout, put it in the right set.
879                 if (style.docbookabstract()) {
880                         hasAbstractLayout = true;
881                         abstractWithLayout.emplace(cpit);
882                         continue;
883                 }
884
885                 // Based on layout information, store this paragraph in one set: should be in <info>, must be,
886                 // or abstract (either because of layout or of position).
887                 if (style.docbookininfo() == "always")
888                         mustBeInInfo.emplace(cpit);
889                 else if (style.docbookininfo() == "maybe")
890                         shouldBeInInfo.emplace(cpit);
891                 else if (documentHasSections && !hasAbstractLayout && detectUnlayoutedAbstract &&
892                                 (style.docbooktag() == "NONE" || style.docbooktag() == "para") &&
893                                 style.docbookwrappertag() == "NONE")
894                         // In this case, it is very likely that style.docbookininfo() == "never"! Be extra careful
895                         // about anything that gets caught here.
896                         abstractNoLayout.emplace(cpit);
897                 else // This should definitely not be in <info>.
898                         break;
899         }
900         // Now, cpit points to the first paragraph that no more has things that could go in <info>.
901         // bpit is the beginning of the <info> part.
902
903         return DocBookInfoTag(shouldBeInInfo, mustBeInInfo,
904                                               hasAbstractLayout ? abstractWithLayout : abstractNoLayout,
905                                               hasAbstractLayout, bpit, cpit);
906 }
907
908 } // end anonymous namespace
909
910
911 ParagraphList::const_iterator makeAny(Text const &text,
912                                       Buffer const &buf,
913                                       XMLStream &xs,
914                                       OutputParams const &runparams,
915                                       ParagraphList::const_iterator par)
916 {
917         switch (par->layout().latextype) {
918         case LATEX_COMMAND:
919                 makeCommand(text, buf, xs, runparams, par);
920                 break;
921         case LATEX_ENVIRONMENT:
922                 makeEnvironment(text, buf, xs, runparams, par);
923                 break;
924         case LATEX_LIST_ENVIRONMENT:
925         case LATEX_ITEM_ENVIRONMENT:
926                 // Only case when makeAny() might consume more than one paragraph.
927                 return makeListEnvironment(text, buf, xs, runparams, par);
928         case LATEX_PARAGRAPH:
929                 makeParagraph(text, buf, xs, runparams, par);
930                 break;
931         case LATEX_BIB_ENVIRONMENT:
932                 makeBibliography(text, buf, xs, runparams, par);
933                 break;
934         }
935         ++par;
936         return par;
937 }
938
939
940 xml::FontTag docbookStartFontTag(xml::FontTypes type)
941 {
942         return xml::FontTag(from_utf8(fontToDocBookTag(type)), from_utf8(fontToAttribute(type)), type);
943 }
944
945
946 xml::EndFontTag docbookEndFontTag(xml::FontTypes type)
947 {
948         return xml::EndFontTag(from_utf8(fontToDocBookTag(type)), type);
949 }
950
951
952 void outputDocBookInfo(
953                 Text const & text,
954                 Buffer const & buf,
955                 XMLStream & xs,
956                 OutputParams const & runparams,
957                 ParagraphList const & paragraphs,
958                 DocBookInfoTag const & info)
959 {
960         // Perform an additional check on the abstract. Sometimes, there are many paragraphs that should go
961         // into the abstract, but none generates actual content. Thus, first generate to a temporary stream,
962         // then only create the <abstract> tag if these paragraphs generate some content.
963         // This check must be performed *before* a decision on whether or not to output <info> is made.
964         bool hasAbstract = !info.abstract.empty();
965         docstring abstract;
966         if (hasAbstract) {
967                 // Generate the abstract XML into a string before further checks.
968                 // Usually, makeAny only generates one paragraph at a time. However, for the specific case of lists, it might
969                 // generate more than one paragraph, as indicated in the return value.
970                 odocstringstream os2;
971                 XMLStream xs2(os2);
972
973                 set<pit_type> doneParas;
974                 for (auto const & p : info.abstract) {
975                         if (doneParas.find(p) == doneParas.end()) {
976                                 auto oldPar = paragraphs.iterator_at(p);
977                                 auto newPar = makeAny(text, buf, xs2, runparams, oldPar);
978
979                                 // Insert the indices of all the paragraphs that were just generated (typically, one).
980                                 // **Make the hypothesis that, when an abstract has a list, all its items are consecutive.**
981                                 pit_type id = p;
982                                 while (oldPar != newPar) {
983                                         doneParas.emplace(id);
984                                         ++oldPar;
985                                         ++id;
986                                 }
987                         }
988                 }
989
990                 // Actually output the abstract if there is something to do. Don't count line feeds or spaces in this,
991                 // even though they must be properly output if there is some abstract.
992                 abstract = os2.str();
993                 docstring cleaned = abstract;
994                 cleaned.erase(std::remove_if(cleaned.begin(), cleaned.end(), lyx::isSpace), cleaned.end());
995
996                 // Nothing? Then there is no abstract!
997                 if (cleaned.empty())
998                         hasAbstract = false;
999         }
1000
1001         // The abstract must go in <info>. Otherwise, decide whether to open <info> based on the layouts.
1002         bool needInfo = !info.mustBeInInfo.empty() || hasAbstract;
1003
1004         // Start the <info> tag if required.
1005         if (needInfo) {
1006                 xs.startDivision(false);
1007                 xs << xml::StartTag("info");
1008                 xs << xml::CR();
1009         }
1010
1011         // Output the elements that should go in <info>, before and after the abstract.
1012         for (auto pit : info.shouldBeInInfo) // Typically, the title: these elements are so important and ubiquitous
1013                 // that mandating a wrapper like <info> would repel users. Thus, generate them first.
1014                 makeAny(text, buf, xs, runparams, paragraphs.iterator_at(pit));
1015         for (auto pit : info.mustBeInInfo)
1016                 makeAny(text, buf, xs, runparams, paragraphs.iterator_at(pit));
1017
1018         // If there is no title, generate one (required for the document to be valid).
1019         // This code is called for the main document, for table cells, etc., so be precise in this condition.
1020         if (text.isMainText() && info.shouldBeInInfo.empty() && !runparams.inInclude) {
1021                 xs << xml::StartTag("title");
1022                 xs << "Untitled Document";
1023                 xs << xml::EndTag("title");
1024                 xs << xml::CR();
1025         }
1026
1027         // Always output the abstract as the last item of the <info>, as it requires special treatment (especially if
1028         // it contains several paragraphs that are empty).
1029         if (hasAbstract) {
1030                 if (info.abstractLayout) {
1031                         xs << XMLStream::ESCAPE_NONE << abstract;
1032                         xs << xml::CR();
1033                 } else {
1034                         string tag = paragraphs[*info.abstract.begin()].layout().docbookforceabstracttag();
1035                         if (tag == "NONE")
1036                                 tag = "abstract";
1037
1038                         if (!xs.isLastTagCR())
1039                                 xs << xml::CR();
1040
1041                         xs << xml::StartTag(tag);
1042                         xs << xml::CR();
1043                         xs << XMLStream::ESCAPE_NONE << abstract;
1044                         xs << xml::EndTag(tag);
1045                         xs << xml::CR();
1046                 }
1047         }
1048
1049         // End the <info> tag if it was started.
1050         if (needInfo) {
1051                 if (!xs.isLastTagCR())
1052                         xs << xml::CR();
1053
1054                 xs << xml::EndTag("info");
1055                 xs << xml::CR();
1056                 xs.endDivision();
1057         }
1058 }
1059
1060
1061 void docbookSimpleAllParagraphs(
1062                 Text const & text,
1063                 Buffer const & buf,
1064                 XMLStream & xs,
1065                 OutputParams const & runparams)
1066 {
1067         // Handle the given text, supposing it has no sections (i.e. a "simple" text). The input may vary in length
1068         // between a single paragraph to a whole document.
1069         pit_type const bpit = runparams.par_begin;
1070         pit_type const epit = runparams.par_end;
1071         ParagraphList const &paragraphs = text.paragraphs();
1072
1073         // First, the <info> tag.
1074         DocBookInfoTag info = getParagraphsWithInfo(paragraphs, bpit, epit, false, true);
1075         outputDocBookInfo(text, buf, xs, runparams, paragraphs, info);
1076
1077         // Then, the content. It starts where the <info> ends.
1078         auto par = paragraphs.iterator_at(info.epit);
1079         auto end = paragraphs.iterator_at(epit);
1080         while (par != end) {
1081                 if (!hasOnlyNotes(*par))
1082                         par = makeAny(text, buf, xs, runparams, par);
1083                 else
1084                         ++par;
1085         }
1086 }
1087
1088
1089 void docbookParagraphs(Text const &text,
1090                                            Buffer const &buf,
1091                                            XMLStream &xs,
1092                                            OutputParams const &runparams) {
1093         ParagraphList const &paragraphs = text.paragraphs();
1094         if (runparams.par_begin == runparams.par_end) {
1095                 runparams.par_begin = 0;
1096                 runparams.par_end = paragraphs.size();
1097         }
1098         pit_type bpit = runparams.par_begin;
1099         pit_type const epit = runparams.par_end;
1100         LASSERT(bpit < epit,
1101                         {
1102                                 xs << XMLStream::ESCAPE_NONE << "<!-- DocBook output error! -->\n";
1103                                 return;
1104                         });
1105
1106         std::stack<std::pair<int, string>> headerLevels; // Used to determine when to open/close sections: store the depth
1107         // of the section and the tag that was used to open it.
1108
1109         // Detect whether the document contains sections. If there are no sections, treatment is largely simplified.
1110         // In particular, there can't be an abstract, unless it is manually marked.
1111         bool documentHasSections;
1112         pit_type eppit;
1113         tie(documentHasSections, eppit) = hasDocumentSectioning(paragraphs, bpit, epit);
1114
1115         // Deal with "simple" documents, i.e. those without sections.
1116         if (!documentHasSections) {
1117                 docbookSimpleAllParagraphs(text, buf, xs, runparams);
1118                 return;
1119         }
1120
1121         // Output the first <info> tag (or just the title).
1122         DocBookInfoTag info = getParagraphsWithInfo(paragraphs, bpit, eppit, true, true);
1123         outputDocBookInfo(text, buf, xs, runparams, paragraphs, info);
1124         bpit = info.epit;
1125
1126         // Then, iterate through the paragraphs of this document.
1127         bool currentlyInAppendix = false;
1128
1129         auto par = text.paragraphs().iterator_at(bpit);
1130         auto end = text.paragraphs().iterator_at(epit);
1131         while (par != end) {
1132                 OutputParams ourparams = runparams;
1133
1134                 if (par->params().startOfAppendix())
1135                         currentlyInAppendix = true;
1136                 if (hasOnlyNotes(*par)) {
1137                         ++par;
1138                         continue;
1139                 }
1140
1141                 Layout const &style = par->layout();
1142
1143                 // Think about adding <section> and/or </section>s.
1144                 if (isLayoutSectioning(style)) {
1145                         int level = style.toclevel;
1146
1147                         // Need to close a previous section if it has the same level or a higher one (close <section> if opening a
1148                         // <h2> after a <h2>, <h3>, <h4>, <h5> or <h6>). More examples:
1149                         //   - current: h2; back: h1; do not close any <section>
1150                         //   - current: h1; back: h2; close two <section> (first the <h2>, then the <h1>, so a new <h1> can come)
1151                         while (!headerLevels.empty() && level <= headerLevels.top().first) {
1152                                 // Output the tag only if it corresponds to a legit section.
1153                                 int stackLevel = headerLevels.top().first;
1154                                 if (stackLevel != Layout::NOT_IN_TOC) {
1155                                         xs << xml::EndTag(headerLevels.top().second);
1156                                         xs << xml::CR();
1157                                 }
1158                                 headerLevels.pop();
1159                         }
1160
1161                         // Open the new section: first push it onto the stack, then output it in DocBook.
1162                         string sectionTag = (currentlyInAppendix && style.docbooksectiontag() == "chapter") ?
1163                                                                 "appendix" : style.docbooksectiontag();
1164                         headerLevels.push(std::make_pair(level, sectionTag));
1165
1166                         // Some sectioning-like elements should not be output (such as FrontMatter).
1167                         if (level != Layout::NOT_IN_TOC) {
1168                                 // Look for a label in the title, i.e. a InsetLabel as a child.
1169                                 docstring id = docstring();
1170                                 for (pos_type i = 0; i < par->size(); ++i) {
1171                                         Inset const *inset = par->getInset(i);
1172                                         if (inset) {
1173                                                 if (auto label = dynamic_cast<InsetLabel const *>(inset)) {
1174                                                         // Generate the attributes for the section if need be.
1175                                                         id += "xml:id=\"" + xml::cleanID(label->screenLabel()) + "\"";
1176
1177                                                         // Don't output the ID as a DocBook <anchor>.
1178                                                         ourparams.docbook_anchors_to_ignore.emplace(label->screenLabel());
1179
1180                                                         // Cannot have multiple IDs per tag. If there is another ID inset in the document, it will
1181                                                         // be output as a DocBook anchor.
1182                                                         break;
1183                                                 }
1184                                         }
1185                                 }
1186
1187                                 // Write the open tag for this section.
1188                                 docstring attrs;
1189                                 if (!id.empty())
1190                                         attrs = id;
1191                                 xs << xml::StartTag(sectionTag, attrs);
1192                                 xs << xml::CR();
1193                         }
1194                 }
1195
1196                 // Close all sections before the bibliography.
1197                 // 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)?
1198                 if (!par->insetList().empty()) {
1199                         Inset const *firstInset = par->getInset(0);
1200                         if (firstInset && (firstInset->lyxCode() == BIBITEM_CODE || firstInset->lyxCode() == BIBTEX_CODE)) {
1201                                 while (!headerLevels.empty()) {
1202                                         int level = headerLevels.top().first;
1203                                         docstring tag = from_utf8("</" + headerLevels.top().second + ">");
1204                                         headerLevels.pop();
1205
1206                                         // Output the tag only if it corresponds to a legit section.
1207                                         if (level != Layout::NOT_IN_TOC) {
1208                                                 xs << XMLStream::ESCAPE_NONE << tag;
1209                                                 xs << xml::CR();
1210                                         }
1211                                 }
1212                         }
1213                 }
1214
1215                 // Generate the <info> tag if a section was just opened.
1216                 // Some sections may require abstracts (mostly parts, in books: DocBookForceAbstractTag will not be NONE),
1217                 // others can still have an abstract (it must be detected so that it can be output at the right place).
1218                 // 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.
1219                 if (isLayoutSectioning(style)) {
1220                         // This abstract may be found between the next paragraph and the next title.
1221                         pit_type cpit = std::distance(text.paragraphs().begin(), par);
1222                         pit_type ppit = std::get<1>(hasDocumentSectioning(paragraphs, cpit + 1L, epit));
1223
1224                         // Generate this abstract (this code corresponds to parts of outputDocBookInfo).
1225                         DocBookInfoTag secInfo = getParagraphsWithInfo(paragraphs, cpit, ppit, true,
1226                                                                                                   style.docbookforceabstracttag() != "NONE");
1227
1228                         if (!secInfo.mustBeInInfo.empty() || !secInfo.shouldBeInInfo.empty() || !secInfo.abstract.empty()) {
1229                                 // Generate the <info>, if required. If DocBookForceAbstractTag != NONE, this abstract will not be in
1230                                 // <info>, unlike other ("standard") abstracts.
1231                                 bool hasStandardAbstract = !secInfo.abstract.empty() && style.docbookforceabstracttag() == "NONE";
1232                                 bool needInfo = !secInfo.mustBeInInfo.empty() || hasStandardAbstract;
1233
1234                                 if (needInfo) {
1235                                         xs.startDivision(false);
1236                                         xs << xml::StartTag("info");
1237                                         xs << xml::CR();
1238                                 }
1239
1240                                 // Output the elements that should go in <info>, before and after the abstract.
1241                                 for (auto pit : secInfo.shouldBeInInfo) // Typically, the title: these elements are so important and ubiquitous
1242                                         // that mandating a wrapper like <info> would repel users. Thus, generate them first.
1243                                         makeAny(text, buf, xs, ourparams, paragraphs.iterator_at(pit));
1244                                 for (auto pit : secInfo.mustBeInInfo)
1245                                         makeAny(text, buf, xs, ourparams, paragraphs.iterator_at(pit));
1246
1247                                 // Deal with the abstract in <info> if it is standard (i.e. its tag is <abstract>).
1248                                 if (!secInfo.abstract.empty() && hasStandardAbstract) {
1249                                         if (!secInfo.abstractLayout) {
1250                                                 xs << xml::StartTag("abstract");
1251                                                 xs << xml::CR();
1252                                         }
1253
1254                                         for (auto const &p : secInfo.abstract)
1255                                                 makeAny(text, buf, xs, ourparams, paragraphs.iterator_at(p));
1256
1257                                         if (!secInfo.abstractLayout) {
1258                                                 xs << xml::EndTag("abstract");
1259                                                 xs << xml::CR();
1260                                         }
1261                                 }
1262
1263                                 // End the <info> tag if it was started.
1264                                 if (needInfo) {
1265                                         if (!xs.isLastTagCR())
1266                                                 xs << xml::CR();
1267
1268                                         xs << xml::EndTag("info");
1269                                         xs << xml::CR();
1270                                         xs.endDivision();
1271                                 }
1272
1273                                 // Deal with the abstract outside <info> if it is not standard (i.e. its tag is layout-defined).
1274                                 if (!secInfo.abstract.empty() && !hasStandardAbstract) {
1275                                         // Assert: style.docbookforceabstracttag() != NONE.
1276                                         xs << xml::StartTag(style.docbookforceabstracttag());
1277                                         xs << xml::CR();
1278                                         for (auto const &p : secInfo.abstract)
1279                                                 makeAny(text, buf, xs, ourparams, paragraphs.iterator_at(p));
1280                                         xs << xml::EndTag(style.docbookforceabstracttag());
1281                                         xs << xml::CR();
1282                                 }
1283
1284                                 // Skip all the text that has just been generated.
1285                                 par = paragraphs.iterator_at(secInfo.epit);
1286                         } else {
1287                                 // No <info> tag to generate, proceed as for normal paragraphs.
1288                                 par = makeAny(text, buf, xs, ourparams, par);
1289                         }
1290                 } else {
1291                         // Generate this paragraph, as it has nothing special.
1292                         par = makeAny(text, buf, xs, ourparams, par);
1293                 }
1294         }
1295
1296         // If need be, close <section>s, but only at the end of the document (otherwise, dealt with at the beginning
1297         // of the loop).
1298         while (!headerLevels.empty() && headerLevels.top().first > Layout::NOT_IN_TOC) {
1299                 docstring tag = from_utf8("</" + headerLevels.top().second + ">");
1300                 headerLevels.pop();
1301                 xs << XMLStream::ESCAPE_NONE << tag;
1302                 xs << xml::CR();
1303         }
1304 }
1305
1306 } // namespace lyx