]> git.lyx.org Git - features.git/commitdiff
Handle empty index subentries (#7820)
authorJuergen Spitzmueller <spitz@lyx.org>
Tue, 1 Nov 2022 09:20:50 +0000 (10:20 +0100)
committerJuergen Spitzmueller <spitz@lyx.org>
Tue, 1 Nov 2022 09:20:50 +0000 (10:20 +0100)
src/insets/InsetIndex.cpp
src/insets/InsetIndex.h
src/insets/InsetIndexMacro.cpp
src/insets/InsetIndexMacro.h

index 13a7b4d1dff1eb677f4a694df4eebfccfb699b8b..b3024bf74cfd05f55070089d11a16e25d2d7d61e 100644 (file)
@@ -160,7 +160,7 @@ void InsetIndex::latex(otexstream & ios, OutputParams const & runparams_in) cons
                getSortkey(os, runparams);
                os << "@";
                os << ourlatex.str();
-               getSubentries(os, runparams);
+               getSubentries(os, runparams, ourlatex.str());
                if (hasSeeRef()) {
                        os << "|";
                        os << insetindexpagerangetranslator_latex().find(params_.range);
@@ -214,7 +214,7 @@ void InsetIndex::latex(otexstream & ios, OutputParams const & runparams_in) cons
 
                odocstringstream subentries;
                otexstream otsub(subentries);
-               getSubentries(otsub, runparams);
+               getSubentries(otsub, runparams, ourlatex.str());
                if (subentries.str().empty()) {
                        // Separate the entries and subentries, i.e., split on "!".
                        // This goes wrong on an escaped "!", but as the escape
@@ -230,6 +230,12 @@ void InsetIndex::latex(otexstream & ios, OutputParams const & runparams_in) cons
                        vector<docstring>::const_iterator it2 = levels_plain.begin();
                        bool first = true;
                        for (; it != end; ++it) {
+                               if ((*it).empty()) {
+                                       emptySubentriesWarning(ourlatex.str());
+                                       if (it2 < levels_plain.end())
+                                               ++it2;
+                                       continue;
+                               }
                                // The separator needs to be put back when
                                // writing the levels, except for the first level
                                if (!first)
@@ -668,7 +674,22 @@ docstring InsetIndex::getSortkeyAsText(OutputParams const & runparams) const
 }
 
 
-void InsetIndex::getSubentries(otexstream & os, OutputParams const & runparams) const
+void InsetIndex::emptySubentriesWarning(docstring const & mainentry) const
+{
+       // Empty subentries crash makeindex. So warn and ignore this.
+       TeXErrors terr;
+       ErrorList & errorList = buffer().errorList("Export");
+       docstring const s = bformat(_("There is an empty index subentry in the entry '%1$s'.\n"
+                                     "It will be ignored in the output."), mainentry);
+       Paragraph const & par = buffer().paragraphs().front();
+       errorList.push_back(ErrorItem(_("Empty index subentry!"), s,
+                                     {par.id(), 0}, {par.id(), -1}));
+       buffer().bufferErrors(terr, errorList);
+}
+
+
+void InsetIndex::getSubentries(otexstream & os, OutputParams const & runparams,
+                              docstring const & mainentry) const
 {
        Paragraph const & par = paragraphs().front();
        InsetList::const_iterator it = par.insetList().begin();
@@ -679,6 +700,10 @@ void InsetIndex::getSubentries(otexstream & os, OutputParams const & runparams)
                        InsetIndexMacro const & iim =
                                static_cast<InsetIndexMacro const &>(inset);
                        if (iim.params().type == InsetIndexMacroParams::Subentry) {
+                               if (iim.hasNoContent()) {
+                                       emptySubentriesWarning(mainentry);
+                                       continue;
+                               }
                                ++i;
                                if (i > 2)
                                        return;
index 1aaf2d79f321c4a9e0d11b3d2063923431cb8c8e..742ecae5459515992d360c98aeb44cf11ea64c06 100644 (file)
@@ -102,7 +102,9 @@ private:
        ///
        docstring getSortkeyAsText(OutputParams const &) const;
        ///
-       void getSubentries(otexstream &, OutputParams const &) const;
+       void emptySubentriesWarning(docstring const & mainentry) const;
+       ///
+       void getSubentries(otexstream &, OutputParams const &, docstring const &) const;
        ///
        std::vector<docstring> getSubentriesAsText(OutputParams const &,
                                                   bool const asLabel = false) const;
index 31754308f8565a20046edc1d3c84976030c489e9..e4dc167ca338d536699d76b0558c6a73de740ec9 100644 (file)
@@ -205,6 +205,12 @@ docstring InsetIndexMacro::getXhtml(XMLStream & xs, OutputParams const & runpara
 }
 
 
+bool InsetIndexMacro::hasNoContent() const
+{
+       return paragraphs().front().empty();
+}
+
+
 void InsetIndexMacro::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
        switch (cmd.action()) {
index 3d56e399de7191fa9345526e41cf7faceeaea1a0..07109f8fafc66610fb97daadbc56bf397e0e82f6 100644 (file)
@@ -66,6 +66,8 @@ public:
        int getPlaintext(odocstringstream &, OutputParams const &, size_t) const;
        ///
        void getDocbook(XMLStream &, OutputParams const &) const;
+       ///
+       bool hasNoContent() const;
 private:
        ///
        InsetCode lyxCode() const override;