]> git.lyx.org Git - lyx.git/blob - src/output_docbook.cpp
Run codespell on src/frontends
[lyx.git] / src / output_docbook.cpp
1 /**
2  * \file output_docbook.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  * \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 "buffer_funcs.h"
18 #include "BufferParams.h"
19 #include "Counters.h"
20 #include "Font.h"
21 #include "Layout.h"
22 #include "OutputParams.h"
23 #include "Paragraph.h"
24 #include "ParagraphList.h"
25 #include "ParagraphParameters.h"
26 #include "xml.h"
27 #include "Text.h"
28 #include "TextClass.h"
29
30 #include "support/lassert.h"
31 #include "support/debug.h"
32 #include "support/lstrings.h"
33 #include "support/lyxalgo.h"
34
35 #include <iostream>
36
37 using namespace std;
38 using namespace lyx::support;
39
40 namespace lyx {
41
42 namespace {
43
44 std::string const fontToDocBookTag(xml::FontTypes type) {
45     switch (type) {
46     case xml::FontTypes::FT_EMPH:
47     case xml::FontTypes::FT_BOLD:
48         return "emphasis";
49     case xml::FontTypes::FT_NOUN:
50         return "person";
51     case xml::FontTypes::FT_UBAR:
52     case xml::FontTypes::FT_WAVE:
53     case xml::FontTypes::FT_DBAR:
54     case xml::FontTypes::FT_SOUT:
55     case xml::FontTypes::FT_XOUT:
56     case xml::FontTypes::FT_ITALIC:
57     case xml::FontTypes::FT_UPRIGHT:
58     case xml::FontTypes::FT_SLANTED:
59     case xml::FontTypes::FT_SMALLCAPS:
60     case xml::FontTypes::FT_ROMAN:
61     case xml::FontTypes::FT_SANS:
62         return "emphasis";
63     case xml::FontTypes::FT_TYPE:
64         return "code";
65     case xml::FontTypes::FT_SIZE_TINY:
66     case xml::FontTypes::FT_SIZE_SCRIPT:
67     case xml::FontTypes::FT_SIZE_FOOTNOTE:
68     case xml::FontTypes::FT_SIZE_SMALL:
69     case xml::FontTypes::FT_SIZE_NORMAL:
70     case xml::FontTypes::FT_SIZE_LARGE:
71     case xml::FontTypes::FT_SIZE_LARGER:
72     case xml::FontTypes::FT_SIZE_LARGEST:
73     case xml::FontTypes::FT_SIZE_HUGE:
74     case xml::FontTypes::FT_SIZE_HUGER:
75     case xml::FontTypes::FT_SIZE_INCREASE:
76     case xml::FontTypes::FT_SIZE_DECREASE:
77         return "emphasis";
78     default:
79         return "";
80     }
81 }
82
83 ParagraphList::const_iterator searchParagraph(
84         ParagraphList::const_iterator p,
85         ParagraphList::const_iterator const & pend)
86 {
87         for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
88                 ;
89
90         return p;
91 }
92
93
94 ParagraphList::const_iterator searchCommand(
95         ParagraphList::const_iterator p,
96         ParagraphList::const_iterator const & pend)
97 {
98         Layout const & bstyle = p->layout();
99
100         for (++p; p != pend; ++p) {
101                 Layout const & style = p->layout();
102                 if (style.latextype == LATEX_COMMAND
103                                 && style.commanddepth <= bstyle.commanddepth)
104                         return p;
105         }
106         return pend;
107 }
108
109
110 ParagraphList::const_iterator searchEnvironment(
111         ParagraphList::const_iterator p,
112         ParagraphList::const_iterator const & pend)
113 {
114         Layout const & bstyle = p->layout();
115         size_t const depth = p->params().depth();
116         for (++p; p != pend; ++p) {
117                 Layout const & style = p->layout();
118                 if (style.latextype == LATEX_COMMAND)
119                         return p;
120
121                 if (style.latextype == LATEX_PARAGRAPH) {
122                         if (p->params().depth() > depth)
123                                 continue;
124                         return p;
125                 }
126
127                 if (p->params().depth() < depth)
128                         return p;
129
130                 if (style.latexname() != bstyle.latexname()
131                                 && p->params().depth() == depth)
132                         return p;
133         }
134         return pend;
135 }
136
137
138 ParagraphList::const_iterator makeParagraph(
139         Buffer const & buf,
140         odocstream & os,
141         OutputParams const & runparams,
142         Text const & text,
143         ParagraphList::const_iterator const & pbegin,
144         ParagraphList::const_iterator const & pend)
145 {
146         ParagraphList const & paragraphs = text.paragraphs();
147         for (ParagraphList::const_iterator par = pbegin; par != pend; ++par) {
148                 if (par != pbegin)
149                         os << '\n';
150                 bool const default_or_plain =
151                         (buf.params().documentClass().isDefaultLayout(par->layout())
152                                 || buf.params().documentClass().isPlainLayout(par->layout()));
153                 if (default_or_plain && par->emptyTag()) {
154                         par->simpleDocBookOnePar(buf, os, runparams,
155                                         text.outerFont(distance(paragraphs.begin(), par)));
156                 } else {
157                         xml::openTag(buf, os, runparams, *par);
158                         par->simpleDocBookOnePar(buf, os, runparams,
159                                         text.outerFont(distance(paragraphs.begin(), par)));
160                         xml::closeTag(os, *par);
161                 }
162         }
163         return pend;
164 }
165
166
167 ParagraphList::const_iterator makeEnvironment(
168         Buffer const & buf,
169         odocstream & os,
170         OutputParams const & runparams,
171         Text const & text,
172         ParagraphList::const_iterator const & pbegin,
173         ParagraphList::const_iterator const & pend)
174 {
175         ParagraphList const & paragraphs = text.paragraphs();
176         ParagraphList::const_iterator par = pbegin;
177
178         Layout const & defaultstyle = buf.params().documentClass().defaultLayout();
179         Layout const & bstyle = par->layout();
180
181         // Opening outter tag
182         xml::openTag(buf, os, runparams, *pbegin);
183         os << '\n';
184         if (bstyle.latextype == LATEX_ENVIRONMENT && bstyle.pass_thru)
185                 os << "<![CDATA[";
186
187         while (par != pend) {
188                 Layout const & style = par->layout();
189                 ParagraphList::const_iterator send;
190                 string id = par->getID(buf, runparams);
191                 string wrapper = "";
192                 pos_type sep = 0;
193
194                 // Opening inner tag
195                 switch (bstyle.latextype) {
196                 case LATEX_ENVIRONMENT:
197                         if (!bstyle.innertag().empty()) {
198                                 xml::openTag(os, bstyle.innertag(), id);
199                         }
200                         break;
201
202                 case LATEX_ITEM_ENVIRONMENT:
203                         if (!bstyle.labeltag().empty()) {
204                                 xml::openTag(os, bstyle.innertag(), id);
205                                 xml::openTag(os, bstyle.labeltag());
206                                 sep = par->firstWordDocBook(os, runparams) + 1;
207                                 xml::closeTag(os, bstyle.labeltag());
208                         }
209                         wrapper = defaultstyle.latexname();
210                         // If a sub list (embedded list) appears next with a
211                         // different depth, then there is no need to open
212                         // another tag at the current depth.
213                         if(par->params().depth() == pbegin->params().depth()) {
214                                 xml::openTag(os, bstyle.itemtag());
215                         }
216                         break;
217                 default:
218                         break;
219                 }
220
221                 switch (style.latextype) {
222                 case LATEX_ENVIRONMENT:
223                 case LATEX_ITEM_ENVIRONMENT: {
224                         if (par->params().depth() == pbegin->params().depth()) {
225                                 xml::openTag(os, wrapper);
226                                 par->simpleDocBookOnePar(buf, os, runparams,
227                                         text.outerFont(distance(paragraphs.begin(), par)), sep);
228                                 xml::closeTag(os, wrapper);
229                                 ++par;
230                         }
231                         else {
232                                 send = searchEnvironment(par, pend);
233                                 par = makeEnvironment(buf, os, runparams, text, par,send);
234                         }
235                         break;
236                 }
237                 case LATEX_PARAGRAPH:
238                         send = searchParagraph(par, pend);
239                         par = makeParagraph(buf, os, runparams, text, par,send);
240                         break;
241                 case LATEX_LIST_ENVIRONMENT:
242                 case LATEX_BIB_ENVIRONMENT:
243                 case LATEX_COMMAND:
244                         // FIXME This means that we are just skipping any paragraph that
245                         // isn't implemented above, and this includes lists.
246                         ++par;
247                         break;
248                 }
249
250                 // Closing inner tag
251                 switch (bstyle.latextype) {
252                 case LATEX_ENVIRONMENT:
253                         if (!bstyle.innertag().empty()) {
254                                 xml::closeTag(os, bstyle.innertag());
255                                 os << '\n';
256                         }
257                         break;
258                 case LATEX_ITEM_ENVIRONMENT:
259                         // If a sub list (embedded list) appears next, then
260                         // there is no need to close the current tag.
261                         // par should have already been incremented to the next
262                         // element. So we can compare the depth of the next
263                         // element with pbegin.
264                         // We need to be careful, that we don't dereference par
265                         // when par == pend but at the same time that the
266                         // current tag is closed.
267                         if((par != pend && par->params().depth() == pbegin->params().depth()) || par == pend) {
268                                 xml::closeTag(os, bstyle.itemtag());
269                         }
270                         if (!bstyle.labeltag().empty())
271                                 xml::closeTag(os, bstyle.innertag());
272                         break;
273                 default:
274                         break;
275                 }
276         }
277
278         if (bstyle.latextype == LATEX_ENVIRONMENT && bstyle.pass_thru)
279                 os << "]]>";
280
281         // Closing outer tag
282         xml::closeTag(os, *pbegin);
283
284         return pend;
285 }
286
287
288 ParagraphList::const_iterator makeCommand(
289         Buffer const & buf,
290         odocstream & os,
291         OutputParams const & runparams,
292         Text const & text,
293         ParagraphList::const_iterator const & pbegin,
294         ParagraphList::const_iterator const & pend)
295 {
296         ParagraphList const & paragraphs = text.paragraphs();
297         ParagraphList::const_iterator par = pbegin;
298         Layout const & bstyle = par->layout();
299
300         //Open outter tag
301         xml::openTag(buf, os, runparams, *pbegin);
302         os << '\n';
303
304         // Label around sectioning number:
305         if (!bstyle.labeltag().empty()) {
306                 xml::openTag(os, bstyle.labeltag());
307                 // We don't care about appendix in DOCBOOK.
308                 os << par->expandDocBookLabel(bstyle, buf.params());
309                 xml::closeTag(os, bstyle.labeltag());
310         }
311
312         // Opend inner tag and  close inner tags
313         xml::openTag(os, bstyle.innertag());
314         par->simpleDocBookOnePar(buf, os, runparams,
315                 text.outerFont(distance(paragraphs.begin(), par)));
316         xml::closeTag(os, bstyle.innertag());
317         os << '\n';
318
319         ++par;
320         while (par != pend) {
321                 Layout const & style = par->layout();
322                 ParagraphList::const_iterator send;
323
324                 switch (style.latextype) {
325                 case LATEX_COMMAND: {
326                         send = searchCommand(par, pend);
327                         par = makeCommand(buf, os, runparams, text, par,send);
328                         break;
329                 }
330                 case LATEX_ENVIRONMENT:
331                 case LATEX_ITEM_ENVIRONMENT: {
332                         send = searchEnvironment(par, pend);
333                         par = makeEnvironment(buf, os, runparams, text, par,send);
334                         break;
335                 }
336                 case LATEX_PARAGRAPH:
337                         send = searchParagraph(par, pend);
338                         par = makeParagraph(buf, os, runparams, text, par,send);
339                         break;
340                 case LATEX_BIB_ENVIRONMENT:
341                 case LATEX_LIST_ENVIRONMENT:
342                         // FIXME This means that we are just skipping any paragraph that
343                         // isn't implemented above.
344                         ++par;
345                         break;
346                 }
347         }
348         // Close outter tag
349         xml::closeTag(os, *pbegin);
350
351         return pend;
352 }
353
354 } // namespace
355
356
357 void docbookParagraphs(Text const & text,
358                        Buffer const & buf,
359                        odocstream & os,
360                        OutputParams const & runparams)
361 {
362         LASSERT(runparams.par_begin <= runparams.par_end,
363                 { os << "<!-- Docbook Output Error -->\n"; return; });
364
365         ParagraphList const & paragraphs = text.paragraphs();
366         ParagraphList::const_iterator par = paragraphs.begin();
367         ParagraphList::const_iterator pend = paragraphs.end();
368
369         // if only part of the paragraphs will be outputed
370         if (runparams.par_begin !=  runparams.par_end) {
371                 par = paragraphs.iterator_at(runparams.par_begin);
372                 pend = paragraphs.iterator_at(runparams.par_end);
373                 // runparams will be passed to nested paragraphs, so
374                 // we have to reset the range parameters.
375                 const_cast<OutputParams&>(runparams).par_begin = 0;
376                 const_cast<OutputParams&>(runparams).par_end = 0;
377         }
378
379         while (par != pend) {
380                 Layout const & style = par->layout();
381                 ParagraphList::const_iterator lastpar = par;
382                 ParagraphList::const_iterator send;
383
384                 switch (style.latextype) {
385                 case LATEX_COMMAND: {
386                         send = searchCommand(par, pend);
387                         par = makeCommand(buf, os, runparams, text, par, send);
388                         break;
389                 }
390                 case LATEX_ENVIRONMENT:
391                 case LATEX_ITEM_ENVIRONMENT: {
392                         send = searchEnvironment(par, pend);
393                         par = makeEnvironment(buf, os, runparams, text, par, send);
394                         break;
395                 }
396                 case LATEX_PARAGRAPH:
397                         send = searchParagraph(par, pend);
398                         par = makeParagraph(buf, os, runparams, text, par, send);
399                         break;
400                 case LATEX_BIB_ENVIRONMENT:
401                 case LATEX_LIST_ENVIRONMENT:
402                         // FIXME This means that we are just skipping any paragraph that
403                         // isn't implemented above.
404                         ++par;
405                         break;
406                 }
407                 // makeEnvironment may process more than one paragraphs and bypass pend
408                 if (distance(lastpar, par) >= distance(lastpar, pend))
409                         break;
410         }
411 }
412
413
414 } // namespace lyx