2006-04-26 13:33:51 +00:00
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
* \file TocBackend.cpp
|
2006-04-26 13:33:51 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Jean-Marc Lasgouttes
|
|
|
|
* \author Angus Leeming
|
|
|
|
* \author Abdelrazak Younes
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2006-11-11 00:35:14 +00:00
|
|
|
#include "TocBackend.h"
|
2006-04-26 13:33:51 +00:00
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Buffer.h"
|
|
|
|
#include "BufferParams.h"
|
2006-04-26 13:33:51 +00:00
|
|
|
#include "FloatList.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "FuncRequest.h"
|
2007-10-18 15:29:51 +00:00
|
|
|
#include "InsetList.h"
|
2007-09-29 20:02:32 +00:00
|
|
|
#include "Layout.h"
|
2006-04-26 13:33:51 +00:00
|
|
|
#include "LyXAction.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Paragraph.h"
|
2008-05-13 08:23:44 +00:00
|
|
|
#include "ParIterator.h"
|
2007-11-07 23:25:08 +00:00
|
|
|
#include "TextClass.h"
|
2006-04-26 13:33:51 +00:00
|
|
|
|
2010-06-04 22:44:58 +00:00
|
|
|
#include "insets/InsetArgument.h"
|
2006-04-26 13:33:51 +00:00
|
|
|
|
|
|
|
#include "support/convert.h"
|
2007-11-29 17:51:58 +00:00
|
|
|
#include "support/debug.h"
|
|
|
|
#include "support/docstream.h"
|
2006-04-26 13:33:51 +00:00
|
|
|
|
2008-04-30 08:26:40 +00:00
|
|
|
#include "support/lassert.h"
|
2008-03-15 00:22:54 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2006-04-26 13:33:51 +00:00
|
|
|
|
2008-03-15 00:22:54 +00:00
|
|
|
|
2007-06-12 12:29:19 +00:00
|
|
|
namespace lyx {
|
2006-04-26 13:33:51 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2007-09-29 20:02:32 +00:00
|
|
|
//
|
2006-11-13 16:53:49 +00:00
|
|
|
// TocItem implementation
|
2007-09-29 20:02:32 +00:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2006-04-26 13:33:51 +00:00
|
|
|
|
2010-04-26 00:43:08 +00:00
|
|
|
TocItem::TocItem(DocIterator const & dit, int d, docstring const & s,
|
2013-03-08 21:18:26 +00:00
|
|
|
bool output_active, docstring const & t) :
|
|
|
|
dit_(dit), depth_(d), str_(s), tooltip_(t), output_(output_active)
|
2006-04-26 13:33:51 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-16 10:36:57 +00:00
|
|
|
int TocItem::id() const
|
2006-04-26 13:33:51 +00:00
|
|
|
{
|
2008-05-13 08:23:44 +00:00
|
|
|
return dit_.paragraph().id();
|
2006-04-26 13:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-16 10:36:57 +00:00
|
|
|
int TocItem::depth() const
|
2006-04-26 13:33:51 +00:00
|
|
|
{
|
|
|
|
return depth_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-13 16:53:49 +00:00
|
|
|
docstring const & TocItem::str() const
|
2006-04-26 13:33:51 +00:00
|
|
|
{
|
|
|
|
return str_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-26 00:43:08 +00:00
|
|
|
docstring const & TocItem::tooltip() const
|
|
|
|
{
|
2012-01-16 15:07:04 +00:00
|
|
|
return tooltip_.empty() ? str_ : tooltip_;
|
2010-04-26 00:43:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-13 16:53:49 +00:00
|
|
|
docstring const TocItem::asString() const
|
2006-04-26 13:33:51 +00:00
|
|
|
{
|
2006-10-11 19:40:50 +00:00
|
|
|
return docstring(4 * depth_, ' ') + str_;
|
2006-04-26 13:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-09 18:42:59 +00:00
|
|
|
DocIterator const & TocItem::dit() const
|
|
|
|
{
|
|
|
|
return dit_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-13 16:53:49 +00:00
|
|
|
FuncRequest TocItem::action() const
|
2006-04-26 13:33:51 +00:00
|
|
|
{
|
2008-05-16 12:48:13 +00:00
|
|
|
string const arg = convert<string>(dit_.paragraph().id())
|
|
|
|
+ ' ' + convert<string>(dit_.pos());
|
|
|
|
return FuncRequest(LFUN_PARAGRAPH_GOTO, arg);
|
2006-04-26 13:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2007-09-29 20:02:32 +00:00
|
|
|
//
|
2006-04-26 13:33:51 +00:00
|
|
|
// TocBackend implementation
|
2007-09-29 20:02:32 +00:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2006-04-26 13:33:51 +00:00
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
Toc const & TocBackend::toc(string const & type) const
|
2006-04-26 13:33:51 +00:00
|
|
|
{
|
|
|
|
// Is the type already supported?
|
|
|
|
TocList::const_iterator it = tocs_.find(type);
|
2013-03-27 18:45:16 +00:00
|
|
|
LASSERT(it != tocs_.end(), { static Toc dummy; return dummy; });
|
2006-04-26 13:33:51 +00:00
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-15 10:13:32 +00:00
|
|
|
Toc & TocBackend::toc(string const & type)
|
|
|
|
{
|
|
|
|
return tocs_[type];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-26 10:37:24 +00:00
|
|
|
bool TocBackend::updateItem(DocIterator const & dit)
|
2007-03-12 11:23:41 +00:00
|
|
|
{
|
2012-12-15 15:47:57 +00:00
|
|
|
if (dit.text()->getTocLevel(dit.pit()) == Layout::NOT_IN_TOC)
|
2008-09-26 10:37:24 +00:00
|
|
|
return false;
|
|
|
|
|
2007-06-12 12:29:19 +00:00
|
|
|
if (toc("tableofcontents").empty()) {
|
|
|
|
// FIXME: should not happen,
|
|
|
|
// a call to TocBackend::update() is missing somewhere
|
2007-11-28 22:12:03 +00:00
|
|
|
LYXERR0("TocBackend::updateItem called but the TOC is empty!");
|
2008-09-26 10:37:24 +00:00
|
|
|
return false;
|
2007-06-12 12:29:19 +00:00
|
|
|
}
|
2007-05-10 17:55:07 +00:00
|
|
|
|
2007-03-12 11:23:41 +00:00
|
|
|
BufferParams const & bufparams = buffer_->params();
|
2008-02-28 01:42:02 +00:00
|
|
|
const int min_toclevel = bufparams.documentClass().min_toclevel();
|
2007-03-12 11:23:41 +00:00
|
|
|
|
2008-05-13 08:23:44 +00:00
|
|
|
TocIterator toc_item = item("tableofcontents", dit);
|
2007-03-12 11:23:41 +00:00
|
|
|
|
|
|
|
docstring tocstring;
|
|
|
|
|
|
|
|
// For each paragraph, traverse its insets and let them add
|
|
|
|
// their toc items
|
2008-05-13 08:23:44 +00:00
|
|
|
Paragraph & par = toc_item->dit_.paragraph();
|
|
|
|
InsetList::const_iterator it = par.insetList().begin();
|
|
|
|
InsetList::const_iterator end = par.insetList().end();
|
2007-03-12 11:23:41 +00:00
|
|
|
for (; it != end; ++it) {
|
2007-04-29 13:39:47 +00:00
|
|
|
Inset & inset = *it->inset;
|
2010-06-04 22:44:58 +00:00
|
|
|
if (inset.lyxCode() == ARG_CODE) {
|
2007-03-12 11:23:41 +00:00
|
|
|
if (!tocstring.empty())
|
|
|
|
break;
|
2008-05-13 08:23:44 +00:00
|
|
|
Paragraph const & inset_par =
|
2010-06-04 22:44:58 +00:00
|
|
|
*static_cast<InsetArgument&>(inset).paragraphs().begin();
|
2008-05-13 08:23:44 +00:00
|
|
|
if (!par.labelString().empty())
|
|
|
|
tocstring = par.labelString() + ' ';
|
2010-12-20 21:55:09 +00:00
|
|
|
tocstring += inset_par.asString(AS_STR_INSETS);
|
2007-03-12 11:23:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-15 15:47:57 +00:00
|
|
|
int const toclevel = toc_item->dit_.text()->getTocLevel(toc_item->dit_.pit());
|
2008-02-24 15:44:11 +00:00
|
|
|
if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
|
2008-02-24 14:59:23 +00:00
|
|
|
&& tocstring.empty())
|
2010-12-20 21:55:09 +00:00
|
|
|
tocstring = par.asString(AS_STR_LABEL | AS_STR_INSETS);
|
2007-03-12 11:23:41 +00:00
|
|
|
|
|
|
|
const_cast<TocItem &>(*toc_item).str_ = tocstring;
|
2008-09-26 10:37:24 +00:00
|
|
|
|
2008-09-30 09:50:54 +00:00
|
|
|
buffer_->updateTocItem("tableofcontents", dit);
|
2008-09-26 10:37:24 +00:00
|
|
|
return true;
|
2006-04-26 13:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-08 21:18:26 +00:00
|
|
|
void TocBackend::update(bool output_active)
|
2006-04-26 13:33:51 +00:00
|
|
|
{
|
|
|
|
tocs_.clear();
|
2010-03-11 19:40:11 +00:00
|
|
|
if (!buffer_->isInternal()) {
|
2009-09-08 01:29:07 +00:00
|
|
|
DocIterator dit;
|
2013-03-08 21:18:26 +00:00
|
|
|
buffer_->inset().addToToc(dit, output_active);
|
2009-09-08 01:29:07 +00:00
|
|
|
}
|
2006-04-26 13:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-09 09:08:59 +00:00
|
|
|
TocIterator TocBackend::item(string const & type,
|
2008-05-13 08:23:44 +00:00
|
|
|
DocIterator const & dit) const
|
2006-04-26 13:33:51 +00:00
|
|
|
{
|
2006-11-11 00:35:14 +00:00
|
|
|
TocList::const_iterator toclist_it = tocs_.find(type);
|
2006-04-26 13:33:51 +00:00
|
|
|
// Is the type supported?
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(toclist_it != tocs_.end(), /**/);
|
2008-06-12 15:31:10 +00:00
|
|
|
return toclist_it->second.item(dit);
|
|
|
|
}
|
|
|
|
|
2006-04-26 13:33:51 +00:00
|
|
|
|
2008-06-12 15:31:10 +00:00
|
|
|
TocIterator Toc::item(DocIterator const & dit) const
|
|
|
|
{
|
|
|
|
TocIterator last = begin();
|
|
|
|
TocIterator it = end();
|
2007-01-30 13:54:28 +00:00
|
|
|
if (it == last)
|
|
|
|
return it;
|
|
|
|
|
2006-04-26 13:33:51 +00:00
|
|
|
--it;
|
|
|
|
|
2008-05-16 15:06:26 +00:00
|
|
|
DocIterator dit_text = dit;
|
|
|
|
if (dit_text.inMathed()) {
|
2008-02-11 07:47:04 +00:00
|
|
|
// We are only interested in text so remove the math CursorSlice.
|
2008-05-16 15:06:26 +00:00
|
|
|
while (dit_text.inMathed())
|
|
|
|
dit_text.pop_back();
|
2008-02-11 07:47:04 +00:00
|
|
|
}
|
2007-02-07 22:24:17 +00:00
|
|
|
|
2007-05-16 16:43:14 +00:00
|
|
|
for (; it != last; --it) {
|
2007-05-17 19:19:37 +00:00
|
|
|
// We verify that we don't compare contents of two
|
|
|
|
// different document. This happens when you
|
|
|
|
// have parent and child documents.
|
2008-05-16 15:06:26 +00:00
|
|
|
if (&it->dit_[0].inset() != &dit_text[0].inset())
|
2007-05-16 16:43:14 +00:00
|
|
|
continue;
|
2008-05-16 15:06:26 +00:00
|
|
|
if (it->dit_ <= dit_text)
|
2006-04-26 13:33:51 +00:00
|
|
|
return it;
|
2007-05-16 16:43:14 +00:00
|
|
|
}
|
2006-04-26 13:33:51 +00:00
|
|
|
|
|
|
|
// We are before the first Toc Item:
|
|
|
|
return last;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-30 12:34:36 +00:00
|
|
|
Toc::iterator Toc::item(int depth, docstring const & str)
|
2008-09-30 11:06:34 +00:00
|
|
|
{
|
|
|
|
if (empty())
|
|
|
|
return end();
|
2008-09-30 12:34:36 +00:00
|
|
|
iterator it = begin();
|
|
|
|
iterator itend = end();
|
2008-09-30 11:18:16 +00:00
|
|
|
for (; it != itend; ++it) {
|
2008-09-30 11:06:34 +00:00
|
|
|
if (it->depth() == depth && it->str() == str)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-08 19:52:18 +00:00
|
|
|
void TocBackend::writePlaintextTocList(string const & type,
|
|
|
|
odocstringstream & os, size_t max_length) const
|
2006-04-26 13:33:51 +00:00
|
|
|
{
|
|
|
|
TocList::const_iterator cit = tocs_.find(type);
|
|
|
|
if (cit != tocs_.end()) {
|
2006-11-13 16:53:49 +00:00
|
|
|
TocIterator ccit = cit->second.begin();
|
|
|
|
TocIterator end = cit->second.end();
|
2013-03-08 19:52:18 +00:00
|
|
|
for (; ccit != end; ++ccit) {
|
2007-11-28 22:12:03 +00:00
|
|
|
os << ccit->asString() << from_utf8("\n");
|
2013-03-08 19:52:18 +00:00
|
|
|
if (os.str().size() > max_length)
|
|
|
|
break;
|
|
|
|
}
|
2006-04-26 13:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|