3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Jean-Marc Lasgouttes
7 * \author Angus Leeming
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
15 #include "TocBackend.h"
18 #include "BufferParams.h"
19 #include "FloatList.h"
20 #include "FuncRequest.h"
21 #include "InsetList.h"
23 #include "LyXAction.h"
24 #include "Paragraph.h"
25 #include "TextClass.h"
27 #include "insets/InsetOptArg.h"
29 #include "support/convert.h"
30 #include "support/debug.h"
31 #include "support/docstream.h"
37 ///////////////////////////////////////////////////////////////////////////
39 // TocItem implementation
41 ///////////////////////////////////////////////////////////////////////////
43 TocItem::TocItem(ParConstIterator const & par_it, int d, docstring const & s)
44 : par_it_(par_it), depth_(d), str_(s)
49 int TocItem::id() const
55 int TocItem::depth() const
61 docstring const & TocItem::str() const
67 docstring const TocItem::asString() const
69 return docstring(4 * depth_, ' ') + str_;
73 FuncRequest TocItem::action() const
75 return FuncRequest(LFUN_PARAGRAPH_GOTO, convert<string>(id()));
79 ///////////////////////////////////////////////////////////////////////////
81 // TocBackend implementation
83 ///////////////////////////////////////////////////////////////////////////
85 Toc const & TocBackend::toc(string const & type) const
87 // Is the type already supported?
88 TocList::const_iterator it = tocs_.find(type);
89 BOOST_ASSERT(it != tocs_.end());
95 Toc & TocBackend::toc(string const & type)
101 void TocBackend::updateItem(ParConstIterator const & par_it)
103 if (toc("tableofcontents").empty()) {
104 // FIXME: should not happen,
105 // a call to TocBackend::update() is missing somewhere
106 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
110 BufferParams const & bufparams = buffer_->params();
111 const int min_toclevel = bufparams.textClass().min_toclevel();
113 TocIterator toc_item = item("tableofcontents", par_it);
117 // For each paragraph, traverse its insets and let them add
119 InsetList::const_iterator it = toc_item->par_it_->insetList().begin();
120 InsetList::const_iterator end = toc_item->par_it_->insetList().end();
121 for (; it != end; ++it) {
122 Inset & inset = *it->inset;
123 if (inset.lyxCode() == OPTARG_CODE) {
124 if (!tocstring.empty())
126 Paragraph const & par =
127 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
128 if (!toc_item->par_it_->labelString().empty())
129 tocstring = toc_item->par_it_->labelString() + ' ';
130 tocstring += par.asString(*buffer_, false);
135 int const toclevel = toc_item->par_it_->layout()->toclevel;
136 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
137 && tocstring.empty())
138 tocstring = toc_item->par_it_->asString(*buffer_, true);
140 const_cast<TocItem &>(*toc_item).str_ = tocstring;
144 void TocBackend::update()
148 BufferParams const & bufparams = buffer_->params();
149 const int min_toclevel = bufparams.textClass().min_toclevel();
151 Toc & toc = tocs_["tableofcontents"];
152 ParConstIterator pit = buffer_->par_iterator_begin();
153 ParConstIterator end = buffer_->par_iterator_end();
154 for (; pit != end; ++pit) {
156 // the string that goes to the toc (could be the optarg)
159 // For each paragraph, traverse its insets and let them add
161 InsetList::const_iterator it = pit->insetList().begin();
162 InsetList::const_iterator end = pit->insetList().end();
163 for (; it != end; ++it) {
164 Inset & inset = *it->inset;
165 inset.addToToc(*buffer_, pit);
166 switch (inset.lyxCode()) {
168 if (!tocstring.empty())
170 Paragraph const & par =
171 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
172 if (!pit->labelString().empty())
173 tocstring = pit->labelString() + ' ';
174 tocstring += par.asString(*buffer_, false);
182 /// now the toc entry for the paragraph
183 int const toclevel = pit->layout()->toclevel;
184 if (toclevel != Layout::NOT_IN_TOC
185 && toclevel >= min_toclevel) {
186 // insert this into the table of contents
187 if (tocstring.empty())
188 tocstring = pit->asString(*buffer_, true);
189 toc.push_back(TocItem(pit, toclevel - min_toclevel,
196 TocIterator TocBackend::item(string const & type,
197 ParConstIterator const & par_it) const
199 TocList::const_iterator toclist_it = tocs_.find(type);
200 // Is the type supported?
201 BOOST_ASSERT(toclist_it != tocs_.end());
203 Toc const & toc_vector = toclist_it->second;
204 TocIterator last = toc_vector.begin();
205 TocIterator it = toc_vector.end();
211 ParConstIterator par_it_text = par_it;
212 if (par_it_text.inMathed()) {
213 // We are only interested in text so remove the math CursorSlice.
214 while (par_it_text.inMathed())
215 par_it_text.pop_back();
218 for (; it != last; --it) {
219 // We verify that we don't compare contents of two
220 // different document. This happens when you
221 // have parent and child documents.
222 if (&it->par_it_[0].inset() != &par_it_text[0].inset())
224 if (it->par_it_ <= par_it_text)
228 // We are before the first Toc Item:
233 void TocBackend::writePlaintextTocList(string const & type, odocstream & os) const
235 TocList::const_iterator cit = tocs_.find(type);
236 if (cit != tocs_.end()) {
237 TocIterator ccit = cit->second.begin();
238 TocIterator end = cit->second.end();
239 for (; ccit != end; ++ccit)
240 os << ccit->asString() << from_utf8("\n");