3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
16 #include "BufferParams.h"
17 #include "DispatchResult.h"
18 #include "FuncRequest.h"
20 #include "LaTeXFeatures.h"
21 #include "MetricsInfo.h"
22 #include "OutputParams.h"
23 #include "output_xhtml.h"
24 #include "Paragraph.h"
25 #include "ParagraphParameters.h"
26 #include "TextClass.h"
27 #include "TocBackend.h"
29 #include "support/debug.h"
30 #include "support/gettext.h"
39 InsetTOC::InsetTOC(Buffer * buf, InsetCommandParams const & p)
40 : InsetCommand(buf, p, "toc")
44 ParamInfo const & InsetTOC::findInfo(string const & /* cmdName */)
46 static ParamInfo param_info_;
47 if (param_info_.empty()) {
48 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
54 docstring InsetTOC::screenLabel() const
56 if (getCmdName() == "tableofcontents")
57 return buffer().B_("Table of Contents");
58 return _("Unknown TOC type");
62 int InsetTOC::plaintext(odocstream & os, OutputParams const &) const
64 os << screenLabel() << "\n\n";
65 buffer().tocBackend().writePlaintextTocList(getCmdName(), os);
66 return PLAINTEXT_NEWLINE;
70 int InsetTOC::docbook(odocstream & os, OutputParams const &) const
72 if (getCmdName() == "tableofcontents")
78 docstring InsetTOC::xhtml(XHTMLStream &, OutputParams const & op) const
80 Layout const & lay = buffer().params().documentClass().htmlTOCLayout();
81 string const & tocclass = lay.defaultCSSClass();
82 string const tocattr = "class='tochead " + tocclass + "'";
84 // we'll use our own stream, because we are going to defer everything.
85 // that's how we deal with the fact that we're probably inside a standard
86 // paragraph, and we don't want to be.
90 Toc const & toc = buffer().tocBackend().toc("tableofcontents");
94 xs << html::StartTag("div", "class='toc'");
97 Language const * lang = buffer().params().language;
98 static string toctitle = N_("Table of Contents");
99 docstring title = lang
100 ? translateIfPossible(from_ascii(toctitle), lang->code())
101 : translateIfPossible(from_ascii(toctitle));
102 xs << html::StartTag("div", tocattr)
104 << html::EndTag("div");
107 Toc::const_iterator it = toc.begin();
108 Toc::const_iterator const en = toc.end();
110 for (; it != en; ++it) {
111 // First, we need to manage increases and decreases of depth
112 int const depth = it->depth();
114 // Ignore stuff above the tocdepth
115 if (depth > buffer().params().tocdepth)
118 if (depth > lastdepth) {
120 // open as many tags as we need to open to get to this level
121 // this includes the tag for the current level
122 for (int i = lastdepth + 1; i <= depth; ++i) {
124 attr << "class='lyxtoc-" << i << "'";
125 xs << html::StartTag("div", attr.str());
129 else if (depth < lastdepth) {
130 // close as many as we have to close to get back to this level
131 // this includes closing the last tag at this level
132 for (int i = lastdepth; i >= depth; --i)
133 xs << html::EndTag("div");
136 attr << "class='lyxtoc-" << depth << "'";
137 xs << html::StartTag("div", attr.str());
140 // no change of level, so close and open
141 xs << html::EndTag("div");
143 attr << "class='lyxtoc-" << depth << "'";
144 xs << html::StartTag("div", attr.str());
147 // Now output TOC info for this entry
148 Paragraph const & par = it->dit().innerParagraph();
149 // First the label, if there is one
150 docstring const & label = par.params().labelString();
153 // Now the content of the TOC entry, taken from the paragraph itself
154 OutputParams ours = op;
157 par.simpleLyXHTMLOnePar(buffer(), xs, ours, dummy);
159 // Now a link to that paragraph
160 string const parattr = "href='#" + par.magicLabel() + "' class='tocarrow'";
161 xs << html::StartTag("a", parattr);
163 // There ought to be a simple way to customize this.
164 // Maybe if we had an InsetLayout for TOC...
165 xs << XHTMLStream::NextRaw() << "⇘";
166 xs << html::EndTag("a");
168 for (int i = lastdepth; i > 0; --i)
169 xs << html::EndTag("div");
170 xs << html::EndTag("div");