]> git.lyx.org Git - features.git/blob - src/insets/InsetTOC.cpp
Remove InsetCommand::mailer_name_.
[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 "DispatchResult.h"
18 #include "Language.h"
19 #include "LaTeXFeatures.h"
20 #include "OutputParams.h"
21 #include "output_xhtml.h"
22 #include "Paragraph.h"
23 #include "ParagraphParameters.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)
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         Layout const & lay = buffer().params().documentClass().htmlTOCLayout();
79         string const & tocclass = lay.defaultCSSClass();
80         string const tocattr = "class='tochead " + tocclass + "'";
81         
82         // we'll use our own stream, because we are going to defer everything.
83         // that's how we deal with the fact that we're probably inside a standard
84         // paragraph, and we don't want to be.
85         odocstringstream ods;
86         XHTMLStream xs(ods);
87
88         Toc const & toc = buffer().tocBackend().toc("tableofcontents");
89         if (toc.empty())
90                 return docstring();
91
92         xs << html::StartTag("div", "class='toc'");
93
94         // Title of TOC
95         Language const * lang = buffer().params().language;
96         static string toctitle = N_("Table of Contents");
97         docstring title = lang 
98                         ? translateIfPossible(from_ascii(toctitle), lang->code())
99                         : translateIfPossible(from_ascii(toctitle));
100         xs << html::StartTag("div", tocattr)
101                  << title
102                  << html::EndTag("div");
103
104         // Output of TOC
105         Toc::const_iterator it = toc.begin();
106         Toc::const_iterator const en = toc.end();
107         int lastdepth = 0;
108         for (; it != en; ++it) {
109                 // First, we need to manage increases and decreases of depth
110                 int const depth = it->depth();
111                 
112                 // Ignore stuff above the tocdepth
113                 if (depth > buffer().params().tocdepth)
114                         continue;
115                 
116                 if (depth > lastdepth) {
117                         xs.cr();
118                         // open as many tags as we need to open to get to this level
119                         // this includes the tag for the current level
120                         for (int i = lastdepth + 1; i <= depth; ++i) {
121                                 stringstream attr;
122                                 attr << "class='lyxtoc-" << i << "'";
123                                 xs << html::StartTag("div", attr.str());
124                         }
125                         lastdepth = depth;
126                 }
127                 else if (depth < lastdepth) {
128                         // close as many as we have to close to get back to this level
129                         // this includes closing the last tag at this level
130                         for (int i = lastdepth; i >= depth; --i) 
131                                 xs << html::EndTag("div");
132                         // now open our tag
133                         stringstream attr;
134                         attr << "class='lyxtoc-" << depth << "'";
135                         xs << html::StartTag("div", attr.str());
136                         lastdepth = depth;
137                 } else {
138                         // no change of level, so close and open
139                         xs << html::EndTag("div");
140                         stringstream attr;
141                         attr << "class='lyxtoc-" << depth << "'";
142                         xs << html::StartTag("div", attr.str());
143                 }
144                 
145                 // Now output TOC info for this entry
146                 Paragraph const & par = it->dit().innerParagraph();
147                 // First the label, if there is one
148                 docstring const & label = par.params().labelString();
149                 if (!label.empty())
150                         xs << label << " ";
151                 // Now the content of the TOC entry, taken from the paragraph itself
152                 OutputParams ours = op;
153                 ours.for_toc = true;
154                 Font const dummy;
155                 par.simpleLyXHTMLOnePar(buffer(), xs, ours, dummy);
156                 xs << " ";
157                 // Now a link to that paragraph
158                 string const parattr = "href='#" + par.magicLabel() + "' class='tocarrow'";
159                 xs << html::StartTag("a", parattr);
160                 // FIXME XHTML 
161                 // There ought to be a simple way to customize this.
162                 // Maybe if we had an InsetLayout for TOC...
163                 xs << XHTMLStream::NextRaw() << "&gt;";
164                 xs << html::EndTag("a");                
165         }
166         for (int i = lastdepth; i > 0; --i) 
167                 xs << html::EndTag("div");
168         xs << html::EndTag("div");
169         return ods.str();
170 }
171
172
173 } // namespace lyx