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 bool TocBackend::addType(std::string const & type)
126 // Is the type already supported?
127 TocList::iterator toclist_it = tocs_.find(type);
128 if (toclist_it != tocs_.end())
131 tocs_.insert(make_pair(type, Toc()));
132 types_.push_back(type);
138 void TocBackend::update()
143 BufferParams const & bufparams = buffer_->params();
144 const int min_toclevel = bufparams.getLyXTextClass().min_toclevel();
146 Toc & toc = tocs_["tableofcontents"];
147 ParConstIterator pit = buffer_->par_iterator_begin();
148 ParConstIterator end = buffer_->par_iterator_end();
149 for (; pit != end; ++pit) {
151 // the string that goes to the toc (could be the optarg)
154 // For each paragraph, traverse its insets and let them add
156 InsetList::const_iterator it = pit->insetlist.begin();
157 InsetList::const_iterator end = pit->insetlist.end();
158 for (; it != end; ++it) {
159 InsetBase & inset = *it->inset;
160 inset.addToToc(tocs_, *buffer_);
161 switch (inset.lyxCode()) {
162 case InsetBase::OPTARG_CODE: {
163 if (!tocstring.empty())
165 Paragraph const & par =
166 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
167 if (!pit->getLabelstring().empty())
168 tocstring = pit->getLabelstring() + ' ';
169 tocstring += par.asString(*buffer_, false);
177 /// now the toc entry for the paragraph
178 int const toclevel = pit->layout()->toclevel;
179 if (toclevel != LyXLayout::NOT_IN_TOC
180 && toclevel >= min_toclevel
181 && toclevel <= bufparams.tocdepth) {
182 // insert this into the table of contents
183 if (tocstring.empty())
184 tocstring = pit->asString(*buffer_, true);
186 TocItem(pit, toclevel - min_toclevel, tocstring));
190 TocList::iterator it = tocs_.begin();
191 for (; it != tocs_.end(); ++it)
192 types_.push_back(it->first);
196 TocIterator const TocBackend::item(
197 std::string const & type, 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 // It would be better to do
214 // par_it_text.backwardInset();
215 // but this method does not exist.
216 while (par_it_text.inMathed())
217 par_it_text.backwardPos();
219 for (; it != last; --it) {
221 // A good solution for Items inside insets would be to do:
223 //if (std::distance(it->par_it_, current) <= 0)
226 // But for an unknown reason, std::distance(current, it->par_it_) always
227 // returns a positive value and std::distance(it->par_it_, current) takes forever...
228 // So for now, we do:
229 if (it->par_it_.pit() <= par_it_text.pit())
233 // We are before the first Toc Item:
238 void TocBackend::writePlaintextTocList(string const & type, odocstream & os) const
240 TocList::const_iterator cit = tocs_.find(type);
241 if (cit != tocs_.end()) {
242 TocIterator ccit = cit->second.begin();
243 TocIterator end = cit->second.end();
244 for (; ccit != end; ++ccit)
245 os << ccit->asString() << '\n';