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 const & tag, string const & 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())
81 os << from_ascii("<" + tag + (attr.empty() ? "" : " " + attr) + ">");
82 taglist.push_back(tag);
87 bool closeTag(odocstream & os, string const & tag)
91 // FIXME Check for proper nesting
93 LYXERR0("Last tag not found when closing `" << tag << "'!");
96 string const & lasttag = taglist.back();
98 LYXERR0("Last tag was `" << lasttag << "' when closing `" << tag << "'!");
102 os << from_ascii("</" + tag + ">");
112 bool openTag(odocstream & os, Layout const & lay)
114 return html::openTag(os, lay.htmltag(), lay.htmlattr());
118 bool closeTag(odocstream & os, Layout const & lay)
120 return html::closeTag(os, lay.htmltag());
124 bool openLabelTag(odocstream & os, Layout const & lay)
126 return html::openTag(os, lay.htmllabel(), lay.htmllabelattr());
130 bool closeLabelTag(odocstream & os, Layout const & lay)
132 return html::closeTag(os, lay.htmllabel());
136 bool openItemTag(odocstream & os, Layout const & lay)
138 return html::openTag(os, lay.htmlitem(), lay.htmlitemattr());
142 bool closeItemTag(odocstream & os, Layout const & lay)
144 return html::closeTag(os, lay.htmlitem());
147 ParagraphList::const_iterator searchParagraph(
148 ParagraphList::const_iterator p,
149 ParagraphList::const_iterator const & pend)
151 for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
158 ParagraphList::const_iterator searchEnvironment(
159 ParagraphList::const_iterator p,
160 ParagraphList::const_iterator const & pend)
162 Layout const & bstyle = p->layout();
163 size_t const depth = p->params().depth();
164 for (++p; p != pend; ++p) {
165 Layout const & style = p->layout();
166 // It shouldn't happen that e.g. a section command occurs inside
167 // a quotation environment, at a higher depth, but as of 6/2009,
168 // it can happen. We pretend that it's just at lowest depth.
169 if (style.latextype == LATEX_COMMAND)
171 // If depth is down, we're done
172 if (p->params().depth() < depth)
174 // If depth is up, we're not done
175 if (p->params().depth() > depth)
177 // Now we know we are at the same depth
178 if (style.latextype == LATEX_PARAGRAPH
179 || style.latexname() != bstyle.latexname())
187 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
189 OutputParams const & runparams,
190 ParagraphList const & paragraphs,
191 ParagraphList::const_iterator const & pbegin,
192 ParagraphList::const_iterator const & pend)
194 ParagraphList::const_iterator par = pbegin;
195 for (; par != pend; ++par) {
196 Layout const & lay = par->layout();
197 if (!lay.counter.empty())
198 buf.params().documentClass().counters().step(lay.counter);
199 // FIXME We should see if there's a label to be output and
200 // do something with it.
203 bool const opened = openTag(os, lay);
204 par->simpleLyXHTMLOnePar(buf, os, runparams,
205 outerFont(distance(paragraphs.begin(), par), paragraphs));
215 ParagraphList::const_iterator makeEnvironment(Buffer const & buf,
217 OutputParams const & runparams,
218 ParagraphList const & paragraphs,
219 ParagraphList::const_iterator const & pbegin,
220 ParagraphList::const_iterator const & pend)
222 ParagraphList::const_iterator par = pbegin;
223 Layout const & bstyle = par->layout();
224 depth_type const origdepth = pbegin->params().depth();
226 // Open tag for this environment
227 bool const main_tag_opened = openTag(os, bstyle);
230 // we will on occasion need to remember a layout from before.
231 Layout const * lastlay = 0;
233 while (par != pend) {
234 Layout const & style = par->layout();
235 if (!style.counter.empty())
236 buf.params().documentClass().counters().step(style.counter);
237 ParagraphList::const_iterator send;
238 // this will be positive, if we want to skip the initial word
239 // (if it's been taken for the label).
242 switch (style.latextype) {
243 case LATEX_ENVIRONMENT:
244 case LATEX_LIST_ENVIRONMENT:
245 case LATEX_ITEM_ENVIRONMENT: {
246 // There are two possiblities in this case.
247 // One is that we are still in the environment in which we
248 // started---which we will be if the depth is the same.
249 if (par->params().depth() == origdepth) {
250 Layout const & cstyle = par->layout();
252 closeItemTag(os, *lastlay);
255 bool const labelfirst = cstyle.htmllabelfirst();
256 bool item_tag_opened;
258 item_tag_opened = openItemTag(os, cstyle);
259 if (cstyle.labeltype == LABEL_MANUAL) {
260 bool const label_tag_opened = openLabelTag(os, cstyle);
261 sep = par->firstWordLyXHTML(os, runparams);
262 if (label_tag_opened)
263 closeLabelTag(os, cstyle);
266 // FIXME Why did I put that first condition??
267 else if (style.latextype == LATEX_ENVIRONMENT
268 && style.labeltype != LABEL_NO_LABEL) {
269 bool const label_tag_opened = openLabelTag(os, cstyle);
270 os << pbegin->expandLabel(style, buf.params(), false);
271 if (label_tag_opened)
272 closeLabelTag(os, cstyle);
276 item_tag_opened = openItemTag(os, cstyle);
278 os << "<span class='item'>";
279 par->simpleLyXHTMLOnePar(buf, os, runparams,
280 outerFont(distance(paragraphs.begin(), par), paragraphs), sep);
284 if (item_tag_opened) {
285 // We may not want to close the tag yet, in particular,
286 // if we're not at the end...
288 // and are doing items...
289 && style.latextype == LATEX_ITEM_ENVIRONMENT
290 // and if the depth has changed...
291 && par->params().depth() != origdepth) {
292 // then we'll save this layout for later, and close it when
293 // we get another item.
296 closeItemTag(os, cstyle);
301 // The other possibility is that the depth has increased, in which
302 // case we need to recurse.
304 send = searchEnvironment(par, pend);
305 par = makeEnvironment(buf, os, runparams, paragraphs, par, send);
309 case LATEX_PARAGRAPH:
310 send = searchParagraph(par, pend);
311 par = makeParagraphs(buf, os, runparams, paragraphs, par, send);
314 case LATEX_BIB_ENVIRONMENT:
322 closeItemTag(os, *lastlay);
324 closeTag(os, bstyle);
330 void makeCommand(Buffer const & buf,
332 OutputParams const & runparams,
333 ParagraphList const & paragraphs,
334 ParagraphList::const_iterator const & pbegin)
336 Layout const & style = pbegin->layout();
337 if (!style.counter.empty())
338 buf.params().documentClass().counters().step(style.counter);
340 bool const main_tag_opened = openTag(os, style);
342 // Label around sectioning number:
343 // FIXME Probably need to account for LABEL_MANUAL
344 if (style.labeltype != LABEL_NO_LABEL) {
345 bool const label_tag_opened = openLabelTag(os, style);
346 os << pbegin->expandLabel(style, buf.params(), false);
347 if (label_tag_opened)
348 closeLabelTag(os, style);
349 // Otherwise the label might run together with the text
353 pbegin->simpleLyXHTMLOnePar(buf, os, runparams,
354 outerFont(distance(paragraphs.begin(), pbegin), paragraphs));
360 } // end anonymous namespace
363 void xhtmlParagraphs(ParagraphList const & paragraphs,
366 OutputParams const & runparams)
368 ParagraphList::const_iterator par = paragraphs.begin();
369 ParagraphList::const_iterator pend = paragraphs.end();
371 while (par != pend) {
372 Layout const & style = par->layout();
373 ParagraphList::const_iterator lastpar = par;
374 ParagraphList::const_iterator send;
376 switch (style.latextype) {
377 case LATEX_COMMAND: {
378 // The files with which we are working never have more than
379 // one paragraph in a command structure.
380 makeCommand(buf, os, runparams, paragraphs, par);
384 case LATEX_ENVIRONMENT:
385 case LATEX_LIST_ENVIRONMENT:
386 case LATEX_ITEM_ENVIRONMENT: {
387 send = searchEnvironment(par, pend);
388 par = makeEnvironment(buf, os, runparams, paragraphs, par,send);
391 case LATEX_PARAGRAPH:
392 send = searchParagraph(par, pend);
393 par = makeParagraphs(buf, os, runparams, paragraphs, par,send);
395 case LATEX_BIB_ENVIRONMENT:
401 // makeEnvironment may process more than one paragraphs and bypass pend
402 if (distance(lastpar, par) >= distance(lastpar, pend))