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