2 * \file output_docbook.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
14 #include "output_docbook.h"
17 #include "bufferparams.h"
21 #include "paragraph.h"
22 #include "paragraph_funcs.h"
23 #include "ParagraphParameters.h"
26 #include "insets/insetcommand.h"
28 #include "support/lstrings.h"
29 #include "support/lyxlib.h"
30 #include "support/tostr.h"
37 using lyx::support::atoi;
38 using lyx::support::split;
39 using lyx::support::subst;
48 void docbookParagraphs(Buffer const & buf,
49 ParagraphList const & paragraphs,
51 OutputParams const & runparams)
53 vector<string> environment_stack(10);
54 vector<string> environment_inner(10);
55 vector<string> command_stack(10);
57 bool command_flag = false;
58 Paragraph::depth_type command_depth = 0;
59 Paragraph::depth_type command_base = 0;
60 Paragraph::depth_type cmd_depth = 0;
61 Paragraph::depth_type depth = 0; // paragraph depth
67 ParagraphList::iterator par = const_cast<ParagraphList&>(paragraphs).begin();
68 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
70 Counters & counters = buf.params().getLyXTextClass().counters();
72 for (; par != pend; ++par) {
74 LyXLayout_ptr const & style = par->layout();
76 // environment tag closing
77 for (; depth > par->params().depth(); --depth) {
78 sgml::closeEnvTags(os, false, environment_inner[depth],
79 item_tag, command_depth + depth);
80 sgml::closeTag(os, depth + command_depth, false, environment_stack[depth]);
81 environment_stack[depth].erase();
82 environment_inner[depth].erase();
85 if (depth == par->params().depth()
86 && environment_stack[depth] != style->latexname()
87 && !environment_stack[depth].empty()) {
88 sgml::closeEnvTags(os, false, environment_inner[depth],
89 item_tag, command_depth + depth);
90 sgml::closeTag(os, depth + command_depth, false, environment_stack[depth]);
92 environment_stack[depth].erase();
93 environment_inner[depth].erase();
98 // Write opening SGML tags.
99 switch (style->latextype) {
100 case LATEX_PARAGRAPH:
101 if (!style->latexparam().empty()) {
102 counters.step("para");
103 int i = counters.value("para");
104 ls = subst(style->latexparam(), "#", tostr(i));
106 sgml::openTag(os, depth + command_depth,
107 false, style->latexname(), ls);
112 //error(ErrorItem(_("Error"), _("Wrong depth for LatexType Command."), par->id(), 0, par->size()));
115 command_name = style->latexname();
117 cmd_depth = style->commanddepth;
120 if (cmd_depth < command_base) {
121 for (Paragraph::depth_type j = command_depth;
122 j >= command_base; --j) {
123 sgml::closeTag(os, j, false, command_stack[j]);
126 command_depth = command_base = cmd_depth;
127 } else if (cmd_depth <= command_depth) {
128 for (int j = command_depth;
129 j >= int(cmd_depth); --j) {
130 sgml::closeTag(os, j, false, command_stack[j]);
133 command_depth = cmd_depth;
135 command_depth = cmd_depth;
137 command_depth = command_base = cmd_depth;
140 if (command_stack.size() == command_depth + 1)
141 command_stack.push_back(string());
142 command_stack[command_depth] = command_name;
144 if (style->latexparam().find('#') != string::npos) {
145 counters.step(style->counter);
147 // treat label as a special case for
148 // more WYSIWYM handling.
149 // This is a hack while paragraphs can't have
150 // attributes, like id in this case.
151 if (par->size() && par->isInset(0)) {
152 InsetBase * inset = par->getInset(0);
153 if (inset->lyxCode() == InsetOld::LABEL_CODE) {
154 command_name += " id=\"";
155 command_name += (static_cast<InsetCommand *>(inset))->getContents();
160 if (!labelid && !style->latexparam().empty()) {
161 ls = style->latexparam();
162 if (ls.find('#') != string::npos) {
163 string el = expandLabel(buf.params().getLyXTextClass(),
165 ls = subst(ls, "#", el);
169 sgml::openTag(os, depth + command_depth, false, command_name, ls);
171 // Label around sectioning number:
172 if (!style->labeltag().empty()) {
173 sgml::openTag(os, depth + 1 + command_depth, false,
175 os << expandLabel(buf.params().getLyXTextClass(), style, false);
176 sgml::closeTag(os, depth + 1 + command_depth, false,
180 // Inner tagged header text, e.g. <title> for sectioning:
181 sgml::openTag(os, depth + 1 + command_depth, false,
185 case LATEX_ENVIRONMENT:
186 case LATEX_ITEM_ENVIRONMENT:
187 if (depth < par->params().depth()) {
188 depth = par->params().depth();
189 environment_stack[depth].erase();
192 if (environment_stack[depth] != style->latexname()) {
193 if (environment_stack.size() == depth + 1) {
194 environment_stack.push_back("!-- --");
195 environment_inner.push_back("!-- --");
197 environment_stack[depth] = style->latexname();
198 environment_inner[depth] = "!-- --";
199 // outputs <environment_stack[depth] latexparam()>
200 sgml::openTag(os, depth + command_depth, false,
201 environment_stack[depth], style->latexparam());
203 sgml::closeEnvTags(os, false, environment_inner[depth],
204 style->itemtag(), command_depth + depth);
207 if (style->latextype == LATEX_ENVIRONMENT) {
208 if (!style->innertag().empty()) {
209 if (style->innertag() == "CDATA")
212 sgml::openTag(os, depth + command_depth, false,
218 environment_inner[depth] = style->innertag();
220 if (!environment_inner[depth].empty())
221 sgml::openTag(os, depth + 1 + command_depth,
222 false, environment_inner[depth]);
225 sgml::openTag(os, depth + command_depth,
226 false, style->latexname());
230 par->simpleDocBookOnePar(buf, os, outerFont(par, paragraphs),
231 runparams, depth + 1 + command_depth, labelid);
233 // write closing SGML tags
234 switch (style->latextype) {
236 sgml::closeTag(os, depth + command_depth, false,
239 case LATEX_ENVIRONMENT:
240 if (!style->innertag().empty()) {
241 if (style->innertag() == "CDATA")
244 sgml::closeTag(os, depth + command_depth, false,
248 case LATEX_ITEM_ENVIRONMENT:
249 item_tag = style->itemtag();
251 case LATEX_PARAGRAPH:
252 sgml::closeTag(os, depth + command_depth, false, style->latexname());
255 sgml::closeTag(os, depth + command_depth, false, style->latexname());
261 for (int d = depth; d >= 0; --d) {
262 if (!environment_stack[depth].empty()) {
263 sgml::closeEnvTags(os, false, environment_inner[depth],
264 item_tag, command_depth + depth);
268 for (int j = command_depth; j >= 0 ; --j)
269 if (!command_stack[j].empty()) {
270 sgml::closeTag(os, j, false, command_stack[j]);