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"
16 #include "BufferParams.h"
17 #include "BufferView.h"
20 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "IndicesList.h"
26 #include "LaTeXFeatures.h"
28 #include "output_latex.h"
29 #include "output_xhtml.h"
31 #include "texstream.h"
32 #include "TextClass.h"
33 #include "TocBackend.h"
35 #include "support/debug.h"
36 #include "support/docstream.h"
37 #include "support/FileName.h"
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
41 #include "frontends/alert.h"
47 #include <QThreadStorage>
50 using namespace lyx::support;
54 /////////////////////////////////////////////////////////////////////
58 ///////////////////////////////////////////////////////////////////////
61 InsetIndex::InsetIndex(Buffer * buf, InsetIndexParams const & params)
62 : InsetCollapsible(buf), params_(params)
66 void InsetIndex::latex(otexstream & ios, OutputParams const & runparams_in) const
68 OutputParams runparams(runparams_in);
69 runparams.inIndexEntry = true;
73 if (buffer().masterBuffer()->params().use_indices && !params_.index.empty()
74 && params_.index != "idx") {
76 os << escape(params_.index);
83 // Get the LaTeX output from InsetText. We need to deconstruct this later
84 // in order to check if we need to generate a sorting key
85 odocstringstream ourlatex;
86 otexstream ots(ourlatex);
87 InsetText::latex(ots, runparams);
88 if (runparams.for_searchAdv != OutputParams::NoSearch) {
89 // No need for special handling, if we are only searching for some patterns
90 os << ourlatex.str() << "}";
94 // For the sorting key, we use the plaintext version
95 odocstringstream ourplain;
96 InsetText::plaintext(ourplain, runparams);
98 // These are the LaTeX and plaintext representations
99 docstring latexstr = ourlatex.str();
100 docstring plainstr = ourplain.str();
102 // This will get what follows | if anything does,
103 // the command (e.g., see, textbf) for pagination
107 // Check for the | separator to strip the cmd.
108 // This goes wrong on an escaped "|", but as the escape
109 // character can be changed in style files, we cannot
111 size_t pos = latexstr.find(from_ascii("|"));
112 if (pos != docstring::npos) {
113 // Put the bit after "|" into cmd...
114 cmd = latexstr.substr(pos + 1);
115 // ...and erase that stuff from latexstr
116 latexstr = latexstr.erase(pos);
117 // ...as well as from plainstr
118 size_t ppos = plainstr.find(from_ascii("|"));
119 if (ppos < plainstr.size())
120 plainstr.erase(ppos);
122 LYXERR0("The `|' separator was not found in the plaintext version!");
125 // Separate the entries and subentries, i.e., split on "!".
126 // This goes wrong on an escaped "!", but as the escape
127 // character can be changed in style files, we cannot
129 std::vector<docstring> const levels =
130 getVectorFromString(latexstr, from_ascii("!"), true);
131 std::vector<docstring> const levels_plain =
132 getVectorFromString(plainstr, from_ascii("!"), true);
134 vector<docstring>::const_iterator it = levels.begin();
135 vector<docstring>::const_iterator end = levels.end();
136 vector<docstring>::const_iterator it2 = levels_plain.begin();
138 for (; it != end; ++it) {
139 // The separator needs to be put back when
140 // writing the levels, except for the first level
146 // Now here comes the reason for this whole procedure:
147 // We try to correctly sort macros and formatted strings.
148 // If we find a command, prepend a plain text
149 // version of the content to get sorting right,
150 // e.g. \index{LyX@\LyX}, \index{text@\textbf{text}}.
151 // We do this on all levels.
152 // We don't do it if the level already contains a '@', though.
153 if (contains(*it, '\\') && !contains(*it, '@')) {
154 // Plaintext might return nothing (e.g. for ERTs).
155 // In that case, we use LaTeX.
156 docstring const spart =
157 (it2 < levels_plain.end() && !(*it2).empty())
159 // Now we need to validate that all characters in
160 // the sorting part are representable in the current
161 // encoding. If not try the LaTeX macro which might
162 // or might not be a good choice, and issue a warning.
163 pair<docstring, docstring> spart_latexed =
164 runparams.encoding->latexString(spart, runparams.dryrun);
165 if (!spart_latexed.second.empty())
166 LYXERR0("Uncodable character in index entry. Sorting might be wrong!");
167 if (spart != spart_latexed.first && !runparams.dryrun) {
168 // FIXME: warning should be passed to the error dialog
169 frontend::Alert::warning(_("Index sorting failed"),
170 bformat(_("LyX's automatic index sorting algorithm faced\n"
171 "problems with the entry '%1$s'.\n"
172 "Please specify the sorting of this entry manually, as\n"
173 "explained in the User Guide."), spart));
175 // Remove remaining \'s from the sort key
176 docstring ppart = subst(spart_latexed.first, from_ascii("\\"), docstring());
177 // Plain quotes need to be escaped, however (#10649), as this
178 // is the default escape character
179 ppart = subst(ppart, from_ascii("\""), from_ascii("\\\""));
181 // Now insert the sortkey, separated by '@'.
185 // Insert the actual level text
186 docstring const tpart = *it;
188 if (it2 < levels_plain.end())
191 // At last, re-insert the command, separated by "|"
197 // In macros with moving arguments, such as \section,
198 // we store the index and output it after the macro (#2154)
199 if (runparams_in.postpone_fragile_stuff)
200 runparams_in.post_macro += os.str();
206 void InsetIndex::docbook(XMLStream & xs, OutputParams const & runparams) const
208 // Get the content of the inset as LaTeX, as some things may be encoded as ERT (like {}).
209 // TODO: if there is an ERT within the index term, its conversion should be tried, in case it becomes useful;
210 // otherwise, ERTs should become comments. For now, they are just copied as-is, which is barely satisfactory.
211 odocstringstream odss;
212 otexstream ots(odss);
213 InsetText::latex(ots, runparams);
214 docstring latexString = trim(odss.str());
216 // Check whether there are unsupported things. @ is supported, but only for sorting, without specific formatting.
217 if (latexString.find(from_utf8("@\\")) != lyx::docstring::npos) {
218 docstring error = from_utf8("Unsupported feature: an index entry contains an @\\. "
219 "Complete entry: \"") + latexString + from_utf8("\"");
221 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
224 // Handle several indices (indicated in the inset instead of the raw latexString).
225 docstring indexType = from_utf8("");
226 if (buffer().masterBuffer()->params().use_indices) {
227 indexType += " type=\"" + params_.index + "\"";
230 // Split the string into its main constituents: terms, and command (see, see also, range).
231 size_t positionVerticalBar = latexString.find(from_ascii("|")); // What comes before | is (sub)(sub)entries.
232 docstring indexTerms = latexString.substr(0, positionVerticalBar);
234 if (positionVerticalBar != lyx::docstring::npos) {
235 command = latexString.substr(positionVerticalBar + 1);
238 // Handle sorting issues, with @.
239 vector<docstring> sortingElements = getVectorFromString(indexTerms, from_ascii("@"), false);
241 if (sortingElements.size() == 2) {
242 sortAs = sortingElements[0];
243 indexTerms = sortingElements[1];
246 // Handle primary, secondary, and tertiary terms (entries, subentries, and subsubentries, for LaTeX).
247 vector<docstring> terms = getVectorFromString(indexTerms, from_ascii("!"), false);
249 // Handle ranges. Happily, (| and |) can only be at the end of the string!
250 bool hasStartRange = latexString.find(from_ascii("|(")) != lyx::docstring::npos;
251 bool hasEndRange = latexString.find(from_ascii("|)")) != lyx::docstring::npos;
252 if (hasStartRange || hasEndRange) {
253 // Remove the ranges from the command if they do not appear at the beginning.
255 while ((index = command.find(from_utf8("|("), index)) != std::string::npos)
256 command.erase(index, 1);
258 while ((index = command.find(from_utf8("|)"), index)) != std::string::npos)
259 command.erase(index, 1);
261 // Remove the ranges when they are the only vertical bar in the complete string.
262 if (command[0] == '(' || command[0] == ')')
266 // Handle see and seealso. As "see" is a prefix of "seealso", the order of the comparisons is important.
267 // Both commands are mutually exclusive!
268 docstring see = from_utf8("");
269 vector<docstring> seeAlsoes;
270 if (command.substr(0, 3) == "see") {
271 // Unescape brackets.
273 while ((index = command.find(from_utf8("\\{"), index)) != std::string::npos)
274 command.erase(index, 1);
276 while ((index = command.find(from_utf8("\\}"), index)) != std::string::npos)
277 command.erase(index, 1);
279 // Retrieve the part between brackets, and remove the complete seealso.
280 size_t positionOpeningBracket = command.find(from_ascii("{"));
281 size_t positionClosingBracket = command.find(from_ascii("}"));
282 docstring list = command.substr(positionOpeningBracket + 1, positionClosingBracket - positionOpeningBracket - 1);
284 // Parse the list of referenced entries (or a single one for see).
285 if (command.substr(0, 7) == "seealso") {
286 seeAlsoes = getVectorFromString(list, from_ascii(","), false);
290 if (see.find(from_ascii(",")) != std::string::npos) {
291 docstring error = from_utf8("Several index terms found as \"see\"! Only one is acceptable. "
292 "Complete entry: \"") + latexString + from_utf8("\"");
294 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
298 // Remove the complete see/seealso from the commands, in case there is something else to parse.
299 command = command.substr(positionClosingBracket + 1);
302 // Some parts of the strings are not parsed, as they do not have anything matching in DocBook: things like
303 // formatting the entry or the page number, other strings for sorting. https://wiki.lyx.org/Tips/Indexing
304 // If there are such things in the index entry, then this code may miserably fail. For example, for "Peter|(textbf",
305 // no range will be detected.
306 // TODO: Could handle formatting as significance="preferred"?
307 if (!command.empty()) {
308 docstring error = from_utf8("Unsupported feature: an index entry contains a | with an unsupported command, ")
309 + command + from_utf8(". ") + from_utf8("Complete entry: \"") + latexString + from_utf8("\"");
311 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
314 // Write all of this down.
315 if (terms.empty() && !hasEndRange) {
316 docstring error = from_utf8("No index term found! Complete entry: \"") + latexString + from_utf8("\"");
318 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
320 // Generate the attributes for ranges. It is based on the terms that are indexed, but the ID must be unique
321 // to this indexing area (xml::cleanID does not guarantee this: for each call with the same arguments,
322 // the same legal ID is produced; here, as the input would be the same, the output must be, by design).
323 // Hence the thread-local storage, as the numbers must strictly be unique, and thus cannot be shared across
324 // a paragraph (making the solution used for HTML worthless). This solution is very similar to the one used in
326 // indexType can only be used for singular and startofrange types!
328 if (!hasStartRange && !hasEndRange) {
331 // Append an ID if uniqueness is not guaranteed across the document.
332 static QThreadStorage<set<docstring>> tKnownTermLists;
333 static QThreadStorage<int> tID;
335 set<docstring> &knownTermLists = tKnownTermLists.localData();
336 int &ID = tID.localData();
338 if (!tID.hasLocalData()) {
342 // Modify the index terms to add the unique ID if needed.
343 docstring newIndexTerms = indexTerms;
344 if (knownTermLists.find(indexTerms) != knownTermLists.end()) {
345 newIndexTerms += from_ascii(string("-") + to_string(ID));
347 // Only increment for the end of range, so that the same number is used for the start of range.
353 // Term list not yet known: add it to the set AFTER the end of range. After
354 if (knownTermLists.find(indexTerms) == knownTermLists.end() && hasEndRange) {
355 knownTermLists.insert(indexTerms);
358 // Generate the attributes.
359 docstring id = xml::cleanID(newIndexTerms);
361 attrs = indexType + " class=\"startofrange\" xml:id=\"" + id + "\"";
363 attrs = " class=\"endofrange\" startref=\"" + id + "\"";
367 // Handle the index terms (including the specific index for this entry).
369 xs << xml::CompTag("indexterm", attrs);
371 xs << xml::StartTag("indexterm", attrs);
372 if (!terms.empty()) { // hasEndRange has no content.
374 if (!sortAs.empty()) {
375 attr = from_utf8("sortas='") + sortAs + from_utf8("'");
378 xs << xml::StartTag("primary", attr);
380 xs << xml::EndTag("primary");
382 if (terms.size() > 1) {
383 xs << xml::StartTag("secondary");
385 xs << xml::EndTag("secondary");
387 if (terms.size() > 2) {
388 xs << xml::StartTag("tertiary");
390 xs << xml::EndTag("tertiary");
393 // Handle see and see also.
395 xs << xml::StartTag("see");
397 xs << xml::EndTag("see");
400 if (!seeAlsoes.empty()) {
401 for (auto &entry : seeAlsoes) {
402 xs << xml::StartTag("seealso");
404 xs << xml::EndTag("seealso");
409 xs << xml::EndTag("indexterm");
415 docstring InsetIndex::xhtml(XMLStream & xs, OutputParams const &) const
417 // we just print an anchor, taking the paragraph ID from
418 // our own interior paragraph, which doesn't get printed
419 std::string const magic = paragraphs().front().magicLabel();
420 std::string const attr = "id='" + magic + "'";
421 xs << xml::CompTag("a", attr);
426 bool InsetIndex::showInsetDialog(BufferView * bv) const
428 bv->showDialog("index", params2string(params_),
429 const_cast<InsetIndex *>(this));
434 void InsetIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
436 switch (cmd.action()) {
438 case LFUN_INSET_MODIFY: {
439 if (cmd.getArg(0) == "changetype") {
440 cur.recordUndoInset(this);
441 params_.index = from_utf8(cmd.getArg(1));
444 InsetIndexParams params;
445 InsetIndex::string2params(to_utf8(cmd.argument()), params);
446 cur.recordUndoInset(this);
447 params_.index = params.index;
448 // what we really want here is a TOC update, but that means
449 // a full buffer update
450 cur.forceBufferUpdate();
454 case LFUN_INSET_DIALOG_UPDATE:
455 cur.bv().updateDialog("index", params2string(params_));
459 InsetCollapsible::doDispatch(cur, cmd);
465 bool InsetIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
466 FuncStatus & flag) const
468 switch (cmd.action()) {
470 case LFUN_INSET_MODIFY:
471 if (cmd.getArg(0) == "changetype") {
472 docstring const newtype = from_utf8(cmd.getArg(1));
473 Buffer const & realbuffer = *buffer().masterBuffer();
474 IndicesList const & indiceslist = realbuffer.params().indiceslist();
475 Index const * index = indiceslist.findShortcut(newtype);
476 flag.setEnabled(index != 0);
478 from_utf8(cmd.getArg(1)) == params_.index);
481 return InsetCollapsible::getStatus(cur, cmd, flag);
483 case LFUN_INSET_DIALOG_UPDATE: {
484 Buffer const & realbuffer = *buffer().masterBuffer();
485 flag.setEnabled(realbuffer.params().use_indices);
490 return InsetCollapsible::getStatus(cur, cmd, flag);
495 ColorCode InsetIndex::labelColor() const
497 if (params_.index.empty() || params_.index == from_ascii("idx"))
498 return InsetCollapsible::labelColor();
500 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.index)
501 + "@" + buffer().fileName().absFileName());
503 c = InsetCollapsible::labelColor();
508 docstring InsetIndex::toolTip(BufferView const &, int, int) const
510 docstring tip = _("Index Entry");
511 if (buffer().params().use_indices && !params_.index.empty()) {
512 Buffer const & realbuffer = *buffer().masterBuffer();
513 IndicesList const & indiceslist = realbuffer.params().indiceslist();
515 Index const * index = indiceslist.findShortcut(params_.index);
517 tip += _("unknown type!");
519 tip += index->index();
523 return toolTipText(tip);
527 docstring const InsetIndex::buttonLabel(BufferView const & bv) const
529 InsetLayout const & il = getLayout();
530 docstring label = translateIfPossible(il.labelstring());
532 if (buffer().params().use_indices && !params_.index.empty()) {
533 Buffer const & realbuffer = *buffer().masterBuffer();
534 IndicesList const & indiceslist = realbuffer.params().indiceslist();
536 Index const * index = indiceslist.findShortcut(params_.index);
538 label += _("unknown type!");
540 label += index->index();
544 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
546 return getNewLabel(label);
550 void InsetIndex::write(ostream & os) const
552 os << to_utf8(layoutName());
554 InsetCollapsible::write(os);
558 void InsetIndex::read(Lexer & lex)
561 InsetCollapsible::read(lex);
565 string InsetIndex::params2string(InsetIndexParams const & params)
574 void InsetIndex::string2params(string const & in, InsetIndexParams & params)
576 params = InsetIndexParams();
580 istringstream data(in);
583 lex.setContext("InsetIndex::string2params");
589 void InsetIndex::addToToc(DocIterator const & cpit, bool output_active,
590 UpdateType utype, TocBackend & backend) const
592 DocIterator pit = cpit;
593 pit.push_back(CursorSlice(const_cast<InsetIndex &>(*this)));
595 string type = "index";
596 if (buffer().masterBuffer()->params().use_indices)
597 type += ":" + to_utf8(params_.index);
598 // this is unlikely to be terribly long
599 text().forOutliner(str, INT_MAX);
600 TocBuilder & b = backend.builder(type);
601 b.pushItem(pit, str, output_active);
602 // Proceed with the rest of the inset.
603 InsetCollapsible::addToToc(cpit, output_active, utype, backend);
608 void InsetIndex::validate(LaTeXFeatures & features) const
610 if (buffer().masterBuffer()->params().use_indices
611 && !params_.index.empty()
612 && params_.index != "idx")
613 features.require("splitidx");
614 InsetCollapsible::validate(features);
618 string InsetIndex::contextMenuName() const
620 return "context-index";
624 bool InsetIndex::hasSettings() const
626 return buffer().masterBuffer()->params().use_indices;
632 /////////////////////////////////////////////////////////////////////
636 ///////////////////////////////////////////////////////////////////////
639 void InsetIndexParams::write(ostream & os) const
643 os << to_utf8(index);
650 void InsetIndexParams::read(Lexer & lex)
653 index = lex.getDocString();
655 index = from_ascii("idx");
659 /////////////////////////////////////////////////////////////////////
663 ///////////////////////////////////////////////////////////////////////
665 InsetPrintIndex::InsetPrintIndex(Buffer * buf, InsetCommandParams const & p)
666 : InsetCommand(buf, p)
670 ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
672 static ParamInfo param_info_;
673 if (param_info_.empty()) {
674 param_info_.add("type", ParamInfo::LATEX_OPTIONAL,
675 ParamInfo::HANDLING_ESCAPE);
676 param_info_.add("name", ParamInfo::LATEX_OPTIONAL,
677 ParamInfo::HANDLING_LATEXIFY);
678 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
684 docstring InsetPrintIndex::screenLabel() const
686 bool const printall = suffixIs(getCmdName(), '*');
687 bool const multind = buffer().masterBuffer()->params().use_indices;
689 && getParam("type") == from_ascii("idx"))
690 || (getParam("type").empty() && !printall))
692 Buffer const & realbuffer = *buffer().masterBuffer();
693 IndicesList const & indiceslist = realbuffer.params().indiceslist();
694 Index const * index = indiceslist.findShortcut(getParam("type"));
695 if (!index && !printall)
696 return _("Unknown index type!");
697 docstring res = printall ? _("All indexes") : index->index();
699 res += " (" + _("non-active") + ")";
700 else if (contains(getCmdName(), "printsubindex"))
701 res += " (" + _("subindex") + ")";
706 bool InsetPrintIndex::isCompatibleCommand(string const & s)
708 return s == "printindex" || s == "printsubindex"
709 || s == "printindex*" || s == "printsubindex*";
713 void InsetPrintIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
715 switch (cmd.action()) {
717 case LFUN_INSET_MODIFY: {
718 if (cmd.argument() == from_ascii("toggle-subindex")) {
719 string scmd = getCmdName();
720 if (contains(scmd, "printindex"))
721 scmd = subst(scmd, "printindex", "printsubindex");
723 scmd = subst(scmd, "printsubindex", "printindex");
727 } else if (cmd.argument() == from_ascii("check-printindex*")) {
728 string scmd = getCmdName();
729 if (suffixIs(scmd, '*'))
733 setParam("type", docstring());
737 InsetCommandParams p(INDEX_PRINT_CODE);
739 InsetCommand::string2params(to_utf8(cmd.argument()), p);
740 if (p.getCmdName().empty()) {
741 cur.noScreenUpdate();
750 InsetCommand::doDispatch(cur, cmd);
756 bool InsetPrintIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
757 FuncStatus & status) const
759 switch (cmd.action()) {
761 case LFUN_INSET_MODIFY: {
762 if (cmd.argument() == from_ascii("toggle-subindex")) {
763 status.setEnabled(buffer().masterBuffer()->params().use_indices);
764 status.setOnOff(contains(getCmdName(), "printsubindex"));
766 } else if (cmd.argument() == from_ascii("check-printindex*")) {
767 status.setEnabled(buffer().masterBuffer()->params().use_indices);
768 status.setOnOff(suffixIs(getCmdName(), '*'));
770 } if (cmd.getArg(0) == "index_print"
771 && cmd.getArg(1) == "CommandInset") {
772 InsetCommandParams p(INDEX_PRINT_CODE);
773 InsetCommand::string2params(to_utf8(cmd.argument()), p);
774 if (suffixIs(p.getCmdName(), '*')) {
775 status.setEnabled(true);
776 status.setOnOff(false);
779 Buffer const & realbuffer = *buffer().masterBuffer();
780 IndicesList const & indiceslist =
781 realbuffer.params().indiceslist();
782 Index const * index = indiceslist.findShortcut(p["type"]);
783 status.setEnabled(index != 0);
784 status.setOnOff(p["type"] == getParam("type"));
787 return InsetCommand::getStatus(cur, cmd, status);
790 case LFUN_INSET_DIALOG_UPDATE: {
791 status.setEnabled(buffer().masterBuffer()->params().use_indices);
796 return InsetCommand::getStatus(cur, cmd, status);
801 void InsetPrintIndex::updateBuffer(ParIterator const &, UpdateType, bool const /*deleted*/)
803 Index const * index =
804 buffer().masterParams().indiceslist().findShortcut(getParam("type"));
806 setParam("name", index->index());
810 void InsetPrintIndex::latex(otexstream & os, OutputParams const & runparams_in) const
812 if (!buffer().masterBuffer()->params().use_indices) {
813 if (getParam("type") == from_ascii("idx"))
814 os << "\\printindex" << termcmd;
817 OutputParams runparams = runparams_in;
818 os << getCommand(runparams);
822 void InsetPrintIndex::validate(LaTeXFeatures & features) const
824 features.require("makeidx");
825 if (buffer().masterBuffer()->params().use_indices)
826 features.require("splitidx");
827 InsetCommand::validate(features);
831 string InsetPrintIndex::contextMenuName() const
833 return buffer().masterBuffer()->params().use_indices ?
834 "context-indexprint" : string();
838 bool InsetPrintIndex::hasSettings() const
840 return buffer().masterBuffer()->params().use_indices;
846 void parseItem(docstring & s, bool for_output)
848 // this does not yet check for escaped things
849 size_type loc = s.find(from_ascii("@"));
850 if (loc != string::npos) {
856 loc = s.find(from_ascii("|"));
857 if (loc != string::npos)
862 void extractSubentries(docstring const & entry, docstring & main,
863 docstring & sub1, docstring & sub2)
867 size_type const loc = entry.find(from_ascii(" ! "));
868 if (loc == string::npos)
871 main = trim(entry.substr(0, loc));
872 size_t const locend = loc + 3;
873 size_type const loc2 = entry.find(from_ascii(" ! "), locend);
874 if (loc2 == string::npos) {
875 sub1 = trim(entry.substr(locend));
877 sub1 = trim(entry.substr(locend, loc2 - locend));
878 sub2 = trim(entry.substr(loc2 + 3));
889 IndexEntry(docstring const & s, DocIterator const & d)
892 extractSubentries(s, main, sub, subsub);
893 parseItem(main, false);
894 parseItem(sub, false);
895 parseItem(subsub, false);
898 bool equal(IndexEntry const & rhs) const
900 return main == rhs.main && sub == rhs.sub && subsub == rhs.subsub;
903 bool same_sub(IndexEntry const & rhs) const
905 return main == rhs.main && sub == rhs.sub;
908 bool same_main(IndexEntry const & rhs) const
910 return main == rhs.main;
919 bool operator<(IndexEntry const & lhs, IndexEntry const & rhs)
921 int comp = compare_no_case(lhs.main, rhs.main);
923 comp = compare_no_case(lhs.sub, rhs.sub);
925 comp = compare_no_case(lhs.subsub, rhs.subsub);
932 docstring InsetPrintIndex::xhtml(XMLStream &, OutputParams const & op) const
934 BufferParams const & bp = buffer().masterBuffer()->params();
936 // we do not presently support multiple indices, so we refuse to print
937 // anything but the main index, so as not to generate multiple indices.
938 // NOTE Multiple index support would require some work. The reason
939 // is that the TOC does not know about multiple indices. Either it would
940 // need to be told about them (not a bad idea), or else the index entries
941 // would need to be collected differently, say, during validation.
942 if (bp.use_indices && getParam("type") != from_ascii("idx"))
945 shared_ptr<Toc const> toc = buffer().tocBackend().toc("index");
949 // Collect the index entries in a form we can use them.
950 Toc::const_iterator it = toc->begin();
951 Toc::const_iterator const en = toc->end();
952 vector<IndexEntry> entries;
953 for (; it != en; ++it)
955 entries.push_back(IndexEntry(it->str(), it->dit()));
958 // not very likely that all the index entries are in notes or
962 stable_sort(entries.begin(), entries.end());
964 Layout const & lay = bp.documentClass().htmlTOCLayout();
965 string const & tocclass = lay.defaultCSSClass();
966 string const tocattr = "class='index " + tocclass + "'";
968 // we'll use our own stream, because we are going to defer everything.
969 // that's how we deal with the fact that we're probably inside a standard
970 // paragraph, and we don't want to be.
971 odocstringstream ods;
974 xs << xml::StartTag("div", tocattr);
975 xs << xml::StartTag(lay.htmltag(), lay.htmlattr())
976 << translateIfPossible(from_ascii("Index"),
977 op.local_font->language()->lang())
978 << xml::EndTag(lay.htmltag());
979 xs << xml::StartTag("ul", "class='main'");
982 vector<IndexEntry>::const_iterator eit = entries.begin();
983 vector<IndexEntry>::const_iterator const een = entries.end();
984 // tracks whether we are already inside a main entry (1),
985 // a sub-entry (2), or a sub-sub-entry (3). see below for the
988 // the last one we saw
990 int entry_number = -1;
991 for (; eit != een; ++eit) {
992 Paragraph const & par = eit->dit.innerParagraph();
993 if (entry_number == -1 || !eit->equal(last)) {
994 if (entry_number != -1) {
995 // not the first time through the loop, so
996 // close last entry or entries, depending.
998 // close this sub-sub-entry
999 xs << xml::EndTag("li") << xml::CR();
1000 // is this another sub-sub-entry within the same sub-entry?
1001 if (!eit->same_sub(last)) {
1003 xs << xml::EndTag("ul") << xml::CR();
1007 // the point of the second test here is that we might get
1008 // here two ways: (i) by falling through from above; (ii) because,
1009 // though the sub-entry hasn't changed, the sub-sub-entry has,
1010 // which means that it is the first sub-sub-entry within this
1011 // sub-entry. In that case, we do not want to close anything.
1012 if (level == 2 && !eit->same_sub(last)) {
1014 xs << xml::EndTag("li") << xml::CR();
1015 // is this another sub-entry with the same main entry?
1016 if (!eit->same_main(last)) {
1018 xs << xml::EndTag("ul") << xml::CR();
1022 // again, we can get here two ways: from above, or because we have
1023 // found the first sub-entry. in the latter case, we do not want to
1025 if (level == 1 && !eit->same_main(last)) {
1027 xs << xml::EndTag("li") << xml::CR();
1031 // we'll be starting new entries
1034 // We need to use our own stream, since we will have to
1035 // modify what we get back.
1036 odocstringstream ent;
1037 XMLStream entstream(ent);
1038 OutputParams ours = op;
1039 ours.for_toc = true;
1040 par.simpleLyXHTMLOnePar(buffer(), entstream, ours, dummy);
1042 // these will contain XHTML versions of the main entry, etc
1043 // remember that everything will already have been escaped,
1044 // so we'll need to use NextRaw() during output.
1048 extractSubentries(ent.str(), main, sub, subsub);
1049 parseItem(main, true);
1050 parseItem(sub, true);
1051 parseItem(subsub, true);
1054 // another subsubentry
1055 xs << xml::StartTag("li", "class='subsubentry'")
1056 << XMLStream::ESCAPE_NONE << subsub;
1057 } else if (level == 2) {
1058 // there are two ways we can be here:
1059 // (i) we can actually be inside a sub-entry already and be about
1060 // to output the first sub-sub-entry. in this case, our sub
1061 // and the last sub will be the same.
1062 // (ii) we can just have closed a sub-entry, possibly after also
1063 // closing a list of sub-sub-entries. here our sub and the last
1064 // sub are different.
1065 // only in the latter case do we need to output the new sub-entry.
1066 // note that in this case, too, though, the sub-entry might already
1067 // have a sub-sub-entry.
1068 if (eit->sub != last.sub)
1069 xs << xml::StartTag("li", "class='subentry'")
1070 << XMLStream::ESCAPE_NONE << sub;
1071 if (!subsub.empty()) {
1072 // it's actually a subsubentry, so we need to start that list
1074 << xml::StartTag("ul", "class='subsubentry'")
1075 << xml::StartTag("li", "class='subsubentry'")
1076 << XMLStream::ESCAPE_NONE << subsub;
1080 // there are also two ways we can be here:
1081 // (i) we can actually be inside an entry already and be about
1082 // to output the first sub-entry. in this case, our main
1083 // and the last main will be the same.
1084 // (ii) we can just have closed an entry, possibly after also
1085 // closing a list of sub-entries. here our main and the last
1086 // main are different.
1087 // only in the latter case do we need to output the new main entry.
1088 // note that in this case, too, though, the main entry might already
1089 // have a sub-entry, or even a sub-sub-entry.
1090 if (eit->main != last.main)
1091 xs << xml::StartTag("li", "class='main'") << main;
1093 // there's a sub-entry, too
1095 << xml::StartTag("ul", "class='subentry'")
1096 << xml::StartTag("li", "class='subentry'")
1097 << XMLStream::ESCAPE_NONE << sub;
1099 if (!subsub.empty()) {
1100 // and a sub-sub-entry
1102 << xml::StartTag("ul", "class='subsubentry'")
1103 << xml::StartTag("li", "class='subsubentry'")
1104 << XMLStream::ESCAPE_NONE << subsub;
1110 // finally, then, we can output the index link itself
1111 string const parattr = "href='#" + par.magicLabel() + "'";
1112 xs << (entry_number == 0 ? ":" : ",");
1113 xs << " " << xml::StartTag("a", parattr)
1114 << ++entry_number << xml::EndTag("a");
1117 // now we have to close all the open levels
1119 xs << xml::EndTag("li") << xml::EndTag("ul") << xml::CR();
1122 xs << xml::EndTag("div") << xml::CR();