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 "InsetList.h"
23 #include "LyXAction.h"
24 #include "Paragraph.h"
25 #include "TextClass.h"
27 #include "insets/InsetOptArg.h"
29 #include "support/convert.h"
30 #include "support/debug.h"
31 #include "support/docstream.h"
38 ///////////////////////////////////////////////////////////////////////////
40 // TocItem implementation
42 ///////////////////////////////////////////////////////////////////////////
44 TocItem::TocItem(ParConstIterator const & par_it, int d,
46 : par_it_(par_it), depth_(d), str_(s)
51 int TocItem::id() const
57 int TocItem::depth() const
63 docstring const & TocItem::str() const
69 docstring const TocItem::asString() const
71 return docstring(4 * depth_, ' ') + str_;
75 FuncRequest TocItem::action() const
77 return FuncRequest(LFUN_PARAGRAPH_GOTO, convert<string>(id()));
81 ///////////////////////////////////////////////////////////////////////////
83 // TocBackend implementation
85 ///////////////////////////////////////////////////////////////////////////
87 Toc const & TocBackend::toc(std::string const & type) const
89 // Is the type already supported?
90 TocList::const_iterator it = tocs_.find(type);
91 BOOST_ASSERT(it != tocs_.end());
97 void TocBackend::updateItem(ParConstIterator const & par_it)
99 if (toc("tableofcontents").empty()) {
100 // FIXME: should not happen,
101 // a call to TocBackend::update() is missing somewhere
102 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
106 BufferParams const & bufparams = buffer_->params();
107 const int min_toclevel = bufparams.getTextClass().min_toclevel();
109 TocIterator toc_item = item("tableofcontents", par_it);
113 // For each paragraph, traverse its insets and let them add
115 InsetList::const_iterator it = toc_item->par_it_->insetList().begin();
116 InsetList::const_iterator end = toc_item->par_it_->insetList().end();
117 for (; it != end; ++it) {
118 Inset & inset = *it->inset;
119 if (inset.lyxCode() == OPTARG_CODE) {
120 if (!tocstring.empty())
122 Paragraph const & par =
123 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
124 if (!toc_item->par_it_->getLabelstring().empty())
125 tocstring = toc_item->par_it_->getLabelstring() + ' ';
126 tocstring += par.asString(*buffer_, false);
131 int const toclevel = toc_item->par_it_->layout()->toclevel;
132 if (toclevel != Layout::NOT_IN_TOC
133 && toclevel >= min_toclevel
134 && tocstring.empty())
135 tocstring = toc_item->par_it_->asString(*buffer_, true);
137 const_cast<TocItem &>(*toc_item).str_ = tocstring;
141 void TocBackend::update()
145 BufferParams const & bufparams = buffer_->params();
146 const int min_toclevel = bufparams.getTextClass().min_toclevel();
148 Toc & toc = tocs_["tableofcontents"];
149 ParConstIterator pit = buffer_->par_iterator_begin();
150 ParConstIterator end = buffer_->par_iterator_end();
151 for (; pit != end; ++pit) {
153 // the string that goes to the toc (could be the optarg)
156 // For each paragraph, traverse its insets and let them add
158 InsetList::const_iterator it = pit->insetList().begin();
159 InsetList::const_iterator end = pit->insetList().end();
160 for (; it != end; ++it) {
161 Inset & inset = *it->inset;
162 inset.addToToc(tocs_, *buffer_, pit);
163 switch (inset.lyxCode()) {
165 if (!tocstring.empty())
167 Paragraph const & par =
168 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
169 if (!pit->getLabelstring().empty())
170 tocstring = pit->getLabelstring() + ' ';
171 tocstring += par.asString(*buffer_, false);
179 /// now the toc entry for the paragraph
180 int const toclevel = pit->layout()->toclevel;
181 if (toclevel != Layout::NOT_IN_TOC
182 && toclevel >= min_toclevel) {
183 // insert this into the table of contents
184 if (tocstring.empty())
185 tocstring = pit->asString(*buffer_, true);
186 toc.push_back(TocItem(pit, toclevel - min_toclevel,
193 TocIterator const TocBackend::item(std::string const & type,
194 ParConstIterator const & par_it) const
196 TocList::const_iterator toclist_it = tocs_.find(type);
197 // Is the type supported?
198 BOOST_ASSERT(toclist_it != tocs_.end());
200 Toc const & toc_vector = toclist_it->second;
201 TocIterator last = toc_vector.begin();
202 TocIterator it = toc_vector.end();
208 ParConstIterator par_it_text = par_it;
209 if (par_it_text.inMathed())
210 // It would be better to do
211 // par_it_text.backwardInset();
212 // but this method does not exist.
213 while (par_it_text.inMathed())
214 par_it_text.backwardPos();
216 for (; it != last; --it) {
217 // We verify that we don't compare contents of two
218 // different document. This happens when you
219 // have parent and child documents.
220 if (&it->par_it_[0].inset() != &par_it_text[0].inset())
222 if (it->par_it_ <= par_it_text)
226 // We are before the first Toc Item:
231 void TocBackend::writePlaintextTocList(string const & type, odocstream & os) const
233 TocList::const_iterator cit = tocs_.find(type);
234 if (cit != tocs_.end()) {
235 TocIterator ccit = cit->second.begin();
236 TocIterator end = cit->second.end();
237 for (; ccit != end; ++ccit)
238 os << ccit->asString() << from_utf8("\n");