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 "LyXAction.h"
22 #include "Paragraph.h"
25 #include "insets/InsetOptArg.h"
27 #include "support/convert.h"
36 ///////////////////////////////////////////////////////////////////////////
37 // TocItem implementation
39 TocItem::TocItem(ParConstIterator const & par_it, int d,
41 : par_it_(par_it), depth_(d), str_(s)
47 size_t pos = s.find(" ");
48 if (pos == string::npos) {
54 string s2 = s.substr(0, pos);
56 if (s2 == "Chapter" || s2 == "Part") {
57 size_t pos2 = s.find(" ", pos + 1);
58 if (pos2 == string::npos) {
59 // Unnumbered Chapter?? This should not happen.
60 uid_ = s.substr(pos + 1);
64 uid_ = s.substr(pos2 + 1);
68 uid_ = s.substr(pos + 1);
72 bool const TocItem::isValid() const
78 int const TocItem::id() const
84 int const TocItem::depth() const
90 docstring const & TocItem::str() const
96 docstring const TocItem::asString() const
98 return docstring(4 * depth_, ' ') + str_;
102 FuncRequest TocItem::action() const
104 return FuncRequest(LFUN_PARAGRAPH_GOTO, convert<string>(id()));
111 ///////////////////////////////////////////////////////////////////////////
112 // TocBackend implementation
114 Toc const & TocBackend::toc(std::string const & type) const
116 // Is the type already supported?
117 TocList::const_iterator it = tocs_.find(type);
118 BOOST_ASSERT(it != tocs_.end());
124 void TocBackend::updateItem(ParConstIterator const & par_it)
126 // TODO should not happen,
127 // a call to TocBackend::update() is missing somewhere
128 if (toc("tableofcontents").empty())
131 BufferParams const & bufparams = buffer_->params();
132 const int min_toclevel = bufparams.getTextClass().min_toclevel();
134 TocIterator toc_item = item("tableofcontents", par_it);
138 // For each paragraph, traverse its insets and let them add
140 InsetList::const_iterator it = toc_item->par_it_->insetlist.begin();
141 InsetList::const_iterator end = toc_item->par_it_->insetlist.end();
142 for (; it != end; ++it) {
143 Inset & inset = *it->inset;
144 if (inset.lyxCode() == Inset::OPTARG_CODE) {
145 if (!tocstring.empty())
147 Paragraph const & par =
148 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
149 if (!toc_item->par_it_->getLabelstring().empty())
150 tocstring = toc_item->par_it_->getLabelstring() + ' ';
151 tocstring += par.asString(*buffer_, false);
156 int const toclevel = toc_item->par_it_->layout()->toclevel;
157 if (toclevel != Layout::NOT_IN_TOC
158 && toclevel >= min_toclevel
159 && toclevel <= bufparams.tocdepth
160 && tocstring.empty())
161 tocstring = toc_item->par_it_->asString(*buffer_, true);
163 const_cast<TocItem &>(*toc_item).str_ = tocstring;
167 void TocBackend::update()
171 BufferParams const & bufparams = buffer_->params();
172 const int min_toclevel = bufparams.getTextClass().min_toclevel();
174 Toc & toc = tocs_["tableofcontents"];
175 ParConstIterator pit = buffer_->par_iterator_begin();
176 ParConstIterator end = buffer_->par_iterator_end();
177 for (; pit != end; ++pit) {
179 // the string that goes to the toc (could be the optarg)
182 // For each paragraph, traverse its insets and let them add
184 InsetList::const_iterator it = pit->insetlist.begin();
185 InsetList::const_iterator end = pit->insetlist.end();
186 for (; it != end; ++it) {
187 Inset & inset = *it->inset;
188 inset.addToToc(tocs_, *buffer_);
189 switch (inset.lyxCode()) {
190 case Inset::OPTARG_CODE: {
191 if (!tocstring.empty())
193 Paragraph const & par =
194 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
195 if (!pit->getLabelstring().empty())
196 tocstring = pit->getLabelstring() + ' ';
197 tocstring += par.asString(*buffer_, false);
205 /// now the toc entry for the paragraph
206 int const toclevel = pit->layout()->toclevel;
207 if (toclevel != Layout::NOT_IN_TOC
208 && toclevel >= min_toclevel
209 && toclevel <= bufparams.tocdepth) {
210 // insert this into the table of contents
211 if (tocstring.empty())
212 tocstring = pit->asString(*buffer_, true);
214 TocItem(pit, toclevel - min_toclevel, tocstring));
220 TocIterator const TocBackend::item(
221 std::string const & type, ParConstIterator const & par_it) const
223 TocList::const_iterator toclist_it = tocs_.find(type);
224 // Is the type supported?
225 BOOST_ASSERT(toclist_it != tocs_.end());
227 Toc const & toc_vector = toclist_it->second;
228 TocIterator last = toc_vector.begin();
229 TocIterator it = toc_vector.end();
235 ParConstIterator par_it_text = par_it;
236 if (par_it_text.inMathed())
237 // It would be better to do
238 // par_it_text.backwardInset();
239 // but this method does not exist.
240 while (par_it_text.inMathed())
241 par_it_text.backwardPos();
243 for (; it != last; --it) {
245 // A good solution for Items inside insets would be to do:
247 //if (std::distance(it->par_it_, current) <= 0)
250 // But for an unknown reason, std::distance(current, it->par_it_) always
251 // returns a positive value and std::distance(it->par_it_, current) takes forever...
252 // So for now, we do:
253 if (it->par_it_.pit() <= par_it_text.pit())
257 // We are before the first Toc Item:
262 void TocBackend::writePlaintextTocList(string const & type, odocstream & os) const
264 TocList::const_iterator cit = tocs_.find(type);
265 if (cit != tocs_.end()) {
266 TocIterator ccit = cit->second.begin();
267 TocIterator end = cit->second.end();
268 for (; ccit != end; ++ccit)
269 os << ccit->asString() << '\n';