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 "output_xhtml.h"
25 #include "Paragraph.h"
26 #include "ParagraphParameters.h"
27 #include "TextClass.h"
28 #include "TocBackend.h"
30 #include "support/debug.h"
31 #include "support/docstream.h"
32 #include "support/gettext.h"
33 #include "support/lassert.h"
42 string cmd2type(string const & cmd)
44 if (cmd == "lstlistoflistings")
51 InsetTOC::InsetTOC(Buffer * buf, InsetCommandParams const & p)
52 : InsetCommand(buf, p)
56 ParamInfo const & InsetTOC::findInfo(string const & /* cmdName */)
58 static ParamInfo param_info_;
59 if (param_info_.empty()) {
60 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
66 bool InsetTOC::isCompatibleCommand(string const & cmd)
68 return cmd == defaultCommand() || cmd == "lstlistoflistings";
72 docstring InsetTOC::screenLabel() const
74 if (getCmdName() == "tableofcontents")
75 return buffer().B_("Table of Contents");
76 if (getCmdName() == "lstlistoflistings")
77 return buffer().B_("List of Listings");
78 return _("Unknown TOC type");
82 void InsetTOC::doDispatch(Cursor & cur, FuncRequest & cmd) {
83 switch (cmd.action()) {
84 case LFUN_MOUSE_RELEASE:
85 if (!cur.selection() && cmd.button() == mouse_button::button1) {
86 cur.bv().showDialog("toc", params2string(params()));
92 InsetCommand::doDispatch(cur, cmd);
97 docstring InsetTOC::layoutName() const
99 if (getCmdName() == "lstlistoflistings") {
100 if (buffer().params().use_minted)
101 return from_ascii("TOC:MintedListings");
103 return from_ascii("TOC:Listings");
105 return from_ascii("TOC");
109 void InsetTOC::validate(LaTeXFeatures & features) const
111 InsetCommand::validate(features);
112 features.useInsetLayout(getLayout());
113 if (getCmdName() == "lstlistoflistings") {
114 if (buffer().params().use_minted)
115 features.require("minted");
117 features.require("listings");
122 int InsetTOC::plaintext(odocstringstream & os,
123 OutputParams const &, size_t max_length) const
125 os << screenLabel() << "\n\n";
126 buffer().tocBackend().writePlaintextTocList(cmd2type(getCmdName()), os, max_length);
127 return PLAINTEXT_NEWLINE;
131 void InsetTOC::docbook(XMLStream &, OutputParams const &) const
133 // TOC are generated automatically by the DocBook processor.
138 void InsetTOC::makeTOCEntry(XMLStream & xs,
139 Paragraph const & par, OutputParams const & op) const
141 string const attr = "href='#" + par.magicLabel() + "' class='tocentry'";
142 xs << xml::StartTag("a", attr);
144 // First the label, if there is one
145 docstring const & label = par.params().labelString();
148 // Now the content of the TOC entry, taken from the paragraph itself
149 OutputParams ours = op;
152 par.simpleLyXHTMLOnePar(buffer(), xs, ours, dummy);
154 xs << xml::EndTag("a") << xml::CR();
158 void InsetTOC::makeTOCWithDepth(XMLStream & xs,
159 Toc const & toc, OutputParams const & op) const
162 for (auto const & tocitem : toc) {
163 // do not output entries that are not actually included in the output,
164 // e.g., stuff in non-active branches or notes or whatever.
165 if (!tocitem.isOutput())
168 // First, we need to manage increases and decreases of depth
169 // If there's no depth to deal with, we artificially set it to 1.
170 int const depth = tocitem.depth();
172 // Ignore stuff above the tocdepth
173 if (depth > buffer().params().tocdepth)
176 if (depth > lastdepth) {
178 // open as many tags as we need to open to get to this level
179 // this includes the tag for the current level
180 for (int i = lastdepth + 1; i <= depth; ++i) {
182 attr << "class='lyxtoc-" << i << "'";
183 xs << xml::StartTag("div", attr.str()) << xml::CR();
187 else if (depth < lastdepth) {
188 // close as many as we have to close to get back to this level
189 // this includes closing the last tag at this level
190 for (int i = lastdepth; i >= depth; --i)
191 xs << xml::EndTag("div") << xml::CR();
194 attr << "class='lyxtoc-" << depth << "'";
195 xs << xml::StartTag("div", attr.str()) << xml::CR();
198 // no change of level, so close and open
199 xs << xml::EndTag("div") << xml::CR();
201 attr << "class='lyxtoc-" << depth << "'";
202 xs << xml::StartTag("div", attr.str()) << xml::CR();
205 // Now output TOC info for this entry
206 Paragraph const & par = tocitem.dit().innerParagraph();
207 makeTOCEntry(xs, par, op);
209 for (int i = lastdepth; i > 0; --i)
210 xs << xml::EndTag("div") << xml::CR();
214 void InsetTOC::makeTOCNoDepth(XMLStream & xs,
215 Toc const & toc, const OutputParams & op) const
217 for (auto const & tocitem : toc) {
218 // do not output entries that are not actually included in the output,
219 // e.g., stuff in non-active branches or notes or whatever.
220 if (!tocitem.isOutput())
223 xs << xml::StartTag("div", "class='lyxtoc-flat'") << xml::CR();
225 Paragraph const & par = tocitem.dit().innerParagraph();
226 makeTOCEntry(xs, par, op);
228 xs << xml::EndTag("div");
233 docstring InsetTOC::xhtml(XMLStream &, OutputParams const & op) const
235 string const & command = getCmdName();
236 if (command != "tableofcontents" && command != "lstlistoflistings") {
237 LYXERR0("TOC type " << command << " not yet implemented.");
238 LASSERT(false, return docstring());
241 shared_ptr<Toc const> toc =
242 buffer().masterBuffer()->tocBackend().toc(cmd2type(command));
246 // we'll use our own stream, because we are going to defer everything.
247 // that's how we deal with the fact that we're probably inside a standard
248 // paragraph, and we don't want to be.
249 odocstringstream ods;
252 xs << xml::StartTag("div", "class='toc'");
255 InsetLayout const & il = getLayout();
256 string const & tag = il.htmltag();
257 docstring title = screenLabel();
258 Layout const & lay = buffer().params().documentClass().htmlTOCLayout();
259 string const & tocclass = lay.defaultCSSClass();
260 string const tocattr = "class='tochead " + tocclass + "'";
261 xs << xml::StartTag(tag, tocattr)
265 // with lists of listings, at least, there is no depth
266 // to worry about. so the code can be simpler.
267 bool const use_depth = (command == "tableofcontents");
271 makeTOCWithDepth(xs, *toc, op);
273 makeTOCNoDepth(xs, *toc, op);
275 xs << xml::EndTag("div") << xml::CR();