]> git.lyx.org Git - lyx.git/blobdiff - src/output_docbook.cpp
Fix #10328.
[lyx.git] / src / output_docbook.cpp
index a796e1978880a278fe75cd59f24d8006a7063ca8..5e15edcee9b47b84d50432e5bd0f6d29f903f064 100644 (file)
@@ -26,6 +26,7 @@
 #include "TextClass.h"
 
 #include "insets/InsetBibtex.h"
+#include "insets/InsetBibitem.h"
 #include "insets/InsetLabel.h"
 #include "insets/InsetNote.h"
 
 #include "support/lstrings.h"
 #include "support/textutils.h"
 
+#include "support/regex.h"
+
 #include <stack>
 #include <iostream>
 #include <algorithm>
+#include <sstream>
 
 using namespace std;
 using namespace lyx::support;
@@ -256,7 +260,8 @@ void closeInnerItemTag(XMLStream &xs, Layout const &lay)
 
 inline void closeItemTag(XMLStream &xs, Layout const &lay)
 {
-       xs << xml::EndTag(lay.docbookitemtag()) << xml::CR();
+       xs << xml::EndTag(lay.docbookitemtag());
+       xs << xml::CR();
 }
 
 // end of convenience functions
@@ -269,6 +274,14 @@ ParagraphList::const_iterator findLastParagraph(
        return p;
 }
 
