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
9 * \author Guillaume Munch
11 * Full author contact details are available in file CREDITS.
16 #include "TocBackend.h"
19 #include "BufferParams.h"
20 #include "FloatList.h"
21 #include "FuncRequest.h"
22 #include "InsetList.h"
24 #include "LyXAction.h"
25 #include "Paragraph.h"
26 #include "ParIterator.h"
27 #include "TextClass.h"
29 #include "insets/InsetArgument.h"
31 #include "support/convert.h"
32 #include "support/debug.h"
33 #include "support/docstream.h"
35 #include "support/lassert.h"
42 ///////////////////////////////////////////////////////////////////////////
44 // TocItem implementation
46 ///////////////////////////////////////////////////////////////////////////
48 TocItem::TocItem(DocIterator const & dit, int d, docstring const & s,
49 bool output_active, docstring const & t) :
50 dit_(dit), depth_(d), str_(s), tooltip_(t), output_(output_active)
55 int TocItem::id() const
57 return dit_.paragraph().id();
61 docstring const & TocItem::tooltip() const
63 return tooltip_.empty() ? str_ : tooltip_;
67 docstring const TocItem::asString() const
69 return docstring(4 * depth_, ' ') + str_;
73 FuncRequest TocItem::action() const
75 string const arg = convert<string>(dit_.paragraph().id())
76 + ' ' + convert<string>(dit_.pos());
77 return FuncRequest(LFUN_PARAGRAPH_GOTO, arg);
81 ///////////////////////////////////////////////////////////////////////////
85 ///////////////////////////////////////////////////////////////////////////
87 TocIterator Toc::item(DocIterator const & dit) const
89 TocIterator last = begin();
90 TocIterator it = end();
96 DocIterator dit_text = dit;
97 if (dit_text.inMathed()) {
98 // We are only interested in text so remove the math CursorSlice.
99 while (dit_text.inMathed())
103 for (; it != last; --it) {
104 // We verify that we don't compare contents of two
105 // different document. This happens when you
106 // have parent and child documents.
107 if (&it->dit_[0].inset() != &dit_text[0].inset())
109 if (it->dit_ <= dit_text)
113 // We are before the first Toc Item:
118 Toc::iterator Toc::item(int depth, docstring const & str)
122 iterator it = begin();
123 iterator itend = end();
124 for (; it != itend; ++it) {
125 if (it->depth() == depth && it->str() == str)
132 ///////////////////////////////////////////////////////////////////////////
134 // TocBuilder implementation
136 ///////////////////////////////////////////////////////////////////////////
138 TocBuilder::TocBuilder(shared_ptr<Toc> toc)
139 : toc_(toc ? toc : make_shared<Toc>()),
145 void TocBuilder::pushItem(DocIterator const & dit, docstring const & s,
146 bool output_active, bool is_captioned)
148 toc_->push_back(TocItem(dit, stack_.size(), s, output_active));
150 toc_->size() - 1, //pos
151 is_captioned, //is_captioned
156 void TocBuilder::captionItem(DocIterator const & dit, docstring const & s,
159 if (!stack_.empty() && !stack_.top().is_captioned) {
160 // The float we entered has not yet been assigned a caption.
161 // Assign the caption string to it.
162 (*toc_)[stack_.top().pos].str(s);
163 stack_.top().is_captioned = true;
165 // This is a new entry.
167 pushItem(dit, s, output_active, true);
171 void TocBuilder::pop()
179 ///////////////////////////////////////////////////////////////////////////
181 // TocBuilderStore implementation
183 ///////////////////////////////////////////////////////////////////////////
185 shared_ptr<TocBuilder> TocBuilderStore::get(string const & type,
188 map_t::const_iterator it = map_.find(type);
189 if (it == map_.end()) {
190 it = map_.insert(std::make_pair(type,
191 make_shared<TocBuilder>(toc))).first;
198 ///////////////////////////////////////////////////////////////////////////
200 // TocBackend implementation
202 ///////////////////////////////////////////////////////////////////////////
204 shared_ptr<Toc const> TocBackend::toc(string const & type) const
206 // Is the type already supported?
207 TocList::const_iterator it = tocs_.find(type);
208 LASSERT(it != tocs_.end(), { return make_shared<Toc>(); });
213 shared_ptr<Toc> TocBackend::toc(string const & type)
215 TocList::const_iterator it = tocs_.find(type);
216 if (it == tocs_.end()) {
217 it = tocs_.insert(std::make_pair(type, make_shared<Toc>())).first;
223 shared_ptr<TocBuilder> TocBackend::builder(string const & type)
225 return builders_.get(type, toc(type));
229 bool TocBackend::updateItem(DocIterator const & dit)
231 if (dit.text()->getTocLevel(dit.pit()) == Layout::NOT_IN_TOC)
234 if (toc("tableofcontents")->empty()) {
235 // FIXME: should not happen,
236 // a call to TocBackend::update() is missing somewhere
237 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
241 BufferParams const & bufparams = buffer_->params();
242 const int min_toclevel = bufparams.documentClass().min_toclevel();
244 TocIterator toc_item = item("tableofcontents", dit);
248 // For each paragraph, traverse its insets and let them add
250 Paragraph & par = toc_item->dit_.paragraph();
251 InsetList::const_iterator it = par.insetList().begin();
252 InsetList::const_iterator end = par.insetList().end();
253 for (; it != end; ++it) {
254 Inset & inset = *it->inset;
255 if (inset.lyxCode() == ARG_CODE) {
256 if (!tocstring.empty())
258 Paragraph const & inset_par =
259 *static_cast<InsetArgument&>(inset).paragraphs().begin();
260 if (!par.labelString().empty())
261 tocstring = par.labelString() + ' ';
262 tocstring += inset_par.asString(AS_STR_INSETS);
267 int const toclevel = toc_item->dit_.text()->getTocLevel(toc_item->dit_.pit());
268 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
269 && tocstring.empty())
270 tocstring = par.asString(AS_STR_LABEL | AS_STR_INSETS);
272 const_cast<TocItem &>(*toc_item).str_ = tocstring;
274 buffer_->updateTocItem("tableofcontents", dit);
279 void TocBackend::update(bool output_active)
281 for (TocList::iterator it = tocs_.begin(); it != tocs_.end(); ++it)
285 if (!buffer_->isInternal()) {
287 buffer_->inset().addToToc(dit, output_active);
292 TocIterator TocBackend::item(string const & type,
293 DocIterator const & dit) const
295 TocList::const_iterator toclist_it = tocs_.find(type);
296 // Is the type supported?
297 // We will try to make the best of it in release mode
298 LASSERT(toclist_it != tocs_.end(), toclist_it = tocs_.begin());
299 return toclist_it->second->item(dit);
303 void TocBackend::writePlaintextTocList(string const & type,
304 odocstringstream & os, size_t max_length) const
306 TocList::const_iterator cit = tocs_.find(type);
307 if (cit != tocs_.end()) {
308 TocIterator ccit = cit->second->begin();
309 TocIterator end = cit->second->end();
310 for (; ccit != end; ++ccit) {
311 os << ccit->asString() << from_utf8("\n");
312 if (os.str().size() > max_length)