]> git.lyx.org Git - lyx.git/blob - src/insets/InsetTOC.cpp
Initial work for InsetTOC. This does actually write a TOC, but without
[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 "LaTeXFeatures.h"
20 #include "MetricsInfo.h"
21 #include "OutputParams.h"
22 #include "output_xhtml.h"
23 #include "Paragraph.h"
24 #include "TextClass.h"
25 #include "TocBackend.h"
26
27 #include "support/debug.h"
28 #include "support/gettext.h"
29
30 #include <ostream>
31
32 using namespace std;
33
34 namespace lyx {
35
36
37 InsetTOC::InsetTOC(Buffer * buf, InsetCommandParams const & p)
38         : InsetCommand(buf, p, "toc")
39 {}
40
41
42 ParamInfo const & InsetTOC::findInfo(string const & /* cmdName */)
43 {
44         static ParamInfo param_info_;
45         if (param_info_.empty()) {
46                 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
47         }
48         return param_info_;
49 }
50
51
52 docstring InsetTOC::screenLabel() const
53 {
54         if (getCmdName() == "tableofcontents")
55                 return buffer().B_("Table of Contents");
56         return _("Unknown TOC type");
57 }
58
59
60 int InsetTOC::plaintext(odocstream & os, OutputParams const &) const
61 {
62         os << screenLabel() << "\n\n";
63         buffer().tocBackend().writePlaintextTocList(getCmdName(), os);
64         return PLAINTEXT_NEWLINE;
65 }
66
67
68 int InsetTOC::docbook(odocstream & os, OutputParams const &) const
69 {
70         if (getCmdName() == "tableofcontents")
71                 os << "<toc></toc>";
72         return 0;
73 }
74
75
76 docstring InsetTOC::xhtml(XHTMLStream &, OutputParams const & op) const
77 {
78         // we'll use our own stream, because we are going to defer everything.
79         // that's how we deal with the fact that we're probably inside a standard
80         // paragraph, and we don't want to be.
81         odocstringstream ods;
82         XHTMLStream xs(ods);
83         // FIXME XHTML
84         // This is temporary. We'll get the main TOC working first. The rest will
85         // then be a fairly simple adapation of this code, I hope.
86         if (getCmdName() != "tableofcontents")
87                 return docstring();
88         Toc const & toc = buffer().tocBackend().toc("tableofcontents");
89         if (toc.empty())
90                 return docstring();
91
92         xs << StartTag("div", "class='toc'");
93
94         // we want to figure out look like a chapter, section, or whatever.
95         // so we're going to look for the layout with the minimum toclevel
96         // number. we'll take the first one, just because.
97         DocumentClass const & dc = buffer().params().documentClass();
98         TextClass::LayoutList::const_iterator lit = dc.begin();
99         TextClass::LayoutList::const_iterator len = dc.end();
100         int minlevel = 1000;
101         Layout const * lay = NULL;
102         for (; lit != len; ++lit) {
103                 int const level = lit->toclevel;
104                 if (level == Layout::NOT_IN_TOC || level >= minlevel)
105                         continue;
106                 lay = &*lit;
107                 minlevel = level;
108         }
109         
110         string const tocclass = lay ? lay->defaultCSSClass() + " ": "";
111         string const tocattr = "class='" + tocclass + "tochead'";
112         
113         xs << StartTag("div", tocattr) 
114            << _("Table of Contents") 
115                  << EndTag("div");
116         Toc::const_iterator it = toc.begin();
117         Toc::const_iterator const en = toc.end();
118         int lastdepth = 0;
119         for (; it != en; ++it) {
120                 Paragraph const & par = it->dit().innerParagraph();
121                 Font const dummy;
122                 int const depth = it->depth();
123                 if (depth > lastdepth) {
124                         xs.cr();
125                         // open as many tags as we need to open to get to this level
126                         // this includes the tag for the current level
127                         for (int i = lastdepth + 1; i <= depth; ++i) {
128                                 stringstream attr;
129                                 attr << "class='lyxtoc-" << i << "'";
130                                 xs << StartTag("div", attr.str());
131                         }
132                         lastdepth = depth;
133                 }
134                 else if (depth < lastdepth) {
135                         // close as many as we have to close to get back to this level
136                         // this includes closing the last tag at this level
137                         for (int i = lastdepth; i >= depth; --i) 
138                                 xs << EndTag("div");
139                         // now open our tag
140                         stringstream attr;
141                         attr << "class='lyxtoc-" << depth << "'";
142                         xs << StartTag("div", attr.str());
143                         lastdepth = depth;
144                 } else {
145                         // no change of level, so close and open
146                         xs << EndTag("div");
147                         stringstream attr;
148                         attr << "class='lyxtoc-" << depth << "'";
149                         xs << StartTag("div", attr.str());
150                 }
151                 par.simpleLyXHTMLOnePar(buffer(), xs, op, dummy, true);
152         }
153         for (int i = lastdepth; i > 0; --i) 
154                 xs << EndTag("div");
155         xs << EndTag("div");
156         return ods.str();
157 }
158
159
160 void InsetTOC::validate(LaTeXFeatures & features) const
161 {
162         if (features.runparams().flavor != OutputParams::HTML)
163                 return;
164         features.addPreambleSnippet("<style type=\"text/css\">\n"
165                         "div.lyxtoc-1 { margin-left: 2em; text-indent: -2em; }\n"
166                         "span.bibtexlabel:before{ content: \"[\"; }\n"
167                         "span.bibtexlabel:after{ content: \"] \"; }\n"
168                         "</style>");
169 }
170
171 } // namespace lyx