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 do something here.
69 docstring htmlize(docstring const & str) {
73 // FIXME This needs to be protected somehow.
74 static vector<string> taglist;
76 bool openTag(odocstream & os, string const & tag, string const & attr)
80 // FIXME This is completely primitive. We need something
82 // Now do some checks on nesting of tags.
84 if (find(taglist.begin(), taglist.end(), "p") != taglist.end())
86 os << from_ascii("<" + tag + (attr.empty() ? "" : " " + attr) + ">");
87 taglist.push_back(tag);
92 bool closeTag(odocstream & os, string const & tag)
96 // FIXME Check for proper nesting
98 LYXERR0("Last tag not found when closing `" << tag << "'!");
101 string const & lasttag = taglist.back();
102 if (lasttag != tag) {
103 LYXERR0("Last tag was `" << lasttag << "' when closing `" << tag << "'!");
107 os << from_ascii("</" + tag + ">");
117 bool openTag(odocstream & os, Layout const & lay)
119 return html::openTag(os, lay.htmltag(), lay.htmlattr());
123 bool closeTag(odocstream & os, Layout const & lay)
125 return html::closeTag(os, lay.htmltag());
129 bool openLabelTag(odocstream & os, Layout const & lay)
131 return html::openTag(os, lay.htmllabel(), lay.htmllabelattr());
135 bool closeLabelTag(odocstream & os, Layout const & lay)
137 return html::closeTag(os, lay.htmllabel());
141 bool openItemTag(odocstream & os, Layout const & lay)
143 return html::openTag(os, lay.htmlitem(), lay.htmlitemattr());
147 bool closeItemTag(odocstream & os, Layout const & lay)
149 return html::closeTag(os, lay.htmlitem());
152 ParagraphList::const_iterator searchParagraph(
153 ParagraphList::const_iterator p,
154 ParagraphList::const_iterator const & pend)
156 for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
163 ParagraphList::const_iterator searchEnvironment(
164 ParagraphList::const_iterator p,
165 ParagraphList::const_iterator const & pend)
167 Layout const & bstyle = p->layout();
168 size_t const depth = p->params().depth();
169 for (++p; p != pend; ++p) {
170 Layout const & style = p->layout();
171 // It shouldn't happen that e.g. a section command occurs inside
172 // a quotation environment, at a higher depth, but as of 6/2009,
173 // it can happen. We pretend that it's just at lowest depth.
174 if (style.latextype == LATEX_COMMAND)
176 // If depth is down, we're done
177 if (p->params().depth() < depth)
179 // If depth is up, we're not done
180 if (p->params().depth() > depth)
182 // Now we know we are at the same depth
183 if (style.latextype == LATEX_PARAGRAPH
184 || style.latexname() != bstyle.latexname())
192 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
194 OutputParams const & runparams,
195 ParagraphList const & paragraphs,
196 ParagraphList::const_iterator const & pbegin,
197 ParagraphList::const_iterator const & pend)
199 ParagraphList::const_iterator par = pbegin;
200 for (; par != pend; ++par) {
201 Layout const & lay = par->layout();
202 if (!lay.counter.empty())
203 buf.params().documentClass().counters().step(lay.counter);
204 // FIXME We should see if there's a label to be output and
205 // do something with it.
208 bool const opened = openTag(os, lay);
209 docstring const deferred = par->simpleLyXHTMLOnePar(buf, os, runparams,
210 outerFont(distance(paragraphs.begin(), par), paragraphs));
215 if (!deferred.empty())
216 os << deferred << '\n';
222 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
224 OutputParams const & runparams,
225 ParagraphList const & paragraphs,
226 ParagraphList::const_iterator const & pbegin,
227 ParagraphList::const_iterator const & pend)
229 os << "<h2 class='bibliography'>"
230 << pbegin->layout().labelstring(false)
232 << "<div class='bibliography'>\n";
233 makeParagraphs(buf, os, runparams, paragraphs, pbegin, pend);
239 ParagraphList::const_iterator makeEnvironment(Buffer const & buf,
241 OutputParams const & runparams,
242 ParagraphList const & paragraphs,
243 ParagraphList::const_iterator const & pbegin,
244 ParagraphList::const_iterator const & pend)
246 ParagraphList::const_iterator par = pbegin;
247 Layout const & bstyle = par->layout();
248 depth_type const origdepth = pbegin->params().depth();
250 // Open tag for this environment
251 bool const main_tag_opened = openTag(os, bstyle);
254 // we will on occasion need to remember a layout from before.
255 Layout const * lastlay = 0;
257 while (par != pend) {
258 Layout const & style = par->layout();
259 if (!style.counter.empty())
260 buf.params().documentClass().counters().step(style.counter);
261 ParagraphList::const_iterator send;
262 // this will be positive, if we want to skip the initial word
263 // (if it's been taken for the label).
266 switch (style.latextype) {
267 case LATEX_ENVIRONMENT:
268 case LATEX_LIST_ENVIRONMENT:
269 case LATEX_ITEM_ENVIRONMENT: {
270 // There are two possiblities in this case.
271 // One is that we are still in the environment in which we
272 // started---which we will be if the depth is the same.
273 if (par->params().depth() == origdepth) {
274 Layout const & cstyle = par->layout();
276 closeItemTag(os, *lastlay);
279 bool const labelfirst = cstyle.htmllabelfirst();
280 bool item_tag_opened = false;
282 item_tag_opened = openItemTag(os, cstyle);
283 if (cstyle.labeltype == LABEL_MANUAL) {
284 bool const label_tag_opened = openLabelTag(os, cstyle);
285 sep = par->firstWordLyXHTML(os, runparams);
286 if (label_tag_opened)
287 closeLabelTag(os, cstyle);
290 // FIXME Why did I put that first condition??
291 else if (style.latextype == LATEX_ENVIRONMENT
292 && style.labeltype != LABEL_NO_LABEL) {
293 bool const label_tag_opened = openLabelTag(os, cstyle);
294 os << pbegin->expandLabel(style, buf.params(), false);
295 if (label_tag_opened)
296 closeLabelTag(os, cstyle);
300 item_tag_opened = openItemTag(os, cstyle);
302 os << "<span class='item'>";
303 par->simpleLyXHTMLOnePar(buf, os, runparams,
304 outerFont(distance(paragraphs.begin(), par), paragraphs), sep);
308 if (item_tag_opened) {
309 // We may not want to close the tag yet, in particular,
310 // if we're not at the end...
312 // and are doing items...
313 && style.latextype == LATEX_ITEM_ENVIRONMENT
314 // and if the depth has changed...
315 && par->params().depth() != origdepth) {
316 // then we'll save this layout for later, and close it when
317 // we get another item.
320 closeItemTag(os, cstyle);
325 // The other possibility is that the depth has increased, in which
326 // case we need to recurse.
328 send = searchEnvironment(par, pend);
329 par = makeEnvironment(buf, os, runparams, paragraphs, par, send);
333 case LATEX_PARAGRAPH:
334 send = searchParagraph(par, pend);
335 par = makeParagraphs(buf, os, runparams, paragraphs, par, send);
338 case LATEX_BIB_ENVIRONMENT:
341 par = makeParagraphs(buf, os, runparams, paragraphs, par, send);
351 closeItemTag(os, *lastlay);
353 closeTag(os, bstyle);
359 void makeCommand(Buffer const & buf,
361 OutputParams const & runparams,
362 ParagraphList const & paragraphs,
363 ParagraphList::const_iterator const & pbegin)
365 Layout const & style = pbegin->layout();
366 if (!style.counter.empty())
367 buf.params().documentClass().counters().step(style.counter);
369 bool const main_tag_opened = openTag(os, style);
371 // Label around sectioning number:
372 // FIXME Probably need to account for LABEL_MANUAL
373 if (style.labeltype != LABEL_NO_LABEL) {
374 bool const label_tag_opened = openLabelTag(os, style);
375 os << pbegin->expandLabel(style, buf.params(), false);
376 if (label_tag_opened)
377 closeLabelTag(os, style);
378 // Otherwise the label might run together with the text
382 pbegin->simpleLyXHTMLOnePar(buf, os, runparams,
383 outerFont(distance(paragraphs.begin(), pbegin), paragraphs));
389 } // end anonymous namespace
392 void xhtmlParagraphs(ParagraphList const & paragraphs,
395 OutputParams const & runparams)
397 ParagraphList::const_iterator par = paragraphs.begin();
398 ParagraphList::const_iterator pend = paragraphs.end();
400 while (par != pend) {
401 Layout const & style = par->layout();
402 ParagraphList::const_iterator lastpar = par;
403 ParagraphList::const_iterator send;
405 switch (style.latextype) {
406 case LATEX_COMMAND: {
407 // The files with which we are working never have more than
408 // one paragraph in a command structure.
409 makeCommand(buf, os, runparams, paragraphs, par);
413 case LATEX_ENVIRONMENT:
414 case LATEX_LIST_ENVIRONMENT:
415 case LATEX_ITEM_ENVIRONMENT: {
416 send = searchEnvironment(par, pend);
417 par = makeEnvironment(buf, os, runparams, paragraphs, par, send);
420 case LATEX_BIB_ENVIRONMENT: {
421 send = searchEnvironment(par, pend);
422 par = makeBibliography(buf, os, runparams, paragraphs, par, send);
425 case LATEX_PARAGRAPH:
426 send = searchParagraph(par, pend);
427 par = makeParagraphs(buf, os, runparams, paragraphs, par, send);
431 // makeEnvironment may process more than one paragraphs and bypass pend
432 if (distance(lastpar, par) >= distance(lastpar, pend))