3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
13 #include "InsetIndex.h"
14 #include "InsetIndexMacro.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
21 #include "DispatchResult.h"
23 #include "ErrorList.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "IndicesList.h"
27 #include "InsetList.h"
30 #include "LaTeXFeatures.h"
33 #include "output_latex.h"
34 #include "output_xhtml.h"
36 #include "texstream.h"
37 #include "TextClass.h"
38 #include "TocBackend.h"
40 #include "support/debug.h"
41 #include "support/docstream.h"
42 #include "support/FileName.h"
43 #include "support/gettext.h"
44 #include "support/lstrings.h"
45 #include "support/Translator.h"
47 #include "frontends/alert.h"
53 #include <QThreadStorage>
56 using namespace lyx::support;
58 // Uncomment to enable InsetIndex-specific debugging mode: the tree for the index will be printed to std::cout.
59 // #define LYX_INSET_INDEX_DEBUG
65 typedef Translator<string, InsetIndexParams::PageRange> PageRangeTranslator;
66 typedef Translator<docstring, InsetIndexParams::PageRange> PageRangeTranslatorLoc;
68 PageRangeTranslator const init_insetindexpagerangetranslator()
70 PageRangeTranslator translator("none", InsetIndexParams::None);
71 translator.addPair("start", InsetIndexParams::Start);
72 translator.addPair("end", InsetIndexParams::End);
76 PageRangeTranslator const init_insetindexpagerangetranslator_latex()
78 PageRangeTranslator translator("", InsetIndexParams::None);
79 translator.addPair("(", InsetIndexParams::Start);
80 translator.addPair(")", InsetIndexParams::End);
85 PageRangeTranslatorLoc const init_insetindexpagerangetranslator_loc()
87 PageRangeTranslatorLoc translator(docstring(), InsetIndexParams::None);
88 translator.addPair(_("Starts page range"), InsetIndexParams::Start);
89 translator.addPair(_("Ends page range"), InsetIndexParams::End);
94 PageRangeTranslator const & insetindexpagerangetranslator()
96 static PageRangeTranslator const prtranslator =
97 init_insetindexpagerangetranslator();
102 PageRangeTranslatorLoc const & insetindexpagerangetranslator_loc()
104 static PageRangeTranslatorLoc const translator =
105 init_insetindexpagerangetranslator_loc();
110 PageRangeTranslator const & insetindexpagerangetranslator_latex()
112 static PageRangeTranslator const lttranslator =
113 init_insetindexpagerangetranslator_latex();
119 /////////////////////////////////////////////////////////////////////
123 ///////////////////////////////////////////////////////////////////////
126 InsetIndex::InsetIndex(Buffer * buf, InsetIndexParams const & params)
127 : InsetCollapsible(buf), params_(params)
131 void InsetIndex::latex(otexstream & ios, OutputParams const & runparams_in) const
133 OutputParams runparams(runparams_in);
134 runparams.inIndexEntry = true;
138 if (buffer().masterBuffer()->params().use_indices && !params_.index.empty()
139 && params_.index != "idx") {
141 os << escape(params_.index);
148 // Get the LaTeX output from InsetText. We need to deconstruct this later
149 // in order to check if we need to generate a sorting key
150 odocstringstream ourlatex;
151 otexstream ots(ourlatex);
152 InsetText::latex(ots, runparams);
153 if (runparams.find_effective()) {
154 // No need for special handling, if we are only searching for some patterns
155 os << ourlatex.str() << "}";
160 getSortkey(os, runparams);
162 os << ourlatex.str();
163 getSubentries(os, runparams, ourlatex.str());
166 os << insetindexpagerangetranslator_latex().find(params_.range);
167 getSeeRefs(os, runparams);
168 } else if (!params_.pagefmt.empty() && params_.pagefmt != "default") {
170 os << insetindexpagerangetranslator_latex().find(params_.range);
171 os << from_utf8(params_.pagefmt);
174 // We check whether we need a sort key.
175 // If so, we use the plaintext version
176 odocstringstream ourplain;
177 InsetText::plaintext(ourplain, runparams);
179 // These are the LaTeX and plaintext representations
180 docstring latexstr = ourlatex.str();
181 docstring plainstr = ourplain.str();
183 // This will get what follows | if anything does,
184 // the command (e.g., see, textbf) for pagination
189 odocstringstream seeref;
190 otexstream otsee(seeref);
191 getSeeRefs(otsee, runparams);
193 } else if (!params_.pagefmt.empty() && params_.pagefmt != "default") {
194 cmd = from_utf8(params_.pagefmt);
196 // Check for the | separator to strip the cmd.
197 // This goes wrong on an escaped "|", but as the escape
198 // character can be changed in style files, we cannot
200 size_t pos = latexstr.find(from_ascii("|"));
201 if (pos != docstring::npos) {
202 // Put the bit after "|" into cmd...
203 cmd = latexstr.substr(pos + 1);
204 // ...and erase that stuff from latexstr
205 latexstr = latexstr.erase(pos);
206 // ...as well as from plainstr
207 size_t ppos = plainstr.find(from_ascii("|"));
208 if (ppos < plainstr.size())
209 plainstr.erase(ppos);
211 LYXERR0("The `|' separator was not found in the plaintext version!");
215 odocstringstream subentries;
216 otexstream otsub(subentries);
217 getSubentries(otsub, runparams, ourlatex.str());
218 if (subentries.str().empty()) {
219 // Separate the entries and subentries, i.e., split on "!".
220 // This goes wrong on an escaped "!", but as the escape
221 // character can be changed in style files, we cannot
223 std::vector<docstring> const levels =
224 getVectorFromString(latexstr, from_ascii("!"), true);
225 std::vector<docstring> const levels_plain =
226 getVectorFromString(plainstr, from_ascii("!"), true);
228 vector<docstring>::const_iterator it = levels.begin();
229 vector<docstring>::const_iterator end = levels.end();
230 vector<docstring>::const_iterator it2 = levels_plain.begin();
232 for (; it != end; ++it) {
234 emptySubentriesWarning(ourlatex.str());
235 if (it2 < levels_plain.end())
239 // The separator needs to be put back when
240 // writing the levels, except for the first level
246 // Now here comes the reason for this whole procedure:
247 // We try to correctly sort macros and formatted strings.
248 // If we find a command, prepend a plain text
249 // version of the content to get sorting right,
250 // e.g. \index{LyX@\LyX}, \index{text@\textbf{text}}.
251 // We do this on all levels.
252 // We don't do it if the level already contains a '@', though.
253 // Plaintext might return nothing (e.g. for ERTs).
254 // In that case, we use LaTeX.
255 docstring const spart = (levels_plain.empty() || (*it2).empty()) ? *it : *it2;
256 processLatexSorting(os, runparams, *it, spart);
257 if (it2 < levels_plain.end())
261 processLatexSorting(os, runparams, latexstr, plainstr);
262 os << subentries.str();
265 // At last, re-insert the command, separated by "|"
268 << insetindexpagerangetranslator_latex().find(params_.range)
274 // In macros with moving arguments, such as \section,
275 // we store the index and output it after the macro (#2154)
276 if (runparams_in.postpone_fragile_stuff)
277 runparams_in.post_macro += os.str();
283 void InsetIndex::processLatexSorting(otexstream & os, OutputParams const & runparams,
284 docstring const latex, docstring const spart) const
286 if (contains(latex, '\\') && !contains(latex, '@')) {
287 // Now we need to validate that all characters in
288 // the sorting part are representable in the current
289 // encoding. If not try the LaTeX macro which might
290 // or might not be a good choice, and issue a warning.
291 pair<docstring, docstring> spart_latexed =
292 runparams.encoding->latexString(spart, runparams.dryrun);
293 if (!spart_latexed.second.empty())
294 LYXERR0("Uncodable character in index entry. Sorting might be wrong!");
295 if (spart != spart_latexed.first && !runparams.dryrun) {
297 ErrorList & errorList = buffer().errorList("Export");
298 docstring const s = bformat(_("LyX's automatic index sorting algorithm faced "
299 "problems with the entry '%1$s'.\n"
300 "Please specify the sorting of this entry manually, as "
301 "explained in the User Guide."), spart);
302 Paragraph const & par = buffer().paragraphs().front();
303 errorList.push_back(ErrorItem(_("Index sorting failed"), s,
304 {par.id(), 0}, {par.id(), -1}));
305 buffer().bufferErrors(terr, errorList);
307 // Remove remaining \'s from the sort key
308 docstring ppart = subst(spart_latexed.first, from_ascii("\\"), docstring());
309 // Plain quotes need to be escaped, however (#10649), as this
310 // is the default escape character
311 ppart = subst(ppart, from_ascii("\""), from_ascii("\\\""));
313 // Now insert the sortkey, separated by '@'.
317 // Insert the actual level text
322 void InsetIndex::docbook(XMLStream & xs, OutputParams const & runparams) const
324 // Two ways of processing this inset are implemented:
325 // - the legacy one, based on parsing the raw LaTeX (before LyX 2.4) -- unlikely to be deprecated
326 // - the modern one, based on precise insets for indexing features
327 // Like the LaTeX implementation, consider the user chooses either of those options.
329 // Get the content of the inset as LaTeX, as some things may be encoded as ERT (like {}).
330 // TODO: if there is an ERT within the index term, its conversion should be tried, in case it becomes useful;
331 // otherwise, ERTs should become comments. For now, they are just copied as-is, which is barely satisfactory.
332 odocstringstream odss;
333 otexstream ots(odss);
334 InsetText::latex(ots, runparams);
335 docstring latexString = trim(odss.str());
337 // Handle several indices (indicated in the inset instead of the raw latexString).
338 docstring indexType = from_utf8("");
339 if (buffer().masterBuffer()->params().use_indices) {
340 indexType += " type=\"" + params_.index + "\"";
343 // Split the string into its main constituents: terms, and command (see, see also, range).
344 size_t positionVerticalBar = latexString.find(from_ascii("|")); // What comes before | is (sub)(sub)entries.
345 docstring indexTerms = latexString.substr(0, positionVerticalBar);
347 if (positionVerticalBar != lyx::docstring::npos) {
348 command = latexString.substr(positionVerticalBar + 1);
351 // Handle sorting issues, with @.
354 sortAs = getSortkeyAsText(runparams);
355 // indexTerms may contain a sort key if the user has both the inset and the manual key.
357 vector<docstring> sortingElements = getVectorFromString(indexTerms, from_ascii("@"), false);
358 if (sortingElements.size() == 2) {
359 sortAs = sortingElements[0];
360 indexTerms = sortingElements[1];
364 // Handle primary, secondary, and tertiary terms (entries, subentries, and subsubentries, for LaTeX).
365 vector<docstring> terms;
366 if (const vector<docstring> potential_terms = getSubentriesAsText(runparams); !potential_terms.empty()) {
367 terms = potential_terms;
368 // The main term is not present in the vector, as it's not a subentry. The main index term is inserted raw in
369 // the index inset. Considering that the user either uses the new or the legacy mechanism, the main term is the
370 // full string within this inset (i.e. without the subinsets).
371 terms.insert(terms.begin(), latexString);
373 terms = getVectorFromString(indexTerms, from_ascii("!"), false);
376 // Handle ranges. Happily, in the raw LaTeX mode, (| and |) can only be at the end of the string!
377 const bool hasInsetRange = params_.range != InsetIndexParams::PageRange::None;
378 const bool hasStartRange = params_.range == InsetIndexParams::PageRange::Start ||
379 latexString.find(from_ascii("|(")) != lyx::docstring::npos;
380 const bool hasEndRange = params_.range == InsetIndexParams::PageRange::End ||
381 latexString.find(from_ascii("|)")) != lyx::docstring::npos;
384 // Remove the ranges from the command if they do not appear at the beginning.
386 while ((index = command.find(from_utf8("|("), index)) != std::string::npos)
387 command.erase(index, 1);
389 while ((index = command.find(from_utf8("|)"), index)) != std::string::npos)
390 command.erase(index, 1);
392 // Remove the ranges when they are the only vertical bar in the complete string.
393 if (command[0] == '(' || command[0] == ')')
397 // Handle see and seealso. As "see" is a prefix of "seealso", the order of the comparisons is important.
398 // Both commands are mutually exclusive!
399 docstring see = getSeeAsText(runparams);
400 vector<docstring> seeAlsoes = getSeeAlsoesAsText(runparams);
402 if (see.empty() && seeAlsoes.empty() && command.substr(0, 3) == "see") {
403 // Unescape brackets.
405 while ((index = command.find(from_utf8("\\{"), index)) != std::string::npos)
406 command.erase(index, 1);
408 while ((index = command.find(from_utf8("\\}"), index)) != std::string::npos)
409 command.erase(index, 1);
411 // Retrieve the part between brackets, and remove the complete seealso.
412 size_t positionOpeningBracket = command.find(from_ascii("{"));
413 size_t positionClosingBracket = command.find(from_ascii("}"));
414 docstring list = command.substr(positionOpeningBracket + 1, positionClosingBracket - positionOpeningBracket - 1);
416 // Parse the list of referenced entries (or a single one for see).
417 if (command.substr(0, 7) == "seealso") {
418 seeAlsoes = getVectorFromString(list, from_ascii(","), false);
422 if (see.find(from_ascii(",")) != std::string::npos) {
423 docstring error = from_utf8("Several index terms found as \"see\"! Only one is acceptable. "
424 "Complete entry: \"") + latexString + from_utf8("\"");
426 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
430 // Remove the complete see/seealso from the commands, in case there is something else to parse.
431 command = command.substr(positionClosingBracket + 1);
434 // Some parts of the strings are not parsed, as they do not have anything matching in DocBook: things like
435 // formatting the entry or the page number, other strings for sorting. https://wiki.lyx.org/Tips/Indexing
436 // If there are such things in the index entry, then this code may miserably fail. For example, for "Peter|(textbf",
437 // no range will be detected.
438 // TODO: Could handle formatting as significance="preferred"?
439 if (!command.empty()) {
440 docstring error = from_utf8("Unsupported feature: an index entry contains a | with an unsupported command, ")
441 + command + from_utf8(". ") + from_utf8("Complete entry: \"") + latexString + from_utf8("\"");
443 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
446 // Write all of this down.
447 if (terms.empty() && !hasEndRange) {
448 docstring error = from_utf8("No index term found! Complete entry: \"") + latexString + from_utf8("\"");
450 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
452 // Generate the attributes for ranges. It is based on the terms that are indexed, but the ID must be unique
453 // to this indexing area (xml::cleanID does not guarantee this: for each call with the same arguments,
454 // the same legal ID is produced; here, as the input would be the same, the output must be, by design).
455 // Hence the thread-local storage, as the numbers must strictly be unique, and thus cannot be shared across
456 // a paragraph (making the solution used for HTML worthless). This solution is very similar to the one used in
458 // indexType can only be used for singular and startofrange types!
460 if (!hasStartRange && !hasEndRange) {
463 // Append an ID if uniqueness is not guaranteed across the document.
464 static QThreadStorage<set<docstring>> tKnownTermLists;
465 static QThreadStorage<int> tID;
467 set<docstring> &knownTermLists = tKnownTermLists.localData();
468 int &ID = tID.localData();
470 if (!tID.hasLocalData()) {
474 // Modify the index terms to add the unique ID if needed.
475 docstring newIndexTerms = indexTerms;
476 if (knownTermLists.find(indexTerms) != knownTermLists.end()) {
477 newIndexTerms += from_ascii(string("-") + to_string(ID));
479 // Only increment for the end of range, so that the same number is used for the start of range.
485 // Term list not yet known: add it to the set AFTER the end of range. After
486 if (knownTermLists.find(indexTerms) == knownTermLists.end() && hasEndRange) {
487 knownTermLists.insert(indexTerms);
490 // Generate the attributes.
491 docstring id = xml::cleanID(newIndexTerms);
493 attrs = indexType + " class=\"startofrange\" xml:id=\"" + id + "\"";
495 attrs = " class=\"endofrange\" startref=\"" + id + "\"";
499 // Handle the index terms (including the specific index for this entry).
501 xs << xml::CompTag("indexterm", attrs);
503 xs << xml::StartTag("indexterm", attrs);
504 if (!terms.empty()) { // hasEndRange has no content.
506 if (!sortAs.empty()) {
507 attr = from_utf8("sortas='") + sortAs + from_utf8("'");
510 xs << xml::StartTag("primary", attr);
512 xs << xml::EndTag("primary");
514 if (terms.size() > 1) {
515 xs << xml::StartTag("secondary");
517 xs << xml::EndTag("secondary");
519 if (terms.size() > 2) {
520 xs << xml::StartTag("tertiary");
522 xs << xml::EndTag("tertiary");
525 // Handle see and see also.
527 xs << xml::StartTag("see");
529 xs << xml::EndTag("see");
532 if (!seeAlsoes.empty()) {
533 for (auto &entry : seeAlsoes) {
534 xs << xml::StartTag("seealso");
536 xs << xml::EndTag("seealso");
541 xs << xml::EndTag("indexterm");
547 docstring InsetIndex::xhtml(XMLStream & xs, OutputParams const &) const
549 // we just print an anchor, taking the paragraph ID from
550 // our own interior paragraph, which doesn't get printed
551 std::string const magic = paragraphs().front().magicLabel();
552 std::string const attr = "id='" + magic + "'";
553 xs << xml::CompTag("a", attr);
558 bool InsetIndex::showInsetDialog(BufferView * bv) const
560 bv->showDialog("index", params2string(params_),
561 const_cast<InsetIndex *>(this));
566 void InsetIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
568 switch (cmd.action()) {
570 case LFUN_INSET_MODIFY: {
571 if (cmd.getArg(0) == "changetype") {
572 cur.recordUndoInset(this);
573 params_.index = from_utf8(cmd.getArg(1));
576 InsetIndexParams params;
577 InsetIndex::string2params(to_utf8(cmd.argument()), params);
578 cur.recordUndoInset(this);
579 params_.index = params.index;
580 params_.range = params.range;
581 params_.pagefmt = params.pagefmt;
582 // what we really want here is a TOC update, but that means
583 // a full buffer update
584 cur.forceBufferUpdate();
588 case LFUN_INSET_DIALOG_UPDATE:
589 cur.bv().updateDialog("index", params2string(params_));
592 case LFUN_PARAGRAPH_BREAK: {
593 // Since this inset in single-par anyway, let's use
594 // return to enter subentries
595 FuncRequest fr(LFUN_INDEXMACRO_INSERT, "subentry");
601 InsetCollapsible::doDispatch(cur, cmd);
607 bool InsetIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
608 FuncStatus & flag) const
610 switch (cmd.action()) {
612 case LFUN_INSET_MODIFY:
613 if (cmd.getArg(0) == "changetype") {
614 docstring const newtype = from_utf8(cmd.getArg(1));
615 Buffer const & realbuffer = *buffer().masterBuffer();
616 IndicesList const & indiceslist = realbuffer.params().indiceslist();
617 Index const * index = indiceslist.findShortcut(newtype);
618 flag.setEnabled(index != 0);
620 from_utf8(cmd.getArg(1)) == params_.index);
623 return InsetCollapsible::getStatus(cur, cmd, flag);
625 case LFUN_INSET_DIALOG_UPDATE: {
626 Buffer const & realbuffer = *buffer().masterBuffer();
627 flag.setEnabled(realbuffer.params().use_indices);
631 case LFUN_PARAGRAPH_BREAK:
632 return macrosPossible("subentry");
634 case LFUN_INDEXMACRO_INSERT:
635 return macrosPossible(cmd.getArg(0));
638 return InsetCollapsible::getStatus(cur, cmd, flag);
643 void InsetIndex::getSortkey(otexstream & os, OutputParams const & runparams) const
645 Paragraph const & par = paragraphs().front();
646 InsetList::const_iterator it = par.insetList().begin();
647 for (; it != par.insetList().end(); ++it) {
648 Inset & inset = *it->inset;
649 if (inset.lyxCode() == INDEXMACRO_SORTKEY_CODE) {
650 InsetIndexMacro const & iim =
651 static_cast<InsetIndexMacro const &>(inset);
652 iim.getLatex(os, runparams);
659 docstring InsetIndex::getSortkeyAsText(OutputParams const & runparams) const
661 Paragraph const & par = paragraphs().front();
662 InsetList::const_iterator it = par.insetList().begin();
663 for (; it != par.insetList().end(); ++it) {
664 Inset & inset = *it->inset;
665 if (inset.lyxCode() == INDEXMACRO_SORTKEY_CODE) {
667 InsetIndexMacro const & iim =
668 static_cast<InsetIndexMacro const &>(inset);
669 iim.getLatex(os, runparams);
673 return from_ascii("");
677 void InsetIndex::emptySubentriesWarning(docstring const & mainentry) const
679 // Empty subentries crash makeindex. So warn and ignore this.
681 ErrorList & errorList = buffer().errorList("Export");
682 docstring const s = bformat(_("There is an empty index subentry in the entry '%1$s'.\n"
683 "It will be ignored in the output."), mainentry);
684 Paragraph const & par = buffer().paragraphs().front();
685 errorList.push_back(ErrorItem(_("Empty index subentry!"), s,
686 {par.id(), 0}, {par.id(), -1}));
687 buffer().bufferErrors(terr, errorList);
691 void InsetIndex::getSubentries(otexstream & os, OutputParams const & runparams,
692 docstring const & mainentry) const
694 Paragraph const & par = paragraphs().front();
695 InsetList::const_iterator it = par.insetList().begin();
697 for (; it != par.insetList().end(); ++it) {
698 Inset & inset = *it->inset;
699 if (inset.lyxCode() == INDEXMACRO_CODE) {
700 InsetIndexMacro const & iim =
701 static_cast<InsetIndexMacro const &>(inset);
702 if (iim.params().type == InsetIndexMacroParams::Subentry) {
703 if (iim.hasNoContent()) {
704 emptySubentriesWarning(mainentry);
711 iim.getLatex(os, runparams);
718 std::vector<docstring> InsetIndex::getSubentriesAsText(OutputParams const & runparams,
719 bool const asLabel) const
721 std::vector<docstring> subentries;
723 Paragraph const & par = paragraphs().front();
724 InsetList::const_iterator it = par.insetList().begin();
726 for (; it != par.insetList().end(); ++it) {
727 Inset & inset = *it->inset;
728 if (inset.lyxCode() == INDEXMACRO_CODE) {
729 InsetIndexMacro const & iim =
730 static_cast<InsetIndexMacro const &>(inset);
731 if (iim.params().type == InsetIndexMacroParams::Subentry) {
737 docstring const sl = iim.getNewLabel(l);
738 subentries.emplace_back(sl);
741 iim.getLatex(os, runparams);
742 subentries.emplace_back(os.str());
752 docstring InsetIndex::getMainSubentryAsText(OutputParams const & runparams) const
755 InsetText::latex(os, runparams);
760 void InsetIndex::getSeeRefs(otexstream & os, OutputParams const & runparams) const
762 Paragraph const & par = paragraphs().front();
763 InsetList::const_iterator it = par.insetList().begin();
764 for (; it != par.insetList().end(); ++it) {
765 Inset & inset = *it->inset;
766 if (inset.lyxCode() == INDEXMACRO_CODE) {
767 InsetIndexMacro const & iim =
768 static_cast<InsetIndexMacro const &>(inset);
769 if (iim.params().type == InsetIndexMacroParams::See
770 || iim.params().type == InsetIndexMacroParams::Seealso) {
771 iim.getLatex(os, runparams);
779 docstring InsetIndex::getSeeAsText(OutputParams const & runparams,
780 bool const asLabel) const
782 Paragraph const & par = paragraphs().front();
783 InsetList::const_iterator it = par.insetList().begin();
784 for (; it != par.insetList().end(); ++it) {
785 Inset & inset = *it->inset;
786 if (inset.lyxCode() == INDEXMACRO_CODE) {
787 InsetIndexMacro const & iim =
788 static_cast<InsetIndexMacro const &>(inset);
789 if (iim.params().type == InsetIndexMacroParams::See) {
792 return iim.getNewLabel(l);
795 iim.getLatex(os, runparams);
801 return from_ascii("");
805 std::vector<docstring> InsetIndex::getSeeAlsoesAsText(OutputParams const & runparams,
806 bool const asLabel) const
808 std::vector<docstring> seeAlsoes;
810 Paragraph const & par = paragraphs().front();
811 InsetList::const_iterator it = par.insetList().begin();
812 for (; it != par.insetList().end(); ++it) {
813 Inset & inset = *it->inset;
814 if (inset.lyxCode() == INDEXMACRO_CODE) {
815 InsetIndexMacro const & iim =
816 static_cast<InsetIndexMacro const &>(inset);
817 if (iim.params().type == InsetIndexMacroParams::Seealso) {
820 seeAlsoes.emplace_back(iim.getNewLabel(l));
823 iim.getLatex(os, runparams);
824 seeAlsoes.emplace_back(os.str());
836 bool hasInsetWithCode(const InsetIndex * const inset_index, const InsetCode code,
837 const std::set<InsetIndexMacroParams::Type> types = {})
839 Paragraph const & par = inset_index->paragraphs().front();
840 InsetList::const_iterator it = par.insetList().begin();
841 for (; it != par.insetList().end(); ++it) {
842 Inset & inset = *it->inset;
843 if (inset.lyxCode() == code) {
847 LASSERT(code == INDEXMACRO_CODE, return false);
848 InsetIndexMacro const & iim =
849 static_cast<InsetIndexMacro const &>(inset);
850 if (types.find(iim.params().type) != types.end())
860 bool InsetIndex::hasSubentries() const
862 return hasInsetWithCode(this, INDEXMACRO_CODE, {InsetIndexMacroParams::Subentry});
866 bool InsetIndex::hasSeeRef() const
868 return hasInsetWithCode(this, INDEXMACRO_CODE, {InsetIndexMacroParams::See, InsetIndexMacroParams::Seealso});
872 bool InsetIndex::hasSortKey() const
874 return hasInsetWithCode(this, INDEXMACRO_SORTKEY_CODE);
878 bool InsetIndex::macrosPossible(string const type) const
880 if (type != "see" && type != "seealso"
881 && type != "sortkey" && type != "subentry")
884 Paragraph const & par = paragraphs().front();
885 InsetList::const_iterator it = par.insetList().begin();
887 for (; it != par.insetList().end(); ++it) {
888 Inset & inset = *it->inset;
889 if (type == "sortkey" && inset.lyxCode() == INDEXMACRO_SORTKEY_CODE)
891 if (inset.lyxCode() == INDEXMACRO_CODE) {
892 InsetIndexMacro const & iim = static_cast<InsetIndexMacro const &>(inset);
893 if ((type == "see" || type == "seealso")
894 && (iim.params().type == InsetIndexMacroParams::See
895 || iim.params().type == InsetIndexMacroParams::Seealso))
897 if (type == "subentry"
898 && iim.params().type == InsetIndexMacroParams::Subentry) {
909 ColorCode InsetIndex::labelColor() const
911 if (params_.index.empty() || params_.index == from_ascii("idx"))
912 return InsetCollapsible::labelColor();
914 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.index)
915 + "@" + buffer().fileName().absFileName());
917 c = InsetCollapsible::labelColor();
922 docstring InsetIndex::toolTip(BufferView const &, int, int) const
924 docstring tip = _("Index Entry");
925 if (buffer().params().use_indices && !params_.index.empty()) {
926 Buffer const & realbuffer = *buffer().masterBuffer();
927 IndicesList const & indiceslist = realbuffer.params().indiceslist();
929 Index const * index = indiceslist.findShortcut(params_.index);
931 tip += _("unknown type!");
933 tip += index->index();
937 docstring res = toolTipText(tip);
938 if (!insetindexpagerangetranslator_loc().find(params_.range).empty())
939 res += "\n" + insetindexpagerangetranslator_loc().find(params_.range);
940 if (!params_.pagefmt.empty() && params_.pagefmt != "default") {
941 res += "\n" + _("Pagination format:") + " ";
942 if (params_.pagefmt == "textbf")
944 else if (params_.pagefmt == "textit")
946 else if (params_.pagefmt == "emph")
947 res += _("emphasized");
949 res += from_utf8(params_.pagefmt);
955 docstring const InsetIndex::buttonLabel(BufferView const & bv) const
957 InsetLayout const & il = getLayout();
958 docstring label = translateIfPossible(il.labelstring());
960 if (buffer().params().use_indices && !params_.index.empty()) {
961 Buffer const & realbuffer = *buffer().masterBuffer();
962 IndicesList const & indiceslist = realbuffer.params().indiceslist();
964 Index const * index = indiceslist.findShortcut(params_.index);
966 label += _("unknown type!");
968 label += index->index();
973 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
976 res = getNewLabel(label);
977 OutputParams const rp(0);
978 vector<docstring> sublbls = getSubentriesAsText(rp, true);
979 for (auto const & sublbl : sublbls) {
980 res += " " + docstring(1, char_type(0x2023));// TRIANGULAR BULLET
983 docstring see = getSeeAsText(rp, true);
984 if (see.empty() && !getSeeAlsoesAsText(rp, true).empty())
985 see = getSeeAlsoesAsText(rp, true).front();
987 res += " " + docstring(1, char_type(0x261e));// WHITE RIGHT POINTING INDEX
991 if (!insetindexpagerangetranslator_latex().find(params_.range).empty())
992 res += " " + from_ascii(insetindexpagerangetranslator_latex().find(params_.range));
997 void InsetIndex::write(ostream & os) const
999 os << to_utf8(layoutName());
1001 InsetCollapsible::write(os);
1005 void InsetIndex::read(Lexer & lex)
1008 InsetCollapsible::read(lex);
1012 string InsetIndex::params2string(InsetIndexParams const & params)
1021 void InsetIndex::string2params(string const & in, InsetIndexParams & params)
1023 params = InsetIndexParams();
1027 istringstream data(in);
1029 lex.setStream(data);
1030 lex.setContext("InsetIndex::string2params");
1036 void InsetIndex::addToToc(DocIterator const & cpit, bool output_active,
1037 UpdateType utype, TocBackend & backend) const
1039 DocIterator pit = cpit;
1040 pit.push_back(CursorSlice(const_cast<InsetIndex &>(*this)));
1042 InsetLayout const & il = getLayout();
1043 docstring label = translateIfPossible(il.labelstring());
1044 if (!il.contentaslabel())
1047 str = getNewLabel(label);
1048 OutputParams const rp(0);
1049 vector<docstring> sublbls = getSubentriesAsText(rp, true);
1050 for (auto const & sublbl : sublbls) {
1051 str += " " + docstring(1, char_type(0x2023));// TRIANGULAR BULLET
1052 str += " " + sublbl;
1054 docstring see = getSeeAsText(rp, true);
1055 if (see.empty() && !getSeeAlsoesAsText(rp, true).empty())
1056 see = getSeeAlsoesAsText(rp, true).front();
1058 str += " " + docstring(1, char_type(0x261e));// WHITE RIGHT POINTING INDEX
1062 string type = "index";
1063 if (buffer().masterBuffer()->params().use_indices)
1064 type += ":" + to_utf8(params_.index);
1065 TocBuilder & b = backend.builder(type);
1066 b.pushItem(pit, str, output_active);
1067 // Proceed with the rest of the inset.
1068 InsetCollapsible::addToToc(cpit, output_active, utype, backend);
1073 void InsetIndex::validate(LaTeXFeatures & features) const
1075 if (buffer().masterBuffer()->params().use_indices
1076 && !params_.index.empty()
1077 && params_.index != "idx")
1078 features.require("splitidx");
1079 InsetCollapsible::validate(features);
1083 string InsetIndex::contextMenuName() const
1085 return "context-index";
1089 string InsetIndex::contextMenu(BufferView const & bv, int x, int y) const
1091 // We override the implementation of InsetCollapsible,
1092 // because we have eytra entries.
1093 string owncm = "context-edit-index;";
1094 return owncm + InsetCollapsible::contextMenu(bv, x, y);
1098 bool InsetIndex::hasSettings() const
1104 bool InsetIndex::insetAllowed(InsetCode code) const
1107 case INDEXMACRO_CODE:
1108 case INDEXMACRO_SORTKEY_CODE:
1113 return InsetCollapsible::insetAllowed(code);
1118 /////////////////////////////////////////////////////////////////////
1122 ///////////////////////////////////////////////////////////////////////
1125 void InsetIndexParams::write(ostream & os) const
1129 os << to_utf8(index);
1134 << insetindexpagerangetranslator().find(range)
1142 void InsetIndexParams::read(Lexer & lex)
1145 index = lex.getDocString();
1147 index = from_ascii("idx");
1148 if (lex.checkFor("range")) {
1149 string st = lex.getString();
1150 if (lex.eatLine()) {
1151 st = lex.getString();
1152 range = insetindexpagerangetranslator().find(lex.getString());
1155 if (lex.checkFor("pageformat") && lex.eatLine()) {
1156 pagefmt = lex.getString();
1161 /////////////////////////////////////////////////////////////////////
1165 ///////////////////////////////////////////////////////////////////////
1167 InsetPrintIndex::InsetPrintIndex(Buffer * buf, InsetCommandParams const & p)
1168 : InsetCommand(buf, p)
1172 ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
1174 static ParamInfo param_info_;
1175 if (param_info_.empty()) {
1176 param_info_.add("type", ParamInfo::LATEX_OPTIONAL,
1177 ParamInfo::HANDLING_ESCAPE);
1178 param_info_.add("name", ParamInfo::LATEX_OPTIONAL,
1179 ParamInfo::HANDLING_LATEXIFY);
1180 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
1186 docstring InsetPrintIndex::screenLabel() const
1188 bool const printall = suffixIs(getCmdName(), '*');
1189 bool const multind = buffer().masterBuffer()->params().use_indices;
1191 && getParam("type") == from_ascii("idx"))
1192 || (getParam("type").empty() && !printall))
1194 Buffer const & realbuffer = *buffer().masterBuffer();
1195 IndicesList const & indiceslist = realbuffer.params().indiceslist();
1196 Index const * index = indiceslist.findShortcut(getParam("type"));
1197 if (!index && !printall)
1198 return _("Unknown index type!");
1199 docstring res = printall ? _("All indexes") : index->index();
1201 res += " (" + _("non-active") + ")";
1202 else if (contains(getCmdName(), "printsubindex"))
1203 res += " (" + _("subindex") + ")";
1208 bool InsetPrintIndex::isCompatibleCommand(string const & s)
1210 return s == "printindex" || s == "printsubindex"
1211 || s == "printindex*" || s == "printsubindex*";
1215 void InsetPrintIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
1217 switch (cmd.action()) {
1219 case LFUN_INSET_MODIFY: {
1220 if (cmd.argument() == from_ascii("toggle-subindex")) {
1221 string scmd = getCmdName();
1222 if (contains(scmd, "printindex"))
1223 scmd = subst(scmd, "printindex", "printsubindex");
1225 scmd = subst(scmd, "printsubindex", "printindex");
1229 } else if (cmd.argument() == from_ascii("check-printindex*")) {
1230 string scmd = getCmdName();
1231 if (suffixIs(scmd, '*'))
1235 setParam("type", docstring());
1239 InsetCommandParams p(INDEX_PRINT_CODE);
1241 InsetCommand::string2params(to_utf8(cmd.argument()), p);
1242 if (p.getCmdName().empty()) {
1243 cur.noScreenUpdate();
1252 InsetCommand::doDispatch(cur, cmd);
1258 bool InsetPrintIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
1259 FuncStatus & status) const
1261 switch (cmd.action()) {
1263 case LFUN_INSET_MODIFY: {
1264 if (cmd.argument() == from_ascii("toggle-subindex")) {
1265 status.setEnabled(buffer().masterBuffer()->params().use_indices);
1266 status.setOnOff(contains(getCmdName(), "printsubindex"));
1268 } else if (cmd.argument() == from_ascii("check-printindex*")) {
1269 status.setEnabled(buffer().masterBuffer()->params().use_indices);
1270 status.setOnOff(suffixIs(getCmdName(), '*'));
1272 } if (cmd.getArg(0) == "index_print"
1273 && cmd.getArg(1) == "CommandInset") {
1274 InsetCommandParams p(INDEX_PRINT_CODE);
1275 InsetCommand::string2params(to_utf8(cmd.argument()), p);
1276 if (suffixIs(p.getCmdName(), '*')) {
1277 status.setEnabled(true);
1278 status.setOnOff(false);
1281 Buffer const & realbuffer = *buffer().masterBuffer();
1282 IndicesList const & indiceslist =
1283 realbuffer.params().indiceslist();
1284 Index const * index = indiceslist.findShortcut(p["type"]);
1285 status.setEnabled(index != 0);
1286 status.setOnOff(p["type"] == getParam("type"));
1289 return InsetCommand::getStatus(cur, cmd, status);
1292 case LFUN_INSET_DIALOG_UPDATE: {
1293 status.setEnabled(buffer().masterBuffer()->params().use_indices);
1298 return InsetCommand::getStatus(cur, cmd, status);
1303 void InsetPrintIndex::updateBuffer(ParIterator const &, UpdateType, bool const /*deleted*/)
1305 Index const * index =
1306 buffer().masterParams().indiceslist().findShortcut(getParam("type"));
1308 setParam("name", index->index());
1312 void InsetPrintIndex::latex(otexstream & os, OutputParams const & runparams_in) const
1314 if (!buffer().masterBuffer()->params().use_indices) {
1315 if (getParam("type") == from_ascii("idx"))
1316 os << "\\printindex" << termcmd;
1319 OutputParams runparams = runparams_in;
1320 os << getCommand(runparams);
1324 void InsetPrintIndex::validate(LaTeXFeatures & features) const
1326 features.require("makeidx");
1327 if (buffer().masterBuffer()->params().use_indices)
1328 features.require("splitidx");
1329 InsetCommand::validate(features);
1333 string InsetPrintIndex::contextMenuName() const
1335 return buffer().masterBuffer()->params().use_indices ?
1336 "context-indexprint" : string();
1340 bool InsetPrintIndex::hasSettings() const
1342 return buffer().masterBuffer()->params().use_indices;
1349 /// Builds an entry for the index.
1350 IndexEntry(const InsetIndex * inset, OutputParams const * runparams) : inset_(inset), runparams_(runparams)
1352 LASSERT(runparams, return);
1354 // Convert the inset as text. The resulting text usually only contains an XHTML anchor (<a id='...'/>) and text.
1355 odocstringstream entry;
1356 OutputParams ours = *runparams;
1357 ours.for_toc = false;
1358 inset_->plaintext(entry, ours);
1359 entry_ = entry.str();
1361 // Determine in which index this entry belongs to.
1362 if (inset_->buffer().masterBuffer()->params().use_indices) {
1363 index_ = inset_->params_.index;
1366 // Attempt parsing the inset.
1373 /// When parsing this entry, some errors may be found; they are reported as a single string.
1374 // It is up to the caller to send this string to LYXERR and the output file, as needed.
1375 const docstring & output_error() const
1377 return output_error_;
1380 void output_error(XMLStream xs) const
1382 LYXERR0(output_error());
1383 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + output_error() + from_utf8(" -->\n"));
1390 std::cout << to_utf8(entry_) << std::endl;
1392 // If a modern parameter is present, this is definitely a modern index inset. Similarly, if it contains the
1393 // usual LaTeX symbols (!|@), then it is definitely a legacy index inset. Otherwise, if it has features of
1394 // neither, it is both: consider this is a modern inset, to trigger the least complex code. Mixing both types
1395 // is not allowed (i.e. behaviour is undefined).
1396 const bool is_definitely_modern = inset_->hasSortKey() || inset_->hasSeeRef() || inset_->hasSubentries()
1397 || inset_->params_.range != InsetIndexParams::PageRange::None;
1398 const bool is_definitely_legacy = entry_.find('@') != std::string::npos
1399 || entry_.find('|') != std::string::npos || entry_.find('!') != std::string::npos;
1401 if (is_definitely_legacy && is_definitely_modern)
1402 output_error_ += from_utf8("Mix of index properties and raw LaTeX index commands is unsupported. ");
1405 // - is_definitely_modern == true:
1406 // - is_definitely_legacy == true: error (return whatever)
1407 // - is_definitely_legacy == false: return modern
1408 // - is_definitely_modern == false:
1409 // - is_definitely_legacy == true: return legacy
1410 // - is_definitely_legacy == false: return modern
1411 return !is_definitely_legacy;
1414 void parseAsModern()
1416 LASSERT(runparams_, return);
1418 if (inset_->hasSortKey()) {
1419 sort_as_ = inset_->getSortkeyAsText(*runparams_);
1422 terms_ = inset_->getSubentriesAsText(*runparams_);
1423 // The main term is not present in the vector, as it's not a subentry. The main index term is inserted raw in
1424 // the index inset. Considering that the user either uses the new or the legacy mechanism, the main term is the
1425 // full string within this inset (i.e. without the subinsets).
1426 terms_.insert(terms_.begin(), inset_->getMainSubentryAsText(*runparams_));
1428 has_start_range_ = inset_->params_.range == InsetIndexParams::PageRange::Start;
1429 has_end_range_ = inset_->params_.range == InsetIndexParams::PageRange::End;
1431 see_ = inset_->getSeeAsText(*runparams_);
1432 see_alsoes_ = inset_->getSeeAlsoesAsText(*runparams_);
1435 void parseAsLegacy() {
1436 // Determine if some features are known not to be supported. For now, this is only formatting like
1437 // \index{alpha@\textbf{alpha}} or \index{alpha@$\alpha$}.
1438 // @ is supported, but only for sorting, without specific formatting.
1439 if (entry_.find(from_utf8("@\\")) != lyx::docstring::npos) {
1440 output_error_ += from_utf8("Unsupported feature: an index entry contains an @\\. "
1441 "Complete entry: \"") + entry_ + from_utf8("\". ");
1443 if (entry_.find(from_utf8("@$")) != lyx::docstring::npos) {
1444 output_error_ += from_utf8("Unsupported feature: an index entry contains an @$. "
1445 "Complete entry: \"") + entry_ + from_utf8("\". ");
1448 // Split the string into its main constituents: terms, and command (see, see also, range).
1449 size_t positionVerticalBar = entry_.find(from_ascii("|")); // What comes before | is (sub)(sub)entries.
1450 docstring indexTerms = entry_.substr(0, positionVerticalBar);
1452 if (positionVerticalBar != lyx::docstring::npos) {
1453 command = entry_.substr(positionVerticalBar + 1);
1456 // Handle sorting issues, with @.
1457 vector<docstring> sortingElements = getVectorFromString(indexTerms, from_ascii("@"), false);
1458 if (sortingElements.size() == 2) {
1459 sort_as_ = sortingElements[0];
1460 indexTerms = sortingElements[1];
1463 // Handle entries, subentries, and subsubentries.
1464 terms_ = getVectorFromString(indexTerms, from_ascii("!"), false);
1466 // Handle ranges. Happily, (| and |) can only be at the end of the string!
1467 has_start_range_ = entry_.find(from_ascii("|(")) != lyx::docstring::npos;
1468 has_end_range_ = entry_.find(from_ascii("|)")) != lyx::docstring::npos;
1470 // - Remove the ranges from the command if they do not appear at the beginning.
1471 size_t range_index = 0;
1472 while ((range_index = command.find(from_utf8("|("), range_index)) != std::string::npos)
1473 command.erase(range_index, 1);
1475 while ((range_index = command.find(from_utf8("|)"), range_index)) != std::string::npos)
1476 command.erase(range_index, 1);
1478 // - Remove the ranges when they are the only vertical bar in the complete string.
1479 if (command[0] == '(' || command[0] == ')')
1480 command.erase(0, 1);
1482 // Handle see and seealso. As "see" is a prefix of "seealso", the order of the comparisons is important.
1483 // Both commands are mutually exclusive!
1484 if (command.substr(0, 3) == "see") {
1485 // Unescape brackets.
1486 size_t index_argument_begin = 0;
1487 while ((index_argument_begin = command.find(from_utf8("\\{"), index_argument_begin)) != std::string::npos)
1488 command.erase(index_argument_begin, 1);
1489 size_t index_argument_end = 0;
1490 while ((index_argument_end = command.find(from_utf8("\\}"), index_argument_end)) != std::string::npos)
1491 command.erase(index_argument_end, 1);
1493 // Retrieve the part between brackets, and remove the complete seealso.
1494 size_t position_opening_bracket = command.find(from_ascii("{"));
1495 size_t position_closing_bracket = command.find(from_ascii("}"));
1496 docstring argument = command.substr(position_opening_bracket + 1,
1497 position_closing_bracket - position_opening_bracket - 1);
1499 // Parse the argument of referenced entries (or a single one for see).
1500 if (command.substr(0, 7) == "seealso") {
1501 see_alsoes_ = getVectorFromString(argument, from_ascii(","), false);
1505 if (see_.find(from_ascii(",")) != std::string::npos) {
1506 output_error_ += from_utf8("Several index_argument_end terms found as \"see\"! Only one is "
1507 "acceptable. Complete entry: \"") + entry_ + from_utf8("\". ");
1511 // Remove the complete see/seealso from the commands, in case there is something else to parse.
1512 command = command.substr(position_closing_bracket + 1);
1515 // Some parts of the strings are not parsed, as they do not have anything matching in DocBook or XHTML:
1516 // things like formatting the entry or the page number, other strings for sorting.
1517 // https://wiki.lyx.org/Tips/Indexing
1518 // If there are such things in the index entry, then this code may miserably fail. For example, for
1519 // "Peter|(textbf", no range will be detected.
1520 if (!command.empty()) {
1521 output_error_ += from_utf8("Unsupported feature: an index entry contains a | with an unsupported command, ")
1522 + command + from_utf8(". Complete entry: \"") + entry_ + from_utf8("\". ");
1528 return terms_.size();
1531 const std::vector<docstring>& terms() const {
1535 std::vector<docstring>& terms() {
1539 const InsetIndex* inset() const {
1544 // Input inset. These should only be used when parsing the inset (either parseAsModern or parseAsLegacy, called in
1545 // the constructor).
1546 const InsetIndex * inset_;
1547 OutputParams const * runparams_;
1549 docstring index_; // Useful when there are multiple indices in the same document.
1551 // Errors, concatenated as a single string, available as soon as parsing is done, const afterwards (i.e. once
1552 // constructor is done).
1553 docstring output_error_;
1555 // Parsed index entry.
1556 std::vector<docstring> terms_; // Up to three entries, in general.
1559 bool has_start_range_;
1560 bool has_end_range_;
1562 vector<docstring> see_alsoes_;
1564 // Operators used for sorting entries (alphabetical order).
1565 friend bool operator<(IndexEntry const & lhs, IndexEntry const & rhs);
1568 bool operator<(IndexEntry const & lhs, IndexEntry const & rhs)
1570 if (lhs.terms_.empty())
1573 for (unsigned i = 0; i < min(rhs.terms_.size(), lhs.terms_.size()); ++i) {
1574 int comp = compare_no_case(lhs.terms_[i], rhs.terms_[i]);
1583 std::string generateCssClassAtDepth(unsigned depth) {
1584 std::string css_class = "entry";
1588 css_class.insert(0, "sub");
1595 std::vector<IndexEntry> entries;
1596 std::vector<IndexNode*> children;
1599 docstring termAtLevel(const IndexNode* node, unsigned depth)
1601 // The typical entry has a depth of 1 to 3: the call stack would then be at most 4 (due to the root node). This
1602 // function could be made constant time by copying the term in each node, but that would make data duplication that
1603 // may fall out of sync; the performance benefit would probably be negligible.
1604 if (!node->entries.empty()) {
1605 LASSERT(node->entries.begin()->terms().size() >= depth + 1, return from_ascii(""));
1606 return node->entries.begin()->terms()[depth];
1609 if (!node->children.empty()) {
1610 return termAtLevel(*node->children.begin(), depth);
1613 LASSERT(false, return from_ascii(""));
1616 void insertIntoNode(const IndexEntry& entry, IndexNode* node, unsigned depth = 0)
1618 // depth == 0 is for the root, not yet the index, hence the increase when going to vector size.
1619 for (IndexNode* child : node->children) {
1620 if (entry.terms()[depth] == termAtLevel(child, depth)) {
1621 if (depth + 1 == entry.terms().size()) { // == child.entries.begin()->terms().size()
1622 // All term entries match: it's an entry.
1623 child->entries.emplace_back(entry);
1626 insertIntoNode(entry, child, depth + 1);
1632 // Out of the loop: no matching child found, create a new (possibly nested) child for this entry. Due to the
1633 // possibility of nestedness, only insert the current entry when the right level is reached. This is needed if the
1634 // first entry for a word has several levels that never appeared.
1635 // In particular, this case is called for the first entry.
1636 IndexNode* new_node = node;
1638 new_node->children.emplace_back(new IndexNode{{}, {}});
1639 new_node = new_node->children.back();
1641 } while (depth + 1 <= entry.terms().size()); // depth == 0: root node, no text associated.
1642 new_node->entries.emplace_back(entry);
1645 IndexNode* buildIndexTree(vector<IndexEntry>& entries)
1647 // Sort the entries, first on the main entry, then the subentry, then the subsubentry,
1648 // thanks to the implementation of operator<.
1649 // If this operation is not performed, the algorithm below is no more correct (and ensuring that it works with
1650 // unsorted entries would make its complexity blow up).
1651 stable_sort(entries.begin(), entries.end());
1653 // Cook the index into a nice tree data structure: entries at a given level in the index as a node, with subentries
1655 auto* index_root = new IndexNode{{}, {}};
1656 for (const IndexEntry& entry : entries) {
1657 insertIntoNode(entry, index_root);
1663 void outputIndexPage(XMLStream & xs, const IndexNode* root_node, unsigned depth = 0) // NOLINT(misc-no-recursion)
1665 LASSERT(root_node->entries.size() + root_node->children.size() > 0, return);
1667 xs << xml::StartTag("li", "class='" + generateCssClassAtDepth(depth) + "'");
1669 xs << XMLStream::ESCAPE_NONE << termAtLevel(root_node, depth);
1670 // By tree assumption, all the entries at this node have the same set of terms.
1672 if (!root_node->entries.empty()) {
1673 xs << XMLStream::ESCAPE_NONE << " — "; // Em dash, i.e. long (---).
1674 unsigned entry_number = 1;
1676 auto writeLinkToEntry = [&xs](const IndexEntry &entry, unsigned entry_number) {
1677 std::string const link_attr = "href='#" + entry.inset()->paragraphs()[0].magicLabel() + "'";
1678 xs << xml::StartTag("a", link_attr);
1679 xs << from_ascii(std::to_string(entry_number));
1680 xs << xml::EndTag("a");
1683 for (unsigned i = 0; i < root_node->entries.size(); ++i) {
1684 const IndexEntry &entry = root_node->entries[i];
1686 switch (entry.inset()->params().range) {
1687 case InsetIndexParams::PageRange::None:
1688 writeLinkToEntry(entry, entry_number);
1690 case InsetIndexParams::PageRange::Start: {
1691 // Try to find the end of the range, if it is just after. Otherwise, the output will be slightly
1692 // scrambled, but understandable. Doing better would mean implementing more of the indexing logic here
1693 // and more complex indexing here (skipping the end is not just incrementing i). Worst case output:
1695 const bool nextEntryIsEnd = i + 1 < root_node->entries.size() &&
1696 root_node->entries[i + 1].inset()->params().range ==
1697 InsetIndexParams::PageRange::End;
1698 // No need to check if both entries are for the same terms: they are in the same IndexNode.
1700 writeLinkToEntry(entry, entry_number);
1701 xs << XMLStream::ESCAPE_NONE << " – "; // En dash, i.e. semi-long (--).
1703 if (nextEntryIsEnd) {
1704 // Skip the next entry in the loop, write it right now, after the dash.
1707 writeLinkToEntry(root_node->entries[i], entry_number);
1711 case InsetIndexParams::PageRange::End:
1712 // This range end was not caught by the range start, do it now to avoid losing content.
1713 xs << XMLStream::ESCAPE_NONE << " – "; // En dash, i.e. semi-long (--).
1714 writeLinkToEntry(root_node->entries[i], entry_number);
1717 if (i < root_node->entries.size() - 1) {
1724 if (!root_node->entries.empty() && !root_node->children.empty()) {
1728 if (!root_node->children.empty()) {
1729 xs << xml::StartTag("ul", "class='" + generateCssClassAtDepth(depth) + "'");
1732 for (const IndexNode* child : root_node->children) {
1733 outputIndexPage(xs, child, depth + 1);
1736 xs << xml::EndTag("ul");
1740 xs << xml::EndTag("li");
1744 #ifdef LYX_INSET_INDEX_DEBUG
1745 void printTree(const IndexNode* root_node, unsigned depth = 0)
1747 static const std::string pattern = " ";
1749 for (unsigned i = 0; i < depth; ++i) {
1752 const std::string prefix_long = prefix + pattern + pattern;
1754 docstring term_at_level;
1756 // The root has no term.
1757 std::cout << "<ROOT>" << std::endl;
1759 LASSERT(depth - 1 <= 10, return); // Check for overflows.
1760 term_at_level = termAtLevel(root_node, depth - 1);
1761 std::cout << prefix << to_utf8(term_at_level) << " (x " << std::to_string(root_node->entries.size()) << ")"
1765 for (const IndexEntry& entry : root_node->entries) {
1766 if (entry.terms().size() != depth) {
1767 std::cout << prefix_long << "ERROR: an entry doesn't have the same number of terms" << std::endl;
1769 if (depth > 0 && entry.terms()[depth - 1] != term_at_level) {
1770 std::cout << prefix_long << "ERROR: an entry doesn't have the right term at depth " << std::to_string(depth)
1775 for (const IndexNode* node : root_node->children) {
1776 printTree(node, depth + 1);
1779 #endif // LYX_INSET_INDEX_DEBUG
1783 docstring InsetPrintIndex::xhtml(XMLStream &, OutputParams const & op) const
1785 BufferParams const & bp = buffer().masterBuffer()->params();
1787 shared_ptr<Toc const> toc = buffer().tocBackend().toc("index");
1791 // Collect the index entries in a form we can use them.
1792 vector<IndexEntry> entries;
1793 const docstring & indexType = params().getParamOr("type", from_ascii("idx"));
1794 for (const TocItem& item : *toc) {
1795 const auto* inset = static_cast<const InsetIndex*>(&(item.dit().inset()));
1796 if (item.isOutput() && inset->params().index == indexType)
1797 entries.emplace_back(IndexEntry{inset, &op});
1800 // If all the index entries are in notes or not displayed, get out sooner.
1801 if (entries.empty())
1804 const IndexNode* index_root = buildIndexTree(entries);
1805 #ifdef LYX_INSET_INDEX_DEBUG
1806 printTree(index_root);
1809 // Start generating the XHTML index.
1810 Layout const & lay = bp.documentClass().htmlTOCLayout();
1811 string const & tocclass = lay.defaultCSSClass();
1812 string const tocattr = "class='index " + tocclass + "'";
1813 docstring const indexName = params().getParamOr("name", from_ascii("Index"));
1815 // we'll use our own stream, because we are going to defer everything.
1816 // that's how we deal with the fact that we're probably inside a standard
1817 // paragraph, and we don't want to be.
1818 odocstringstream ods;
1821 xs << xml::StartTag("div", tocattr);
1823 xs << xml::StartTag(lay.htmltag(), lay.htmlattr());
1824 xs << translateIfPossible(indexName, op.local_font->language()->lang());
1825 xs << xml::EndTag(lay.htmltag());
1827 xs << xml::StartTag("ul", "class='main'");
1830 LASSERT(index_root->entries.empty(), return docstring()); // No index entry should have zero terms.
1831 for (const IndexNode* node : index_root->children) {
1832 outputIndexPage(xs, node);
1835 xs << xml::EndTag("ul");
1837 xs << xml::EndTag("div");