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/insetfloat.h"
26 #include "insets/insetoptarg.h"
27 #include "insets/insetwrap.h"
29 #include "support/convert.h"
38 ///////////////////////////////////////////////////////////////////////////
39 // TocBackend::Item implementation
41 TocBackend::Item::Item(ParConstIterator const & par_it, int d,
43 : par_it_(par_it), depth_(d), str_(s)
49 size_t pos = s.find(" ");
50 if (pos == string::npos) {
56 string s2 = s.substr(0, pos);
58 if (s2 == "Chapter" || s2 == "Part") {
59 size_t pos2 = s.find(" ", pos + 1);
60 if (pos2 == string::npos) {
61 // Unnumbered Chapter?? This should not happen.
62 uid_ = s.substr(pos + 1);
66 uid_ = s.substr(pos2 + 1);
70 uid_ = s.substr(pos + 1);
74 bool const TocBackend::Item::isValid() const
80 int const TocBackend::Item::id() const
86 int const TocBackend::Item::depth() const
92 docstring const & TocBackend::Item::str() const
98 docstring const TocBackend::Item::asString() const
100 return docstring(4 * depth_, ' ') + str_;
104 FuncRequest TocBackend::Item::action() const
106 return FuncRequest(LFUN_PARAGRAPH_GOTO, convert<string>(id()));
113 ///////////////////////////////////////////////////////////////////////////
114 // TocBackend implementation
116 TocBackend::Toc const & TocBackend::toc(std::string const & type) const
118 // Is the type already supported?
119 TocList::const_iterator it = tocs_.find(type);
120 BOOST_ASSERT(it != tocs_.end());
126 bool TocBackend::addType(std::string const & type)
128 // Is the type already supported?
129 TocList::iterator toclist_it = tocs_.find(type);
130 if (toclist_it != tocs_.end())
133 tocs_.insert(make_pair(type, Toc()));
134 types_.push_back(type);
140 void TocBackend::update()
145 BufferParams const & bufparams = buffer_->params();
146 const int min_toclevel = bufparams.getLyXTextClass().min_toclevel();
148 ParConstIterator pit = buffer_->par_iterator_begin();
149 ParConstIterator end = buffer_->par_iterator_end();
150 for (; pit != end; ++pit) {
152 // the string that goes to the toc (could be the optarg)
155 // For each paragraph, traverse its insets and look for
156 // FLOAT_CODE or WRAP_CODE
157 InsetList::const_iterator it = pit->insetlist.begin();
158 InsetList::const_iterator end = pit->insetlist.end();
159 for (; it != end; ++it) {
160 switch (it->inset->lyxCode()) {
161 case InsetBase::FLOAT_CODE:
162 static_cast<InsetFloat*>(it->inset)
163 ->addToToc(tocs_, *buffer_);
165 case InsetBase::WRAP_CODE:
166 static_cast<InsetWrap*>(it->inset)
167 ->addToToc(tocs_, *buffer_);
169 case InsetBase::OPTARG_CODE: {
170 if (!tocstring.empty())
172 Paragraph const & par = *static_cast<InsetOptArg*>(it->inset)->paragraphs().begin();
173 if (!pit->getLabelstring().empty())
174 tocstring = pit->getLabelstring() + ' ';
175 tocstring += par.asString(*buffer_, false);
183 /// now the toc entry for the paragraph
184 int const toclevel = pit->layout()->toclevel;
185 if (toclevel != LyXLayout::NOT_IN_TOC
186 && toclevel >= min_toclevel
187 && toclevel <= bufparams.tocdepth) {
188 // insert this into the table of contents
189 if (tocstring.empty())
190 tocstring = pit->asString(*buffer_, true);
191 Item const item(pit, toclevel - min_toclevel, tocstring);
192 tocs_["tableofcontents"].push_back(item);
196 TocList::iterator it = tocs_.begin();
197 for (; it != tocs_.end(); ++it)
198 types_.push_back(it->first);
202 TocBackend::TocIterator const TocBackend::item(
203 std::string const & type, ParConstIterator const & par_it) const
205 TocList::const_iterator toclist_it = tocs_.find(type);
206 // Is the type supported?
207 BOOST_ASSERT(toclist_it != tocs_.end());
209 Toc const & toc_vector = toclist_it->second;
210 TocBackend::TocIterator last = toc_vector.begin();
211 TocBackend::TocIterator it = toc_vector.end();
214 for (; it != last; --it) {
216 // A good solution for Items inside insets would be to do:
218 //if (std::distance(it->par_it_, current) <= 0)
221 // But for an unknown reason, std::distance(current, it->par_it_) always
222 // returns a positive value and std::distance(it->par_it_, current) takes forever...
223 // So for now, we do:
224 if (it->par_it_.pit() <= par_it.pit())
228 // We are before the first Toc Item:
233 void TocBackend::asciiTocList(string const & type, odocstream & os) const
235 TocList::const_iterator cit = tocs_.find(type);
236 if (cit != tocs_.end()) {
237 Toc::const_iterator ccit = cit->second.begin();
238 Toc::const_iterator end = cit->second.end();
239 for (; ccit != end; ++ccit)
240 os << ccit->asString() << '\n';