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 "BufferView.h"
19 #include "DispatchResult.h"
21 #include "FuncRequest.h"
23 #include "LaTeXFeatures.h"
24 #include "OutputParams.h"
25 #include "output_xhtml.h"
26 #include "Paragraph.h"
27 #include "ParagraphParameters.h"
28 #include "TextClass.h"
29 #include "TocBackend.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
34 #include "support/lassert.h"
43 string cmd2type(string const & cmd)
45 if (cmd == "lstlistoflistings")
52 InsetTOC::InsetTOC(Buffer * buf, InsetCommandParams const & p)
53 : InsetCommand(buf, p)
57 ParamInfo const & InsetTOC::findInfo(string const & /* cmdName */)
59 static ParamInfo param_info_;
60 if (param_info_.empty()) {
61 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
67 bool InsetTOC::isCompatibleCommand(string const & cmd)
69 return cmd == defaultCommand() || cmd == "lstlistoflistings";
73 docstring InsetTOC::screenLabel() const
75 if (getCmdName() == "tableofcontents")
76 return buffer().B_("Table of Contents");
77 if (getCmdName() == "lstlistoflistings")
78 return buffer().B_("List of Listings");
79 return _("Unknown TOC type");
83 void InsetTOC::doDispatch(Cursor & cur, FuncRequest & cmd) {
84 switch (cmd.action()) {
85 case LFUN_MOUSE_RELEASE:
86 if (!cur.selection() && cmd.button() == mouse_button::button1) {
87 cur.bv().showDialog("toc", params2string(params()));
93 InsetCommand::doDispatch(cur, cmd);
98 docstring InsetTOC::layoutName() const
100 if (getCmdName() == "lstlistoflistings") {
101 if (buffer().params().use_minted)
102 return from_ascii("TOC:MintedListings");
104 return from_ascii("TOC:Listings");
106 return from_ascii("TOC");
110 void InsetTOC::validate(LaTeXFeatures & features) const
112 InsetCommand::validate(features);
113 features.useInsetLayout(getLayout());
114 if (getCmdName() == "lstlistoflistings") {
115 if (buffer().params().use_minted)
116 features.require("minted");
118 features.require("listings");
123 int InsetTOC::plaintext(odocstringstream & os,
124 OutputParams const &, size_t max_length) const
126 os << screenLabel() << "\n\n";
127 buffer().tocBackend().writePlaintextTocList(cmd2type(getCmdName()), os, max_length);
128 return PLAINTEXT_NEWLINE;
132 void InsetTOC::docbook(XMLStream &, OutputParams const &) const
134 // TOC are generated automatically by the DocBook processor.
139 void InsetTOC::makeTOCEntry(XMLStream & xs,
140 Paragraph const & par, OutputParams const & op) const
142 string const attr = "href='#" + par.magicLabel() + "' class='tocentry'";
143 xs << xml::StartTag("a", attr);
145 // First the label, if there is one
146 docstring const & label = par.params().labelString();
149 // Now the content of the TOC entry, taken from the paragraph itself
150 OutputParams ours = op;
153 par.simpleLyXHTMLOnePar(buffer(), xs, ours, dummy);
155 xs << xml::EndTag("a") << xml::CR();
159 void InsetTOC::makeTOCWithDepth(XMLStream & xs,
160 Toc const & toc, OutputParams const & op) const
162 Toc::const_iterator it = toc.begin();
163 Toc::const_iterator const en = toc.end();
165 for (; it != en; ++it) {
166 // do not output entries that are not actually included in the output,
167 // e.g., stuff in non-active branches or notes or whatever.
171 // First, we need to manage increases and decreases of depth
172 // If there's no depth to deal with, we artificially set it to 1.
173 int const depth = it->depth();
175 // Ignore stuff above the tocdepth
176 if (depth > buffer().params().tocdepth)
179 if (depth > lastdepth) {
181 // open as many tags as we need to open to get to this level
182 // this includes the tag for the current level
183 for (int i = lastdepth + 1; i <= depth; ++i) {
185 attr << "class='lyxtoc-" << i << "'";
186 xs << xml::StartTag("div", attr.str()) << xml::CR();
190 else if (depth < lastdepth) {
191 // close as many as we have to close to get back to this level
192 // this includes closing the last tag at this level
193 for (int i = lastdepth; i >= depth; --i)
194 xs << xml::EndTag("div") << xml::CR();
197 attr << "class='lyxtoc-" << depth << "'";
198 xs << xml::StartTag("div", attr.str()) << xml::CR();
201 // no change of level, so close and open
202 xs << xml::EndTag("div") << xml::CR();
204 attr << "class='lyxtoc-" << depth << "'";
205 xs << xml::StartTag("div", attr.str()) << xml::CR();
208 // Now output TOC info for this entry
209 Paragraph const & par = it->dit().innerParagraph();
210 makeTOCEntry(xs, par, op);
212 for (int i = lastdepth; i > 0; --i)
213 xs << xml::EndTag("div") << xml::CR();
217 void InsetTOC::makeTOCNoDepth(XMLStream & xs,
218 Toc const & toc, const OutputParams & op) const
220 Toc::const_iterator it = toc.begin();
221 Toc::const_iterator const en = toc.end();
222 for (; it != en; ++it) {
223 // do not output entries that are not actually included in the output,
224 // e.g., stuff in non-active branches or notes or whatever.
228 xs << xml::StartTag("div", "class='lyxtoc-flat'") << xml::CR();
230 Paragraph const & par = it->dit().innerParagraph();
231 makeTOCEntry(xs, par, op);
233 xs << xml::EndTag("div");
238 docstring InsetTOC::xhtml(XMLStream &, OutputParams const & op) const
240 string const & command = getCmdName();
241 if (command != "tableofcontents" && command != "lstlistoflistings") {
242 LYXERR0("TOC type " << command << " not yet implemented.");
243 LASSERT(false, return docstring());
246 shared_ptr<Toc const> toc =
247 buffer().masterBuffer()->tocBackend().toc(cmd2type(command));
251 // we'll use our own stream, because we are going to defer everything.
252 // that's how we deal with the fact that we're probably inside a standard
253 // paragraph, and we don't want to be.
254 odocstringstream ods;
257 xs << xml::StartTag("div", "class='toc'");
260 InsetLayout const & il = getLayout();
261 string const & tag = il.htmltag();
262 docstring title = screenLabel();
263 Layout const & lay = buffer().params().documentClass().htmlTOCLayout();
264 string const & tocclass = lay.defaultCSSClass();
265 string const tocattr = "class='tochead " + tocclass + "'";
266 xs << xml::StartTag(tag, tocattr)
270 // with lists of listings, at least, there is no depth
271 // to worry about. so the code can be simpler.
272 bool const use_depth = (command == "tableofcontents");
276 makeTOCWithDepth(xs, *toc, op);
278 makeTOCNoDepth(xs, *toc, op);
280 xs << xml::EndTag("div") << xml::CR();