]> git.lyx.org Git - lyx.git/blob - src/insets/InsetIndex.cpp
384e88cb105f8eedfa235c8d3b3fc20f26e18544
[lyx.git] / src / insets / InsetIndex.cpp
1 /**
2  * \file InsetIndex.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11 #include <config.h>
12
13 #include "InsetIndex.h"
14
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
18 #include "ColorSet.h"
19 #include "DispatchResult.h"
20 #include "Encoding.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "IndicesList.h"
24 #include "LaTeXFeatures.h"
25 #include "Lexer.h"
26 #include "output_latex.h"
27 #include "output_xhtml.h"
28 #include "sgml.h"
29 #include "TocBackend.h"
30
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
35
36 #include "frontends/alert.h"
37
38 #include <ostream>
39
40 using namespace std;
41 using namespace lyx::support;
42
43 namespace lyx {
44
45 /////////////////////////////////////////////////////////////////////
46 //
47 // InsetIndex
48 //
49 ///////////////////////////////////////////////////////////////////////
50
51
52 InsetIndex::InsetIndex(Buffer * buf, InsetIndexParams const & params)
53         : InsetCollapsable(buf), params_(params)
54 {}
55
56
57 int InsetIndex::latex(odocstream & os,
58                       OutputParams const & runparams_in) const
59 {
60         OutputParams runparams(runparams_in);
61         runparams.inIndexEntry = true;
62
63         if (buffer().masterBuffer()->params().use_indices && !params_.index.empty()
64             && params_.index != "idx") {
65                 os << "\\sindex[";
66                 os << params_.index;
67                 os << "]{";
68         } else {
69                 os << "\\index";
70                 os << '{';
71         }
72         int i = 0;
73
74         // get contents of InsetText as LaTeX and plaintext
75         odocstringstream ourlatex;
76         InsetText::latex(ourlatex, runparams);
77         odocstringstream ourplain;
78         InsetText::plaintext(ourplain, runparams);
79         docstring latexstr = ourlatex.str();
80         docstring plainstr = ourplain.str();
81
82         // this will get what follows | if anything does
83         docstring cmd;
84
85         // check for the | separator
86         // FIXME This would go wrong on an escaped "|", but
87         // how far do we want to go here?
88         size_t pos = latexstr.find(from_ascii("|"));
89         if (pos != docstring::npos) {
90                 // put the bit after "|" into cmd...
91                 cmd = latexstr.substr(pos + 1);
92                 // ...and erase that stuff from latexstr
93                 latexstr = latexstr.erase(pos);
94                 // ...and similarly from plainstr
95                 size_t ppos = plainstr.find(from_ascii("|"));
96                 if (ppos < plainstr.size())
97                         plainstr.erase(ppos);
98                 else
99                         LYXERR0("The `|' separator was not found in the plaintext version!");
100         }
101
102         // Separate the entires and subentries, i.e., split on "!"
103         // FIXME This would do the wrong thing with escaped ! characters
104         std::vector<docstring> const levels =
105                 getVectorFromString(latexstr, from_ascii("!"), true);
106         std::vector<docstring> const levels_plain =
107                 getVectorFromString(plainstr, from_ascii("!"), true);
108
109         vector<docstring>::const_iterator it = levels.begin();
110         vector<docstring>::const_iterator end = levels.end();
111         vector<docstring>::const_iterator it2 = levels_plain.begin();
112         bool first = true;
113         for (; it != end; ++it) {
114                 // write the separator except the first time
115                 if (!first)
116                         os << '!';
117                 else
118                         first = false;
119
120                 // correctly sort macros and formatted strings
121                 // if we do find a command, prepend a plain text
122                 // version of the content to get sorting right,
123                 // e.g. \index{LyX@\LyX}, \index{text@\textbf{text}}
124                 // Don't do that if the user entered '@' himself, though.
125                 if (contains(*it, '\\') && !contains(*it, '@')) {
126                         // Plaintext might return nothing (e.g. for ERTs)
127                         docstring const spart = 
128                                 (it2 < levels_plain.end() && !(*it2).empty())
129                                 ? *it2 : *it;
130                         // Now we need to validate that all characters in
131                         // the sorting part are representable in the current
132                         // encoding. If not try the LaTeX macro which might
133                         // or might not be a good choice, and issue a warning.
134                         docstring spart2;
135                         for (size_t n = 0; n < spart.size(); ++n) {
136                                 try {
137                                         spart2 += runparams.encoding->latexChar(spart[n]);
138                                 } catch (EncodingException & /* e */) {
139                                         LYXERR0("Uncodable character in index entry. Sorting might be wrong!");
140                                 }
141                         }
142                         if (spart != spart2 && !runparams.dryrun) {
143                                 // FIXME: warning should be passed to the error dialog
144                                 frontend::Alert::warning(_("Index sorting failed"),
145                                 bformat(_("LyX's automatic index sorting algorithm faced\n"
146                                   "problems with the entry '%1$s'.\n"
147                                   "Please specify the sorting of this entry manually, as\n"
148                                   "explained in the User Guide."), spart));
149                         }
150                         // remove remaining \'s for the sorting part
151                         docstring const ppart =
152                                 subst(spart2, from_ascii("\\"), docstring());
153                         os << ppart;
154                         os << '@';
155                         i += count_char(ppart, '\n');
156                 }
157                 docstring const tpart = *it;
158                 os << tpart;
159                 i += count_char(tpart, '\n');
160                 if (it2 < levels_plain.end())
161                         ++it2;
162         }
163         // write the bit that followed "|"
164         if (!cmd.empty()) {
165                 os << "|" << cmd;
166                 i += count_char(cmd, '\n');
167         }
168         os << '}';
169         return i;
170 }
171
172
173 int InsetIndex::docbook(odocstream & os, OutputParams const & runparams) const
174 {
175         os << "<indexterm><primary>";
176         int const i = InsetText::docbook(os, runparams);
177         os << "</primary></indexterm>";
178         return i;
179 }
180
181
182 docstring InsetIndex::xhtml(XHTMLStream & xs, OutputParams const &) const
183 {
184         // we just print an anchor, taking the paragraph ID from 
185         // our own interior paragraph, which doesn't get printed
186         std::string const magic = paragraphs().front().magicLabel();
187         std::string const attr = "id='" + magic + "'";
188         xs << html::CompTag("a", attr);
189         return docstring();
190 }
191
192
193 bool InsetIndex::showInsetDialog(BufferView * bv) const
194 {
195         bv->showDialog("index", params2string(params_),
196                         const_cast<InsetIndex *>(this));
197         return true;
198 }
199
200
201 void InsetIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
202 {
203         switch (cmd.action()) {
204
205         case LFUN_INSET_MODIFY: {
206                 if (cmd.getArg(0) == "changetype") {
207                         params_.index = from_utf8(cmd.getArg(1));
208                         break;
209                 }
210                 InsetIndexParams params;
211                 InsetIndex::string2params(to_utf8(cmd.argument()), params);
212                 params_.index = params.index;
213                 break;
214         }
215
216         case LFUN_INSET_DIALOG_UPDATE:
217                 cur.bv().updateDialog("index", params2string(params_));
218                 break;
219
220         default:
221                 InsetCollapsable::doDispatch(cur, cmd);
222                 break;
223         }
224 }
225
226
227 bool InsetIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
228                 FuncStatus & flag) const
229 {
230         switch (cmd.action()) {
231
232         case LFUN_INSET_MODIFY:
233                 if (cmd.getArg(0) == "changetype") {
234                         docstring const newtype = from_utf8(cmd.getArg(1));
235                         Buffer const & realbuffer = *buffer().masterBuffer();
236                         IndicesList const & indiceslist = realbuffer.params().indiceslist();
237                         Index const * index = indiceslist.findShortcut(newtype);
238                         flag.setEnabled(index != 0);
239                         flag.setOnOff(
240                                 from_utf8(cmd.getArg(1)) == params_.index);
241                         return true;
242                 }
243                 flag.setEnabled(true);
244                 return true;
245
246         case LFUN_INSET_DIALOG_UPDATE: {
247                 Buffer const & realbuffer = *buffer().masterBuffer();
248                 flag.setEnabled(realbuffer.params().use_indices);
249                 return true;
250         }
251
252         default:
253                 return InsetCollapsable::getStatus(cur, cmd, flag);
254         }
255 }
256
257
258 ColorCode InsetIndex::labelColor() const
259 {
260         if (params_.index.empty() || params_.index == from_ascii("idx"))
261                 return InsetCollapsable::labelColor();
262         // FIXME UNICODE
263         ColorCode c = lcolor.getFromLyXName(to_utf8(params_.index));
264         if (c == Color_none)
265                 c = InsetCollapsable::labelColor();
266         return c;
267 }
268
269
270 docstring InsetIndex::toolTip(BufferView const &, int, int) const
271 {
272         docstring tip = _("Index Entry");
273         if (buffer().params().use_indices && !params_.index.empty()) {
274                 Buffer const & realbuffer = *buffer().masterBuffer();
275                 IndicesList const & indiceslist = realbuffer.params().indiceslist();
276                 tip += " (";
277                 Index const * index = indiceslist.findShortcut(params_.index);
278                 if (!index)
279                         tip += _("unknown type!");
280                 else
281                         tip += index->index();
282                 tip += ")";
283         }
284         tip += ": ";
285         OutputParams rp(&buffer().params().encoding());
286         odocstringstream ods;
287         InsetText::plaintext(ods, rp);
288         tip += ods.str();
289         return wrapParas(tip);
290 }
291
292
293 docstring const InsetIndex::buttonLabel(BufferView const & bv) const
294 {
295         InsetLayout const & il = getLayout();
296         docstring label = translateIfPossible(il.labelstring());
297
298         if (buffer().params().use_indices && !params_.index.empty()) {
299                 Buffer const & realbuffer = *buffer().masterBuffer();
300                 IndicesList const & indiceslist = realbuffer.params().indiceslist();
301                 label += " (";
302                 Index const * index = indiceslist.findShortcut(params_.index);
303                 if (!index)
304                         label += _("unknown type!");
305                 else
306                         label += index->index();
307                 label += ")";
308         }
309
310         if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
311                 return label;
312         return getNewLabel(label);
313 }
314
315
316 void InsetIndex::write(ostream & os) const
317 {
318         os << to_utf8(name());
319         params_.write(os);
320         InsetCollapsable::write(os);
321 }
322
323
324 void InsetIndex::read(Lexer & lex)
325 {
326         params_.read(lex);
327         InsetCollapsable::read(lex);
328 }
329
330
331 string InsetIndex::params2string(InsetIndexParams const & params)
332 {
333         ostringstream data;
334         data << "index";
335         params.write(data);
336         return data.str();
337 }
338
339
340 void InsetIndex::string2params(string const & in, InsetIndexParams & params)
341 {
342         params = InsetIndexParams();
343         if (in.empty())
344                 return;
345
346         istringstream data(in);
347         Lexer lex;
348         lex.setStream(data);
349         lex.setContext("InsetIndex::string2params");
350         lex >> "index";
351         params.read(lex);
352 }
353
354
355 void InsetIndex::addToToc(DocIterator const & cpit)
356 {
357         DocIterator pit = cpit;
358         pit.push_back(CursorSlice(*this));
359         docstring const item = text().asString(0, 1, AS_STR_LABEL | AS_STR_INSETS);
360         buffer().tocBackend().toc("index").push_back(TocItem(pit, 0, item));
361         // Proceed with the rest of the inset.
362         InsetCollapsable::addToToc(cpit);
363 }
364
365
366 void InsetIndex::validate(LaTeXFeatures & features) const
367 {
368         if (buffer().masterBuffer()->params().use_indices
369             && !params_.index.empty()
370             && params_.index != "idx")
371                 features.require("splitidx");
372 }
373
374
375 docstring InsetIndex::contextMenu(BufferView const &, int, int) const
376 {
377         return from_ascii("context-index");
378 }
379
380
381 bool InsetIndex::hasSettings() const
382 {
383         return buffer().masterBuffer()->params().use_indices;
384 }
385
386
387
388
389 /////////////////////////////////////////////////////////////////////
390 //
391 // InsetIndexParams
392 //
393 ///////////////////////////////////////////////////////////////////////
394
395
396 void InsetIndexParams::write(ostream & os) const
397 {
398         os << ' ';
399         if (!index.empty())
400                 os << to_utf8(index);
401         else
402                 os << "idx";
403         os << '\n';
404 }
405
406
407 void InsetIndexParams::read(Lexer & lex)
408 {
409         if (lex.eatLine())
410                 index = lex.getDocString();
411         else
412                 index = from_ascii("idx");
413 }
414
415
416 /////////////////////////////////////////////////////////////////////
417 //
418 // InsetPrintIndex
419 //
420 ///////////////////////////////////////////////////////////////////////
421
422 InsetPrintIndex::InsetPrintIndex(Buffer * buf, InsetCommandParams const & p)
423         : InsetCommand(buf, p, "index_print")
424 {}
425
426
427 ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
428 {
429         static ParamInfo param_info_;
430         if (param_info_.empty()) {
431                 param_info_.add("type", ParamInfo::LATEX_OPTIONAL);
432                 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
433         }
434         return param_info_;
435 }
436
437
438 docstring InsetPrintIndex::screenLabel() const
439 {
440         bool const printall = suffixIs(getCmdName(), '*');
441         bool const multind = buffer().masterBuffer()->params().use_indices;
442         if ((!multind
443              && getParam("type") == from_ascii("idx"))
444             || (getParam("type").empty() && !printall))
445                 return _("Index");
446         Buffer const & realbuffer = *buffer().masterBuffer();
447         IndicesList const & indiceslist = realbuffer.params().indiceslist();
448         Index const * index = indiceslist.findShortcut(getParam("type"));
449         if (!index && !printall)
450                 return _("Unknown index type!");
451         docstring res = printall ? _("All indices") : index->index();
452         if (!multind)
453                 res += " (" + _("non-active") + ")";
454         else if (contains(getCmdName(), "printsubindex"))
455                 res += " (" + _("subindex") + ")";
456         return res;
457 }
458
459
460 bool InsetPrintIndex::isCompatibleCommand(string const & s)
461 {
462         return s == "printindex" || s == "printsubindex"
463                 || s == "printindex*" || s == "printsubindex*";
464 }
465
466
467 void InsetPrintIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
468 {
469         switch (cmd.action()) {
470
471         case LFUN_INSET_MODIFY: {
472                 if (cmd.argument() == from_ascii("toggle-subindex")) {
473                         string cmd = getCmdName();
474                         if (contains(cmd, "printindex"))
475                                 cmd = subst(cmd, "printindex", "printsubindex");
476                         else
477                                 cmd = subst(cmd, "printsubindex", "printindex");
478                         setCmdName(cmd);
479                         break;
480                 } else if (cmd.argument() == from_ascii("check-printindex*")) {
481                         string cmd = getCmdName();
482                         if (suffixIs(cmd, '*'))
483                                 break;
484                         cmd += '*';
485                         setParam("type", docstring());
486                         setCmdName(cmd);
487                         break;
488                 }
489                 InsetCommandParams p(INDEX_PRINT_CODE);
490                 // FIXME UNICODE
491                 InsetCommand::string2params("index_print",
492                         to_utf8(cmd.argument()), p);
493                 if (p.getCmdName().empty()) {
494                         cur.noUpdate();
495                         break;
496                 }
497                 setParams(p);
498                 break;
499         }
500
501         default:
502                 InsetCommand::doDispatch(cur, cmd);
503                 break;
504         }
505 }
506
507
508 bool InsetPrintIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
509         FuncStatus & status) const
510 {
511         switch (cmd.action()) {
512
513         case LFUN_INSET_MODIFY: {
514                 if (cmd.argument() == from_ascii("toggle-subindex")) {
515                         status.setEnabled(buffer().masterBuffer()->params().use_indices);
516                         status.setOnOff(contains(getCmdName(), "printsubindex"));
517                         return true;
518                 } else if (cmd.argument() == from_ascii("check-printindex*")) {
519                         status.setEnabled(buffer().masterBuffer()->params().use_indices);
520                         status.setOnOff(suffixIs(getCmdName(), '*'));
521                         return true;
522                 } if (cmd.getArg(0) == "index_print"
523                     && cmd.getArg(1) == "CommandInset") {
524                         InsetCommandParams p(INDEX_PRINT_CODE);
525                         InsetCommand::string2params("index_print",
526                                 to_utf8(cmd.argument()), p);
527                         if (suffixIs(p.getCmdName(), '*')) {
528                                 status.setEnabled(true);
529                                 status.setOnOff(false);
530                                 return true;
531                         }
532                         Buffer const & realbuffer = *buffer().masterBuffer();
533                         IndicesList const & indiceslist =
534                                 realbuffer.params().indiceslist();
535                         Index const * index = indiceslist.findShortcut(p["type"]);
536                         status.setEnabled(index != 0);
537                         status.setOnOff(p["type"] == getParam("type"));
538                         return true;
539                 } else
540                         return InsetCommand::getStatus(cur, cmd, status);
541         }
542         
543         case LFUN_INSET_DIALOG_UPDATE: {
544                 status.setEnabled(buffer().masterBuffer()->params().use_indices);
545                 return true;
546         }
547
548         default:
549                 return InsetCommand::getStatus(cur, cmd, status);
550         }
551 }
552
553
554 int InsetPrintIndex::latex(odocstream & os, OutputParams const & runparams_in) const
555 {
556         if (!buffer().masterBuffer()->params().use_indices) {
557                 if (getParam("type") == from_ascii("idx"))
558                         os << "\\printindex{}";
559                 return 0;
560         }
561         OutputParams runparams = runparams_in;
562         os << getCommand(runparams);
563         return 0;
564 }
565
566
567 void InsetPrintIndex::validate(LaTeXFeatures & features) const
568 {
569         features.require("makeidx");
570         if (buffer().masterBuffer()->params().use_indices)
571                 features.require("splitidx");
572 }
573
574
575 docstring InsetPrintIndex::contextMenu(BufferView const &, int, int) const
576 {
577         return buffer().masterBuffer()->params().use_indices ?
578                 from_ascii("context-indexprint") : docstring();
579 }
580
581
582 bool InsetPrintIndex::hasSettings() const
583 {
584         return buffer().masterBuffer()->params().use_indices;
585 }
586
587
588 namespace {
589
590 void parseItem(docstring & s, bool for_output)
591 {
592         // this does not yet check for escaped things
593         size_type loc = s.find(from_ascii("@"));
594         if (loc != string::npos) {
595                 if (for_output)
596                         s.erase(0, loc + 1);
597                 else
598                         s.erase(loc);
599         }
600         loc = s.find(from_ascii("|"));
601         if (loc != string::npos)
602                 s.erase(loc);
603 }
604
605         
606 void extractSubentries(docstring const & entry, docstring & main,
607                 docstring & sub1, docstring & sub2)
608 {
609         if (entry.empty())
610                 return;
611         size_type const loc = entry.find(from_ascii(" ! "));
612         if (loc == string::npos)
613                 main = entry;
614         else {
615                 main = trim(entry.substr(0, loc));
616                 size_t const locend = loc + 3;
617                 size_type const loc2 = entry.find(from_ascii(" ! "), locend);
618                 if (loc2 == string::npos) {
619                         sub1 = trim(entry.substr(locend));
620                 } else {
621                         sub1 = trim(entry.substr(locend, loc2 - locend));
622                         sub2 = trim(entry.substr(loc2 + 3));
623                 }
624         }
625 }
626
627
628 struct IndexEntry
629 {
630         IndexEntry() 
631         {}
632         
633         IndexEntry(docstring const & s, DocIterator const & d) 
634                         : dit(d)
635         {
636                 extractSubentries(s, main, sub, subsub);
637                 parseItem(main, false);
638                 parseItem(sub, false);
639                 parseItem(subsub, false);
640         }
641         
642         bool equal(IndexEntry const & rhs) const
643         {
644                 return main == rhs.main && sub == rhs.sub && subsub == rhs.subsub;
645         }
646         
647         bool same_sub(IndexEntry const & rhs) const
648         {
649                 return main == rhs.main && sub == rhs.sub;
650         }
651         
652         bool same_main(IndexEntry const & rhs) const
653         {
654                 return main == rhs.main;
655         }
656         
657         docstring main;
658         docstring sub;
659         docstring subsub;
660         DocIterator dit;
661 };
662
663 bool operator<(IndexEntry const & lhs, IndexEntry const & rhs)
664 {
665         return lhs.main < rhs.main
666                         || (lhs.main == rhs.main && lhs.sub < rhs.sub)
667                         || (lhs.main == rhs.main && lhs.sub == rhs.sub && lhs.subsub < rhs.subsub);
668 }
669
670 } // anon namespace
671
672
673 docstring InsetPrintIndex::xhtml(XHTMLStream &, OutputParams const & op) const
674 {
675         BufferParams const & bp = buffer().masterBuffer()->params();
676
677         // we do not presently support multiple indices, so we refuse to print
678         // anything but the main index, so as not to generate multiple indices.
679         // NOTE Multiple index support would require some work. The reason
680         // is that the TOC does not know about multiple indices. Either it would
681         // need to be told about them (not a bad idea), or else the index entries
682         // would need to be collected differently, say, during validation.
683         if (bp.use_indices && getParam("type") != from_ascii("idx"))
684                 return docstring();
685         
686         Toc const & toc = buffer().tocBackend().toc("index");
687         if (toc.empty())
688                 return docstring();
689
690         // Collection the index entries in a form we can use them.
691         Toc::const_iterator it = toc.begin();
692         Toc::const_iterator const en = toc.end();
693         vector<IndexEntry> entries;
694         for (; it != en; ++it)
695                 entries.push_back(IndexEntry(it->str(), it->dit()));
696         stable_sort(entries.begin(), entries.end());
697
698         Layout const & lay = bp.documentClass().htmlTOCLayout();
699         string const & tocclass = lay.defaultCSSClass();
700         string const tocattr = "class='tochead " + tocclass + "'";
701
702         // we'll use our own stream, because we are going to defer everything.
703         // that's how we deal with the fact that we're probably inside a standard
704         // paragraph, and we don't want to be.
705         odocstringstream ods;
706         XHTMLStream xs(ods);
707
708         xs << html::StartTag("div", "class='index'");
709         xs << html::StartTag(lay.htmltag(), lay.htmlattr()) 
710                  << _("Index") 
711                  << html::EndTag(lay.htmltag());
712         xs << html::StartTag("ul", "class='main'");
713         Font const dummy;
714
715         vector<IndexEntry>::const_iterator eit = entries.begin();
716         vector<IndexEntry>::const_iterator const een = entries.end();
717         // tracks whether we are already inside a main entry (1),
718         // a sub-entry (2), or a sub-sub-entry (3). see below for the
719         // details.
720         int level = 1;
721         // the last one we saw
722         IndexEntry last;
723         int entry_number = -1;
724         for (; eit != een; ++eit) {
725                 Paragraph const & par = eit->dit.innerParagraph();
726                 if (entry_number == -1 || !eit->equal(last)) {
727                         if (entry_number != -1) {
728                                 // not the first time through the loop, so
729                                 // close last entry or entries, depending.
730                                 if (level == 3) {
731                                         // close this sub-sub-entry
732                                         xs << html::EndTag("li");
733                                         xs.cr();
734                                         // is this another sub-sub-entry within the same sub-entry?
735                                         if (!eit->same_sub(last)) {
736                                                 // close this level
737                                                 xs << html::EndTag("ul");
738                                                 xs.cr();
739                                                 level = 2;
740                                         }
741                                 }
742                                 // the point of the second test here is that we might get
743                                 // here two ways: (i) by falling through from above; (ii) because,
744                                 // though the sub-entry hasn't changed, the sub-sub-entry has,
745                                 // which means that it is the first sub-sub-entry within this
746                                 // sub-entry. In that case, we do not want to close anything.
747                                 if (level == 2 && !eit->same_sub(last)) {
748                                         // close sub-entry 
749                                         xs << html::EndTag("li");
750                                         xs.cr();
751                                         // is this another sub-entry with the same main entry?
752                                         if (!eit->same_main(last)) {
753                                                 // close this level
754                                                 xs << html::EndTag("ul");
755                                                 xs.cr();
756                                                 level = 1;
757                                         }
758                                 }
759                                 // again, we can get here two ways: from above, or because we have
760                                 // found the first sub-entry. in the latter case, we do not want to
761                                 // close the entry.
762                                 if (level == 1 && !eit->same_main(last)) {
763                                         // close entry
764                                         xs << html::EndTag("li");
765                                         xs.cr();
766                                 }
767                         }
768
769                         // we'll be starting new entries
770                         entry_number = 0;
771
772                         // We need to use our own stream, since we will have to
773                         // modify what we get back.
774                         odocstringstream ent;
775                         XHTMLStream entstream(ent);
776                         OutputParams ours = op;
777                         ours.for_toc = true;
778                         par.simpleLyXHTMLOnePar(buffer(), entstream, ours, dummy);
779         
780                         // these will contain XHTML versions of the main entry, etc
781                         // remember that everything will already have been escaped,
782                         // so we'll need to use NextRaw() during output.
783                         docstring main;
784                         docstring sub;
785                         docstring subsub;
786                         extractSubentries(ent.str(), main, sub, subsub);
787                         parseItem(main, true);
788                         parseItem(sub, true);
789                         parseItem(subsub, true);
790         
791                         if (level == 3) {
792                                 // another subsubentry
793                                 xs << html::StartTag("li", "class='subsubentry'") 
794                                    << XHTMLStream::NextRaw() << subsub;
795                         } else if (level == 2) {
796                                 // there are two ways we can be here: 
797                                 // (i) we can actually be inside a sub-entry already and be about
798                                 //     to output the first sub-sub-entry. in this case, our sub
799                                 //     and the last sub will be the same.
800                                 // (ii) we can just have closed a sub-entry, possibly after also
801                                 //     closing a list of sub-sub-entries. here our sub and the last
802                                 //     sub are different.
803                                 // only in the latter case do we need to output the new sub-entry.
804                                 // note that in this case, too, though, the sub-entry might already
805                                 // have a sub-sub-entry.
806                                 if (eit->sub != last.sub)
807                                         xs << html::StartTag("li", "class='subentry'") 
808                                            << XHTMLStream::NextRaw() << sub;
809                                 if (!subsub.empty()) {
810                                         // it's actually a subsubentry, so we need to start that list
811                                         xs.cr();
812                                         xs << html::StartTag("ul", "class='subsubentry'") 
813                                            << html::StartTag("li", "class='subsubentry'") 
814                                            << XHTMLStream::NextRaw() << subsub;
815                                         level = 3;
816                                 } 
817                         } else {
818                                 // there are also two ways we can be here: 
819                                 // (i) we can actually be inside an entry already and be about
820                                 //     to output the first sub-entry. in this case, our main
821                                 //     and the last main will be the same.
822                                 // (ii) we can just have closed an entry, possibly after also
823                                 //     closing a list of sub-entries. here our main and the last
824                                 //     main are different.
825                                 // only in the latter case do we need to output the new main entry.
826                                 // note that in this case, too, though, the main entry might already
827                                 // have a sub-entry, or even a sub-sub-entry.
828                                 if (eit->main != last.main)
829                                         xs << html::StartTag("li", "class='main'") << main;
830                                 if (!sub.empty()) {
831                                         // there's a sub-entry, too
832                                         xs.cr();
833                                         xs << html::StartTag("ul", "class='subentry'") 
834                                            << html::StartTag("li", "class='subentry'") 
835                                            << XHTMLStream::NextRaw() << sub;
836                                         level = 2;
837                                         if (!subsub.empty()) {
838                                                 // and a sub-sub-entry
839                                                 xs.cr();
840                                                 xs << html::StartTag("ul", "class='subsubentry'") 
841                                                    << html::StartTag("li", "class='subsubentry'") 
842                                                    << XHTMLStream::NextRaw() << subsub;
843                                                 level = 3;
844                                         }
845                                 } 
846                         }
847                 }
848                 // finally, then, we can output the index link itself
849                 string const parattr = "href='#" + par.magicLabel() + "'";
850                 xs << (entry_number == 0 ? ":" : ",");
851                 xs << " " << html::StartTag("a", parattr)
852                    << ++entry_number << html::EndTag("a");
853                 last = *eit;
854         }
855         // now we have to close all the open levels
856         while (level > 0) {
857                 xs << html::EndTag("li") << html::EndTag("ul");
858                 xs.cr();
859                 --level;
860         }
861         xs << html::EndTag("div");
862         xs.cr();
863         return ods.str();
864 }
865
866 } // namespace lyx