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/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 return from_ascii("TOC:Listings");
101 return from_ascii("TOC");
105 void InsetTOC::validate(LaTeXFeatures & features) const
107 InsetCommand::validate(features);
108 features.useInsetLayout(getLayout());
109 if (getCmdName() == "lstlistoflistings") {
110 if (buffer().params().use_minted)
111 features.require("minted");
113 features.require("listings");
118 int InsetTOC::plaintext(odocstringstream & os,
119 OutputParams const &, size_t max_length) const
121 os << screenLabel() << "\n\n";
122 buffer().tocBackend().writePlaintextTocList(cmd2type(getCmdName()), os, max_length);
123 return PLAINTEXT_NEWLINE;
127 int InsetTOC::docbook(odocstream & os, OutputParams const &) const
129 if (getCmdName() == "tableofcontents")
135 void InsetTOC::makeTOCEntry(XHTMLStream & xs,
136 Paragraph const & par, OutputParams const & op) const
138 string const attr = "href='#" + par.magicLabel() + "' class='tocentry'";
139 xs << html::StartTag("a", attr);
141 // First the label, if there is one
142 docstring const & label = par.params().labelString();
145 // Now the content of the TOC entry, taken from the paragraph itself
146 OutputParams ours = op;
149 par.simpleLyXHTMLOnePar(buffer(), xs, ours, dummy);
151 xs << html::EndTag("a") << html::CR();
155 void InsetTOC::makeTOCWithDepth(XHTMLStream & xs,
156 Toc const & toc, OutputParams const & op) const
158 Toc::const_iterator it = toc.begin();
159 Toc::const_iterator const en = toc.end();
161 for (; it != en; ++it) {
162 // do not output entries that are not actually included in the output,
163 // e.g., stuff in non-active branches or notes or whatever.
167 // First, we need to manage increases and decreases of depth
168 // If there's no depth to deal with, we artifically set it to 1.
169 int const depth = it->depth();
171 // Ignore stuff above the tocdepth
172 if (depth > buffer().params().tocdepth)
175 if (depth > lastdepth) {
177 // open as many tags as we need to open to get to this level
178 // this includes the tag for the current level
179 for (int i = lastdepth + 1; i <= depth; ++i) {
181 attr << "class='lyxtoc-" << i << "'";
182 xs << html::StartTag("div", attr.str()) << html::CR();
186 else if (depth < lastdepth) {
187 // close as many as we have to close to get back to this level
188 // this includes closing the last tag at this level
189 for (int i = lastdepth; i >= depth; --i)
190 xs << html::EndTag("div") << html::CR();
193 attr << "class='lyxtoc-" << depth << "'";
194 xs << html::StartTag("div", attr.str()) << html::CR();
197 // no change of level, so close and open
198 xs << html::EndTag("div") << html::CR();
200 attr << "class='lyxtoc-" << depth << "'";
201 xs << html::StartTag("div", attr.str()) << html::CR();
204 // Now output TOC info for this entry
205 Paragraph const & par = it->dit().innerParagraph();
206 makeTOCEntry(xs, par, op);
208 for (int i = lastdepth; i > 0; --i)
209 xs << html::EndTag("div") << html::CR();
213 void InsetTOC::makeTOCNoDepth(XHTMLStream & xs,
214 Toc const & toc, const OutputParams & op) const
216 Toc::const_iterator it = toc.begin();
217 Toc::const_iterator const en = toc.end();
218 for (; it != en; ++it) {
219 // do not output entries that are not actually included in the output,
220 // e.g., stuff in non-active branches or notes or whatever.
224 xs << html::StartTag("div", "class='lyxtoc-flat'") << html::CR();
226 Paragraph const & par = it->dit().innerParagraph();
227 makeTOCEntry(xs, par, op);
229 xs << html::EndTag("div");
234 docstring InsetTOC::xhtml(XHTMLStream &, OutputParams const & op) const
236 string const & command = getCmdName();
237 if (command != "tableofcontents" && command != "lstlistoflistings") {
238 LYXERR0("TOC type " << command << " not yet implemented.");
239 LASSERT(false, return docstring());
242 shared_ptr<Toc const> toc =
243 buffer().masterBuffer()->tocBackend().toc(cmd2type(command));
247 // we'll use our own stream, because we are going to defer everything.
248 // that's how we deal with the fact that we're probably inside a standard
249 // paragraph, and we don't want to be.
250 odocstringstream ods;
253 xs << html::StartTag("div", "class='toc'");
256 InsetLayout const & il = getLayout();
257 string const & tag = il.htmltag();
258 docstring title = screenLabel();
259 Layout const & lay = buffer().params().documentClass().htmlTOCLayout();
260 string const & tocclass = lay.defaultCSSClass();
261 string const tocattr = "class='tochead " + tocclass + "'";
262 xs << html::StartTag(tag, tocattr)
264 << html::EndTag(tag);
266 // with lists of listings, at least, there is no depth
267 // to worry about. so the code can be simpler.
268 bool const use_depth = (command == "tableofcontents");
272 makeTOCWithDepth(xs, *toc, op);
274 makeTOCNoDepth(xs, *toc, op);
276 xs << html::EndTag("div") << html::CR();