]> git.lyx.org Git - lyx.git/blob - src/output_docbook.C
Rename ascii to plaintext and LatexRunParams to OutputParams.
[lyx.git] / src / output_docbook.C
1 /**
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.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author José Matos
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "output_docbook.h"
15
16 #include "buffer.h"
17 #include "bufferparams.h"
18 #include "paragraph.h"
19 #include "paragraph_funcs.h"
20 #include "ParagraphParameters.h"
21 #include "sgml.h"
22
23 #include "insets/insetcommand.h"
24
25 #include "support/lstrings.h"
26 #include "support/lyxlib.h"
27
28 #include <stack>
29
30 #ifdef HAVE_LOCALE
31 #endif
32
33 using lyx::support::atoi;
34 using lyx::support::split;
35
36 using std::endl;
37 using std::ostream;
38 using std::stack;
39 using std::vector;
40 using std::string;
41
42
43 void docbookParagraphs(Buffer const & buf,
44                        ParagraphList const & paragraphs,
45                        ostream & os,
46                        OutputParams const & runparams)
47 {
48         vector<string> environment_stack(10);
49         vector<string> environment_inner(10);
50         vector<string> command_stack(10);
51
52         bool command_flag = false;
53         Paragraph::depth_type command_depth = 0;
54         Paragraph::depth_type command_base = 0;
55         Paragraph::depth_type cmd_depth = 0;
56         Paragraph::depth_type depth = 0; // paragraph depth
57
58         string item_name;
59         string command_name;
60
61         ParagraphList::iterator par = const_cast<ParagraphList&>(paragraphs).begin();
62         ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
63
64         for (; par != pend; ++par) {
65                 string sgmlparam;
66                 string c_depth;
67                 string c_params;
68                 int desc_on = 0; // description mode
69
70                 LyXLayout_ptr const & style = par->layout();
71
72                 // environment tag closing
73                 for (; depth > par->params().depth(); --depth) {
74                         if (!environment_inner[depth].empty()) 
75                         sgml::closeEnvTags(os, false, environment_inner[depth], 
76                                         command_depth + depth);
77                         sgml::closeTag(os, depth + command_depth, false, environment_stack[depth]);
78                         environment_stack[depth].erase();
79                         environment_inner[depth].erase();
80                 }
81
82                 if (depth == par->params().depth()
83                    && environment_stack[depth] != style->latexname()
84                    && !environment_stack[depth].empty()) {
85                                 sgml::closeEnvTags(os, false, environment_inner[depth], 
86                                         command_depth + depth);
87                         sgml::closeTag(os, depth + command_depth, false, environment_stack[depth]);
88
89                         environment_stack[depth].erase();
90                         environment_inner[depth].erase();
91                 }
92
93                 // Write opening SGML tags.
94                 switch (style->latextype) {
95                 case LATEX_PARAGRAPH:
96                         sgml::openTag(os, depth + command_depth,
97                                     false, style->latexname());
98                         break;
99
100                 case LATEX_COMMAND:
101                         if (depth != 0)
102                                 //error(ErrorItem(_("Error"), _("Wrong depth for LatexType Command."), par->id(), 0, par->size()));
103                                 ;
104                         
105                         command_name = style->latexname();
106
107                         sgmlparam = style->latexparam();
108                         c_params = split(sgmlparam, c_depth,'|');
109
110                         cmd_depth = atoi(c_depth);
111
112                         if (command_flag) {
113                                 if (cmd_depth < command_base) {
114                                         for (Paragraph::depth_type j = command_depth;
115                                              j >= command_base; --j) {
116                                                 sgml::closeTag(os, j, false, command_stack[j]);
117                                                 os << endl;
118                                         }
119                                         command_depth = command_base = cmd_depth;
120                                 } else if (cmd_depth <= command_depth) {
121                                         for (int j = command_depth;
122                                              j >= int(cmd_depth); --j) {
123                                                 sgml::closeTag(os, j, false, command_stack[j]);
124                                                 os << endl;
125                                         }
126                                         command_depth = cmd_depth;
127                                 } else
128                                         command_depth = cmd_depth;
129                         } else {
130                                 command_depth = command_base = cmd_depth;
131                                 command_flag = true;
132                         }
133                         if (command_stack.size() == command_depth + 1)
134                                 command_stack.push_back(string());
135                         command_stack[command_depth] = command_name;
136
137                         // treat label as a special case for
138                         // more WYSIWYM handling.
139                         // This is a hack while paragraphs can't have
140                         // attributes, like id in this case.
141                         if (par->isInset(0)) {
142                                 InsetOld * inset = par->getInset(0);
143                                 InsetOld::Code lyx_code = inset->lyxCode();
144                                 if (lyx_code == InsetOld::LABEL_CODE) {
145                                         command_name += " id=\"";
146                                         command_name += (static_cast<InsetCommand *>(inset))->getContents();
147                                         command_name += '"';
148                                         desc_on = 3;
149                                 }
150                         }
151
152                         sgml::openTag(os, depth + command_depth, false, command_name);
153
154                         item_name = c_params.empty() ? "title" : c_params;
155                         sgml::openTag(os, depth + 1 + command_depth, false, item_name);
156                         break;
157
158                 case LATEX_ENVIRONMENT:
159                 case LATEX_ITEM_ENVIRONMENT:
160                         if (depth < par->params().depth()) {
161                                 depth = par->params().depth();
162                                 environment_stack[depth].erase();
163                         }
164
165                         if (environment_stack[depth] != style->latexname()) {
166                                 if (environment_stack.size() == depth + 1) {
167                                         environment_stack.push_back("!-- --");
168                                         environment_inner.push_back("!-- --");
169                                 }
170                                 environment_stack[depth] = style->latexname();
171                                 environment_inner[depth] = "!-- --";
172                                 sgml::openTag(os, depth + command_depth, false, environment_stack[depth]);
173                         } else {
174                                         sgml::closeEnvTags(os, false, environment_inner[depth], 
175                                                 command_depth + depth);
176                         }
177
178                         if (style->latextype == LATEX_ENVIRONMENT) {
179                                 if (!style->latexparam().empty()) {
180                                         if (style->latexparam() == "CDATA")
181                                                 os << "<![CDATA[";
182                                         else
183                                                 sgml::openTag(os, depth + command_depth, false, style->latexparam());
184                                 }
185                                 break;
186                         }
187
188                         desc_on = (style->labeltype == LABEL_MANUAL);
189
190                         environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
191                         sgml::openTag(os, depth + 1 + command_depth,
192                                     false, environment_inner[depth]);
193
194                         item_name = desc_on ? "term" : "para";
195                         sgml::openTag(os, depth + 1 + command_depth,
196                                     false, item_name);
197                         break;
198                 default:
199                         sgml::openTag(os, depth + command_depth,
200                                     false, style->latexname());
201                         break;
202                 }
203
204                 par->simpleDocBookOnePar(buf, os, outerFont(par, paragraphs), desc_on,
205                                          runparams, depth + 1 + command_depth);
206
207                 string end_tag;
208                 // write closing SGML tags
209                 switch (style->latextype) {
210                 case LATEX_COMMAND:
211                         end_tag = c_params.empty() ? "title" : c_params;
212                         sgml::closeTag(os, depth + command_depth,
213                                      false, end_tag);
214                         break;
215                 case LATEX_ENVIRONMENT:
216                         if (!style->latexparam().empty()) {
217                                 if (style->latexparam() == "CDATA")
218                                         os << "]]>";
219                                 else
220                                         sgml::closeTag(os, depth + command_depth, false, style->latexparam());
221                         }
222                         break;
223                 case LATEX_ITEM_ENVIRONMENT:
224                         if (desc_on == 1) break;
225                         end_tag = "para";
226                         sgml::closeTag(os, depth + 1 + command_depth, false, end_tag);
227                         break;
228                 case LATEX_PARAGRAPH:
229                         sgml::closeTag(os, depth + command_depth, false, style->latexname());
230                         break;
231                 default:
232                         sgml::closeTag(os, depth + command_depth, false, style->latexname());
233                         break;
234                 }
235         }
236
237         // Close open tags
238         for (int d = depth; d >= 0; --d) {
239                 if (!environment_stack[depth].empty()) {
240                                 sgml::closeEnvTags(os, false, environment_inner[depth], 
241                                         command_depth + depth);
242                 }
243         }
244
245         for (int j = command_depth; j >= 0 ; --j)
246                 if (!command_stack[j].empty()) {
247                         sgml::closeTag(os, j, false, command_stack[j]);
248                         os << endl;
249                 }
250 }
251