]> git.lyx.org Git - lyx.git/blob - src/insets/InsetTOC.cpp
Routines for calculating numerical labels for BibTeX citations.
[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 want to look like a chapter, section, or whatever.
79         // so we're going to look for the layout with the minimum toclevel
80         // number > 0, because we don't want Part. 
81         // we'll take the first one, just because.
82         // FIXME This could be specified in the layout file.
83         DocumentClass const & dc = buffer().params().documentClass();
84         TextClass::LayoutList::const_iterator lit = dc.begin();
85         TextClass::LayoutList::const_iterator len = dc.end();
86         int minlevel = 1000;
87         Layout const * lay = NULL;
88         for (; lit != len; ++lit) {
89                 int const level = lit->toclevel;
90                 if (level > 0 && (level == Layout::NOT_IN_TOC || level >= minlevel))
91                         continue;
92                 lay = &*lit;
93                 minlevel = level;
94         }
95         
96         string const tocclass = lay ? " " + lay->defaultCSSClass(): "";
97         string const tocattr = "class='tochead" + tocclass + "'";
98         
99         // we'll use our own stream, because we are going to defer everything.
100         // that's how we deal with the fact that we're probably inside a standard
101         // paragraph, and we don't want to be.
102         odocstringstream ods;
103         XHTMLStream xs(ods);
104
105         string const & cmdname = getCmdName();
106         if (cmdname == "tableofcontents") {
107                 Toc const & toc = buffer().tocBackend().toc("tableofcontents");
108                 if (toc.empty())
109                         return docstring();
110         
111                 xs << StartTag("div", "class='toc'");
112                 xs << StartTag("div", tocattr) 
113                          << _("Table of Contents") 
114                          << EndTag("div");
115                 Toc::const_iterator it = toc.begin();
116                 Toc::const_iterator const en = toc.end();
117                 int lastdepth = 0;
118                 for (; it != en; ++it) {
119                         Paragraph const & par = it->dit().innerParagraph();
120                         int const depth = it->depth();
121                         if (depth > buffer().params().tocdepth)
122                                 continue;
123                         Font const dummy;
124                         if (depth > lastdepth) {
125                                 xs.cr();
126                                 // open as many tags as we need to open to get to this level
127                                 // this includes the tag for the current level
128                                 for (int i = lastdepth + 1; i <= depth; ++i) {
129                                         stringstream attr;
130                                         attr << "class='lyxtoc-" << i << "'";
131                                         xs << StartTag("div", attr.str());
132                                 }
133                                 lastdepth = depth;
134                         }
135                         else if (depth < lastdepth) {
136                                 // close as many as we have to close to get back to this level
137                                 // this includes closing the last tag at this level
138                                 for (int i = lastdepth; i >= depth; --i) 
139                                         xs << EndTag("div");
140                                 // now open our tag
141                                 stringstream attr;
142                                 attr << "class='lyxtoc-" << depth << "'";
143                                 xs << StartTag("div", attr.str());
144                                 lastdepth = depth;
145                         } else {
146                                 // no change of level, so close and open
147                                 xs << EndTag("div");
148                                 stringstream attr;
149                                 attr << "class='lyxtoc-" << depth << "'";
150                                 xs << StartTag("div", attr.str());
151                         }
152                         string const parattr = "href='#" + par.magicLabel() + "' class='tocarrow'";
153                         par.simpleLyXHTMLOnePar(buffer(), xs, op, dummy, true);
154                         xs << " ";
155                         xs << StartTag("a", parattr);
156                         // FIXME XHTML 
157                         // There ought to be a simple way to customize this.
158                         xs << XHTMLStream::NextRaw() << "&seArr;";
159                         xs << EndTag("a");              
160                 }
161                 for (int i = lastdepth; i > 0; --i) 
162                         xs << EndTag("div");
163                 xs << EndTag("div");
164         }
165         return ods.str();
166 }
167
168
169 } // namespace lyx