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 "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"
34 #include <boost/next_prior.hpp>
38 using namespace lyx::support;
44 docstring escapeChar(char_type c)
68 // escape what needs escaping
69 docstring htmlize(docstring const & str) {
71 docstring::const_iterator it = str.begin();
72 docstring::const_iterator en = str.end();
73 for (; it != en; ++it)
78 // FIXME This needs to be protected somehow.
79 static vector<string> taglist;
81 bool openTag(odocstream & os, string const & tag, string const & attr)
85 os << from_ascii("<" + tag + (attr.empty() ? "" : " " + attr) + ">");
86 taglist.push_back(tag);
91 bool closeTag(odocstream & os, string const & tag)
95 // FIXME Check for proper nesting
97 LYXERR0("Last tag not found when closing `" << tag << "'!");
100 string const & lasttag = taglist.back();
101 if (lasttag != tag) {
102 LYXERR0("Last tag was `" << lasttag << "' when closing `" << tag << "'!");
106 os << from_ascii("</" + tag + ">");
116 bool openTag(odocstream & os, Layout const & lay)
118 return html::openTag(os, lay.htmltag(), lay.htmlattr());
122 bool closeTag(odocstream & os, Layout const & lay)
124 return html::closeTag(os, lay.htmltag());
128 bool openLabelTag(odocstream & os, Layout const & lay)
130 return html::openTag(os, lay.htmllabeltag(), lay.htmllabelattr());
134 bool closeLabelTag(odocstream & os, Layout const & lay)
136 return html::closeTag(os, lay.htmllabeltag());
140 bool openItemTag(odocstream & os, Layout const & lay)
142 return html::openTag(os, lay.htmlitemtag(), lay.htmlitemattr());
146 bool closeItemTag(odocstream & os, Layout const & lay)
148 return html::closeTag(os, lay.htmlitemtag());
151 ParagraphList::const_iterator searchParagraphHtml(
152 ParagraphList::const_iterator p,
153 ParagraphList::const_iterator const & pend)
155 for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
162 ParagraphList::const_iterator searchEnvironmentHtml(
163 ParagraphList::const_iterator const pstart,
164 ParagraphList::const_iterator const & pend)
166 ParagraphList::const_iterator p = pstart;
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())
191 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
193 OutputParams const & runparams,
195 ParagraphList::const_iterator const & pbegin,
196 ParagraphList::const_iterator const & pend)
198 ParagraphList::const_iterator const begin = text.paragraphs().begin();
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.
209 // FIXME Should we really allow anything other than 'p' here?
211 // If we are already in a paragraph, and this is the first one, then we
212 // do not want to open the paragraph tag.
214 (par == pbegin && runparams.html_in_par) ? false : openTag(os, lay);
215 docstring const deferred = par->simpleLyXHTMLOnePar(buf, os, runparams,
216 text.outerFont(distance(begin, par)));
218 // We want to issue the closing tag if either:
219 // (i) We opened it, and either html_in_par is false,
220 // or we're not in the last paragraph, anyway.
221 // (ii) We didn't open it and html_in_par is true,
222 // but we are in the first par, and there is a next par.
223 ParagraphList::const_iterator nextpar = par;
225 bool const needClose =
226 (opened && (!runparams.html_in_par || nextpar != pend))
227 || (!opened && runparams.html_in_par && par == pbegin && nextpar != pend);
232 if (!deferred.empty())
233 os << deferred << '\n';
239 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
241 OutputParams const & runparams,
243 ParagraphList::const_iterator const & pbegin,
244 ParagraphList::const_iterator const & pend)
246 os << "<h2 class='bibliography'>"
247 << pbegin->layout().labelstring(false)
249 << "<div class='bibliography'>\n";
250 makeParagraphs(buf, os, runparams, text, pbegin, pend);
257 bool isNormalEnv(Layout const & lay)
259 return lay.latextype == LATEX_ENVIRONMENT;
263 ParagraphList::const_iterator makeEnvironmentHtml(Buffer const & buf,
265 OutputParams const & runparams,
267 ParagraphList::const_iterator const & pbegin,
268 ParagraphList::const_iterator const & pend)
270 ParagraphList::const_iterator const begin = text.paragraphs().begin();
271 ParagraphList::const_iterator par = pbegin;
272 Layout const & bstyle = par->layout();
273 depth_type const origdepth = pbegin->params().depth();
275 // Open tag for this environment
276 bool const main_tag_opened = openTag(os, bstyle);
279 // we will on occasion need to remember a layout from before.
280 Layout const * lastlay = 0;
282 while (par != pend) {
283 Layout const & style = par->layout();
284 // the counter only gets stepped if we're in some kind of list,
285 // or if it's the first time through.
286 if (!style.counter.empty() && (par == pbegin || !isNormalEnv(style)))
287 buf.params().documentClass().counters().step(style.counter);
288 ParagraphList::const_iterator send;
289 // this will be positive, if we want to skip the initial word
290 // (if it's been taken for the label).
293 switch (style.latextype) {
294 case LATEX_ENVIRONMENT:
295 case LATEX_LIST_ENVIRONMENT:
296 case LATEX_ITEM_ENVIRONMENT: {
297 // There are two possiblities in this case.
298 // One is that we are still in the environment in which we
299 // started---which we will be if the depth is the same.
300 if (par->params().depth() == origdepth) {
301 LASSERT(bstyle == style, /* */);
303 closeItemTag(os, *lastlay);
306 bool item_tag_opened = false;
307 bool const labelfirst = style.htmllabelfirst();
308 bool madelabel = false;
309 if (isNormalEnv(style)) {
310 // in this case, we print the label only for the first
311 // paragraph (as in a theorem).
312 item_tag_opened = openItemTag(os, style);
313 if (par == pbegin && style.htmllabeltag() != "NONE") {
314 docstring const lbl =
315 pbegin->expandLabel(style, buf.params(), false);
317 bool const label_tag_opened = openLabelTag(os, style);
319 if (label_tag_opened)
320 closeLabelTag(os, style);
324 } else { // some kind of list
326 item_tag_opened = openItemTag(os, style);
327 if (style.labeltype == LABEL_MANUAL
328 && style.htmllabeltag() != "NONE") {
329 madelabel = openLabelTag(os, style);
330 sep = par->firstWordLyXHTML(os, runparams);
332 closeLabelTag(os, style);
335 else if (style.labeltype != LABEL_NO_LABEL
336 && style.htmllabeltag() != "NONE") {
337 madelabel = openLabelTag(os, style);
338 os << par->expandLabel(style, buf.params(), false);
340 closeLabelTag(os, style);
344 item_tag_opened = openItemTag(os, style);
346 os << "<span class='" << style.name() << "inneritem'>";
348 par->simpleLyXHTMLOnePar(buf, os, runparams,
349 text.outerFont(distance(begin, par)), sep);
350 if (!isNormalEnv(style) && !labelfirst && madelabel)
353 if (item_tag_opened) {
354 // We may not want to close the tag yet, in particular,
355 // if we're not at the end...
357 // and are doing items...
358 && style.latextype == LATEX_ITEM_ENVIRONMENT
359 // and if the depth has changed...
360 && par->params().depth() != origdepth) {
361 // then we'll save this layout for later, and close it when
362 // we get another item.
365 closeItemTag(os, style);
369 // The other possibility is that the depth has increased, in which
370 // case we need to recurse.
372 send = searchEnvironmentHtml(par, pend);
373 par = makeEnvironmentHtml(buf, os, runparams, text, par, send);
377 case LATEX_PARAGRAPH:
378 send = searchParagraphHtml(par, pend);
379 par = makeParagraphs(buf, os, runparams, text, par, send);
382 case LATEX_BIB_ENVIRONMENT:
385 par = makeParagraphs(buf, os, runparams, text, par, send);
395 closeItemTag(os, *lastlay);
397 closeTag(os, bstyle);
403 void makeCommand(Buffer const & buf,
405 OutputParams const & runparams,
407 ParagraphList::const_iterator const & pbegin)
409 Layout const & style = pbegin->layout();
410 if (!style.counter.empty())
411 buf.params().documentClass().counters().step(style.counter);
413 bool const main_tag_opened = openTag(os, style);
415 // Label around sectioning number:
416 // FIXME Probably need to account for LABEL_MANUAL
417 if (style.labeltype != LABEL_NO_LABEL) {
418 bool const label_tag_opened = openLabelTag(os, style);
419 os << pbegin->expandLabel(style, buf.params(), false);
420 if (label_tag_opened)
421 closeLabelTag(os, style);
422 // Otherwise the label might run together with the text
426 ParagraphList::const_iterator const begin = text.paragraphs().begin();
427 pbegin->simpleLyXHTMLOnePar(buf, os, runparams,
428 text.outerFont(distance(begin, pbegin)));
434 } // end anonymous namespace
437 void xhtmlParagraphs(Text const & text,
440 OutputParams const & runparams)
442 ParagraphList const & paragraphs = text.paragraphs();
443 ParagraphList::const_iterator par = paragraphs.begin();
444 ParagraphList::const_iterator pend = paragraphs.end();
446 OutputParams ourparams = runparams;
447 while (par != pend) {
448 Layout const & style = par->layout();
449 ParagraphList::const_iterator lastpar = par;
450 ParagraphList::const_iterator send;
452 switch (style.latextype) {
453 case LATEX_COMMAND: {
454 // The files with which we are working never have more than
455 // one paragraph in a command structure.
457 // if (ourparams.html_in_par)
458 // fix it so we don't get sections inside standard, e.g.
459 // note that we may then need to make runparams not const, so we
460 // can communicate that back.
461 // FIXME Maybe this fix should be in the routines themselves, in case
462 // they are called from elsewhere.
463 makeCommand(buf, os, ourparams, text, par);
467 case LATEX_ENVIRONMENT:
468 case LATEX_LIST_ENVIRONMENT:
469 case LATEX_ITEM_ENVIRONMENT: {
470 // FIXME Same fix here.
471 send = searchEnvironmentHtml(par, pend);
472 par = makeEnvironmentHtml(buf, os, ourparams, text, par, send);
475 case LATEX_BIB_ENVIRONMENT: {
476 // FIXME Same fix here.
477 send = searchEnvironmentHtml(par, pend);
478 par = makeBibliography(buf, os, ourparams, text, par, send);
481 case LATEX_PARAGRAPH:
482 send = searchParagraphHtml(par, pend);
483 par = makeParagraphs(buf, os, ourparams, text, par, send);
487 // makeEnvironment may process more than one paragraphs and bypass pend
488 if (distance(lastpar, par) >= distance(lastpar, pend))