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"
|
2006-04-26 13:33:51 +00:00
|
|
|
#include "LyXAction.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Paragraph.h"
|
2006-04-26 13:33:51 +00:00
|
|
|
#include "debug.h"
|
|
|
|
|
2007-04-25 01:24:38 +00:00
|
|
|
#include "insets/InsetOptArg.h"
|
2006-04-26 13:33:51 +00:00
|
|
|
|
|
|
|
#include "support/convert.h"
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
namespace lyx {
|
2006-10-11 19:40:50 +00:00
|
|
|
|
2006-04-26 13:33:51 +00:00
|
|
|
using std::vector;
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2006-11-13 16:53:49 +00:00
|
|
|
// TocItem implementation
|
2006-04-26 13:33:51 +00:00
|
|
|
|
2006-11-13 16:53:49 +00:00
|
|
|
TocItem::TocItem(ParConstIterator const & par_it, int d,
|
2007-05-16 16:43:14 +00:00
|
|
|
docstring const & s, bool child)
|
|
|
|
: par_it_(par_it), depth_(d), str_(s), child_(child)
|
2006-04-26 13:33:51 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
if (!uid_.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
size_t pos = s.find(" ");
|
|
|
|
if (pos == string::npos) {
|
|
|
|
// Non labelled item
|
|
|
|
uid_ = s;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
string s2 = s.substr(0, pos);
|
|
|
|
|
|
|
|
if (s2 == "Chapter" || s2 == "Part") {
|
|
|
|
size_t pos2 = s.find(" ", pos + 1);
|
|
|
|
if (pos2 == string::npos) {
|
|
|
|
// Unnumbered Chapter?? This should not happen.
|
|
|
|
uid_ = s.substr(pos + 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Chapter or Part
|
|
|
|
uid_ = s.substr(pos2 + 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Numbered Item.
|
|
|
|
uid_ = s.substr(pos + 1);
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2006-11-13 16:53:49 +00:00
|
|
|
bool const TocItem::isValid() const
|
2006-04-26 13:33:51 +00:00
|
|
|
{
|
|
|
|
return depth_ != -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-13 16:53:49 +00:00
|
|
|
int const TocItem::id() const
|
2006-04-26 13:33:51 +00:00
|
|
|
{
|
|
|
|
return par_it_->id();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-13 16:53:49 +00:00
|
|
|
int const 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_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-13 16:53:49 +00:00
|
|
|
FuncRequest TocItem::action() const
|
2006-04-26 13:33:51 +00:00
|
|
|
{
|
2006-05-05 20:23:12 +00:00
|
|
|
return FuncRequest(LFUN_PARAGRAPH_GOTO, convert<string>(id()));
|
2006-04-26 13:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// TocBackend implementation
|
|
|
|
|
2006-11-13 16:53:49 +00:00
|
|
|
Toc const & TocBackend::toc(std::string const & type) const
|
2006-04-26 13:33:51 +00:00
|
|
|
{
|
|
|
|
// Is the type already supported?
|
|
|
|
TocList::const_iterator it = tocs_.find(type);
|
|
|
|
BOOST_ASSERT(it != tocs_.end());
|
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-12 11:23:41 +00:00
|
|
|
void TocBackend::updateItem(ParConstIterator const & par_it)
|
|
|
|
{
|
2007-05-16 16:43:14 +00:00
|
|
|
// TODO should not happen,
|
|
|
|
// a call to TocBackend::update() is missing somewhere
|
|
|
|
if (toc("tableofcontents").empty())
|
2007-05-10 17:55:07 +00:00
|
|
|
return;
|
|
|
|
|
2007-03-12 11:23:41 +00:00
|
|
|
BufferParams const & bufparams = buffer_->params();
|
2007-04-29 19:53:54 +00:00
|
|
|
const int min_toclevel = bufparams.getTextClass().min_toclevel();
|
2007-03-12 11:23:41 +00:00
|
|
|
|
|
|
|
TocIterator toc_item = item("tableofcontents", par_it);
|
|
|
|
|
|
|
|
docstring tocstring;
|
|
|
|
|
|
|
|
// For each paragraph, traverse its insets and let them add
|
|
|
|
// their toc items
|
|
|
|
InsetList::const_iterator it = toc_item->par_it_->insetlist.begin();
|
|
|
|
InsetList::const_iterator end = toc_item->par_it_->insetlist.end();
|
|
|
|
for (; it != end; ++it) {
|
2007-04-29 13:39:47 +00:00
|
|
|
Inset & inset = *it->inset;
|
|
|
|
if (inset.lyxCode() == Inset::OPTARG_CODE) {
|
2007-03-12 11:23:41 +00:00
|
|
|
if (!tocstring.empty())
|
|
|
|
break;
|
|
|
|
Paragraph const & par =
|
|
|
|
*static_cast<InsetOptArg&>(inset).paragraphs().begin();
|
|
|
|
if (!toc_item->par_it_->getLabelstring().empty())
|
|
|
|
tocstring = toc_item->par_it_->getLabelstring() + ' ';
|
|
|
|
tocstring += par.asString(*buffer_, false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int const toclevel = toc_item->par_it_->layout()->toclevel;
|
2007-04-29 18:58:28 +00:00
|
|
|
if (toclevel != Layout::NOT_IN_TOC
|
2007-03-12 11:23:41 +00:00
|
|
|
&& toclevel >= min_toclevel
|
|
|
|
&& tocstring.empty())
|
|
|
|
tocstring = toc_item->par_it_->asString(*buffer_, true);
|
|
|
|
|
|
|
|
const_cast<TocItem &>(*toc_item).str_ = tocstring;
|
2006-04-26 13:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TocBackend::update()
|
|
|
|
{
|
|
|
|
tocs_.clear();
|
|
|
|
|
|
|
|
BufferParams const & bufparams = buffer_->params();
|
2007-04-29 19:53:54 +00:00
|
|
|
const int min_toclevel = bufparams.getTextClass().min_toclevel();
|
2006-04-26 13:33:51 +00:00
|
|
|
|
2007-05-16 16:43:14 +00:00
|
|
|
// Is this a child document?
|
|
|
|
bool const child_document = buffer_->getMasterBuffer() != buffer_;
|
|
|
|
|
2007-01-09 15:48:04 +00:00
|
|
|
Toc & toc = tocs_["tableofcontents"];
|
2006-04-26 13:33:51 +00:00
|
|
|
ParConstIterator pit = buffer_->par_iterator_begin();
|
|
|
|
ParConstIterator end = buffer_->par_iterator_end();
|
|
|
|
for (; pit != end; ++pit) {
|
|
|
|
|
|
|
|
// the string that goes to the toc (could be the optarg)
|
2006-10-11 19:40:50 +00:00
|
|
|
docstring tocstring;
|
2006-04-26 13:33:51 +00:00
|
|
|
|
2006-11-13 16:53:49 +00:00
|
|
|
// For each paragraph, traverse its insets and let them add
|
|
|
|
// their toc items
|
2006-04-26 13:33:51 +00:00
|
|
|
InsetList::const_iterator it = pit->insetlist.begin();
|
|
|
|
InsetList::const_iterator end = pit->insetlist.end();
|
|
|
|
for (; it != end; ++it) {
|
2007-04-29 13:39:47 +00:00
|
|
|
Inset & inset = *it->inset;
|
2007-01-09 15:48:04 +00:00
|
|
|
inset.addToToc(tocs_, *buffer_);
|
|
|
|
switch (inset.lyxCode()) {
|
2007-04-29 13:39:47 +00:00
|
|
|
case Inset::OPTARG_CODE: {
|
2006-04-26 13:33:51 +00:00
|
|
|
if (!tocstring.empty())
|
|
|
|
break;
|
2007-01-09 15:48:04 +00:00
|
|
|
Paragraph const & par =
|
|
|
|
*static_cast<InsetOptArg&>(inset).paragraphs().begin();
|
2006-04-26 13:33:51 +00:00
|
|
|
if (!pit->getLabelstring().empty())
|
2006-10-20 19:26:23 +00:00
|
|
|
tocstring = pit->getLabelstring() + ' ';
|
2006-04-26 13:33:51 +00:00
|
|
|
tocstring += par.asString(*buffer_, false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// now the toc entry for the paragraph
|
|
|
|
int const toclevel = pit->layout()->toclevel;
|
2007-04-29 18:58:28 +00:00
|
|
|
if (toclevel != Layout::NOT_IN_TOC
|
2007-05-15 15:09:19 +00:00
|
|
|
&& toclevel >= min_toclevel) {
|
2006-04-26 13:33:51 +00:00
|
|
|
// insert this into the table of contents
|
|
|
|
if (tocstring.empty())
|
|
|
|
tocstring = pit->asString(*buffer_, true);
|
2007-05-16 16:43:14 +00:00
|
|
|
toc.push_back(TocItem(pit, toclevel - min_toclevel,
|
|
|
|
tocstring, child_document));
|
2006-04-26 13:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-13 16:53:49 +00:00
|
|
|
TocIterator const TocBackend::item(
|
2006-11-11 00:35:14 +00:00
|
|
|
std::string const & type, ParConstIterator const & par_it) 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?
|
|
|
|
BOOST_ASSERT(toclist_it != tocs_.end());
|
|
|
|
|
|
|
|
Toc const & toc_vector = toclist_it->second;
|
2006-11-13 16:53:49 +00:00
|
|
|
TocIterator last = toc_vector.begin();
|
|
|
|
TocIterator it = toc_vector.end();
|
2007-01-30 13:54:28 +00:00
|
|
|
if (it == last)
|
|
|
|
return it;
|
|
|
|
|
2006-04-26 13:33:51 +00:00
|
|
|
--it;
|
|
|
|
|
2007-02-07 22:24:17 +00:00
|
|
|
ParConstIterator par_it_text = par_it;
|
|
|
|
if (par_it_text.inMathed())
|
|
|
|
// It would be better to do
|
|
|
|
// par_it_text.backwardInset();
|
|
|
|
// but this method does not exist.
|
|
|
|
while (par_it_text.inMathed())
|
|
|
|
par_it_text.backwardPos();
|
|
|
|
|
2007-05-16 16:43:14 +00:00
|
|
|
for (; it != last; --it) {
|
|
|
|
if (it->child_)
|
|
|
|
continue;
|
2007-05-15 17:07:55 +00:00
|
|
|
if (it->par_it_ <= par_it_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-18 22:29:50 +00:00
|
|
|
void TocBackend::writePlaintextTocList(string const & type, odocstream & os) 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();
|
2006-04-26 13:33:51 +00:00
|
|
|
for (; ccit != end; ++ccit)
|
|
|
|
os << ccit->asString() << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|