]> git.lyx.org Git - lyx.git/blob - src/insets/InsetTOC.cpp
Better TOC output for XHTML, per Rob and Pavel.
[lyx.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 "Cursor.h"
18 #include "DispatchResult.h"
19 #include "Font.h"
20 #include "FuncRequest.h"
21 #include "Language.h"
22 #include "LaTeXFeatures.h"
23 #include "OutputParams.h"
24 #include "output_xhtml.h"
25 #include "Paragraph.h"
26 #include "ParagraphParameters.h"
27 #include "TextClass.h"
28 #include "TocBackend.h"
29
30 #include "support/debug.h"
31 #include "support/gettext.h"
32
33 #include <ostream>
34
35 using namespace std;
36
37 namespace lyx {
38
39
40 InsetTOC::InsetTOC(Buffer * buf, InsetCommandParams const & p)
41         : InsetCommand(buf, p)
42 {}
43
44
45 ParamInfo const & InsetTOC::findInfo(string const & /* cmdName */)
46 {
47         static ParamInfo param_info_;
48         if (param_info_.empty()) {
49                 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
50         }
51         return param_info_;
52 }
53
54
55 docstring InsetTOC::screenLabel() const
56 {
57         if (getCmdName() == "tableofcontents")
58                 return buffer().B_("Table of Contents");
59         return _("Unknown TOC type");
60 }
61
62
63 void InsetTOC::doDispatch(Cursor & cur, FuncRequest & cmd) {
64         switch (cmd.action()) {
65         case LFUN_MOUSE_RELEASE:
66                 if (!cur.selection() && cmd.button() == mouse_button::button1) {
67                         showInsetDialog(&cur.bv());
68                         cur.dispatched();
69                 }
70                 break;
71         
72         default:
73                 InsetCommand::doDispatch(cur, cmd);
74         }
75 }
76
77
78 int InsetTOC::plaintext(odocstream & os, OutputParams const &) const
79 {
80         os << screenLabel() << "\n\n";
81         buffer().tocBackend().writePlaintextTocList(getCmdName(), os);
82         return PLAINTEXT_NEWLINE;
83 }
84
85
86 int InsetTOC::docbook(odocstream & os, OutputParams const &) const
87 {
88         if (getCmdName() == "tableofcontents")
89                 os << "<toc></toc>";
90         return 0;
91 }
92
93
94 docstring InsetTOC::xhtml(XHTMLStream &, OutputParams const & op) const
95 {
96         Layout const & lay = buffer().params().documentClass().htmlTOCLayout();
97         string const & tocclass = lay.defaultCSSClass();
98         string const tocattr = "class='tochead " + tocclass + "'";
99         
100         // we'll use our own stream, because we are going to defer everything.
101         // that's how we deal with the fact that we're probably inside a standard
102         // paragraph, and we don't want to be.
103         odocstringstream ods;
104         XHTMLStream xs(ods);
105
106         Toc const & toc = buffer().tocBackend().toc("tableofcontents");
107         if (toc.empty())
108                 return docstring();
109
110         xs << html::StartTag("div", "class='toc'");
111
112         // Title of TOC
113         static string toctitle = N_("Table of Contents");
114         docstring title = buffer().B_(toctitle);
115         xs << html::StartTag("div", tocattr)
116                  << title
117                  << html::EndTag("div");
118
119         // Output of TOC
120         Toc::const_iterator it = toc.begin();
121         Toc::const_iterator const en = toc.end();
122         int lastdepth = 0;
123         for (; it != en; ++it) {
124                 // First, we need to manage increases and decreases of depth
125                 int const depth = it->depth();
126                 
127                 // Ignore stuff above the tocdepth
128                 if (depth > buffer().params().tocdepth)
129                         continue;
130                 
131                 if (depth > lastdepth) {
132                         xs << html::CR();
133                         // open as many tags as we need to open to get to this level
134                         // this includes the tag for the current level
135                         for (int i = lastdepth + 1; i <= depth; ++i) {
136                                 stringstream attr;
137                                 attr << "class='lyxtoc-" << i << "'";
138                                 xs << html::StartTag("div", attr.str()) << html::CR();
139                         }
140                         lastdepth = depth;
141                 }
142                 else if (depth < lastdepth) {
143                         // close as many as we have to close to get back to this level
144                         // this includes closing the last tag at this level
145                         for (int i = lastdepth; i >= depth; --i) 
146                                 xs << html::EndTag("div") << html::CR();
147                         // now open our tag
148                         stringstream attr;
149                         attr << "class='lyxtoc-" << depth << "'";
150                         xs << html::StartTag("div", attr.str()) << html::CR();
151                         lastdepth = depth;
152                 } else {
153                         // no change of level, so close and open
154                         xs << html::EndTag("div") << html::CR();
155                         stringstream attr;
156                         attr << "class='lyxtoc-" << depth << "'";
157                         xs << html::StartTag("div", attr.str()) << html::CR();
158                 }
159                 
160                 // Now output TOC info for this entry
161                 Paragraph const & par = it->dit().innerParagraph();
162
163                 string const attr = "href='#" + par.magicLabel() + "' class='tocentry'";
164                 xs << html::StartTag("a", attr);
165
166                 // First the label, if there is one
167                 docstring const & label = par.params().labelString();
168                 if (!label.empty())
169                         xs << label << " ";
170                 // Now the content of the TOC entry, taken from the paragraph itself
171                 OutputParams ours = op;
172                 ours.for_toc = true;
173                 Font const dummy;
174                 par.simpleLyXHTMLOnePar(buffer(), xs, ours, dummy);
175
176                 xs << html::EndTag("a") << " ";
177
178                 // Now a link to that paragraph
179                 string const parattr = "href='#" + par.magicLabel() + "' class='tocarrow'";
180                 xs << html::StartTag("a", parattr);
181                 // FIXME XHTML 
182                 // There ought to be a simple way to customize this.
183                 // Maybe if we had an InsetLayout for TOC...
184                 xs << XHTMLStream::ESCAPE_NONE << "&gt;";
185                 xs << html::EndTag("a");                
186         }
187         for (int i = lastdepth; i > 0; --i) 
188                 xs << html::EndTag("div") << html::CR();
189         xs << html::EndTag("div") << html::CR();
190         return ods.str();
191 }
192
193
194 } // namespace lyx