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::const_iterator par = paragraphs.begin();
68 ParagraphList::const_iterator pend = 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();
96 string ls = par->getDocbookId();
98 ls = " id = \"" + ls + "\"";
100 // Write opening SGML tags.
101 switch (style->latextype) {
102 case LATEX_PARAGRAPH:
103 if (!style->latexparam().empty()) {
104 counters.step("para");
105 int i = counters.value("para");
106 ls = subst(style->latexparam(), "#", tostr(i));
109 sgml::openTag(os, depth + command_depth,
110 false, style->latexname(), ls);
115 //error(ErrorItem(_("Error"), _("Wrong depth for LatexType Command."), par->id(), 0, par->size()));
118 command_name = style->latexname();
120 cmd_depth = style->commanddepth;
123 if (cmd_depth < command_base) {
124 for (Paragraph::depth_type j = command_depth;
125 j >= command_base; --j) {
126 sgml::closeTag(os, j, false, command_stack[j]);
129 command_depth = command_base = cmd_depth;
130 } else if (cmd_depth <= command_depth) {
131 for (int j = command_depth;
132 j >= int(cmd_depth); --j) {
133 sgml::closeTag(os, j, false, command_stack[j]);
136 command_depth = cmd_depth;
138 command_depth = cmd_depth;
140 command_depth = command_base = cmd_depth;
143 if (command_stack.size() == command_depth + 1)
144 command_stack.push_back(string());
145 command_stack[command_depth] = command_name;
147 if (style->latexparam().find('#') != string::npos) {
148 counters.step(style->counter);
151 if (!style->latexparam().empty()) {
152 ls = style->latexparam();
153 if (ls.find('#') != string::npos) {
154 string el = expandLabel(buf.params().getLyXTextClass(),
156 ls = subst(ls, "#", el);
160 sgml::openTag(os, depth + command_depth, false, command_name, ls);
162 // Label around sectioning number:
163 if (!style->labeltag().empty()) {
164 sgml::openTag(os, depth + 1 + command_depth, false,
166 os << expandLabel(buf.params().getLyXTextClass(), style, false);
167 sgml::closeTag(os, depth + 1 + command_depth, false,
171 // Inner tagged header text, e.g. <title> for sectioning:
172 sgml::openTag(os, depth + 1 + command_depth, false,
176 case LATEX_ENVIRONMENT:
177 case LATEX_ITEM_ENVIRONMENT:
178 if (depth < par->params().depth()) {
179 depth = par->params().depth();
180 environment_stack[depth].erase();
183 if (environment_stack[depth] != style->latexname()) {
184 if (environment_stack.size() == depth + 1) {
185 environment_stack.push_back("!-- --");
186 environment_inner.push_back("!-- --");
188 environment_stack[depth] = style->latexname();
189 environment_inner[depth] = "!-- --";
190 // outputs <environment_stack[depth] latexparam()>
191 sgml::openTag(os, depth + command_depth, false,
192 environment_stack[depth], style->latexparam() + ls);
194 sgml::closeEnvTags(os, false, environment_inner[depth],
195 style->itemtag(), command_depth + depth);
198 if (style->latextype == LATEX_ENVIRONMENT) {
199 if (!style->innertag().empty()) {
200 if (style->innertag() == "CDATA")
203 sgml::openTag(os, depth + command_depth, false,
209 environment_inner[depth] = style->innertag();
211 if (!environment_inner[depth].empty())
212 sgml::openTag(os, depth + 1 + command_depth,
213 false, environment_inner[depth]);
216 sgml::openTag(os, depth + command_depth,
217 false, style->latexname(), ls);
221 par->simpleDocBookOnePar(buf, os,
222 outerFont(par - paragraphs.begin(), paragraphs),
223 runparams, depth + 1 + command_depth);
225 // write closing SGML tags
226 switch (style->latextype) {
228 sgml::closeTag(os, depth + command_depth, false,
231 case LATEX_ENVIRONMENT:
232 if (!style->innertag().empty()) {
233 if (style->innertag() == "CDATA")
236 sgml::closeTag(os, depth + command_depth, false,
240 case LATEX_ITEM_ENVIRONMENT:
241 item_tag = style->itemtag();
243 case LATEX_PARAGRAPH:
244 sgml::closeTag(os, depth + command_depth, false, style->latexname());
247 sgml::closeTag(os, depth + command_depth, false, style->latexname());
253 for (int d = depth; d >= 0; --d) {
254 if (!environment_stack[depth].empty()) {
255 sgml::closeEnvTags(os, false, environment_inner[depth],
256 item_tag, command_depth + depth);
260 for (int j = command_depth; j >= 0 ; --j)
261 if (!command_stack[j].empty()) {
262 sgml::closeTag(os, j, false, command_stack[j]);