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;
62 typedef Translator<string, InsetIndexParams::PageRange> PageRangeTranslator;
63 typedef Translator<docstring, InsetIndexParams::PageRange> PageRangeTranslatorLoc;
65 PageRangeTranslator const init_insetindexpagerangetranslator()
67 PageRangeTranslator translator("none", InsetIndexParams::None);
68 translator.addPair("start", InsetIndexParams::Start);
69 translator.addPair("end", InsetIndexParams::End);
73 PageRangeTranslator const init_insetindexpagerangetranslator_latex()
75 PageRangeTranslator translator("", InsetIndexParams::None);
76 translator.addPair("(", InsetIndexParams::Start);
77 translator.addPair(")", InsetIndexParams::End);
82 PageRangeTranslatorLoc const init_insetindexpagerangetranslator_loc()
84 PageRangeTranslatorLoc translator(docstring(), InsetIndexParams::None);
85 translator.addPair(_("Starts page range"), InsetIndexParams::Start);
86 translator.addPair(_("Ends page range"), InsetIndexParams::End);
91 PageRangeTranslator const & insetindexpagerangetranslator()
93 static PageRangeTranslator const prtranslator =
94 init_insetindexpagerangetranslator();
99 PageRangeTranslatorLoc const & insetindexpagerangetranslator_loc()
101 static PageRangeTranslatorLoc const translator =
102 init_insetindexpagerangetranslator_loc();
107 PageRangeTranslator const & insetindexpagerangetranslator_latex()
109 static PageRangeTranslator const lttranslator =
110 init_insetindexpagerangetranslator_latex();
116 /////////////////////////////////////////////////////////////////////
120 ///////////////////////////////////////////////////////////////////////
123 InsetIndex::InsetIndex(Buffer * buf, InsetIndexParams const & params)
124 : InsetCollapsible(buf), params_(params)
128 void InsetIndex::latex(otexstream & ios, OutputParams const & runparams_in) const
130 OutputParams runparams(runparams_in);
131 runparams.inIndexEntry = true;
135 if (buffer().masterBuffer()->params().use_indices && !params_.index.empty()
136 && params_.index != "idx") {
138 os << escape(params_.index);
145 // Get the LaTeX output from InsetText. We need to deconstruct this later
146 // in order to check if we need to generate a sorting key
147 odocstringstream ourlatex;
148 otexstream ots(ourlatex);
149 InsetText::latex(ots, runparams);
150 if (runparams.find_effective()) {
151 // No need for special handling, if we are only searching for some patterns
152 os << ourlatex.str() << "}";
157 getSortkey(os, runparams);
159 os << ourlatex.str();
160 getSubentries(os, runparams);
163 os << insetindexpagerangetranslator_latex().find(params_.range);
164 getSeeRefs(os, runparams);
165 } else if (!params_.pagefmt.empty() && params_.pagefmt != "default") {
167 os << insetindexpagerangetranslator_latex().find(params_.range);
168 os << from_utf8(params_.pagefmt);
171 // We check whether we need a sort key.
172 // If so, we use the plaintext version
173 odocstringstream ourplain;
174 InsetText::plaintext(ourplain, runparams);
176 // These are the LaTeX and plaintext representations
177 docstring latexstr = ourlatex.str();
178 docstring plainstr = ourplain.str();
180 // This will get what follows | if anything does,
181 // the command (e.g., see, textbf) for pagination
186 odocstringstream seeref;
187 otexstream otsee(seeref);
188 getSeeRefs(otsee, runparams);
190 } else if (!params_.pagefmt.empty() && params_.pagefmt != "default") {
191 cmd = from_utf8(params_.pagefmt);
193 // Check for the | separator to strip the cmd.
194 // This goes wrong on an escaped "|", but as the escape
195 // character can be changed in style files, we cannot
197 size_t pos = latexstr.find(from_ascii("|"));
198 if (pos != docstring::npos) {
199 // Put the bit after "|" into cmd...
200 cmd = latexstr.substr(pos + 1);
201 // ...and erase that stuff from latexstr
202 latexstr = latexstr.erase(pos);
203 // ...as well as from plainstr
204 size_t ppos = plainstr.find(from_ascii("|"));
205 if (ppos < plainstr.size())
206 plainstr.erase(ppos);
208 LYXERR0("The `|' separator was not found in the plaintext version!");
212 odocstringstream subentries;
213 otexstream otsub(subentries);
214 getSubentries(otsub, runparams);
215 if (subentries.str().empty()) {
216 // Separate the entries and subentries, i.e., split on "!".
217 // This goes wrong on an escaped "!", but as the escape
218 // character can be changed in style files, we cannot
220 std::vector<docstring> const levels =
221 getVectorFromString(latexstr, from_ascii("!"), true);
222 std::vector<docstring> const levels_plain =
223 getVectorFromString(plainstr, from_ascii("!"), true);
225 vector<docstring>::const_iterator it = levels.begin();
226 vector<docstring>::const_iterator end = levels.end();
227 vector<docstring>::const_iterator it2 = levels_plain.begin();
229 for (; it != end; ++it) {
230 // The separator needs to be put back when
231 // writing the levels, except for the first level
237 // Now here comes the reason for this whole procedure:
238 // We try to correctly sort macros and formatted strings.
239 // If we find a command, prepend a plain text
240 // version of the content to get sorting right,
241 // e.g. \index{LyX@\LyX}, \index{text@\textbf{text}}.
242 // We do this on all levels.
243 // We don't do it if the level already contains a '@', though.
244 // Plaintext might return nothing (e.g. for ERTs).
245 // In that case, we use LaTeX.
246 docstring const spart = (levels_plain.empty() || (*it2).empty()) ? *it : *it2;
247 processLatexSorting(os, runparams, *it, spart);
248 if (it2 < levels_plain.end())
252 processLatexSorting(os, runparams, latexstr, plainstr);
253 os << subentries.str();
256 // At last, re-insert the command, separated by "|"
259 << insetindexpagerangetranslator_latex().find(params_.range)
265 // In macros with moving arguments, such as \section,
266 // we store the index and output it after the macro (#2154)
267 if (runparams_in.postpone_fragile_stuff)
268 runparams_in.post_macro += os.str();
274 void InsetIndex::processLatexSorting(otexstream & os, OutputParams const & runparams,
275 docstring const latex, docstring const spart) const
277 if (contains(latex, '\\') && !contains(latex, '@')) {
278 // Now we need to validate that all characters in
279 // the sorting part are representable in the current
280 // encoding. If not try the LaTeX macro which might
281 // or might not be a good choice, and issue a warning.
282 pair<docstring, docstring> spart_latexed =
283 runparams.encoding->latexString(spart, runparams.dryrun);
284 if (!spart_latexed.second.empty())
285 LYXERR0("Uncodable character in index entry. Sorting might be wrong!");
286 if (spart != spart_latexed.first && !runparams.dryrun) {
288 ErrorList & errorList = buffer().errorList("Export");
289 docstring const s = bformat(_("LyX's automatic index sorting algorithm faced "
290 "problems with the entry '%1$s'.\n"
291 "Please specify the sorting of this entry manually, as "
292 "explained in the User Guide."), spart);
293 Paragraph const & par = buffer().paragraphs().front();
294 errorList.push_back(ErrorItem(_("Index sorting failed"), s,
295 {par.id(), 0}, {par.id(), -1}));
296 buffer().bufferErrors(terr, errorList);
298 // Remove remaining \'s from the sort key
299 docstring ppart = subst(spart_latexed.first, from_ascii("\\"), docstring());
300 // Plain quotes need to be escaped, however (#10649), as this
301 // is the default escape character
302 ppart = subst(ppart, from_ascii("\""), from_ascii("\\\""));
304 // Now insert the sortkey, separated by '@'.
308 // Insert the actual level text
313 void InsetIndex::docbook(XMLStream & xs, OutputParams const & runparams) const
315 // Two ways of processing this inset are implemented:
316 // - the legacy one, based on parsing the raw LaTeX (before LyX 2.4) -- unlikely to be deprecated
317 // - the modern one, based on precise insets for indexing features
318 // Like the LaTeX implementation, consider the user chooses either of those options.
320 // Get the content of the inset as LaTeX, as some things may be encoded as ERT (like {}).
321 // TODO: if there is an ERT within the index term, its conversion should be tried, in case it becomes useful;
322 // otherwise, ERTs should become comments. For now, they are just copied as-is, which is barely satisfactory.
323 odocstringstream odss;
324 otexstream ots(odss);
325 InsetText::latex(ots, runparams);
326 docstring latexString = trim(odss.str());
328 // Handle several indices (indicated in the inset instead of the raw latexString).
329 docstring indexType = from_utf8("");
330 if (buffer().masterBuffer()->params().use_indices) {
331 indexType += " type=\"" + params_.index + "\"";
334 // Split the string into its main constituents: terms, and command (see, see also, range).
335 size_t positionVerticalBar = latexString.find(from_ascii("|")); // What comes before | is (sub)(sub)entries.
336 docstring indexTerms = latexString.substr(0, positionVerticalBar);
338 if (positionVerticalBar != lyx::docstring::npos) {
339 command = latexString.substr(positionVerticalBar + 1);
342 // Handle sorting issues, with @.
345 sortAs = getSortkeyAsText(runparams);
346 // indexTerms may contain a sort key if the user has both the inset and the manual key.
348 vector<docstring> sortingElements = getVectorFromString(indexTerms, from_ascii("@"), false);
349 if (sortingElements.size() == 2) {
350 sortAs = sortingElements[0];
351 indexTerms = sortingElements[1];
355 // Handle primary, secondary, and tertiary terms (entries, subentries, and subsubentries, for LaTeX).
356 vector<docstring> terms;
357 if (const vector<docstring> potential_terms = getSubentriesAsText(runparams); !potential_terms.empty()) {
358 terms = potential_terms;
359 // The main term is not present in the vector, as it's not a subentry. The main index term is inserted raw in
360 // the index inset. Considering that the user either uses the new or the legacy mechanism, the main term is the
361 // full string within this inset (i.e. without the subinsets).
362 terms.insert(terms.begin(), latexString);
364 terms = getVectorFromString(indexTerms, from_ascii("!"), false);
367 // Handle ranges. Happily, in the raw LaTeX mode, (| and |) can only be at the end of the string!
368 const bool hasInsetRange = params_.range != InsetIndexParams::PageRange::None;
369 const bool hasStartRange = params_.range == InsetIndexParams::PageRange::Start ||
370 latexString.find(from_ascii("|(")) != lyx::docstring::npos;
371 const bool hasEndRange = params_.range == InsetIndexParams::PageRange::End ||
372 latexString.find(from_ascii("|)")) != lyx::docstring::npos;
375 // Remove the ranges from the command if they do not appear at the beginning.
377 while ((index = command.find(from_utf8("|("), index)) != std::string::npos)
378 command.erase(index, 1);
380 while ((index = command.find(from_utf8("|)"), index)) != std::string::npos)
381 command.erase(index, 1);
383 // Remove the ranges when they are the only vertical bar in the complete string.
384 if (command[0] == '(' || command[0] == ')')
388 // Handle see and seealso. As "see" is a prefix of "seealso", the order of the comparisons is important.
389 // Both commands are mutually exclusive!
390 docstring see = getSeeAsText(runparams);
391 vector<docstring> seeAlsoes = getSeeAlsoesAsText(runparams);
393 if (see.empty() && seeAlsoes.empty() && command.substr(0, 3) == "see") {
394 // Unescape brackets.
396 while ((index = command.find(from_utf8("\\{"), index)) != std::string::npos)
397 command.erase(index, 1);
399 while ((index = command.find(from_utf8("\\}"), index)) != std::string::npos)
400 command.erase(index, 1);
402 // Retrieve the part between brackets, and remove the complete seealso.
403 size_t positionOpeningBracket = command.find(from_ascii("{"));
404 size_t positionClosingBracket = command.find(from_ascii("}"));
405 docstring list = command.substr(positionOpeningBracket + 1, positionClosingBracket - positionOpeningBracket - 1);
407 // Parse the list of referenced entries (or a single one for see).
408 if (command.substr(0, 7) == "seealso") {
409 seeAlsoes = getVectorFromString(list, from_ascii(","), false);
413 if (see.find(from_ascii(",")) != std::string::npos) {
414 docstring error = from_utf8("Several index terms found as \"see\"! Only one is acceptable. "
415 "Complete entry: \"") + latexString + from_utf8("\"");
417 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
421 // Remove the complete see/seealso from the commands, in case there is something else to parse.
422 command = command.substr(positionClosingBracket + 1);
425 // Some parts of the strings are not parsed, as they do not have anything matching in DocBook: things like
426 // formatting the entry or the page number, other strings for sorting. https://wiki.lyx.org/Tips/Indexing
427 // If there are such things in the index entry, then this code may miserably fail. For example, for "Peter|(textbf",
428 // no range will be detected.
429 // TODO: Could handle formatting as significance="preferred"?
430 if (!command.empty()) {
431 docstring error = from_utf8("Unsupported feature: an index entry contains a | with an unsupported command, ")
432 + command + from_utf8(". ") + from_utf8("Complete entry: \"") + latexString + from_utf8("\"");
434 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
437 // Write all of this down.
438 if (terms.empty() && !hasEndRange) {
439 docstring error = from_utf8("No index term found! Complete entry: \"") + latexString + from_utf8("\"");
441 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
443 // Generate the attributes for ranges. It is based on the terms that are indexed, but the ID must be unique
444 // to this indexing area (xml::cleanID does not guarantee this: for each call with the same arguments,
445 // the same legal ID is produced; here, as the input would be the same, the output must be, by design).
446 // Hence the thread-local storage, as the numbers must strictly be unique, and thus cannot be shared across
447 // a paragraph (making the solution used for HTML worthless). This solution is very similar to the one used in
449 // indexType can only be used for singular and startofrange types!
451 if (!hasStartRange && !hasEndRange) {
454 // Append an ID if uniqueness is not guaranteed across the document.
455 static QThreadStorage<set<docstring>> tKnownTermLists;
456 static QThreadStorage<int> tID;
458 set<docstring> &knownTermLists = tKnownTermLists.localData();
459 int &ID = tID.localData();
461 if (!tID.hasLocalData()) {
465 // Modify the index terms to add the unique ID if needed.
466 docstring newIndexTerms = indexTerms;
467 if (knownTermLists.find(indexTerms) != knownTermLists.end()) {
468 newIndexTerms += from_ascii(string("-") + to_string(ID));
470 // Only increment for the end of range, so that the same number is used for the start of range.
476 // Term list not yet known: add it to the set AFTER the end of range. After
477 if (knownTermLists.find(indexTerms) == knownTermLists.end() && hasEndRange) {
478 knownTermLists.insert(indexTerms);
481 // Generate the attributes.
482 docstring id = xml::cleanID(newIndexTerms);
484 attrs = indexType + " class=\"startofrange\" xml:id=\"" + id + "\"";
486 attrs = " class=\"endofrange\" startref=\"" + id + "\"";
490 // Handle the index terms (including the specific index for this entry).
492 xs << xml::CompTag("indexterm", attrs);
494 xs << xml::StartTag("indexterm", attrs);
495 if (!terms.empty()) { // hasEndRange has no content.
497 if (!sortAs.empty()) {
498 attr = from_utf8("sortas='") + sortAs + from_utf8("'");
501 xs << xml::StartTag("primary", attr);
503 xs << xml::EndTag("primary");
505 if (terms.size() > 1) {
506 xs << xml::StartTag("secondary");
508 xs << xml::EndTag("secondary");
510 if (terms.size() > 2) {
511 xs << xml::StartTag("tertiary");
513 xs << xml::EndTag("tertiary");
516 // Handle see and see also.
518 xs << xml::StartTag("see");
520 xs << xml::EndTag("see");
523 if (!seeAlsoes.empty()) {
524 for (auto &entry : seeAlsoes) {
525 xs << xml::StartTag("seealso");
527 xs << xml::EndTag("seealso");
532 xs << xml::EndTag("indexterm");
538 docstring InsetIndex::xhtml(XMLStream & xs, OutputParams const &) const
540 // we just print an anchor, taking the paragraph ID from
541 // our own interior paragraph, which doesn't get printed
542 std::string const magic = paragraphs().front().magicLabel();
543 std::string const attr = "id='" + magic + "'";
544 xs << xml::CompTag("a", attr);
549 bool InsetIndex::showInsetDialog(BufferView * bv) const
551 bv->showDialog("index", params2string(params_),
552 const_cast<InsetIndex *>(this));
557 void InsetIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
559 switch (cmd.action()) {
561 case LFUN_INSET_MODIFY: {
562 if (cmd.getArg(0) == "changetype") {
563 cur.recordUndoInset(this);
564 params_.index = from_utf8(cmd.getArg(1));
567 InsetIndexParams params;
568 InsetIndex::string2params(to_utf8(cmd.argument()), params);
569 cur.recordUndoInset(this);
570 params_.index = params.index;
571 params_.range = params.range;
572 params_.pagefmt = params.pagefmt;
573 // what we really want here is a TOC update, but that means
574 // a full buffer update
575 cur.forceBufferUpdate();
579 case LFUN_INSET_DIALOG_UPDATE:
580 cur.bv().updateDialog("index", params2string(params_));
583 case LFUN_PARAGRAPH_BREAK: {
584 // Since this inset in single-par anyway, let's use
585 // return to enter subentries
586 FuncRequest fr(LFUN_INDEXMACRO_INSERT, "subentry");
592 InsetCollapsible::doDispatch(cur, cmd);
598 bool InsetIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
599 FuncStatus & flag) const
601 switch (cmd.action()) {
603 case LFUN_INSET_MODIFY:
604 if (cmd.getArg(0) == "changetype") {
605 docstring const newtype = from_utf8(cmd.getArg(1));
606 Buffer const & realbuffer = *buffer().masterBuffer();
607 IndicesList const & indiceslist = realbuffer.params().indiceslist();
608 Index const * index = indiceslist.findShortcut(newtype);
609 flag.setEnabled(index != 0);
611 from_utf8(cmd.getArg(1)) == params_.index);
614 return InsetCollapsible::getStatus(cur, cmd, flag);
616 case LFUN_INSET_DIALOG_UPDATE: {
617 Buffer const & realbuffer = *buffer().masterBuffer();
618 flag.setEnabled(realbuffer.params().use_indices);
622 case LFUN_PARAGRAPH_BREAK:
623 return macrosPossible("subentry");
625 case LFUN_INDEXMACRO_INSERT:
626 return macrosPossible(cmd.getArg(0));
629 return InsetCollapsible::getStatus(cur, cmd, flag);
634 void InsetIndex::getSortkey(otexstream & os, OutputParams const & runparams) const
636 Paragraph const & par = paragraphs().front();
637 InsetList::const_iterator it = par.insetList().begin();
638 for (; it != par.insetList().end(); ++it) {
639 Inset & inset = *it->inset;
640 if (inset.lyxCode() == INDEXMACRO_SORTKEY_CODE) {
641 InsetIndexMacro const & iim =
642 static_cast<InsetIndexMacro const &>(inset);
643 iim.getLatex(os, runparams);
650 docstring InsetIndex::getSortkeyAsText(OutputParams const & runparams) const
652 Paragraph const & par = paragraphs().front();
653 InsetList::const_iterator it = par.insetList().begin();
654 for (; it != par.insetList().end(); ++it) {
655 Inset & inset = *it->inset;
656 if (inset.lyxCode() == INDEXMACRO_SORTKEY_CODE) {
658 InsetIndexMacro const & iim =
659 static_cast<InsetIndexMacro const &>(inset);
660 iim.getLatex(os, runparams);
664 return from_ascii("");
668 void InsetIndex::getSubentries(otexstream & os, OutputParams const & runparams) const
670 Paragraph const & par = paragraphs().front();
671 InsetList::const_iterator it = par.insetList().begin();
673 for (; it != par.insetList().end(); ++it) {
674 Inset & inset = *it->inset;
675 if (inset.lyxCode() == INDEXMACRO_CODE) {
676 InsetIndexMacro const & iim =
677 static_cast<InsetIndexMacro const &>(inset);
678 if (iim.params().type == InsetIndexMacroParams::Subentry) {
683 iim.getLatex(os, runparams);
690 std::vector<docstring> InsetIndex::getSubentriesAsText(OutputParams const & runparams,
691 bool const asLabel) const
693 std::vector<docstring> subentries;
695 Paragraph const & par = paragraphs().front();
696 InsetList::const_iterator it = par.insetList().begin();
698 for (; it != par.insetList().end(); ++it) {
699 Inset & inset = *it->inset;
700 if (inset.lyxCode() == INDEXMACRO_CODE) {
701 InsetIndexMacro const & iim =
702 static_cast<InsetIndexMacro const &>(inset);
703 if (iim.params().type == InsetIndexMacroParams::Subentry) {
709 docstring const sl = iim.getNewLabel(l);
710 subentries.emplace_back(sl);
713 iim.getLatex(os, runparams);
714 subentries.emplace_back(os.str());
724 docstring InsetIndex::getMainSubentryAsText(OutputParams const & runparams) const
727 InsetText::latex(os, runparams);
732 void InsetIndex::getSeeRefs(otexstream & os, OutputParams const & runparams) const
734 Paragraph const & par = paragraphs().front();
735 InsetList::const_iterator it = par.insetList().begin();
736 for (; it != par.insetList().end(); ++it) {
737 Inset & inset = *it->inset;
738 if (inset.lyxCode() == INDEXMACRO_CODE) {
739 InsetIndexMacro const & iim =
740 static_cast<InsetIndexMacro const &>(inset);
741 if (iim.params().type == InsetIndexMacroParams::See
742 || iim.params().type == InsetIndexMacroParams::Seealso) {
743 iim.getLatex(os, runparams);
751 docstring InsetIndex::getSeeAsText(OutputParams const & runparams) const
753 Paragraph const & par = paragraphs().front();
754 InsetList::const_iterator it = par.insetList().begin();
755 for (; it != par.insetList().end(); ++it) {
756 Inset & inset = *it->inset;
757 if (inset.lyxCode() == INDEXMACRO_CODE) {
758 InsetIndexMacro const & iim =
759 static_cast<InsetIndexMacro const &>(inset);
760 if (iim.params().type == InsetIndexMacroParams::See) {
762 iim.getLatex(os, runparams);
767 return from_ascii("");
771 std::vector<docstring> InsetIndex::getSeeAlsoesAsText(OutputParams const & runparams) const
773 std::vector<docstring> seeAlsoes;
775 Paragraph const & par = paragraphs().front();
776 InsetList::const_iterator it = par.insetList().begin();
777 for (; it != par.insetList().end(); ++it) {
778 Inset & inset = *it->inset;
779 if (inset.lyxCode() == INDEXMACRO_CODE) {
780 InsetIndexMacro const & iim =
781 static_cast<InsetIndexMacro const &>(inset);
782 if (iim.params().type == InsetIndexMacroParams::Seealso) {
784 iim.getLatex(os, runparams);
785 seeAlsoes.emplace_back(os.str());
796 bool hasInsetWithCode(const InsetIndex * const inset_index, const InsetCode code,
797 const std::set<InsetIndexMacroParams::Type> types = {})
799 Paragraph const & par = inset_index->paragraphs().front();
800 InsetList::const_iterator it = par.insetList().begin();
801 for (; it != par.insetList().end(); ++it) {
802 Inset & inset = *it->inset;
803 if (inset.lyxCode() == code) {
807 LASSERT(code == INDEXMACRO_CODE, return false);
808 InsetIndexMacro const & iim =
809 static_cast<InsetIndexMacro const &>(inset);
810 if (types.find(iim.params().type) != types.end())
820 bool InsetIndex::hasSubentries() const
822 return hasInsetWithCode(this, INDEXMACRO_CODE, {InsetIndexMacroParams::Subentry});
826 bool InsetIndex::hasSeeRef() const
828 return hasInsetWithCode(this, INDEXMACRO_CODE, {InsetIndexMacroParams::See, InsetIndexMacroParams::Seealso});
832 bool InsetIndex::hasSortKey() const
834 return hasInsetWithCode(this, INDEXMACRO_SORTKEY_CODE);
838 bool InsetIndex::macrosPossible(string const type) const
840 if (type != "see" && type != "seealso"
841 && type != "sortkey" && type != "subentry")
844 Paragraph const & par = paragraphs().front();
845 InsetList::const_iterator it = par.insetList().begin();
847 for (; it != par.insetList().end(); ++it) {
848 Inset & inset = *it->inset;
849 if (type == "sortkey" && inset.lyxCode() == INDEXMACRO_SORTKEY_CODE)
851 if (inset.lyxCode() == INDEXMACRO_CODE) {
852 InsetIndexMacro const & iim = static_cast<InsetIndexMacro const &>(inset);
853 if ((type == "see" || type == "seealso")
854 && (iim.params().type == InsetIndexMacroParams::See
855 || iim.params().type == InsetIndexMacroParams::Seealso))
857 if (type == "subentry"
858 && iim.params().type == InsetIndexMacroParams::Subentry) {
869 ColorCode InsetIndex::labelColor() const
871 if (params_.index.empty() || params_.index == from_ascii("idx"))
872 return InsetCollapsible::labelColor();
874 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.index)
875 + "@" + buffer().fileName().absFileName());
877 c = InsetCollapsible::labelColor();
882 docstring InsetIndex::toolTip(BufferView const &, int, int) const
884 docstring tip = _("Index Entry");
885 if (buffer().params().use_indices && !params_.index.empty()) {
886 Buffer const & realbuffer = *buffer().masterBuffer();
887 IndicesList const & indiceslist = realbuffer.params().indiceslist();
889 Index const * index = indiceslist.findShortcut(params_.index);
891 tip += _("unknown type!");
893 tip += index->index();
897 docstring res = toolTipText(tip);
898 if (!insetindexpagerangetranslator_loc().find(params_.range).empty())
899 res += "\n" + insetindexpagerangetranslator_loc().find(params_.range);
900 if (!params_.pagefmt.empty() && params_.pagefmt != "default") {
901 res += "\n" + _("Pagination format:") + " ";
902 if (params_.pagefmt == "textbf")
904 else if (params_.pagefmt == "textit")
906 else if (params_.pagefmt == "emph")
907 res += _("emphasized");
909 res += from_utf8(params_.pagefmt);
915 docstring const InsetIndex::buttonLabel(BufferView const & bv) const
917 InsetLayout const & il = getLayout();
918 docstring label = translateIfPossible(il.labelstring());
920 if (buffer().params().use_indices && !params_.index.empty()) {
921 Buffer const & realbuffer = *buffer().masterBuffer();
922 IndicesList const & indiceslist = realbuffer.params().indiceslist();
924 Index const * index = indiceslist.findShortcut(params_.index);
926 label += _("unknown type!");
928 label += index->index();
933 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
936 res = getNewLabel(label);
937 OutputParams const rp(0);
938 vector<docstring> sublbls = getSubentriesAsText(rp, true);
939 for (auto const & sublbl : sublbls) {
940 res += " " + docstring(1, char_type(0x2023));// TRIANGULAR BULLET
944 if (!insetindexpagerangetranslator_latex().find(params_.range).empty())
945 res += " " + from_ascii(insetindexpagerangetranslator_latex().find(params_.range));
950 void InsetIndex::write(ostream & os) const
952 os << to_utf8(layoutName());
954 InsetCollapsible::write(os);
958 void InsetIndex::read(Lexer & lex)
961 InsetCollapsible::read(lex);
965 string InsetIndex::params2string(InsetIndexParams const & params)
974 void InsetIndex::string2params(string const & in, InsetIndexParams & params)
976 params = InsetIndexParams();
980 istringstream data(in);
983 lex.setContext("InsetIndex::string2params");
989 void InsetIndex::addToToc(DocIterator const & cpit, bool output_active,
990 UpdateType utype, TocBackend & backend) const
992 DocIterator pit = cpit;
993 pit.push_back(CursorSlice(const_cast<InsetIndex &>(*this)));
995 InsetLayout const & il = getLayout();
996 docstring label = translateIfPossible(il.labelstring());
997 if (!il.contentaslabel())
1000 str = getNewLabel(label);
1001 OutputParams const rp(0);
1002 vector<docstring> sublbls = getSubentriesAsText(rp, true);
1003 for (auto const & sublbl : sublbls) {
1004 str += " " + docstring(1, char_type(0x2023));// TRIANGULAR BULLET
1005 str += " " + sublbl;
1008 string type = "index";
1009 if (buffer().masterBuffer()->params().use_indices)
1010 type += ":" + to_utf8(params_.index);
1011 TocBuilder & b = backend.builder(type);
1012 b.pushItem(pit, str, output_active);
1013 // Proceed with the rest of the inset.
1014 InsetCollapsible::addToToc(cpit, output_active, utype, backend);
1019 void InsetIndex::validate(LaTeXFeatures & features) const
1021 if (buffer().masterBuffer()->params().use_indices
1022 && !params_.index.empty()
1023 && params_.index != "idx")
1024 features.require("splitidx");
1025 InsetCollapsible::validate(features);
1029 string InsetIndex::contextMenuName() const
1031 return "context-index";
1035 string InsetIndex::contextMenu(BufferView const & bv, int x, int y) const
1037 // We override the implementation of InsetCollapsible,
1038 // because we have eytra entries.
1039 string owncm = "context-edit-index;";
1040 return owncm + InsetCollapsible::contextMenu(bv, x, y);
1044 bool InsetIndex::hasSettings() const
1050 bool InsetIndex::insetAllowed(InsetCode code) const
1053 case INDEXMACRO_CODE:
1054 case INDEXMACRO_SORTKEY_CODE:
1059 return InsetCollapsible::insetAllowed(code);
1064 /////////////////////////////////////////////////////////////////////
1068 ///////////////////////////////////////////////////////////////////////
1071 void InsetIndexParams::write(ostream & os) const
1075 os << to_utf8(index);
1080 << insetindexpagerangetranslator().find(range)
1088 void InsetIndexParams::read(Lexer & lex)
1091 index = lex.getDocString();
1093 index = from_ascii("idx");
1094 if (lex.checkFor("range")) {
1095 string st = lex.getString();
1096 if (lex.eatLine()) {
1097 st = lex.getString();
1098 range = insetindexpagerangetranslator().find(lex.getString());
1101 if (lex.checkFor("pageformat") && lex.eatLine()) {
1102 pagefmt = lex.getString();
1107 /////////////////////////////////////////////////////////////////////
1111 ///////////////////////////////////////////////////////////////////////
1113 InsetPrintIndex::InsetPrintIndex(Buffer * buf, InsetCommandParams const & p)
1114 : InsetCommand(buf, p)
1118 ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
1120 static ParamInfo param_info_;
1121 if (param_info_.empty()) {
1122 param_info_.add("type", ParamInfo::LATEX_OPTIONAL,
1123 ParamInfo::HANDLING_ESCAPE);
1124 param_info_.add("name", ParamInfo::LATEX_OPTIONAL,
1125 ParamInfo::HANDLING_LATEXIFY);
1126 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
1132 docstring InsetPrintIndex::screenLabel() const
1134 bool const printall = suffixIs(getCmdName(), '*');
1135 bool const multind = buffer().masterBuffer()->params().use_indices;
1137 && getParam("type") == from_ascii("idx"))
1138 || (getParam("type").empty() && !printall))
1140 Buffer const & realbuffer = *buffer().masterBuffer();
1141 IndicesList const & indiceslist = realbuffer.params().indiceslist();
1142 Index const * index = indiceslist.findShortcut(getParam("type"));
1143 if (!index && !printall)
1144 return _("Unknown index type!");
1145 docstring res = printall ? _("All indexes") : index->index();
1147 res += " (" + _("non-active") + ")";
1148 else if (contains(getCmdName(), "printsubindex"))
1149 res += " (" + _("subindex") + ")";
1154 bool InsetPrintIndex::isCompatibleCommand(string const & s)
1156 return s == "printindex" || s == "printsubindex"
1157 || s == "printindex*" || s == "printsubindex*";
1161 void InsetPrintIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
1163 switch (cmd.action()) {
1165 case LFUN_INSET_MODIFY: {
1166 if (cmd.argument() == from_ascii("toggle-subindex")) {
1167 string scmd = getCmdName();
1168 if (contains(scmd, "printindex"))
1169 scmd = subst(scmd, "printindex", "printsubindex");
1171 scmd = subst(scmd, "printsubindex", "printindex");
1175 } else if (cmd.argument() == from_ascii("check-printindex*")) {
1176 string scmd = getCmdName();
1177 if (suffixIs(scmd, '*'))
1181 setParam("type", docstring());
1185 InsetCommandParams p(INDEX_PRINT_CODE);
1187 InsetCommand::string2params(to_utf8(cmd.argument()), p);
1188 if (p.getCmdName().empty()) {
1189 cur.noScreenUpdate();
1198 InsetCommand::doDispatch(cur, cmd);
1204 bool InsetPrintIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
1205 FuncStatus & status) const
1207 switch (cmd.action()) {
1209 case LFUN_INSET_MODIFY: {
1210 if (cmd.argument() == from_ascii("toggle-subindex")) {
1211 status.setEnabled(buffer().masterBuffer()->params().use_indices);
1212 status.setOnOff(contains(getCmdName(), "printsubindex"));
1214 } else if (cmd.argument() == from_ascii("check-printindex*")) {
1215 status.setEnabled(buffer().masterBuffer()->params().use_indices);
1216 status.setOnOff(suffixIs(getCmdName(), '*'));
1218 } if (cmd.getArg(0) == "index_print"
1219 && cmd.getArg(1) == "CommandInset") {
1220 InsetCommandParams p(INDEX_PRINT_CODE);
1221 InsetCommand::string2params(to_utf8(cmd.argument()), p);
1222 if (suffixIs(p.getCmdName(), '*')) {
1223 status.setEnabled(true);
1224 status.setOnOff(false);
1227 Buffer const & realbuffer = *buffer().masterBuffer();
1228 IndicesList const & indiceslist =
1229 realbuffer.params().indiceslist();
1230 Index const * index = indiceslist.findShortcut(p["type"]);
1231 status.setEnabled(index != 0);
1232 status.setOnOff(p["type"] == getParam("type"));
1235 return InsetCommand::getStatus(cur, cmd, status);
1238 case LFUN_INSET_DIALOG_UPDATE: {
1239 status.setEnabled(buffer().masterBuffer()->params().use_indices);
1244 return InsetCommand::getStatus(cur, cmd, status);
1249 void InsetPrintIndex::updateBuffer(ParIterator const &, UpdateType, bool const /*deleted*/)
1251 Index const * index =
1252 buffer().masterParams().indiceslist().findShortcut(getParam("type"));
1254 setParam("name", index->index());
1258 void InsetPrintIndex::latex(otexstream & os, OutputParams const & runparams_in) const
1260 if (!buffer().masterBuffer()->params().use_indices) {
1261 if (getParam("type") == from_ascii("idx"))
1262 os << "\\printindex" << termcmd;
1265 OutputParams runparams = runparams_in;
1266 os << getCommand(runparams);
1270 void InsetPrintIndex::validate(LaTeXFeatures & features) const
1272 features.require("makeidx");
1273 if (buffer().masterBuffer()->params().use_indices)
1274 features.require("splitidx");
1275 InsetCommand::validate(features);
1279 string InsetPrintIndex::contextMenuName() const
1281 return buffer().masterBuffer()->params().use_indices ?
1282 "context-indexprint" : string();
1286 bool InsetPrintIndex::hasSettings() const
1288 return buffer().masterBuffer()->params().use_indices;
1295 /// Builds an entry for the index.
1296 IndexEntry(const InsetIndex * inset, OutputParams const * runparams) : inset_(inset), runparams_(runparams)
1298 LASSERT(runparams, return);
1300 // Convert the inset as text. The resulting text usually only contains an XHTML anchor (<a id='...'/>) and text.
1301 odocstringstream entry;
1302 OutputParams ours = *runparams;
1303 ours.for_toc = false;
1304 inset_->plaintext(entry, ours);
1305 entry_ = entry.str();
1307 // Determine in which index this entry belongs to.
1308 if (inset_->buffer().masterBuffer()->params().use_indices) {
1309 index_ = inset_->params_.index;
1312 // Attempt parsing the inset.
1319 /// When parsing this entry, some errors may be found; they are reported as a single string.
1320 // It is up to the caller to send this string to LYXERR and the output file, as needed.
1321 const docstring & output_error() const
1323 return output_error_;
1326 void output_error(XMLStream xs) const
1328 LYXERR0(output_error());
1329 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + output_error() + from_utf8(" -->\n"));
1336 std::cout << to_utf8(entry_) << std::endl;
1338 // If a modern parameter is present, this is definitely a modern index inset. Similarly, if it contains the
1339 // usual LaTeX symbols (!|@), then it is definitely a legacy index inset. Otherwise, if it has features of
1340 // neither, it is both: consider this is a modern inset, to trigger the least complex code. Mixing both types
1341 // is not allowed (i.e. behaviour is undefined).
1342 const bool is_definitely_modern = inset_->hasSortKey() || inset_->hasSeeRef() || inset_->hasSubentries()
1343 || inset_->params_.range != InsetIndexParams::PageRange::None;
1344 const bool is_definitely_legacy = entry_.find('@') != std::string::npos
1345 || entry_.find('|') != std::string::npos || entry_.find('!') != std::string::npos;
1347 if (is_definitely_legacy && is_definitely_modern)
1348 output_error_ += from_utf8("Mix of index properties and raw LaTeX index commands is unsupported. ");
1351 // - is_definitely_modern == true:
1352 // - is_definitely_legacy == true: error (return whatever)
1353 // - is_definitely_legacy == false: return modern
1354 // - is_definitely_modern == false:
1355 // - is_definitely_legacy == true: return legacy
1356 // - is_definitely_legacy == false: return modern
1357 return !is_definitely_legacy;
1360 void parseAsModern()
1362 LASSERT(runparams_, return);
1364 if (inset_->hasSortKey()) {
1365 sort_as_ = inset_->getSortkeyAsText(*runparams_);
1368 terms_ = inset_->getSubentriesAsText(*runparams_);
1369 // The main term is not present in the vector, as it's not a subentry. The main index term is inserted raw in
1370 // the index inset. Considering that the user either uses the new or the legacy mechanism, the main term is the
1371 // full string within this inset (i.e. without the subinsets).
1372 terms_.insert(terms_.begin(), inset_->getMainSubentryAsText(*runparams_));
1374 has_start_range_ = inset_->params_.range == InsetIndexParams::PageRange::Start;
1375 has_end_range_ = inset_->params_.range == InsetIndexParams::PageRange::End;
1377 see_ = inset_->getSeeAsText(*runparams_);
1378 see_alsoes_ = inset_->getSeeAlsoesAsText(*runparams_);
1381 void parseAsLegacy() {
1382 // Determine if some features are known not to be supported. For now, this is only formatting like
1383 // \index{alpha@\textbf{alpha}} or \index{alpha@$\alpha$}.
1384 // @ is supported, but only for sorting, without specific formatting.
1385 if (entry_.find(from_utf8("@\\")) != lyx::docstring::npos) {
1386 output_error_ += from_utf8("Unsupported feature: an index entry contains an @\\. "
1387 "Complete entry: \"") + entry_ + from_utf8("\". ");
1389 if (entry_.find(from_utf8("@$")) != lyx::docstring::npos) {
1390 output_error_ += from_utf8("Unsupported feature: an index entry contains an @$. "
1391 "Complete entry: \"") + entry_ + from_utf8("\". ");
1394 // Split the string into its main constituents: terms, and command (see, see also, range).
1395 size_t positionVerticalBar = entry_.find(from_ascii("|")); // What comes before | is (sub)(sub)entries.
1396 docstring indexTerms = entry_.substr(0, positionVerticalBar);
1398 if (positionVerticalBar != lyx::docstring::npos) {
1399 command = entry_.substr(positionVerticalBar + 1);
1402 // Handle sorting issues, with @.
1403 vector<docstring> sortingElements = getVectorFromString(indexTerms, from_ascii("@"), false);
1404 if (sortingElements.size() == 2) {
1405 sort_as_ = sortingElements[0];
1406 indexTerms = sortingElements[1];
1409 // Handle entries, subentries, and subsubentries.
1410 terms_ = getVectorFromString(indexTerms, from_ascii("!"), false);
1412 // Handle ranges. Happily, (| and |) can only be at the end of the string!
1413 has_start_range_ = entry_.find(from_ascii("|(")) != lyx::docstring::npos;
1414 has_end_range_ = entry_.find(from_ascii("|)")) != lyx::docstring::npos;
1416 // - Remove the ranges from the command if they do not appear at the beginning.
1417 size_t range_index = 0;
1418 while ((range_index = command.find(from_utf8("|("), range_index)) != std::string::npos)
1419 command.erase(range_index, 1);
1421 while ((range_index = command.find(from_utf8("|)"), range_index)) != std::string::npos)
1422 command.erase(range_index, 1);
1424 // - Remove the ranges when they are the only vertical bar in the complete string.
1425 if (command[0] == '(' || command[0] == ')')
1426 command.erase(0, 1);
1428 // Handle see and seealso. As "see" is a prefix of "seealso", the order of the comparisons is important.
1429 // Both commands are mutually exclusive!
1430 if (command.substr(0, 3) == "see") {
1431 // Unescape brackets.
1432 size_t index_argument_begin = 0;
1433 while ((index_argument_begin = command.find(from_utf8("\\{"), index_argument_begin)) != std::string::npos)
1434 command.erase(index_argument_begin, 1);
1435 size_t index_argument_end = 0;
1436 while ((index_argument_end = command.find(from_utf8("\\}"), index_argument_end)) != std::string::npos)
1437 command.erase(index_argument_end, 1);
1439 // Retrieve the part between brackets, and remove the complete seealso.
1440 size_t position_opening_bracket = command.find(from_ascii("{"));
1441 size_t position_closing_bracket = command.find(from_ascii("}"));
1442 docstring argument = command.substr(position_opening_bracket + 1,
1443 position_closing_bracket - position_opening_bracket - 1);
1445 // Parse the argument of referenced entries (or a single one for see).
1446 if (command.substr(0, 7) == "seealso") {
1447 see_alsoes_ = getVectorFromString(argument, from_ascii(","), false);
1451 if (see_.find(from_ascii(",")) != std::string::npos) {
1452 output_error_ += from_utf8("Several index_argument_end terms found as \"see\"! Only one is "
1453 "acceptable. Complete entry: \"") + entry_ + from_utf8("\". ");
1457 // Remove the complete see/seealso from the commands, in case there is something else to parse.
1458 command = command.substr(position_closing_bracket + 1);
1461 // Some parts of the strings are not parsed, as they do not have anything matching in DocBook or XHTML:
1462 // things like formatting the entry or the page number, other strings for sorting.
1463 // https://wiki.lyx.org/Tips/Indexing
1464 // If there are such things in the index entry, then this code may miserably fail. For example, for
1465 // "Peter|(textbf", no range will be detected.
1466 if (!command.empty()) {
1467 output_error_ += from_utf8("Unsupported feature: an index entry contains a | with an unsupported command, ")
1468 + command + from_utf8(". Complete entry: \"") + entry_ + from_utf8("\". ");
1474 return terms_.size();
1477 const std::vector<docstring>& terms() const {
1481 std::vector<docstring>& terms() {
1485 const InsetIndex* inset() const {
1490 // Input inset. These should only be used when parsing the inset (either parseAsModern or parseAsLegacy, called in
1491 // the constructor).
1492 const InsetIndex * inset_;
1493 OutputParams const * runparams_;
1495 docstring index_; // Useful when there are multiple indices in the same document.
1497 // Errors, concatenated as a single string, available as soon as parsing is done, const afterwards (i.e. once
1498 // constructor is done).
1499 docstring output_error_;
1501 // Parsed index entry.
1502 std::vector<docstring> terms_; // Up to three entries, in general.
1505 bool has_start_range_;
1506 bool has_end_range_;
1508 vector<docstring> see_alsoes_;
1510 // Operators used for sorting entries (alphabetical order).
1511 friend bool operator<(IndexEntry const & lhs, IndexEntry const & rhs);
1514 bool operator<(IndexEntry const & lhs, IndexEntry const & rhs)
1516 if (lhs.terms_.empty())
1519 for (unsigned i = 0; i < min(rhs.terms_.size(), lhs.terms_.size()); ++i) {
1520 int comp = compare_no_case(lhs.terms_[i], rhs.terms_[i]);
1529 std::string generateCssClassAtDepth(unsigned depth) {
1530 std::string css_class = "entry";
1534 css_class.insert(0, "sub");
1541 std::vector<IndexEntry> entries;
1542 std::vector<IndexNode*> children;
1545 docstring termAtLevel(const IndexNode* node, unsigned depth)
1547 // 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
1548 // function could be made constant time by copying the term in each node, but that would make data duplication that
1549 // may fall out of sync; the performance benefit would probably be negligible.
1550 if (!node->entries.empty()) {
1551 LASSERT(node->entries.begin()->terms().size() >= depth + 1, return from_ascii(""));
1552 return node->entries.begin()->terms()[depth];
1555 if (!node->children.empty()) {
1556 return termAtLevel(*node->children.begin(), depth);
1559 LASSERT(false, return from_ascii(""));
1562 void insertIntoNode(const IndexEntry& entry, IndexNode* node, unsigned depth = 0)
1564 // depth == 0 is for the root, not yet the index, hence the increase when going to vector size.
1565 for (IndexNode* child : node->children) {
1566 if (entry.terms()[depth] == termAtLevel(child, depth)) {
1567 if (depth + 1 == entry.terms().size()) { // == child.entries.begin()->terms().size()
1568 // All term entries match: it's an entry.
1569 child->entries.emplace_back(entry);
1572 insertIntoNode(entry, child, depth + 1);
1578 // Out of the loop: no matching child found, create a new (possibly nested) child for this entry. Due to the
1579 // possibility of nestedness, only insert the current entry when the right level is reached. This is needed if the
1580 // first entry for a word has several levels that never appeared.
1581 // In particular, this case is called for the first entry.
1582 IndexNode* new_node = node;
1584 new_node->children.emplace_back(new IndexNode{{}, {}});
1585 new_node = new_node->children.back();
1587 } while (depth + 1 <= entry.terms().size()); // depth == 0: root node, no text associated.
1588 new_node->entries.emplace_back(entry);
1591 IndexNode* buildIndexTree(vector<IndexEntry>& entries)
1593 // Sort the entries, first on the main entry, then the subentry, then the subsubentry,
1594 // thanks to the implementation of operator<.
1595 // If this operation is not performed, the algorithm below is no more correct (and ensuring that it works with
1596 // unsorted entries would make its complexity blow up).
1597 stable_sort(entries.begin(), entries.end());
1599 // Cook the index into a nice tree data structure: entries at a given level in the index as a node, with subentries
1601 auto* index_root = new IndexNode{{}, {}};
1602 for (const IndexEntry& entry : entries) {
1603 insertIntoNode(entry, index_root);
1609 void outputIndexPage(XMLStream & xs, const IndexNode* root_node, unsigned depth = 0) // NOLINT(misc-no-recursion)
1611 LASSERT(root_node->entries.size() + root_node->children.size() > 0, return);
1613 xs << xml::StartTag("li", "class='" + generateCssClassAtDepth(depth) + "'");
1615 xs << XMLStream::ESCAPE_NONE << termAtLevel(root_node, depth);
1616 // By tree assumption, all the entries at this node have the same set of terms.
1618 if (!root_node->entries.empty()) {
1619 xs << XMLStream::ESCAPE_NONE << " — "; // Em dash, i.e. long (---).
1620 unsigned entry_number = 1;
1622 auto writeLinkToEntry = [&xs](const IndexEntry &entry, unsigned entry_number) {
1623 std::string const link_attr = "href='#" + entry.inset()->paragraphs()[0].magicLabel() + "'";
1624 xs << xml::StartTag("a", link_attr);
1625 xs << from_ascii(std::to_string(entry_number));
1626 xs << xml::EndTag("a");
1629 for (unsigned i = 0; i < root_node->entries.size(); ++i) {
1630 const IndexEntry &entry = root_node->entries[i];
1632 switch (entry.inset()->params().range) {
1633 case InsetIndexParams::PageRange::None:
1634 writeLinkToEntry(entry, entry_number);
1636 case InsetIndexParams::PageRange::Start: {
1637 // Try to find the end of the range, if it is just after. Otherwise, the output will be slightly
1638 // scrambled, but understandable. Doing better would mean implementing more of the indexing logic here
1639 // and more complex indexing here (skipping the end is not just incrementing i). Worst case output:
1641 const bool nextEntryIsEnd = i + 1 < root_node->entries.size() &&
1642 root_node->entries[i + 1].inset()->params().range ==
1643 InsetIndexParams::PageRange::End;
1644 // No need to check if both entries are for the same terms: they are in the same IndexNode.
1646 writeLinkToEntry(entry, entry_number);
1647 xs << XMLStream::ESCAPE_NONE << " – "; // En dash, i.e. semi-long (--).
1649 if (nextEntryIsEnd) {
1650 // Skip the next entry in the loop, write it right now, after the dash.
1653 writeLinkToEntry(root_node->entries[i], entry_number);
1657 case InsetIndexParams::PageRange::End:
1658 // This range end was not caught by the range start, do it now to avoid losing content.
1659 xs << XMLStream::ESCAPE_NONE << " – "; // En dash, i.e. semi-long (--).
1660 writeLinkToEntry(root_node->entries[i], entry_number);
1663 if (i < root_node->entries.size() - 1) {
1670 if (!root_node->entries.empty() && !root_node->children.empty()) {
1674 if (!root_node->children.empty()) {
1675 xs << xml::StartTag("ul", "class='" + generateCssClassAtDepth(depth) + "'");
1678 for (const IndexNode* child : root_node->children) {
1679 outputIndexPage(xs, child, depth + 1);
1682 xs << xml::EndTag("ul");
1686 xs << xml::EndTag("li");
1690 // Only useful for debugging.
1691 void printTree(const IndexNode* root_node, unsigned depth = 0)
1693 static const std::string pattern = " ";
1695 for (unsigned i = 0; i < depth; ++i) {
1698 const std::string prefix_long = prefix + pattern + pattern;
1700 docstring term_at_level;
1702 // The root has no term.
1703 std::cout << "<ROOT>" << std::endl;
1705 LASSERT(depth - 1 <= 10, return); // Check for overflows.
1706 term_at_level = termAtLevel(root_node, depth - 1);
1707 std::cout << prefix << to_utf8(term_at_level) << " (x " << std::to_string(root_node->entries.size()) << ")"
1711 for (const IndexEntry& entry : root_node->entries) {
1712 if (entry.terms().size() != depth) {
1713 std::cout << prefix_long << "ERROR: an entry doesn't have the same number of terms" << std::endl;
1715 if (depth > 0 && entry.terms()[depth - 1] != term_at_level) {
1716 std::cout << prefix_long << "ERROR: an entry doesn't have the right term at depth " << std::to_string(depth)
1721 for (const IndexNode* node : root_node->children) {
1722 printTree(node, depth + 1);
1728 docstring InsetPrintIndex::xhtml(XMLStream &, OutputParams const & op) const
1730 BufferParams const & bp = buffer().masterBuffer()->params();
1732 shared_ptr<Toc const> toc = buffer().tocBackend().toc("index");
1736 // Collect the index entries in a form we can use them.
1737 vector<IndexEntry> entries;
1738 const docstring & indexType = params().getParamOr("type", from_ascii("idx"));
1739 for (const TocItem& item : *toc) {
1740 const auto* inset = static_cast<const InsetIndex*>(&(item.dit().inset()));
1741 if (item.isOutput() && inset->params().index == indexType)
1742 entries.emplace_back(IndexEntry{inset, &op});
1745 // If all the index entries are in notes or not displayed, get out sooner.
1746 if (entries.empty())
1749 const IndexNode* index_root = buildIndexTree(entries);
1751 printTree(index_root);
1754 // Start generating the XHTML index.
1755 Layout const & lay = bp.documentClass().htmlTOCLayout();
1756 string const & tocclass = lay.defaultCSSClass();
1757 string const tocattr = "class='index " + tocclass + "'";
1758 docstring const indexName = params().getParamOr("name", from_ascii("Index"));
1760 // we'll use our own stream, because we are going to defer everything.
1761 // that's how we deal with the fact that we're probably inside a standard
1762 // paragraph, and we don't want to be.
1763 odocstringstream ods;
1766 xs << xml::StartTag("div", tocattr);
1768 xs << xml::StartTag(lay.htmltag(), lay.htmlattr());
1769 xs << translateIfPossible(indexName, op.local_font->language()->lang());
1770 xs << xml::EndTag(lay.htmltag());
1772 xs << xml::StartTag("ul", "class='main'");
1775 LASSERT(index_root->entries.empty(), return docstring()); // No index entry should have zero terms.
1776 for (const IndexNode* node : index_root->children) {
1777 outputIndexPage(xs, node);
1780 xs << xml::EndTag("ul");
1782 xs << xml::EndTag("div");