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.
6 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
14 #include "output_docbook.h"
17 #include "buffer_funcs.h"
18 #include "BufferParams.h"
22 #include "OutputParams.h"
23 #include "Paragraph.h"
24 #include "ParagraphList.h"
25 #include "ParagraphParameters.h"
28 #include "TextClass.h"
30 #include "support/lassert.h"
31 #include "support/debug.h"
32 #include "support/lstrings.h"
33 #include "support/lyxalgo.h"
38 using namespace lyx::support;
44 std::string const fontToDocBookTag(xml::FontTypes type) {
46 case xml::FontTypes::FT_EMPH:
47 case xml::FontTypes::FT_BOLD:
49 case xml::FontTypes::FT_NOUN:
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:
63 case xml::FontTypes::FT_TYPE:
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:
83 ParagraphList::const_iterator searchParagraph(
84 ParagraphList::const_iterator p,
85 ParagraphList::const_iterator const & pend)
87 for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
94 ParagraphList::const_iterator searchCommand(
95 ParagraphList::const_iterator p,
96 ParagraphList::const_iterator const & pend)
98 Layout const & bstyle = p->layout();
100 for (++p; p != pend; ++p) {
101 Layout const & style = p->layout();
102 if (style.latextype == LATEX_COMMAND
103 && style.commanddepth <= bstyle.commanddepth)
110 ParagraphList::const_iterator searchEnvironment(
111 ParagraphList::const_iterator p,
112 ParagraphList::const_iterator const & pend)
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)
121 if (style.latextype == LATEX_PARAGRAPH) {
122 if (p->params().depth() > depth)
127 if (p->params().depth() < depth)
130 if (style.latexname() != bstyle.latexname()
131 && p->params().depth() == depth)
138 ParagraphList::const_iterator makeParagraph(
141 OutputParams const & runparams,
143 ParagraphList::const_iterator const & pbegin,
144 ParagraphList::const_iterator const & pend)
146 ParagraphList const & paragraphs = text.paragraphs();
147 for (ParagraphList::const_iterator par = pbegin; par != pend; ++par) {
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)));
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);
167 ParagraphList::const_iterator makeEnvironment(
170 OutputParams const & runparams,
172 ParagraphList::const_iterator const & pbegin,
173 ParagraphList::const_iterator const & pend)
175 ParagraphList const & paragraphs = text.paragraphs();
176 ParagraphList::const_iterator par = pbegin;
178 Layout const & defaultstyle = buf.params().documentClass().defaultLayout();
179 Layout const & bstyle = par->layout();
181 // Opening outter tag
182 xml::openTag(buf, os, runparams, *pbegin);
184 if (bstyle.latextype == LATEX_ENVIRONMENT && bstyle.pass_thru)
187 while (par != pend) {
188 Layout const & style = par->layout();
189 ParagraphList::const_iterator send;
190 string id = par->getID(buf, runparams);
195 switch (bstyle.latextype) {
196 case LATEX_ENVIRONMENT:
197 if (!bstyle.innertag().empty()) {
198 xml::openTag(os, bstyle.innertag(), id);
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());
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());
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);
232 send = searchEnvironment(par, pend);
233 par = makeEnvironment(buf, os, runparams, text, par,send);
237 case LATEX_PARAGRAPH:
238 send = searchParagraph(par, pend);
239 par = makeParagraph(buf, os, runparams, text, par,send);
241 case LATEX_LIST_ENVIRONMENT:
242 case LATEX_BIB_ENVIRONMENT:
244 // FIXME This means that we are just skipping any paragraph that
245 // isn't implemented above, and this includes lists.
251 switch (bstyle.latextype) {
252 case LATEX_ENVIRONMENT:
253 if (!bstyle.innertag().empty()) {
254 xml::closeTag(os, bstyle.innertag());
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());
270 if (!bstyle.labeltag().empty())
271 xml::closeTag(os, bstyle.innertag());
278 if (bstyle.latextype == LATEX_ENVIRONMENT && bstyle.pass_thru)
282 xml::closeTag(os, *pbegin);
288 ParagraphList::const_iterator makeCommand(
291 OutputParams const & runparams,
293 ParagraphList::const_iterator const & pbegin,
294 ParagraphList::const_iterator const & pend)
296 ParagraphList const & paragraphs = text.paragraphs();
297 ParagraphList::const_iterator par = pbegin;
298 Layout const & bstyle = par->layout();
301 xml::openTag(buf, os, runparams, *pbegin);
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());
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());
320 while (par != pend) {
321 Layout const & style = par->layout();
322 ParagraphList::const_iterator send;
324 switch (style.latextype) {
325 case LATEX_COMMAND: {
326 send = searchCommand(par, pend);
327 par = makeCommand(buf, os, runparams, text, par,send);
330 case LATEX_ENVIRONMENT:
331 case LATEX_ITEM_ENVIRONMENT: {
332 send = searchEnvironment(par, pend);
333 par = makeEnvironment(buf, os, runparams, text, par,send);
336 case LATEX_PARAGRAPH:
337 send = searchParagraph(par, pend);
338 par = makeParagraph(buf, os, runparams, text, par,send);
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.
349 xml::closeTag(os, *pbegin);
357 void docbookParagraphs(Text const & text,
360 OutputParams const & runparams)
362 LASSERT(runparams.par_begin <= runparams.par_end,
363 { os << "<!-- Docbook Output Error -->\n"; return; });
365 ParagraphList const & paragraphs = text.paragraphs();
366 ParagraphList::const_iterator par = paragraphs.begin();
367 ParagraphList::const_iterator pend = paragraphs.end();
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;
379 while (par != pend) {
380 Layout const & style = par->layout();
381 ParagraphList::const_iterator lastpar = par;
382 ParagraphList::const_iterator send;
384 switch (style.latextype) {
385 case LATEX_COMMAND: {
386 send = searchCommand(par, pend);
387 par = makeCommand(buf, os, runparams, text, par, send);
390 case LATEX_ENVIRONMENT:
391 case LATEX_ITEM_ENVIRONMENT: {
392 send = searchEnvironment(par, pend);
393 par = makeEnvironment(buf, os, runparams, text, par, send);
396 case LATEX_PARAGRAPH:
397 send = searchParagraph(par, pend);
398 par = makeParagraph(buf, os, runparams, text, par, send);
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.
407 // makeEnvironment may process more than one paragraphs and bypass pend
408 if (distance(lastpar, par) >= distance(lastpar, pend))