+ParagraphList::const_iterator findLastBibliographyParagraph(
+               ParagraphList::const_iterator p,
+               ParagraphList::const_iterator const & pend) {
+       for (++p; p != pend && p->layout().latextype == LATEX_BIB_ENVIRONMENT; ++p);
+
+       return p;
+}
+
 
 ParagraphList::const_iterator findEndOfEnvironment(
                ParagraphList::const_iterator const & pstart,
@@ -303,6 +316,70 @@ ParagraphList::const_iterator findEndOfEnvironment(
 }
 
 
+ParagraphList::const_iterator makeParagraphBibliography(
+               Buffer const &buf,
+               XMLStream &xs,
+               OutputParams const &runparams,
+               Text const &text,
+               ParagraphList::const_iterator const & pbegin,
+               ParagraphList::const_iterator const & pend)
+{
+       auto const begin = text.paragraphs().begin();
+       auto const end = text.paragraphs().end();
+
+       // Find the paragraph *before* pbegin.
+       ParagraphList::const_iterator pbegin_before = begin;
+       if (pbegin != begin) {
+               ParagraphList::const_iterator pbegin_before_next = begin;
+               ++pbegin_before_next;
+
+               while (pbegin_before_next != pbegin) {
+                       ++pbegin_before;
+                       ++pbegin_before_next;
+               }
+       }
+
+       ParagraphList::const_iterator par = pbegin;
+
+       // If this is the first paragraph in a bibliography, open the bibliography tag.
+       if (pbegin != begin && pbegin_before->layout().latextype != LATEX_BIB_ENVIRONMENT) {
+               xs << xml::StartTag("bibliography");
+               xs << xml::CR();
+       }
+
+       // Generate the required paragraphs, but only if they are .
+       for (; par != pend; ++par) {
+               // Start the precooked bibliography entry. This is very much like opening a paragraph tag.
+               // Don't forget the citation ID!
+               docstring attr;
+               for (auto i = 0; i < par->size(); ++i) {
+                       Inset const *ip = par->getInset(0);
+                       if (ip != nullptr && ip->lyxCode() == BIBITEM_CODE) {
+                               const auto * bibitem = dynamic_cast<const InsetBibitem*>(par->getInset(i));
+                               attr = from_utf8("xml:id='") + bibitem->getParam("key") + from_utf8("'");
+                               break;
+                       }
+               }
+               xs << xml::StartTag(from_utf8("bibliomixed"), attr);
+
+               // Generate the entry.
+               par->simpleDocBookOnePar(buf, xs, runparams, text.outerFont(distance(begin, par)), true, true, 0);
+
+               // End the precooked bibliography entry.
+               xs << xml::EndTag("bibliomixed");
+               xs << xml::CR();
+       }
+
+       // If this is the last paragraph in a bibliography, close the bibliography tag.
+       if (par == end || par->layout().latextype != LATEX_BIB_ENVIRONMENT) {
+               xs << xml::EndTag("bibliography");
+               xs << xml::CR();
+       }
+
+       return pend;
+}
+
+
 ParagraphList::const_iterator makeParagraphs(
                Buffer const &buf,
                XMLStream &xs,
@@ -352,7 +429,7 @@ ParagraphList::const_iterator makeParagraphs(
                        Inset const * firstInset = par->getInset(0);
 
                        // Floats cannot be in paragraphs.
-                       special_case = to_ascii(firstInset->layoutName()).substr(0, 6) == "Float:";
+                       special_case = to_utf8(firstInset->layoutName()).substr(0, 6) == "Float:";
 
                        // Bibliographies cannot be in paragraphs.
                        if (!special_case && firstInset->asInsetCommand())
@@ -386,15 +463,26 @@ ParagraphList::const_iterator makeParagraphs(
                                ((open_par && (!runparams.docbook_in_par || nextpar != pend))
                                || (!open_par && runparams.docbook_in_par && par == pbegin && nextpar != pend));
 
-               if (open_par) {
-                       openParTag(xs, lay);
-               }
+               // Determine if this paragraph has some real content. Things like new pages are not caught
+               // by Paragraph::empty(), even though they do not generate anything useful in DocBook.
+               odocstringstream os2;
+               XMLStream xs2(os2);
+               par->simpleDocBookOnePar(buf, xs2, runparams, text.outerFont(distance(begin, par)), open_par, close_par, 0);
 
-               par->simpleDocBookOnePar(buf, xs, runparams, text.outerFont(distance(begin, par)), open_par, close_par, 0);
+               docstring cleaned = os2.str();
+               static const lyx::regex reg("[ \\r\\n]*");
+               cleaned = from_utf8(lyx::regex_replace(to_utf8(cleaned), reg, string("")));
 
-               if (close_par) {
-                       closeTag(xs, lay);
-                       xs << xml::CR();
+               if (!cleaned.empty()) {
+                       if (open_par)
+                               openParTag(xs, lay);
+
+                       xs << XMLStream::ESCAPE_NONE << os2.str();
+
+                       if (close_par) {
+                               closeTag(xs, lay);
+                               xs << xml::CR();
+                       }
                }
        }
        return pend;
@@ -444,10 +532,14 @@ ParagraphList::const_iterator makeEnvironment(
                                LATTEST(bstyle == style);
                                if (lastlay != nullptr) {
                                        closeItemTag(xs, *lastlay);
+                                       if (lastlay->docbookitemwrappertag() != "NONE") {
+                                               xs << xml::EndTag(lastlay->docbookitemwrappertag());
+                                               xs << xml::CR();
+                                       }
                                        lastlay = nullptr;
                                }
 
-                               // this will be positive, if we want to skip the
+                               // this will be positive if we want to skip the
                                // initial word (if it's been taken for the label).
                                pos_type sep = 0;
 
@@ -470,8 +562,10 @@ ParagraphList::const_iterator makeEnvironment(
                                                                openLabelTag(xs, style);
                                                                xs << lbl;
                                                                closeLabelTag(xs, style);
+                                                       } else {
+                                                               // No new line after closeLabelTag.
+                                                               xs << xml::CR();
                                                        }
-                                                       xs << xml::CR();
                                                }
                                        } else { // some kind of list
                                                if (style.labeltype == LABEL_MANUAL) {
@@ -480,23 +574,44 @@ ParagraphList::const_iterator makeEnvironment(
                                                        openLabelTag(xs, style);
                                                        sep = par->firstWordDocBook(xs, runparams);
                                                        closeLabelTag(xs, style);
-                                                       xs << xml::CR();
                                                } else {
                                                        openLabelTag(xs, style);
                                                        xs << par->params().labelString();
                                                        closeLabelTag(xs, style);
-                                                       xs << xml::CR();
                                                }
                                        }
                                } // end label output
 
+                               // Start generating the item.
                                bool wasInParagraph = runparams.docbook_in_par;
                                openItemTag(xs, style);
                                bool getsIntoParagraph = openInnerItemTag(xs, style);
                                OutputParams rp = runparams;
                                rp.docbook_in_par = wasInParagraph | getsIntoParagraph;
 
-                               par->simpleDocBookOnePar(buf, xs, rp, text.outerFont(distance(begin, par)), true, true, sep);
+                               // Maybe the item is completely empty, i.e. if the first word ends at the end of the current paragraph
+                               // AND if the next paragraph doesn't have the same depth (if there is such a paragraph).
+                               // Common case: there is only the first word on the line, but there is a nested list instead
+                               // of more text.
+                               bool emptyItem = false;
+                               if (sep == par->size()) {
+                                       auto next_par = par;
+                                       ++next_par;
+                                       if (next_par == text.paragraphs().end()) // There is no next paragraph.
+                                               emptyItem = true;
+                                       else // There is a next paragraph: check depth.
+                                               emptyItem = par->params().depth() >= next_par->params().depth();
+                               }
+
+                               if (emptyItem) {
+                                       // Avoid having an empty item, this is not valid DocBook. A single character is enough to force
+                                       // generation of a full <para>.
+                                       xs << ' ';
+                               } else {
+                                       // Generate the rest of the paragraph, if need be.
+                                       par->simpleDocBookOnePar(buf, xs, rp, text.outerFont(distance(begin, par)), true, true, sep);
+                               }
+
                                ++par;
                                if (getsIntoParagraph)
                                        closeInnerItemTag(xs, style);
@@ -521,8 +636,7 @@ ParagraphList::const_iterator makeEnvironment(
                                        }
                                }
                        }
-                       // The other possibility is that the depth has increased, in which
-                       // case we need to recurse.
+                       // The other possibility is that the depth has increased.
                        else {
                                send = findEndOfEnvironment(par, pend);
                                par = makeEnvironment(buf, xs, runparams, text, par, send);
@@ -534,7 +648,8 @@ ParagraphList::const_iterator makeEnvironment(
                        par = makeParagraphs(buf, xs, runparams, text, par, send);
                        break;
                case LATEX_BIB_ENVIRONMENT:
-                       // Handled in InsetBibtex.
+                       send = findLastBibliographyParagraph(par, pend);
+                       par = makeParagraphBibliography(buf, xs, runparams, text, par, send);
                        break;
                case LATEX_COMMAND:
                        ++par;
@@ -542,8 +657,13 @@ ParagraphList::const_iterator makeEnvironment(
                }
        }
 
-       if (lastlay != 0)
+       if (lastlay != nullptr) {
                closeItemTag(xs, *lastlay);
+               if (lastlay->docbookitemwrappertag() != "NONE") {
+                       xs << xml::EndTag(lastlay->docbookitemwrappertag());
+                       xs << xml::CR();
+               }
+       }
        closeTag(xs, bstyle);
        xs << xml::CR();
        return pend;
@@ -605,7 +725,8 @@ pair<ParagraphList::const_iterator, ParagraphList::const_iterator> makeAny(
                        break;
                }
                case LATEX_BIB_ENVIRONMENT: {
-                       // Handled in InsetBibtex.
+                       send = findLastBibliographyParagraph(par, pend);
+                       par = makeParagraphBibliography(buf, xs, ourparams, text, par, send);
                        break;
                }
                case LATEX_PARAGRAPH: {
@@ -659,10 +780,6 @@ DocBookInfoTag getParagraphsWithInfo(ParagraphList const &paragraphs, pit_type c
                // Based on layout information, store this paragraph in one set: should be in <info>, must be.
                Layout const &style = par.layout();
 
-               std::cout << "Name: " << to_utf8(style.name()) << std::endl;
-               std::cout << "  DocBook tag: " << style.docbooktag() << std::endl;
-               std::cout << "  In info: " << style.docbookininfo() << std::endl;
-
                if (style.docbookininfo() == "always") {
                        mustBeInInfo.emplace(cpit);
                } else if (style.docbookininfo() == "maybe") {
@@ -724,14 +841,11 @@ pit_type generateDocBookParagraphWithoutSectioning(
                        (epit == (int) paragraphs.size()) ?
                        paragraphs.end() : paragraphs.iterator_at(epit);
 
-       std::cout << "generateDocBookParagraphWithoutSectioning" << std::endl;
        while (bpit < epit) {
-               std::cout << "iteration; bpit: " << bpit << std::endl;
                tie(par, send) = makeAny(text, buf, xs, runparams, par, send, pend);
                bpit += distance(lastStartedPar, par);
                lastStartedPar = par;
        }
-       std::cout << "generateDocBookParagraphWithoutSectioning has looped; bpit: " << bpit << std::endl;
 
        return bpit;
 }
@@ -756,8 +870,29 @@ void outputDocBookInfo(
        pit_type epitInfo;
        tie(shouldBeInInfo, mustBeInInfo, bpitInfo, epitInfo) = info;
 
-       // The abstract must go in <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 = hasAbstractBetween(paragraphs, bpitAbstract, epitAbstract);
+       docstring abstract;
+       if (hasAbstract) {
+               odocstringstream os2;
+               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.
+               docstring abstractContent = os2.str();
+               static const lyx::regex reg("[ \\r\\n]*");
+               abstractContent = from_utf8(lyx::regex_replace(to_utf8(abstractContent), reg, string("")));
+
+               // Nothing? Then there is no abstract!
+               if (abstractContent.empty())
+                       hasAbstract = false;
+       }
+
+       // The abstract must go in <info>.
        bool needInfo = !mustBeInInfo.empty() || hasAbstract;
 
        // Start the <info> tag if required.
@@ -770,16 +905,14 @@ void outputDocBookInfo(
        // Output the elements that should go in <info>.
        generateDocBookParagraphWithoutSectioning(text, buf, xs, runparams, paragraphs, bpitInfo, epitInfo);
 
-       if (hasAbstract) {
+       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.startDivision(false);
-               generateDocBookParagraphWithoutSectioning(text, buf, xs, runparams, paragraphs, bpitAbstract, epitAbstract);
-               xs.endDivision();
+               xs << XMLStream::ESCAPE_NONE << abstract;
                xs << xml::EndTag(tag);
                xs << xml::CR();
        }