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