]> git.lyx.org Git - features.git/blobdiff - src/output_docbook.cpp
DocBook: fix issues with nested labeling lists.
[features.git] / src / output_docbook.cpp
index d29362eb51336f3aa04f85191fd7e0b8abffc200..489230d5cc1eb0c3093e9bbcd7a414baee4fef3a 100644 (file)
@@ -14,7 +14,6 @@
 #include "Buffer.h"
 #include "buffer_funcs.h"
 #include "BufferParams.h"
-#include "Counters.h"
 #include "Font.h"
 #include "InsetList.h"
 #include "Layout.h"
@@ -27,6 +26,7 @@
 #include "TextClass.h"
 
 #include "insets/InsetBibtex.h"
+#include "insets/InsetBibitem.h"
 #include "insets/InsetLabel.h"
 #include "insets/InsetNote.h"
 
@@ -257,7 +257,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
@@ -304,6 +305,69 @@ 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.
+       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) {
+                       if (par->getInset(0)->lyxCode() == BIBITEM_CODE) {
+                               const auto * bibitem = dynamic_cast<const InsetBibitem*>(par->getInset(i));
+                               attr = from_utf8("xml:id='") + bibitem->bibLabel() + 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,
@@ -316,9 +380,6 @@ ParagraphList::const_iterator makeParagraphs(
        ParagraphList::const_iterator par = pbegin;
        for (; par != pend; ++par) {
                Layout const &lay = par->layout();
-               if (!lay.counter.empty())
-                       buf.masterBuffer()->params().
-                                       documentClass().counters().step(lay.counter, OutputUpdate);
 
                // We want to open the paragraph tag if:
                //   (i) the current layout permits multiple paragraphs
@@ -434,19 +495,6 @@ ParagraphList::const_iterator makeEnvironment(
 
        while (par != pend) {
                Layout const & style = par->layout();
-               // the counter only gets stepped if we're in some kind of list,
-               // or if it's the first time through.
-               // note that enum, etc, are handled automatically.
-               // FIXME There may be a bug here about user defined enumeration
-               // types. If so, then we'll need to take the counter and add "i",
-               // "ii", etc, as with enum.
-               Counters & cnts = buf.masterBuffer()->params().documentClass().counters();
-               docstring const & cntr = style.counter;
-               if (!style.counter.empty()
-                       && (par == pbegin || !isNormalEnv(style))
-                       && cnts.hasCounter(cntr)
-                               )
-                       cnts.step(cntr, OutputUpdate);
                ParagraphList::const_iterator send;
 
                // Actual content of this paragraph.
@@ -461,10 +509,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;
 
@@ -507,13 +559,35 @@ ParagraphList::const_iterator makeEnvironment(
                                        }
                                } // 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.
+                               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);
@@ -538,8 +612,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);
@@ -551,7 +624,8 @@ ParagraphList::const_iterator makeEnvironment(
                        par = makeParagraphs(buf, xs, runparams, text, par, send);
                        break;
                case LATEX_BIB_ENVIRONMENT:
-                       // Handled in InsetBibtex.
+                       send = findLastParagraph(par, pend);
+                       par = makeParagraphBibliography(buf, xs, runparams, text, par, send);
                        break;
                case LATEX_COMMAND:
                        ++par;
@@ -559,8 +633,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;
@@ -575,9 +654,6 @@ void makeCommand(
                ParagraphList::const_iterator const & pbegin)
 {
        Layout const &style = pbegin->layout();
-       if (!style.counter.empty())
-               buf.masterBuffer()->params().
-                               documentClass().counters().step(style.counter, OutputUpdate);
 
        // No need for labels, as they are handled by DocBook tags.
 
@@ -625,7 +701,8 @@ pair<ParagraphList::const_iterator, ParagraphList::const_iterator> makeAny(
                        break;
                }
                case LATEX_BIB_ENVIRONMENT: {
-                       // Handled in InsetBibtex.
+                       send = findLastParagraph(par, pend);
+                       par = makeParagraphBibliography(buf, xs, ourparams, text, par, send);
                        break;
                }
                case LATEX_PARAGRAPH: {
@@ -669,7 +746,16 @@ DocBookInfoTag getParagraphsWithInfo(ParagraphList const &paragraphs, pit_type c
 
        pit_type cpit = bpit;
        while (cpit < epit) {
-               Layout const &style = paragraphs[cpit].layout();
+               // Skip paragraphs only containing one note.
+               Paragraph const &par = paragraphs[cpit];
+               if (par.size() == 1 && dynamic_cast<InsetNote*>(paragraphs[cpit].insetList().get(0))) {
+                       cpit += 1;
+                       continue;
+               }
+
+               // Based on layout information, store this paragraph in one set: should be in <info>, must be.
+               Layout const &style = par.layout();
+
                if (style.docbookininfo() == "always") {
                        mustBeInInfo.emplace(cpit);
                } else if (style.docbookininfo() == "maybe") {
@@ -680,7 +766,8 @@ DocBookInfoTag getParagraphsWithInfo(ParagraphList const &paragraphs, pit_type c
                }
                cpit += 1;
        }
-       // Now, bpit points to the last paragraph that has things that could go in <info>.
+       // Now, cpit points to the last paragraph that has things that could go in <info>.
+       // bpit is still the beginning of the <info> part.
 
        return make_tuple(shouldBeInInfo, mustBeInInfo, bpit, cpit);
 }
@@ -733,6 +820,7 @@ pit_type generateDocBookParagraphWithoutSectioning(
        while (bpit < epit) {
                tie(par, send) = makeAny(text, buf, xs, runparams, par, send, pend);
                bpit += distance(lastStartedPar, par);
+               lastStartedPar = par;
        }
 
        return bpit;