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
|
2015-09-01 16:08:35 +00:00
|
|
|
* \author Guillaume Munch
|
2006-04-26 13:33:51 +00:00
|
|
|
*
|
|
|
|
* 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"
|
2007-10-18 15:29:51 +00:00
|
|
|
#include "InsetList.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Paragraph.h"
|
2007-11-07 23:25:08 +00:00
|
|
|
#include "TextClass.h"
|
2006-04-26 13:33:51 +00:00
|
|
|
|
2017-01-08 18:17:57 +00:00
|
|
|
#include "insets/InsetText.h"
|
2006-04-26 13:33:51 +00:00
|
|
|
|
2007-11-29 17:51:58 +00:00
|
|
|
#include "support/debug.h"
|
|
|
|
#include "support/docstream.h"
|
2015-11-03 16:47:25 +00:00
|
|
|
#include "support/gettext.h"
|
2008-04-30 08:26:40 +00:00
|
|
|
#include "support/lassert.h"
|
2015-10-04 18:38:47 +00:00
|
|
|
#include "support/lstrings.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
|
|
|
|
2015-09-27 06:05:00 +00:00
|
|
|
|
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,
|
2016-06-06 19:02:49 +00:00
|
|
|
bool output_active, FuncRequest action)
|
|
|
|
: dit_(dit), depth_(d), str_(s), output_(output_active),
|
2016-06-02 20:40:11 +00:00
|
|
|
action_(action)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-01 16:08:35 +00:00
|
|
|
docstring const TocItem::asString() const
|
2006-04-26 13:33:51 +00:00
|
|
|
{
|
2015-12-02 21:56:10 +00:00
|
|
|
static char_type const cross = 0x2716; // ✖ U+2716 HEAVY MULTIPLICATION X
|
|
|
|
static char_type const thin = 0x2009; // U+2009 THIN SPACE
|
2015-09-27 06:05:00 +00:00
|
|
|
docstring prefix;
|
|
|
|
if (!output_) {
|
|
|
|
prefix += cross;
|
2015-12-02 21:56:10 +00:00
|
|
|
prefix += thin;
|
2015-09-27 06:05:00 +00:00
|
|
|
}
|
|
|
|
return prefix + str_;
|
2006-04-26 13:33:51 +00:00
|
|
|
}
|
|
|
|
|
2015-10-04 18:48:31 +00:00
|
|
|
|
2015-09-01 16:08:35 +00:00
|
|
|
FuncRequest TocItem::action() const
|
2010-04-26 00:43:08 +00:00
|
|
|
{
|
2015-09-27 06:05:00 +00:00
|
|
|
if (action_.action() == LFUN_UNKNOWN_ACTION) {
|
2017-01-09 22:15:16 +00:00
|
|
|
return FuncRequest(LFUN_PARAGRAPH_GOTO, dit_.paragraphGotoArgument());
|
2015-09-27 06:05:00 +00:00
|
|
|
} else
|
|
|
|
return action_;
|
2010-04-26 00:43:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-01 16:08:35 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2017-01-09 22:15:16 +00:00
|
|
|
// TocBackend implementation
|
2015-09-01 16:08:35 +00:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-01-08 19:06:50 +00:00
|
|
|
Toc::const_iterator TocBackend::findItem(Toc const & toc,
|
|
|
|
DocIterator const & dit)
|
2006-04-26 13:33:51 +00:00
|
|
|
{
|
2016-01-08 19:06:50 +00:00
|
|
|
Toc::const_iterator last = toc.begin();
|
|
|
|
Toc::const_iterator it = toc.end();
|
2015-09-01 16:08:35 +00:00
|
|
|
if (it == last)
|
|
|
|
return it;
|
|
|
|
--it;
|
2016-01-08 19:06:50 +00:00
|
|
|
DocIterator dit_text = dit.getInnerText();
|
2015-09-01 16:08:35 +00:00
|
|
|
|
|
|
|
for (; it != last; --it) {
|
|
|
|
// We verify that we don't compare contents of two
|
|
|
|
// different document. This happens when you
|
|
|
|
// have parent and child documents.
|
2017-01-09 22:15:16 +00:00
|
|
|
if (&it->dit()[0].inset() != &dit_text[0].inset())
|
2015-09-01 16:08:35 +00:00
|
|
|
continue;
|
2017-01-09 22:15:16 +00:00
|
|
|
if (it->dit() <= dit_text)
|
2015-09-01 16:08:35 +00:00
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are before the first Toc Item:
|
|
|
|
return last;
|
2006-04-26 13:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-08 19:06:50 +00:00
|
|
|
Toc::iterator TocBackend::findItem(Toc & toc, int depth, docstring const & str)
|
2009-04-09 18:42:59 +00:00
|
|
|
{
|
2016-01-08 19:06:50 +00:00
|
|
|
if (toc.empty())
|
|
|
|
return toc.end();
|
|
|
|
Toc::iterator it = toc.begin();
|
|
|
|
Toc::iterator itend = toc.end();
|
2015-09-01 16:08:35 +00:00
|
|
|
for (; it != itend; ++it) {
|
|
|
|
if (it->depth() == depth && it->str() == str)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return it;
|
2009-04-09 18:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-01 16:08:35 +00:00
|
|
|
shared_ptr<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);
|
2016-06-02 17:13:55 +00:00
|
|
|
LASSERT(it != tocs_.end(), { return make_shared<Toc>(); });
|
2015-09-01 16:08:35 +00:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2006-04-26 13:33:51 +00:00
|
|
|
|
2015-09-01 16:08:35 +00:00
|
|
|
shared_ptr<Toc> TocBackend::toc(string const & type)
|
|
|
|
{
|
2016-06-02 20:40:11 +00:00
|
|
|
// std::map::insert only really performs the insertion if the key is not
|
|
|
|
// already bound, and otherwise returns an iterator to the element already
|
|
|
|
// there, see manual.
|
|
|
|
return tocs_.insert({type, make_shared<Toc>()}).first->second;
|
2006-04-26 13:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-02 20:40:11 +00:00
|
|
|
TocBuilder & TocBackend::builder(string const & type)
|
2008-02-15 10:13:32 +00:00
|
|
|
{
|
2016-06-02 20:40:11 +00:00
|
|
|
auto p = make_unique<TocBuilder>(toc(type));
|
|
|
|
return * builders_.insert(make_pair(type, move(p))).first->second;
|
2008-02-15 10:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-04 18:38:47 +00:00
|
|
|
// FIXME: This function duplicates functionality from InsetText::iterateForToc.
|
|
|
|
// Both have their own way of computing the TocItem for "tableofcontents". The
|
|
|
|
// TocItem creation and update should be made in a dedicated function and
|
|
|
|
// updateItem should be rewritten to uniformly update the matching items from
|
|
|
|
// all TOCs.
|
2015-11-30 22:25:03 +00:00
|
|
|
bool TocBackend::updateItem(DocIterator const & dit_in)
|
2007-03-12 11:23:41 +00:00
|
|
|
{
|
2015-11-30 22:25:03 +00:00
|
|
|
// we need a text
|
|
|
|
DocIterator dit = dit_in.getInnerText();
|
|
|
|
|
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;
|
|
|
|
|
2015-09-01 16:08:35 +00:00
|
|
|
if (toc("tableofcontents")->empty()) {
|
2007-06-12 12:29:19 +00:00
|
|
|
// 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
|
|
|
|
2016-01-08 19:06:50 +00:00
|
|
|
Toc::const_iterator 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
|
2015-10-04 18:38:47 +00:00
|
|
|
//
|
|
|
|
// FIXME: This is supposed to accomplish the same as the body of
|
|
|
|
// InsetText::iterateForToc(), probably
|
2017-01-09 22:15:16 +00:00
|
|
|
Paragraph & par = toc_item->dit().paragraph();
|
2008-05-13 08:23:44 +00:00
|
|
|
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) {
|
2015-10-04 18:38:47 +00:00
|
|
|
tocstring = par.labelString();
|
2007-03-12 11:23:41 +00:00
|
|
|
if (!tocstring.empty())
|
2015-10-04 18:38:47 +00:00
|
|
|
tocstring += ' ';
|
|
|
|
inset.asInsetText()->text().forOutliner(tocstring,TOC_ENTRY_LENGTH);
|
2007-03-12 11:23:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 22:15:16 +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())
|
2015-10-04 18:38:47 +00:00
|
|
|
par.forOutliner(tocstring, TOC_ENTRY_LENGTH);
|
2007-03-12 11:23:41 +00:00
|
|
|
|
2015-10-04 18:38:47 +00:00
|
|
|
support::truncateWithEllipsis(tocstring, TOC_ENTRY_LENGTH);
|
2015-09-27 06:05:00 +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
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-27 06:05:00 +00:00
|
|
|
void TocBackend::update(bool output_active, UpdateType utype)
|
2006-04-26 13:33:51 +00:00
|
|
|
{
|
2015-09-01 16:08:35 +00:00
|
|
|
for (TocList::iterator it = tocs_.begin(); it != tocs_.end(); ++it)
|
|
|
|
it->second->clear();
|
2006-04-26 13:33:51 +00:00
|
|
|
tocs_.clear();
|
2015-09-01 16:08:35 +00:00
|
|
|
builders_.clear();
|
2010-03-11 19:40:11 +00:00
|
|
|
if (!buffer_->isInternal()) {
|
2009-09-08 01:29:07 +00:00
|
|
|
DocIterator dit;
|
2015-09-27 06:05:00 +00:00
|
|
|
buffer_->inset().addToToc(dit, output_active, utype);
|
2009-09-08 01:29:07 +00:00
|
|
|
}
|
2006-04-26 13:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-08 19:06:50 +00:00
|
|
|
Toc::const_iterator TocBackend::item(string const & type,
|
|
|
|
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?
|
2013-04-25 21:27:10 +00:00
|
|
|
// We will try to make the best of it in release mode
|
|
|
|
LASSERT(toclist_it != tocs_.end(), toclist_it = tocs_.begin());
|
2016-01-08 19:06:50 +00:00
|
|
|
return findItem(*toclist_it->second, dit);
|
2008-09-30 11:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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()) {
|
2016-01-08 19:06:50 +00:00
|
|
|
Toc::const_iterator ccit = cit->second->begin();
|
|
|
|
Toc::const_iterator 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-02 17:13:55 +00:00
|
|
|
docstring TocBackend::outlinerName(string const & type) const
|
2015-11-03 16:47:25 +00:00
|
|
|
{
|
|
|
|
return translateIfPossible(
|
|
|
|
buffer_->params().documentClass().outlinerName(type));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-30 15:27:43 +00:00
|
|
|
bool TocBackend::isOther(std::string const & type)
|
|
|
|
{
|
|
|
|
return type == "graphics"
|
|
|
|
|| type == "note"
|
|
|
|
|| type == "branch"
|
|
|
|
|| type == "change"
|
|
|
|
|| type == "label"
|
|
|
|
|| type == "citation"
|
|
|
|
|| type == "equation"
|
|
|
|
|| type == "footnote"
|
|
|
|
|| type == "marginalnote"
|
|
|
|
|| type == "nomencl"
|
|
|
|
|| type == "listings"
|
|
|
|
|| type == "math-macro"
|
|
|
|
|| type == "external"
|
|
|
|
|| type == "senseless"
|
|
|
|
|| type == "index"
|
|
|
|
|| type.substr(0,6) == "index:";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-26 13:33:51 +00:00
|
|
|
} // namespace lyx
|