]> git.lyx.org Git - lyx.git/blobdiff - src/output_docbook.cpp
Fix #10328.
[lyx.git] / src / output_docbook.cpp
index 443004143444106cffa8b6c907b7185ea7cc6d7e..5e15edcee9b47b84d50432e5bd0f6d29f903f064 100644 (file)
@@ -50,7 +50,7 @@ namespace lyx {
 
 namespace {
 
-std::string fontToDocBookTag(xml::FontTypes type)
+std::string const fontToDocBookTag(xml::FontTypes type)
 {
        switch (type) {
        case xml::FontTypes::FT_EMPH:
@@ -90,7 +90,6 @@ std::string fontToDocBookTag(xml::FontTypes type)
        }
 }
 
-
 string fontToRole(xml::FontTypes type)
 {
        // Specific fonts are achieved with roles. The only common ones are "" for basic emphasis,
@@ -104,13 +103,14 @@ string fontToRole(xml::FontTypes type)
                return "";
        case xml::FontTypes::FT_BOLD:
                return "bold";
-       case xml::FontTypes::FT_NOUN: // Outputs a <person>
-       case xml::FontTypes::FT_TYPE: // Outputs a <code>
-               return "";
+       case xml::FontTypes::FT_NOUN:
+               return ""; // Outputs a <person>
+       case xml::FontTypes::FT_TYPE:
+               return ""; // Outputs a <code>
        case xml::FontTypes::FT_UBAR:
                return "underline";
 
-       // All other roles are non-standard for DocBook.
+               // All other roles are non-standard for DocBook.
 
        case xml::FontTypes::FT_WAVE:
                return "wave";
@@ -189,71 +189,28 @@ namespace {
 
 // convenience functions
 
-void openParTag(XMLStream & xs, const Paragraph * par, const Paragraph * prevpar)
+void openParTag(XMLStream &xs, Layout const &lay)
 {
-       Layout const & lay = par->layout();
-
-       if (par == prevpar)
-               prevpar = nullptr;
-
-       // When should the wrapper be opened here? Only if the previous paragraph has the SAME wrapper tag
-       // (usually, they won't have the same layout) and the CURRENT one allows merging.
-       // The main use case is author information in several paragraphs: if the name of the author is the
-       // first paragraph of an author, then merging with the previous tag does not make sense. Say the
-       // next paragraph is the affiliation, then it should be output in the same <author> tag (different
-       // layout, same wrapper tag).
-       bool openWrapper = lay.docbookwrappertag() != "NONE";
-       if (prevpar != nullptr) {
-               Layout const & prevlay = prevpar->layout();
-               if (prevlay.docbookwrappertag() != "NONE") {
-                       openWrapper = prevlay.docbookwrappertag() == lay.docbookwrappertag()
-                                       && !lay.docbookwrappermergewithprevious();
-               }
-       }
-
-       // Main logic.
-       if (openWrapper)
+       if (lay.docbookwrappertag() != "NONE") {
                xs << xml::StartTag(lay.docbookwrappertag(), lay.docbookwrapperattr());
+       }
 
        string tag = lay.docbooktag();
        if (tag == "Plain Layout")
                tag = "para";
 
-       if (!xs.isTagOpen(xml::ParTag(tag, lay.docbookattr()), 1)) // Don't nest a paragraph directly in a paragraph.
-               xs << xml::ParTag(tag, lay.docbookattr());
-
-       if (lay.docbookitemtag() != "NONE")
-               xs << xml::StartTag(lay.docbookitemtag(), lay.docbookitemattr());
+       xs << xml::ParTag(tag, lay.docbookattr());
 }
 
 
-void closeTag(XMLStream & xs, Paragraph const * par, Paragraph const * nextpar)
+void closeTag(XMLStream &xs, Layout const &lay)
 {
-       Layout const & lay = par->layout();
-
-       if (par == nextpar)
-               nextpar = nullptr;
-
-       // See comment in openParTag.
-       bool closeWrapper = lay.docbookwrappertag() != "NONE";
-       if (nextpar != nullptr) {
-               Layout const & nextlay = nextpar->layout();
-               if (nextlay.docbookwrappertag() != "NONE") {
-                       closeWrapper = nextlay.docbookwrappertag() == lay.docbookwrappertag()
-                                       && !nextlay.docbookwrappermergewithprevious();
-               }
-       }
-
-       // Main logic.
-       if (lay.docbookitemtag() != "NONE")
-               xs << xml::EndTag(lay.docbookitemtag());
-
        string tag = lay.docbooktag();
        if (tag == "Plain Layout")
                tag = "para";
 
        xs << xml::EndTag(tag);
-       if (closeWrapper)
+       if (lay.docbookwrappertag() != "NONE")
                xs << xml::EndTag(lay.docbookwrappertag());
 }
 
@@ -271,14 +228,14 @@ void closeLabelTag(XMLStream & xs, Layout const & lay)
 }
 
 
-void openItemTag(XMLStream & xs, Layout const & lay)
+void openItemTag(XMLStream &xs, Layout const &lay)
 {
        xs << xml::StartTag(lay.docbookitemtag(), lay.docbookitemattr());
 }
 
 
 // Return true when new elements are output in a paragraph, false otherwise.
-bool openInnerItemTag(XMLStream & xs, Layout const & lay)
+bool openInnerItemTag(XMLStream &xs, Layout const &lay)
 {
        if (lay.docbookiteminnertag() != "NONE") {
                xs << xml::CR();
@@ -292,7 +249,7 @@ bool openInnerItemTag(XMLStream & xs, Layout const & lay)
 }
 
 
-void closeInnerItemTag(XMLStream & xs, Layout const & lay)
+void closeInnerItemTag(XMLStream &xs, Layout const &lay)
 {
        if (lay.docbookiteminnertag()!= "NONE") {
                xs << xml::EndTag(lay.docbookiteminnertag());
@@ -301,7 +258,7 @@ void closeInnerItemTag(XMLStream & xs, Layout const & lay)
 }
 
 
-inline void closeItemTag(XMLStream & xs, Layout const & lay)
+inline void closeItemTag(XMLStream &xs, Layout const &lay)
 {
        xs << xml::EndTag(lay.docbookitemtag());
        xs << xml::CR();
@@ -309,11 +266,10 @@ inline void closeItemTag(XMLStream & xs, Layout const & lay)
 
 // end of convenience functions
 
-ParagraphList::const_iterator findLast(
+ParagraphList::const_iterator findLastParagraph(
                ParagraphList::const_iterator p,
-               ParagraphList::const_iterator const & pend,
-               LatexType type) {
-       for (++p; p != pend && p->layout().latextype == type; ++p);
+               ParagraphList::const_iterator const & pend) {
+       for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p);
 
        return p;
 }
@@ -332,8 +288,8 @@ ParagraphList::const_iterator findEndOfEnvironment(
                ParagraphList::const_iterator const & pend)
 {
        ParagraphList::const_iterator p = pstart;
+       Layout const &bstyle = p->layout();
        size_t const depth = p->params().depth();
-
        for (++p; p != pend; ++p) {
                Layout const &style = p->layout();
                // It shouldn't happen that e.g. a section command occurs inside
@@ -353,10 +309,9 @@ ParagraphList::const_iterator findEndOfEnvironment(
                // FIXME I am not sure about the first check.
                // Surely we *could* have different layouts that count as
                // LATEX_PARAGRAPH, right?
-               if (style.latextype == LATEX_PARAGRAPH || style != p->layout())
+               if (style.latextype == LATEX_PARAGRAPH || style != bstyle)
                        return p;
        }
-
        return pend;
 }
 
@@ -433,12 +388,11 @@ ParagraphList::const_iterator makeParagraphs(
                ParagraphList::const_iterator const & pbegin,
                ParagraphList::const_iterator const & pend)
 {
-       auto const begin = text.paragraphs().begin();
-       auto const end = text.paragraphs().end();
+       ParagraphList::const_iterator const begin = text.paragraphs().begin();
        ParagraphList::const_iterator par = pbegin;
-       ParagraphList::const_iterator prevpar = pbegin;
+       for (; par != pend; ++par) {
+               Layout const &lay = par->layout();
 
-       for (; par != pend; prevpar = par, ++par) {
                // We want to open the paragraph tag if:
                //   (i) the current layout permits multiple paragraphs
                //  (ii) we are either not already inside a paragraph (HTMLIsBlock) OR
@@ -454,7 +408,7 @@ ParagraphList::const_iterator makeParagraphs(
                // because of branches, e.g., a branch that contains an entire new section.
                // We do not really want to wrap that whole thing in a <div>...</div>.
                bool special_case = false;
-               Inset const *specinset = par->size() == 1 ? par->getInset(0) : nullptr;
+               Inset const *specinset = par->size() == 1 ? par->getInset(0) : 0;
                if (specinset && !specinset->getLayout().htmlisblock()) { // TODO: Convert htmlisblock to a DocBook parameter?
                        Layout const &style = par->layout();
                        FontInfo const first_font = style.labeltype == LABEL_MANUAL ?
@@ -468,7 +422,7 @@ ParagraphList::const_iterator makeParagraphs(
                }
 
                // Plain layouts must be ignored.
-               if (!special_case && buf.params().documentClass().isPlainLayout(par->layout()) && !runparams.docbook_force_pars)
+               if (!special_case && buf.params().documentClass().isPlainLayout(lay) && !runparams.docbook_force_pars)
                        special_case = true;
                // TODO: Could get rid of this with a DocBook equivalent to htmlisblock?
                if (!special_case && par->size() == 1 && par->getInset(0)) {
@@ -503,7 +457,7 @@ ParagraphList::const_iterator makeParagraphs(
                //              or we're not in the last paragraph, anyway.
                //   (ii) We didn't open it and docbook_in_par is true,
                //              but we are in the first par, and there is a next par.
-               auto nextpar = par;
+               ParagraphList::const_iterator nextpar = par;
                ++nextpar;
                bool const close_par =
                                ((open_par && (!runparams.docbook_in_par || nextpar != pend))
@@ -521,12 +475,12 @@ ParagraphList::const_iterator makeParagraphs(
 
                if (!cleaned.empty()) {
                        if (open_par)
-                               openParTag(xs, &*par, &*prevpar);
+                               openParTag(xs, lay);
 
                        xs << XMLStream::ESCAPE_NONE << os2.str();
 
                        if (close_par) {
-                               closeTag(xs, &*par, (nextpar != end) ? &*nextpar : nullptr);
+                               closeTag(xs, lay);
                                xs << xml::CR();
                        }
                }
@@ -550,43 +504,23 @@ ParagraphList::const_iterator makeEnvironment(
                ParagraphList::const_iterator const & pbegin,
                ParagraphList::const_iterator const & pend)
 {
-       auto const begin = text.paragraphs().begin();
-       auto const end = text.paragraphs().end();
+       ParagraphList::const_iterator const begin = text.paragraphs().begin();
        ParagraphList::const_iterator par = pbegin;
+       Layout const &bstyle = par->layout();
        depth_type const origdepth = pbegin->params().depth();
 
-       // Output the opening tag for this environment.
-       {
-               // Find the previous paragraph.
-               auto prevpar = begin;
-               if (prevpar != par) {
-                       auto prevpar_next = prevpar;
-                       ++prevpar_next;
-
-                       while (prevpar_next != par) {
-                               ++prevpar_next;
-                               ++prevpar;
-                       }
-               }
-
-               // Open tag for this environment.
-               openParTag(xs, &*par, &*prevpar);
-               xs << xml::CR();
-       }
+       // open tag for this environment
+       openParTag(xs, bstyle);
+       xs << xml::CR();
 
        // we will on occasion need to remember a layout from before.
        Layout const *lastlay = nullptr;
-       auto prevpar = par;
 
        while (par != pend) {
                Layout const & style = par->layout();
                ParagraphList::const_iterator send;
 
-               auto parnext = par;
-               ++parnext;
-
                // Actual content of this paragraph.
-               prevpar = par;
                switch (style.latextype) {
                case LATEX_ENVIRONMENT:
                case LATEX_LIST_ENVIRONMENT:
@@ -595,7 +529,7 @@ ParagraphList::const_iterator makeEnvironment(
                        // One is that we are still in the environment in which we
                        // started---which we will be if the depth is the same.
                        if (par->params().depth() == origdepth) {
-                               LATTEST(par->layout() == style);
+                               LATTEST(bstyle == style);
                                if (lastlay != nullptr) {
                                        closeItemTag(xs, *lastlay);
                                        if (lastlay->docbookitemwrappertag() != "NONE") {
@@ -710,15 +644,15 @@ ParagraphList::const_iterator makeEnvironment(
                        break;
                }
                case LATEX_PARAGRAPH:
-//                     send = findLast(par, pend, LATEX_PARAGRAPH);
-                       par = makeParagraphs(buf, xs, runparams, text, par, parnext);
+                       send = findLastParagraph(par, pend);
+                       par = makeParagraphs(buf, xs, runparams, text, par, send);
                        break;
                case LATEX_BIB_ENVIRONMENT:
-//                     send = findLast(par, pend, LATEX_BIB_ENVIRONMENT);
-                       makeParagraphBibliography(buf, xs, runparams, text, par, parnext);
+                       send = findLastBibliographyParagraph(par, pend);
+                       par = makeParagraphBibliography(buf, xs, runparams, text, par, send);
                        break;
                case LATEX_COMMAND:
-                       par = parnext;
+                       ++par;
                        break;
                }
        }
@@ -730,10 +664,7 @@ ParagraphList::const_iterator makeEnvironment(
                        xs << xml::CR();
                }
        }
-//     auto nextpar = par;
-//     ++nextpar;
-       closeTag(xs, &*prevpar, &*par);
-//     closeTag(xs, &*par, (nextpar != end) ? &*nextpar : nullptr);
+       closeTag(xs, bstyle);
        xs << xml::CR();
        return pend;
 }
@@ -746,31 +677,16 @@ void makeCommand(
                Text const & text,
                ParagraphList::const_iterator const & pbegin)
 {
+       Layout const &style = pbegin->layout();
+
        // No need for labels, as they are handled by DocBook tags.
-       auto const begin = text.paragraphs().begin();
-       auto const end = text.paragraphs().end();
-       auto nextpar = pbegin;
-       ++nextpar;
-
-       // Find the previous paragraph.
-       auto prevpar = begin;
-       if (prevpar != pbegin) {
-               auto prevpar_next = prevpar;
-               ++prevpar_next;
-
-               while (prevpar_next != pbegin) {
-                       ++prevpar_next;
-                       ++prevpar;
-               }
-       }
 
-       // Generate this command.
-       openParTag(xs, &*pbegin, &*prevpar);
+       openParTag(xs, style);
 
+       ParagraphList::const_iterator const begin = text.paragraphs().begin();
        pbegin->simpleDocBookOnePar(buf, xs, runparams,
                                                                text.outerFont(distance(begin, pbegin)));
-
-       closeTag(xs, &*pbegin, (nextpar != end) ? &*nextpar : nullptr);
+       closeTag(xs, style);
        xs << xml::CR();
 }
 
@@ -783,99 +699,51 @@ pair<ParagraphList::const_iterator, ParagraphList::const_iterator> makeAny(
                ParagraphList::const_iterator send,
                ParagraphList::const_iterator pend)
 {
-       switch (par->layout().latextype) {
-       case LATEX_COMMAND: {
-               // The files with which we are working never have more than
-               // one paragraph in a command structure.
-               // FIXME
-               // if (ourparams.docbook_in_par)
-               //   fix it so we don't get sections inside standard, e.g.
-               // note that we may then need to make runparams not const, so we
-               // can communicate that back.
-               // FIXME Maybe this fix should be in the routines themselves, in case
-               // they are called from elsewhere.
-               makeCommand(buf, xs, ourparams, text, par);
-               ++par;
-               break;
-       }
-       case LATEX_ENVIRONMENT:
-       case LATEX_LIST_ENVIRONMENT:
-       case LATEX_ITEM_ENVIRONMENT:
-               // FIXME Same fix here.
-               send = findEndOfEnvironment(par, pend);
-               par = makeEnvironment(buf, xs, ourparams, text, par, send);
-               break;
-       case LATEX_PARAGRAPH:
-               send = findLast(par, pend, LATEX_PARAGRAPH);
-               par = makeParagraphs(buf, xs, ourparams, text, par, send);
-               break;
-       case LATEX_BIB_ENVIRONMENT:
-               send = findLast(par, pend, LATEX_BIB_ENVIRONMENT);
-               par = makeParagraphBibliography(buf, xs, ourparams, text, par, send);
-               break;
+       Layout const & style = par->layout();
+
+       switch (style.latextype) {
+               case LATEX_COMMAND: {
+                       // The files with which we are working never have more than
+                       // one paragraph in a command structure.
+                       // FIXME
+                       // if (ourparams.docbook_in_par)
+                       //   fix it so we don't get sections inside standard, e.g.
+                       // note that we may then need to make runparams not const, so we
+                       // can communicate that back.
+                       // FIXME Maybe this fix should be in the routines themselves, in case
+                       // they are called from elsewhere.
+                       makeCommand(buf, xs, ourparams, text, par);
+                       ++par;
+                       break;
+               }
+               case LATEX_ENVIRONMENT:
+               case LATEX_LIST_ENVIRONMENT:
+               case LATEX_ITEM_ENVIRONMENT: {
+                       // FIXME Same fix here.
+                       send = findEndOfEnvironment(par, pend);
+                       par = makeEnvironment(buf, xs, ourparams, text, par, send);
+                       break;
+               }
+               case LATEX_BIB_ENVIRONMENT: {
+                       send = findLastBibliographyParagraph(par, pend);
+                       par = makeParagraphBibliography(buf, xs, ourparams, text, par, send);
+                       break;
+               }
+               case LATEX_PARAGRAPH: {
+                       send = findLastParagraph(par, pend);
+                       par = makeParagraphs(buf, xs, ourparams, text, par, send);
+                       break;
+               }
        }
 
        return make_pair(par, send);
 }
 
-ParagraphList::const_iterator makeAnySimple(
-               Text const &text,
-               Buffer const &buf,
-               XMLStream &xs,
-               OutputParams const &ourparams,
-               ParagraphList::const_iterator par)
-{
-       auto parnext = par;
-       ++parnext;
-
-       switch (par->layout().latextype) {
-       case LATEX_COMMAND: {
-               // The files with which we are working never have more than
-               // one paragraph in a command structure.
-               // FIXME
-               // if (ourparams.docbook_in_par)
-               //   fix it so we don't get sections inside standard, e.g.
-               // note that we may then need to make runparams not const, so we
-               // can communicate that back.
-               // FIXME Maybe this fix should be in the routines themselves, in case
-               // they are called from elsewhere.
-               makeCommand(buf, xs, ourparams, text, par);
-               return parnext;
-       }
-       case LATEX_ENVIRONMENT:
-       case LATEX_LIST_ENVIRONMENT:
-       case LATEX_ITEM_ENVIRONMENT:
-               // FIXME Same fix here.
-               return makeEnvironment(buf, xs, ourparams, text, par, parnext);
-       case LATEX_PARAGRAPH:
-               return makeParagraphs(buf, xs, ourparams, text, par, parnext);
-       case LATEX_BIB_ENVIRONMENT:
-               return makeParagraphBibliography(buf, xs, ourparams, text, par, parnext);
-       }
-
-       // This should never happen. Return the next paragraph to avoid an infinite loop.
-       return parnext;
-}
-
 } // end anonymous namespace
 
 
 using DocBookDocumentSectioning = tuple<bool, pit_type>;
-
-
-struct DocBookInfoTag
-{
-       const set<pit_type> shouldBeInInfo;
-       const set<pit_type> mustBeInInfo;
-       const set<pit_type> abstract;
-       pit_type bpit;
-       pit_type epit;
-
-       DocBookInfoTag(const set<pit_type> & shouldBeInInfo, const set<pit_type> & mustBeInInfo,
-                                  const set<pit_type> & abstract, pit_type bpit, pit_type epit) :
-                                  shouldBeInInfo(shouldBeInInfo), mustBeInInfo(mustBeInInfo), abstract(abstract),
-                                  bpit(bpit), epit(epit) {}
-};
+using DocBookInfoTag = tuple<set<pit_type>, set<pit_type>, pit_type, pit_type>;
 
 
 DocBookDocumentSectioning hasDocumentSectioning(ParagraphList const &paragraphs, pit_type bpit, pit_type const epit) {
@@ -896,43 +764,19 @@ DocBookDocumentSectioning hasDocumentSectioning(ParagraphList const &paragraphs,
 }
 
 
-bool hasOnlyNotes(Paragraph const & par)
-{
-       for (int i = 0; i < par.size(); ++i)
-               if (!par.isInset(i) || !dynamic_cast<InsetNote *>(par.insetList().get(i)))
-                       return false;
-       return true;
-}
-
-
-DocBookInfoTag getParagraphsWithInfo(ParagraphList const &paragraphs, pit_type bpit, pit_type const epit) {
+DocBookInfoTag getParagraphsWithInfo(ParagraphList const &paragraphs, pit_type const bpit, pit_type const epit) {
        set<pit_type> shouldBeInInfo;
        set<pit_type> mustBeInInfo;
-       set<pit_type> abstract;
-
-       // Find the first non empty paragraph by mutating bpit.
-       while (bpit < epit) {
-               Paragraph const &par = paragraphs[bpit];
-               if (par.empty() || hasOnlyNotes(par))
-                       bpit += 1;
-               else
-                       break;
-       }
 
-       // Find the last info-like paragraph.
        pit_type cpit = bpit;
-       bool hasAbstractLayout = false;
        while (cpit < epit) {
                // Skip paragraphs only containing one note.
-               Paragraph const & par = paragraphs[cpit];
-               if (hasOnlyNotes(par)) {
+               Paragraph const &par = paragraphs[cpit];
+               if (par.size() == 1 && dynamic_cast<InsetNote*>(paragraphs[cpit].insetList().get(0))) {
                        cpit += 1;
                        continue;
                }
 
-               if (par.layout().docbookabstract())
-                       hasAbstractLayout = true;
-
                // Based on layout information, store this paragraph in one set: should be in <info>, must be.
                Layout const &style = par.layout();
 
@@ -942,55 +786,42 @@ DocBookInfoTag getParagraphsWithInfo(ParagraphList const &paragraphs, pit_type b
                        shouldBeInInfo.emplace(cpit);
                } else {
                        // Hypothesis: the <info> parts should be grouped together near the beginning bpit.
-                       // There may be notes in between, but nothing else.
                        break;
                }
                cpit += 1;
        }
        // Now, cpit points to the last paragraph that has things that could go in <info>.
-       // bpit is the beginning of the <info> part.
-
-       // Go once again through the list of paragraphs to find the abstract. If there is an abstract
-       // layout, only consider it. Otherwise, an abstract is just a sequence of paragraphs with text.
-       if (hasAbstractLayout) {
-               pit_type pit = bpit;
-               while (pit < cpit) { // Don't overshoot the <info> part.
-                       if (paragraphs[pit].layout().docbookabstract())
-                               abstract.emplace(pit);
-                       pit++;
-               }
-       } else {
-               pit_type lastAbstract = epit + 1; // A nonsensical value.
-               docstring lastAbstractLayout;
-
-               pit_type pit = bpit;
-               while (pit < cpit) { // Don't overshoot the <info> part.
-                       const Paragraph & par = paragraphs.at(pit);
-                       if (!par.insetList().empty()) {
-                               for (const auto &i : par.insetList()) {
-                                       if (i.inset->getText(0) != nullptr) {
-                                               if (lastAbstract == epit + 1) {
-                                                       // First paragraph that matches the heuristic definition of abstract.
-                                                       lastAbstract = pit;
-                                                       lastAbstractLayout = par.layout().name();
-                                               } else if (pit > lastAbstract + 1 || par.layout().name() != lastAbstractLayout) {
-                                                       // This is either too far from the last abstract paragraph or doesn't
-                                                       // have the right layout name, BUT there has already been an abstract
-                                                       // in this document: done with detecting the abstract.
-                                                       goto done; // Easier to get out of two nested loops.
-                                               }
+       // bpit is still the beginning of the <info> part.
 
-                                               abstract.emplace(pit);
-                                               break;
-                                       }
+       return make_tuple(shouldBeInInfo, mustBeInInfo, bpit, cpit);
+}
+
+
+bool hasAbstractBetween(ParagraphList const &paragraphs, pit_type const bpitAbstract, pit_type const epitAbstract)
+{
+       // Hypothesis: the paragraphs between bpitAbstract and epitAbstract can be considered an abstract because they
+       // are just after a document or part title.
+       if (epitAbstract - bpitAbstract <= 0)
+               return false;
+
+       // If there is something between these paragraphs, check if it's compatible with an abstract (i.e. some text).
+       pit_type bpit = bpitAbstract;
+       while (bpit < epitAbstract) {
+               const Paragraph &p = paragraphs.at(bpit);
+
+               if (p.layout().name() == from_ascii("Abstract"))
+                       return true;
+
+               if (!p.insetList().empty()) {
+                       for (const auto &i : p.insetList()) {
+                               if (i.inset->getText(0) != nullptr) {
+                                       return true;
                                }
                        }
-                       pit++;
                }
+               bpit++;
        }
-
-       done:
-       return DocBookInfoTag(shouldBeInInfo, mustBeInInfo, abstract, bpit, cpit);
+       return false;
 }
 
 
@@ -1000,22 +831,17 @@ pit_type generateDocBookParagraphWithoutSectioning(
                XMLStream & xs,
                OutputParams const & runparams,
                ParagraphList const & paragraphs,
-               DocBookInfoTag const & info)
+               pit_type bpit,
+               pit_type epit)
 {
-       auto bpit = info.bpit;
        auto par = paragraphs.iterator_at(bpit);
        auto lastStartedPar = par;
        ParagraphList::const_iterator send;
        auto const pend =
-                       (info.epit == (int) paragraphs.size()) ?
-                       paragraphs.end() : paragraphs.iterator_at(info.epit);
-
-       while (bpit < info.epit) {
-               if (info.abstract.find(bpit) != info.abstract.end()) {
-                       bpit += 1;
-                       continue;
-               }
+                       (epit == (int) paragraphs.size()) ?
+                       paragraphs.end() : paragraphs.iterator_at(epit);
 
+       while (bpit < epit) {
                tie(par, send) = makeAny(text, buf, xs, runparams, par, send, pend);
                bpit += distance(lastStartedPar, par);
                lastStartedPar = par;
@@ -1031,34 +857,35 @@ void outputDocBookInfo(
                XMLStream & xs,
                OutputParams const & runparams,
                ParagraphList const & paragraphs,
-               DocBookInfoTag const & info)
+               DocBookInfoTag const & info,
+               pit_type bpitAbstract,
+               pit_type const epitAbstract)
 {
+       // Consider everything between bpitAbstract and epitAbstract (excluded) as paragraphs for the abstract.
+       // Use bpitAbstract >= epitAbstract to indicate there is no abstract.
+
+       set<pit_type> shouldBeInInfo;
+       set<pit_type> mustBeInInfo;
+       pit_type bpitInfo;
+       pit_type epitInfo;
+       tie(shouldBeInInfo, mustBeInInfo, bpitInfo, epitInfo) = info;
+
        // Perform an additional check on the abstract. Sometimes, there are many paragraphs that should go
        // into the abstract, but none generates actual content. Thus, first generate to a temporary stream,
        // then only create the <abstract> tag if these paragraphs generate some content.
        // This check must be performed *before* a decision on whether or not to output <info> is made.
-       bool hasAbstract = !info.abstract.empty();
+       bool hasAbstract = hasAbstractBetween(paragraphs, bpitAbstract, epitAbstract);
        docstring abstract;
        if (hasAbstract) {
                odocstringstream os2;
-               {
-                       XMLStream xs2(os2);
-                       auto bpit = *std::min_element(info.abstract.begin(), info.abstract.end());
-                       auto epit = 1 + *std::max_element(info.abstract.begin(), info.abstract.end());
-                       // info.abstract is inclusive, epit is exclusive, hence +1 for looping.
-
-                       while (bpit < epit) {
-                               makeAnySimple(text, buf, xs2, runparams, paragraphs.iterator_at(bpit));
-                               xs2 << XMLStream::ESCAPE_NONE << from_ascii("<!-- " + to_string(bpit) + " -->");
-                               bpit += 1;
-                       }
-               }
+               XMLStream xs2(os2);
+               generateDocBookParagraphWithoutSectioning(text, buf, xs2, runparams, paragraphs, bpitAbstract, epitAbstract);
 
                // Actually output the abstract if there is something to do. Don't count line feeds or spaces in this,
                // even though they must be properly output if there is some abstract.
-               abstract = os2.str();
+               docstring abstractContent = os2.str();
                static const lyx::regex reg("[ \\r\\n]*");
-               docstring abstractContent = from_utf8(lyx::regex_replace(to_utf8(abstract), reg, string("")));
+               abstractContent = from_utf8(lyx::regex_replace(to_utf8(abstractContent), reg, string("")));
 
                // Nothing? Then there is no abstract!
                if (abstractContent.empty())
@@ -1066,7 +893,7 @@ void outputDocBookInfo(
        }
 
        // The abstract must go in <info>.
-       bool needInfo = !info.mustBeInInfo.empty() || hasAbstract;
+       bool needInfo = !mustBeInInfo.empty() || hasAbstract;
 
        // Start the <info> tag if required.
        if (needInfo) {
@@ -1075,29 +902,19 @@ void outputDocBookInfo(
                xs << xml::CR();
        }
 
-       // Output the elements that should go in <info>, before and after the abstract.
-       xs << XMLStream::ESCAPE_NONE << "<!-- shouldBeInInfo -->";
-       for (auto pit : info.shouldBeInInfo) // Typically, the title: these elements are so important and ubiquitous
-               // that mandating a wrapper like <info> would repel users.
-               makeAnySimple(text, buf, xs, runparams, paragraphs.iterator_at(pit));
-       xs << XMLStream::ESCAPE_NONE << "<!-- mustBeInInfo -->";
-       for (auto pit : info.mustBeInInfo)
-               if (info.abstract.find(pit) == info.abstract.end()) // The abstract must be in info, but is dealt with after.
-                       makeAnySimple(text, buf, xs, runparams, paragraphs.iterator_at(pit));
-       xs << XMLStream::ESCAPE_NONE << "<!-- /info -->";
+       // Output the elements that should go in <info>.
+       generateDocBookParagraphWithoutSectioning(text, buf, xs, runparams, paragraphs, bpitInfo, epitInfo);
 
-       if (hasAbstract) {
-//             string tag = paragraphs[*info.abstract.begin()].layout().docbookforceabstracttag();
-//             if (tag == "NONE")
-//                     tag = "abstract";
-//
-//             xs << xml::StartTag(tag);
-//             xs << xml::CR();
-               xs << XMLStream::ESCAPE_NONE << "<!-- abs -->";
+       if (hasAbstract && !abstract.empty()) { // The second test is probably superfluous.
+               string tag = paragraphs[bpitAbstract].layout().docbookforceabstracttag();
+               if (tag == "NONE")
+                       tag = "abstract";
+
+               xs << xml::StartTag(tag);
+               xs << xml::CR();
                xs << XMLStream::ESCAPE_NONE << abstract;
-               xs << XMLStream::ESCAPE_NONE << "<!-- /abs -->";
-//             xs << xml::EndTag(tag);
-//             xs << xml::CR();
+               xs << xml::EndTag(tag);
+               xs << xml::CR();
        }
 
        // End the <info> tag if it was started.
@@ -1122,7 +939,17 @@ void docbookFirstParagraphs(
        ParagraphList const &paragraphs = text.paragraphs();
        pit_type bpit = runparams.par_begin;
        DocBookInfoTag info = getParagraphsWithInfo(paragraphs, bpit, epit);
-       outputDocBookInfo(text, buf, xs, runparams, paragraphs, info);
+       outputDocBookInfo(text, buf, xs, runparams, paragraphs, info, get<3>(info), epit);
+}
+
+
+bool isParagraphEmpty(const Paragraph &par)
+{
+       InsetList const &insets = par.insetList();
+       size_t insetsLength = distance(insets.begin(), insets.end());
+       bool hasParagraphOnlyNote = insetsLength == 1 && insets.get(0) && insets.get(0)->asInsetCollapsible() &&
+                                                               dynamic_cast<InsetNote *>(insets.get(0));
+       return hasParagraphOnlyNote;
 }
 
 
@@ -1132,23 +959,35 @@ void docbookSimpleAllParagraphs(
                XMLStream & xs,
                OutputParams const & runparams)
 {
-       // Handle the given text, supposing it has no sections (i.e. a "simple" text). The input may vary in length
-       // between a single paragraph to a whole document.
+       // Handle the document, supposing it has no sections (i.e. a "simple" document).
 
        // First, the <info> tag.
        ParagraphList const &paragraphs = text.paragraphs();
        pit_type bpit = runparams.par_begin;
        pit_type const epit = runparams.par_end;
        DocBookInfoTag info = getParagraphsWithInfo(paragraphs, bpit, epit);
-       outputDocBookInfo(text, buf, xs, runparams, paragraphs, info);
+       outputDocBookInfo(text, buf, xs, runparams, paragraphs, info, 0, 0);
+       bpit = get<3>(info); // Generate the content starting from the end of the <info> part.
+
+       // Then, the content.
+       ParagraphList::const_iterator const pend =
+                       (epit == (int) paragraphs.size()) ?
+                       paragraphs.end() : paragraphs.iterator_at(epit);
 
-       // Then, the content. It starts where the <info> ends.
-       bpit = info.epit;
        while (bpit < epit) {
                auto par = paragraphs.iterator_at(bpit);
-               if (!hasOnlyNotes(*par))
-                       makeAnySimple(text, buf, xs, runparams, par);
-               bpit += 1;
+               ParagraphList::const_iterator const lastStartedPar = par;
+               ParagraphList::const_iterator send;
+
+               if (isParagraphEmpty(*par)) {
+                       ++par;
+                       bpit += distance(lastStartedPar, par);
+                       continue;
+               }
+
+               // Generate this paragraph.
+               tie(par, send) = makeAny(text, buf, xs, runparams, par, send, pend);
+               bpit += distance(lastStartedPar, par);
        }
 }
 
@@ -1202,7 +1041,7 @@ void docbookParagraphs(Text const &text,
                ParagraphList::const_iterator const lastStartedPar = par;
                ParagraphList::const_iterator send;
 
-               if (hasOnlyNotes(*par)) {
+               if (isParagraphEmpty(*par)) {
                        ++par;
                        bpit += distance(lastStartedPar, par);
                        continue;
@@ -1297,4 +1136,4 @@ void docbookParagraphs(Text const &text,
        }
 }
 
-} // namespace lyx
\ No newline at end of file
+} // namespace lyx