]> git.lyx.org Git - features.git/blob - src/insets/InsetTOC.cpp
Fix bug #8608: Don't output index entries from notes, etc.
[features.git] / src / insets / InsetTOC.cpp
1 /**
2  * \file InsetTOC.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetTOC.h"
14
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
18 #include "Cursor.h"
19 #include "DispatchResult.h"
20 #include "Font.h"
21 #include "FuncRequest.h"
22 #include "Language.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"
30
31 #include "support/debug.h"
32 #include "support/gettext.h"
33
34 #include <ostream>
35
36 using namespace std;
37
38 namespace lyx {
39
40 namespace {
41 string cmd2type(string const & cmd)
42 {
43         if (cmd == "lstlistoflistings")
44                 return "listing";
45         return cmd;
46 }
47 }
48
49
50 InsetTOC::InsetTOC(Buffer * buf, InsetCommandParams const & p)
51         : InsetCommand(buf, p)
52 {}
53
54
55 ParamInfo const & InsetTOC::findInfo(string const & /* cmdName */)
56 {
57         static ParamInfo param_info_;
58         if (param_info_.empty()) {
59                 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
60         }
61         return param_info_;
62 }
63
64
65 bool InsetTOC::isCompatibleCommand(string const & cmd)
66 {
67         return cmd == defaultCommand() || cmd == "lstlistoflistings";
68 }
69
70
71 docstring InsetTOC::screenLabel() const
72 {
73         if (getCmdName() == "tableofcontents")
74                 return buffer().B_("Table of Contents");
75         if (getCmdName() == "lstlistoflistings")
76                 return buffer().B_("List of Listings");
77         return _("Unknown TOC type");
78 }
79
80
81 void InsetTOC::doDispatch(Cursor & cur, FuncRequest & cmd) {
82         switch (cmd.action()) {
83         case LFUN_MOUSE_RELEASE:
84                 if (!cur.selection() && cmd.button() == mouse_button::button1) {
85                         cur.bv().showDialog("toc", params2string(params()));
86                         cur.dispatched();
87                 }
88                 break;
89         
90         default:
91                 InsetCommand::doDispatch(cur, cmd);
92         }
93 }
94
95
96 docstring InsetTOC::layoutName() const
97 {
98         if (getCmdName() == "lstlistoflistings")
99                 return from_ascii("TOC:Listings");
100         return docstring();
101 }
102
103
104 void InsetTOC::validate(LaTeXFeatures & features) const
105 {
106         InsetCommand::validate(features);
107         features.useInsetLayout(getLayout());
108         if (getCmdName() == "lstlistoflistings")
109                 features.require("listings");
110 }
111
112
113 int InsetTOC::plaintext(odocstringstream & os,
114         OutputParams const &, size_t max_length) const
115 {
116         os << screenLabel() << "\n\n";
117         buffer().tocBackend().writePlaintextTocList(cmd2type(getCmdName()), os, max_length);
118         return PLAINTEXT_NEWLINE;
119 }
120
121
122 int InsetTOC::docbook(odocstream & os, OutputParams const &) const
123 {
124         if (getCmdName() == "tableofcontents")
125                 os << "<toc></toc>";
126         return 0;
127 }
128
129
130 docstring InsetTOC::xhtml(XHTMLStream &, OutputParams const & op) const
131 {
132         if (getCmdName() != "tableofcontents")
133                 return docstring();
134
135         Layout const & lay = buffer().params().documentClass().htmlTOCLayout();
136         string const & tocclass = lay.defaultCSSClass();
137         string const tocattr = "class='tochead " + tocclass + "'";
138         
139         // we'll use our own stream, because we are going to defer everything.
140         // that's how we deal with the fact that we're probably inside a standard
141         // paragraph, and we don't want to be.
142         odocstringstream ods;
143         XHTMLStream xs(ods);
144
145         Toc const & toc = buffer().tocBackend().toc(cmd2type(getCmdName()));
146         if (toc.empty())
147                 return docstring();
148
149         xs << html::StartTag("div", "class='toc'");
150
151         // Title of TOC
152         docstring title = screenLabel();
153         xs << html::StartTag("div", tocattr)
154                  << title
155                  << html::EndTag("div");
156
157         // Output of TOC
158         Toc::const_iterator it = toc.begin();
159         Toc::const_iterator const en = toc.end();
160         int lastdepth = 0;
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.
164                 if (!it->isOutput())
165                         continue;
166
167                 // First, we need to manage increases and decreases of depth
168                 int const depth = it->depth();
169                 
170                 // Ignore stuff above the tocdepth
171                 if (depth > buffer().params().tocdepth)
172                         continue;
173                 
174                 if (depth > lastdepth) {
175                         xs << html::CR();
176                         // open as many tags as we need to open to get to this level
177                         // this includes the tag for the current level
178                         for (int i = lastdepth + 1; i <= depth; ++i) {
179                                 stringstream attr;
180                                 attr << "class='lyxtoc-" << i << "'";
181                                 xs << html::StartTag("div", attr.str()) << html::CR();
182                         }
183                         lastdepth = depth;
184                 }
185                 else if (depth < lastdepth) {
186                         // close as many as we have to close to get back to this level
187                         // this includes closing the last tag at this level
188                         for (int i = lastdepth; i >= depth; --i) 
189                                 xs << html::EndTag("div") << html::CR();
190                         // now open our tag
191                         stringstream attr;
192                         attr << "class='lyxtoc-" << depth << "'";
193                         xs << html::StartTag("div", attr.str()) << html::CR();
194                         lastdepth = depth;
195                 } else {
196                         // no change of level, so close and open
197                         xs << html::EndTag("div") << html::CR();
198                         stringstream attr;
199                         attr << "class='lyxtoc-" << depth << "'";
200                         xs << html::StartTag("div", attr.str()) << html::CR();
201                 }
202                 
203                 // Now output TOC info for this entry
204                 Paragraph const & par = it->dit().innerParagraph();
205
206                 string const attr = "href='#" + par.magicLabel() + "' class='tocentry'";
207                 xs << html::StartTag("a", attr);
208
209                 // First the label, if there is one
210                 docstring const & label = par.params().labelString();
211                 if (!label.empty())
212                         xs << label << " ";
213                 // Now the content of the TOC entry, taken from the paragraph itself
214                 OutputParams ours = op;
215                 ours.for_toc = true;
216                 Font const dummy;
217                 par.simpleLyXHTMLOnePar(buffer(), xs, ours, dummy);
218
219                 xs << html::EndTag("a") << " ";
220
221                 // Now a link to that paragraph
222                 string const parattr = "href='#" + par.magicLabel() + "' class='tocarrow'";
223                 xs << html::StartTag("a", parattr);
224                 // FIXME XHTML 
225                 // There ought to be a simple way to customize this.
226                 // Maybe if we had an InsetLayout for TOC...
227                 xs << XHTMLStream::ESCAPE_NONE << "&gt;";
228                 xs << html::EndTag("a");                
229         }
230         for (int i = lastdepth; i > 0; --i) 
231                 xs << html::EndTag("div") << html::CR();
232         xs << html::EndTag("div") << html::CR();
233         return ods.str();
234 }
235
236
237 } // namespace lyx