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"
21 #include "FloatList.h"
22 #include "FuncRequest.h"
23 #include "InsetList.h"
25 #include "LyXAction.h"
26 #include "Paragraph.h"
27 #include "ParIterator.h"
28 #include "TextClass.h"
30 #include "insets/InsetArgument.h"
32 #include "support/convert.h"
33 #include "support/debug.h"
34 #include "support/docstream.h"
35 #include "support/gettext.h"
36 #include "support/lassert.h"
37 #include "support/lstrings.h"
45 ///////////////////////////////////////////////////////////////////////////
47 // TocItem implementation
49 ///////////////////////////////////////////////////////////////////////////
51 TocItem::TocItem(DocIterator const & dit, int d, docstring const & s,
52 bool output_active, FuncRequest action)
53 : dit_(dit), depth_(d), str_(s), output_(output_active),
59 int TocItem::id() const
61 return dit_.paragraph().id();
65 docstring const TocItem::asString() const
67 static char_type const cross = 0x2716; // ✖ U+2716 HEAVY MULTIPLICATION X
68 static char_type const thin = 0x2009; // U+2009 THIN SPACE
79 // convert a DocIterator into an argument to LFUN_PARAGRAPH_GOTO
80 docstring paragraph_goto_arg(DocIterator const & dit)
82 CursorSlice const & s = dit.innerTextSlice();
83 return convert<docstring>(s.paragraph().id()) + ' ' +
84 convert<docstring>(s.pos());
89 FuncRequest TocItem::action() const
91 if (action_.action() == LFUN_UNKNOWN_ACTION) {
92 return FuncRequest(LFUN_PARAGRAPH_GOTO, paragraph_goto_arg(dit_));
98 ///////////////////////////////////////////////////////////////////////////
100 // Toc implementation
102 ///////////////////////////////////////////////////////////////////////////
104 Toc::const_iterator TocBackend::findItem(Toc const & toc,
105 DocIterator const & dit)
107 Toc::const_iterator last = toc.begin();
108 Toc::const_iterator it = toc.end();
112 DocIterator dit_text = dit.getInnerText();
114 for (; it != last; --it) {
115 // We verify that we don't compare contents of two
116 // different document. This happens when you
117 // have parent and child documents.
118 if (&it->dit_[0].inset() != &dit_text[0].inset())
120 if (it->dit_ <= dit_text)
124 // We are before the first Toc Item:
129 Toc::iterator TocBackend::findItem(Toc & toc, int depth, docstring const & str)
133 Toc::iterator it = toc.begin();
134 Toc::iterator itend = toc.end();
135 for (; it != itend; ++it) {
136 if (it->depth() == depth && it->str() == str)
143 ///////////////////////////////////////////////////////////////////////////
145 // TocBuilder implementation
147 ///////////////////////////////////////////////////////////////////////////
149 TocBuilder::TocBuilder(shared_ptr<Toc> toc)
150 : toc_(toc ? toc : make_shared<Toc>()),
156 void TocBuilder::pushItem(DocIterator const & dit, docstring const & s,
157 bool output_active, bool is_captioned)
159 toc_->push_back(TocItem(dit, stack_.size(), s, output_active));
161 toc_->size() - 1, //pos
162 is_captioned, //is_captioned
167 void TocBuilder::captionItem(DocIterator const & dit, docstring const & s,
170 // first show the float before moving to the caption
171 docstring arg = "paragraph-goto " + paragraph_goto_arg(dit);
173 arg = "paragraph-goto " +
174 paragraph_goto_arg((*toc_)[stack_.top().pos].dit_) + ";" + arg;
175 FuncRequest func(LFUN_COMMAND_SEQUENCE, arg);
177 if (!stack_.empty() && !stack_.top().is_captioned) {
178 // The float we entered has not yet been assigned a caption.
179 // Assign the caption string to it.
180 TocItem & captionable = (*toc_)[stack_.top().pos];
182 captionable.setAction(func);
183 stack_.top().is_captioned = true;
185 // This is a new entry.
187 // the dit is at the float's level, e.g. for the contextual menu of
189 DocIterator captionable_dit = dit;
190 captionable_dit.pop_back();
191 pushItem(captionable_dit, s, output_active, true);
192 (*toc_)[stack_.top().pos].setAction(func);
196 void TocBuilder::pop()
204 ///////////////////////////////////////////////////////////////////////////
206 // TocBackend implementation
208 ///////////////////////////////////////////////////////////////////////////
210 shared_ptr<Toc const> TocBackend::toc(string const & type) const
212 // Is the type already supported?
213 TocList::const_iterator it = tocs_.find(type);
214 LASSERT(it != tocs_.end(), { return make_shared<Toc>(); });
219 shared_ptr<Toc> TocBackend::toc(string const & type)
221 // std::map::insert only really performs the insertion if the key is not
222 // already bound, and otherwise returns an iterator to the element already
223 // there, see manual.
224 return tocs_.insert({type, make_shared<Toc>()}).first->second;
228 TocBuilder & TocBackend::builder(string const & type)
230 auto p = make_unique<TocBuilder>(toc(type));
231 return * builders_.insert(make_pair(type, move(p))).first->second;
235 // FIXME: This function duplicates functionality from InsetText::iterateForToc.
236 // Both have their own way of computing the TocItem for "tableofcontents". The
237 // TocItem creation and update should be made in a dedicated function and
238 // updateItem should be rewritten to uniformly update the matching items from
240 bool TocBackend::updateItem(DocIterator const & dit_in)
243 DocIterator dit = dit_in.getInnerText();
245 if (dit.text()->getTocLevel(dit.pit()) == Layout::NOT_IN_TOC)
248 if (toc("tableofcontents")->empty()) {
249 // FIXME: should not happen,
250 // a call to TocBackend::update() is missing somewhere
251 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
255 BufferParams const & bufparams = buffer_->params();
256 const int min_toclevel = bufparams.documentClass().min_toclevel();
258 Toc::const_iterator toc_item = item("tableofcontents", dit);
262 // For each paragraph, traverse its insets and let them add
265 // FIXME: This is supposed to accomplish the same as the body of
266 // InsetText::iterateForToc(), probably
267 Paragraph & par = toc_item->dit_.paragraph();
268 InsetList::const_iterator it = par.insetList().begin();
269 InsetList::const_iterator end = par.insetList().end();
270 for (; it != end; ++it) {
271 Inset & inset = *it->inset;
272 if (inset.lyxCode() == ARG_CODE) {
273 tocstring = par.labelString();
274 if (!tocstring.empty())
276 inset.asInsetText()->text().forOutliner(tocstring,TOC_ENTRY_LENGTH);
281 int const toclevel = toc_item->dit_.text()->
282 getTocLevel(toc_item->dit_.pit());
283 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
284 && tocstring.empty())
285 par.forOutliner(tocstring, TOC_ENTRY_LENGTH);
287 support::truncateWithEllipsis(tocstring, TOC_ENTRY_LENGTH);
288 const_cast<TocItem &>(*toc_item).str(tocstring);
290 buffer_->updateTocItem("tableofcontents", dit);
295 void TocBackend::update(bool output_active, UpdateType utype)
297 for (TocList::iterator it = tocs_.begin(); it != tocs_.end(); ++it)
301 if (!buffer_->isInternal()) {
303 buffer_->inset().addToToc(dit, output_active, utype);
308 Toc::const_iterator TocBackend::item(string const & type,
309 DocIterator const & dit) const
311 TocList::const_iterator toclist_it = tocs_.find(type);
312 // Is the type supported?
313 // We will try to make the best of it in release mode
314 LASSERT(toclist_it != tocs_.end(), toclist_it = tocs_.begin());
315 return findItem(*toclist_it->second, dit);
319 void TocBackend::writePlaintextTocList(string const & type,
320 odocstringstream & os, size_t max_length) const
322 TocList::const_iterator cit = tocs_.find(type);
323 if (cit != tocs_.end()) {
324 Toc::const_iterator ccit = cit->second->begin();
325 Toc::const_iterator end = cit->second->end();
326 for (; ccit != end; ++ccit) {
327 os << ccit->asString() << from_utf8("\n");
328 if (os.str().size() > max_length)
335 docstring TocBackend::outlinerName(string const & type) const
337 return translateIfPossible(
338 buffer_->params().documentClass().outlinerName(type));