]> git.lyx.org Git - lyx.git/blob - src/insets/InsetTOC.cpp
7270d18851a0d100b4143949b53ae2b9cee3f51c
[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 "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         Language const * lang = buffer().params().language;
95         static string toctitle = N_("Table of Contents");
96         docstring title = lang 
97                         ? translateIfPossible(from_ascii(toctitle), lang->code())
98                         : translateIfPossible(from_ascii(toctitle));
99         xs << html::StartTag("div", tocattr)
100                  << title
101                  << html::EndTag("div");
102         Toc::const_iterator it = toc.begin();
103         Toc::const_iterator const en = toc.end();
104         int lastdepth = 0;
105         for (; it != en; ++it) {
106                 Paragraph const & par = it->dit().innerParagraph();
107                 int const depth = it->depth();
108                 if (depth > buffer().params().tocdepth)
109                         continue;
110                 Font const dummy;
111                 if (depth > lastdepth) {
112                         xs.cr();
113                         // open as many tags as we need to open to get to this level
114                         // this includes the tag for the current level
115                         for (int i = lastdepth + 1; i <= depth; ++i) {
116                                 stringstream attr;
117                                 attr << "class='lyxtoc-" << i << "'";
118                                 xs << html::StartTag("div", attr.str());
119                         }
120                         lastdepth = depth;
121                 }
122                 else if (depth < lastdepth) {
123                         // close as many as we have to close to get back to this level
124                         // this includes closing the last tag at this level
125                         for (int i = lastdepth; i >= depth; --i) 
126                                 xs << html::EndTag("div");
127                         // now open our tag
128                         stringstream attr;
129                         attr << "class='lyxtoc-" << depth << "'";
130                         xs << html::StartTag("div", attr.str());
131                         lastdepth = depth;
132                 } else {
133                         // no change of level, so close and open
134                         xs << html::EndTag("div");
135                         stringstream attr;
136                         attr << "class='lyxtoc-" << depth << "'";
137                         xs << html::StartTag("div", attr.str());
138                 }
139                 string const parattr = "href='#" + par.magicLabel() + "' class='tocarrow'";
140                 OutputParams ours = op;
141                 ours.for_toc = true;
142                 par.simpleLyXHTMLOnePar(buffer(), xs, ours, dummy);
143                 xs << " ";
144                 xs << html::StartTag("a", parattr);
145                 // FIXME XHTML 
146                 // There ought to be a simple way to customize this.
147                 xs << XHTMLStream::NextRaw() << "&seArr;";
148                 xs << html::EndTag("a");                
149         }
150         for (int i = lastdepth; i > 0; --i) 
151                 xs << html::EndTag("div");
152         xs << html::EndTag("div");
153         return ods.str();
154 }
155
156
157 } // namespace lyx