2 * \file output_xhtml.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * This code is based upon output_docbook.cpp
10 * Full author contact details are available in file CREDITS.
15 #include "output_xhtml.h"
18 #include "buffer_funcs.h"
19 #include "BufferParams.h"
22 #include "OutputParams.h"
23 #include "Paragraph.h"
24 #include "paragraph_funcs.h"
25 #include "ParagraphList.h"
26 #include "ParagraphParameters.h"
28 #include "TextClass.h"
30 #include "support/lassert.h"
31 #include "support/debug.h"
32 #include "support/lstrings.h"
34 #include <boost/next_prior.hpp>
38 using namespace lyx::support;
44 docstring escapeChar(char_type c)
68 // FIXME This needs to be protected somehow.
69 static vector<string> taglist;
71 bool openTag(odocstream & os, string tag, string attr)
75 // FIXME This is completely primitive. We need something
77 // Now do some checks on nesting of tags.
79 if (find(taglist.begin(), taglist.end(), "p") != taglist.end())
83 os << from_ascii("<" + tag + attr + ">");
84 taglist.push_back(tag);
89 bool closeTag(odocstream & os, string tag)
93 // FIXME Check for proper nesting
95 LYXERR0("Last tag not found when closing `" << tag << "'!");
98 string const & lasttag = taglist.back();
100 LYXERR0("Last tag was `" << lasttag << "' when closing `" << tag << "'!");
104 os << from_ascii("</" + tag + ">");
114 bool openTag(odocstream & os, Layout const & lay)
116 return html::openTag(os, lay.htmltag(), lay.htmlattr());
120 bool closeTag(odocstream & os, Layout const & lay)
122 return html::closeTag(os, lay.htmltag());
126 bool openLabelTag(odocstream & os, Layout const & lay)
128 return html::openTag(os, lay.htmllabel(), lay.htmllabelattr());
132 bool closeLabelTag(odocstream & os, Layout const & lay)
134 return html::closeTag(os, lay.htmllabel());
138 bool openItemTag(odocstream & os, Layout const & lay)
140 return html::openTag(os, lay.htmlitem(), lay.htmlitemattr());
144 bool closeItemTag(odocstream & os, Layout const & lay)
146 return html::closeTag(os, lay.htmlitem());
149 ParagraphList::const_iterator searchParagraph(
150 ParagraphList::const_iterator p,
151 ParagraphList::const_iterator const & pend)
153 for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
160 ParagraphList::const_iterator searchEnvironment(
161 ParagraphList::const_iterator p,
162 ParagraphList::const_iterator const & pend)
164 Layout const & bstyle = p->layout();
165 size_t const depth = p->params().depth();
166 for (++p; p != pend; ++p) {
167 Layout const & style = p->layout();
168 // It shouldn't happen that e.g. a section command occurs inside
169 // a quotation environment, at a higher depth, but as of 6/2009,
170 // it can happen. We pretend that it's just at lowest depth.
171 if (style.latextype == LATEX_COMMAND)
173 // If depth is down, we're done
174 if (p->params().depth() < depth)
176 // If depth is up, we're not done
177 if (p->params().depth() > depth)
179 // Now we know we are at the same depth
180 if (style.latextype == LATEX_PARAGRAPH
181 || style.latexname() != bstyle.latexname())
189 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
191 OutputParams const & runparams,
192 ParagraphList const & paragraphs,
193 ParagraphList::const_iterator const & pbegin,
194 ParagraphList::const_iterator const & pend)
196 ParagraphList::const_iterator par = pbegin;
197 for (; par != pend; ++par) {
198 Layout const & lay = par->layout();
201 bool const opened = openTag(os, lay);
202 par->simpleLyXHTMLOnePar(buf, os, runparams,
203 outerFont(distance(paragraphs.begin(), par), paragraphs));
212 ParagraphList::const_iterator makeEnvironment(Buffer const & buf,
214 OutputParams const & runparams,
215 ParagraphList const & paragraphs,
216 ParagraphList::const_iterator const & pbegin,
217 ParagraphList::const_iterator const & pend) {
219 ParagraphList::const_iterator par = pbegin;
220 Layout const & bstyle = par->layout();
221 depth_type const origdepth = pbegin->params().depth();
223 // Open tag for this environment
224 bool const main_tag_opened = openTag(os, bstyle);
227 // we will on occasion need to remember a layout from before.
228 Layout const * lastlay = 0;
230 while (par != pend) {
231 Layout const & style = par->layout();
232 ParagraphList::const_iterator send;
233 // this will be positive, if we want to skip the initial word
234 // (if it's been taken for the label).
237 switch (style.latextype) {
238 case LATEX_ENVIRONMENT:
239 // case LATEX_LIST_ENVIRONMENT??
240 case LATEX_ITEM_ENVIRONMENT: {
241 // There are two possiblities in this case.
242 // One is that we are still in the environment in which we
243 // started---which we will be if the depth is the same.
244 if (par->params().depth() == origdepth) {
246 closeItemTag(os, *lastlay);
249 Layout const & cstyle = par->layout();
250 if (cstyle.labeltype == LABEL_MANUAL) {
251 bool const label_tag_opened = openLabelTag(os, cstyle);
252 sep = par->firstWordLyXHTML(os, runparams);
253 if (label_tag_opened)
254 closeLabelTag(os, cstyle);
256 } else if (style.latextype == LATEX_ENVIRONMENT
257 && style.labeltype != LABEL_NO_LABEL) {
258 bool const label_tag_opened = openLabelTag(os, cstyle);
259 if (!style.counter.empty())
260 buf.params().documentClass().counters().step(cstyle.counter);
261 os << pbegin->expandLabel(style, buf.params(), false);
262 if (label_tag_opened)
263 closeLabelTag(os, cstyle);
267 bool const item_tag_opened = openItemTag(os, cstyle);
268 par->simpleLyXHTMLOnePar(buf, os, runparams,
269 outerFont(distance(paragraphs.begin(), par), paragraphs), sep);
271 // We may not want to close the tag yet, in particular,
273 // if we're not at the end...
275 // and are doing items...
276 && style.latextype == LATEX_ITEM_ENVIRONMENT
277 // and if the depth has changed...
278 && par->params().depth() != origdepth) {
279 // then we'll save this layout for later, and close it when
280 // we get another item.
283 closeItemTag(os, cstyle); // FIXME
287 // The other possibility is that the depth has increased, in which
288 // case we need to recurse.
290 send = searchEnvironment(par, pend);
291 par = makeEnvironment(buf, os, runparams, paragraphs, par, send);
295 case LATEX_PARAGRAPH:
296 send = searchParagraph(par, pend);
297 par = makeParagraphs(buf, os, runparams, paragraphs, par, send);
300 case LATEX_BIB_ENVIRONMENT:
307 closeItemTag(os, *lastlay);
309 closeTag(os, bstyle);
315 void makeCommand(Buffer const & buf,
317 OutputParams const & runparams,
318 ParagraphList const & paragraphs,
319 ParagraphList::const_iterator const & pbegin)
321 Layout const & style = pbegin->layout();
323 bool const main_tag_opened = openTag(os, style);
325 // Label around sectioning number:
326 if (style.labeltype != LABEL_NO_LABEL) {
327 bool const label_tag_opened = openLabelTag(os, style);
328 if (!style.counter.empty())
329 buf.params().documentClass().counters().step(style.counter);
330 os << pbegin->expandLabel(style, buf.params(), false);
331 if (label_tag_opened)
332 closeLabelTag(os, style);
333 // Otherwise the label might run together with the text
337 pbegin->simpleLyXHTMLOnePar(buf, os, runparams,
338 outerFont(distance(paragraphs.begin(), pbegin), paragraphs));
343 } // end anonymous namespace
346 void xhtmlParagraphs(ParagraphList const & paragraphs,
349 OutputParams const & runparams)
351 ParagraphList::const_iterator par = paragraphs.begin();
352 ParagraphList::const_iterator pend = paragraphs.end();
354 while (par != pend) {
355 Layout const & style = par->layout();
356 ParagraphList::const_iterator lastpar = par;
357 ParagraphList::const_iterator send;
359 switch (style.latextype) {
360 case LATEX_COMMAND: {
361 // The files with which we are working never have more than
362 // one paragraph in a command structure.
363 makeCommand(buf, os, runparams, paragraphs, par);
367 case LATEX_ENVIRONMENT:
368 case LATEX_ITEM_ENVIRONMENT: {
369 send = searchEnvironment(par, pend);
370 par = makeEnvironment(buf, os, runparams, paragraphs, par,send);
373 case LATEX_PARAGRAPH:
374 send = searchParagraph(par, pend);
375 par = makeParagraphs(buf, os, runparams, paragraphs, par,send);
381 // makeEnvironment may process more than one paragraphs and bypass pend
382 if (distance(lastpar, par) >= distance(lastpar, pend))