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 // 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 os << from_ascii("<" + tag + (attr.empty() ? "" : " " + attr) + ">");
81 taglist.push_back(tag);
86 bool closeTag(odocstream & os, string const & tag)
90 // FIXME Check for proper nesting
92 LYXERR0("Last tag not found when closing `" << tag << "'!");
95 string const & lasttag = taglist.back();
97 LYXERR0("Last tag was `" << lasttag << "' when closing `" << tag << "'!");
101 os << from_ascii("</" + tag + ">");
111 bool openTag(odocstream & os, Layout const & lay)
113 return html::openTag(os, lay.htmltag(), lay.htmlattr());
117 bool closeTag(odocstream & os, Layout const & lay)
119 return html::closeTag(os, lay.htmltag());
123 bool openLabelTag(odocstream & os, Layout const & lay)
125 return html::openTag(os, lay.htmllabel(), lay.htmllabelattr());
129 bool closeLabelTag(odocstream & os, Layout const & lay)
131 return html::closeTag(os, lay.htmllabel());
135 bool openItemTag(odocstream & os, Layout const & lay)
137 return html::openTag(os, lay.htmlitem(), lay.htmlitemattr());
141 bool closeItemTag(odocstream & os, Layout const & lay)
143 return html::closeTag(os, lay.htmlitem());
146 ParagraphList::const_iterator searchParagraphHtml(
147 ParagraphList::const_iterator p,
148 ParagraphList::const_iterator const & pend)
150 for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
157 ParagraphList::const_iterator searchEnvironmentHtml(
158 ParagraphList::const_iterator const pstart,
159 ParagraphList::const_iterator const & pend)
161 ParagraphList::const_iterator p = pstart;
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())
186 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
188 OutputParams const & runparams,
190 ParagraphList::const_iterator const & pbegin,
191 ParagraphList::const_iterator const & pend)
193 ParagraphList::const_iterator const begin = text.paragraphs().begin();
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.
204 // FIXME Should we really allow anything other than 'p' here?
206 // If we are already in a paragraph, and this is the first one, then we
207 // do not want to open the paragraph tag.
209 (par == pbegin && runparams.html_in_par) ? false : openTag(os, lay);
210 docstring const deferred = par->simpleLyXHTMLOnePar(buf, os, runparams,
211 text.outerFont(distance(begin, par)));
213 // We want to issue the closing tag if either:
214 // (i) We opened it, and either html_in_par is false,
215 // or we're not in the last paragraph, anyway.
216 // (ii) We didn't open it and html_in_par is true,
217 // but we are in the first par, and there is a next par.
218 ParagraphList::const_iterator nextpar = par;
220 bool const needClose =
221 (opened && (!runparams.html_in_par || nextpar != pend))
222 || (!opened && runparams.html_in_par && par == pbegin && nextpar != pend);
227 if (!deferred.empty())
228 os << deferred << '\n';
234 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
236 OutputParams const & runparams,
238 ParagraphList::const_iterator const & pbegin,
239 ParagraphList::const_iterator const & pend)
241 os << "<h2 class='bibliography'>"
242 << pbegin->layout().labelstring(false)
244 << "<div class='bibliography'>\n";
245 makeParagraphs(buf, os, runparams, text, pbegin, pend);
252 bool isNormalEnv(Layout const & lay)
254 return lay.latextype == LATEX_ENVIRONMENT;
258 ParagraphList::const_iterator makeEnvironmentHtml(Buffer const & buf,
260 OutputParams const & runparams,
262 ParagraphList::const_iterator const & pbegin,
263 ParagraphList::const_iterator const & pend)
265 ParagraphList::const_iterator const begin = text.paragraphs().begin();
266 ParagraphList::const_iterator par = pbegin;
267 Layout const & bstyle = par->layout();
268 depth_type const origdepth = pbegin->params().depth();
270 // Open tag for this environment
271 bool const main_tag_opened = openTag(os, bstyle);
274 // we will on occasion need to remember a layout from before.
275 Layout const * lastlay = 0;
277 while (par != pend) {
278 Layout const & style = par->layout();
279 // the counter only gets stepped if we're in some kind of list,
280 // or if it's the first time through.
281 if (!style.counter.empty() && (par == pbegin || !isNormalEnv(style)))
282 buf.params().documentClass().counters().step(style.counter);
283 ParagraphList::const_iterator send;
284 // this will be positive, if we want to skip the initial word
285 // (if it's been taken for the label).
288 switch (style.latextype) {
289 case LATEX_ENVIRONMENT:
290 case LATEX_LIST_ENVIRONMENT:
291 case LATEX_ITEM_ENVIRONMENT: {
292 // There are two possiblities in this case.
293 // One is that we are still in the environment in which we
294 // started---which we will be if the depth is the same.
295 if (par->params().depth() == origdepth) {
296 LASSERT(bstyle == style, /* */);
298 closeItemTag(os, *lastlay);
301 bool item_tag_opened = false;
302 bool const labelfirst = style.htmllabelfirst();
303 bool madelabel = false;
304 if (isNormalEnv(style)) {
305 // in this case, we print the label only for the first
306 // paragraph (as in a theorem).
307 item_tag_opened = openItemTag(os, style);
308 if (par == pbegin && style.htmllabel() != "NONE") {
309 docstring const lbl =
310 pbegin->expandLabel(style, buf.params(), false);
312 bool const label_tag_opened = openLabelTag(os, style);
314 if (label_tag_opened)
315 closeLabelTag(os, style);
319 } else { // some kind of list
321 item_tag_opened = openItemTag(os, style);
322 if (style.labeltype == LABEL_MANUAL
323 && style.htmllabel() != "NONE") {
324 madelabel = openLabelTag(os, style);
325 sep = par->firstWordLyXHTML(os, runparams);
327 closeLabelTag(os, style);
330 else if (style.labeltype != LABEL_NO_LABEL
331 && style.htmllabel() != "NONE") {
332 madelabel = openLabelTag(os, style);
333 os << par->expandLabel(style, buf.params(), false);
335 closeLabelTag(os, style);
339 item_tag_opened = openItemTag(os, style);
341 os << "<span class='" << style.name() << "inneritem'>";
343 par->simpleLyXHTMLOnePar(buf, os, runparams,
344 text.outerFont(distance(begin, par)), sep);
345 if (!isNormalEnv(style) && !labelfirst && madelabel)
348 if (item_tag_opened) {
349 // We may not want to close the tag yet, in particular,
350 // if we're not at the end...
352 // and are doing items...
353 && style.latextype == LATEX_ITEM_ENVIRONMENT
354 // and if the depth has changed...
355 && par->params().depth() != origdepth) {
356 // then we'll save this layout for later, and close it when
357 // we get another item.
360 closeItemTag(os, style);
364 // The other possibility is that the depth has increased, in which
365 // case we need to recurse.
367 send = searchEnvironmentHtml(par, pend);
368 par = makeEnvironmentHtml(buf, os, runparams, text, par, send);
372 case LATEX_PARAGRAPH:
373 send = searchParagraphHtml(par, pend);
374 par = makeParagraphs(buf, os, runparams, text, par, send);
377 case LATEX_BIB_ENVIRONMENT:
380 par = makeParagraphs(buf, os, runparams, text, par, send);
390 closeItemTag(os, *lastlay);
392 closeTag(os, bstyle);
398 void makeCommand(Buffer const & buf,
400 OutputParams const & runparams,
402 ParagraphList::const_iterator const & pbegin)
404 Layout const & style = pbegin->layout();
405 if (!style.counter.empty())
406 buf.params().documentClass().counters().step(style.counter);
408 bool const main_tag_opened = openTag(os, style);
410 // Label around sectioning number:
411 // FIXME Probably need to account for LABEL_MANUAL
412 if (style.labeltype != LABEL_NO_LABEL) {
413 bool const label_tag_opened = openLabelTag(os, style);
414 os << pbegin->expandLabel(style, buf.params(), false);
415 if (label_tag_opened)
416 closeLabelTag(os, style);
417 // Otherwise the label might run together with the text
421 ParagraphList::const_iterator const begin = text.paragraphs().begin();
422 pbegin->simpleLyXHTMLOnePar(buf, os, runparams,
423 text.outerFont(distance(begin, pbegin)));
429 } // end anonymous namespace
432 void xhtmlParagraphs(Text const & text,
435 OutputParams const & runparams)
437 ParagraphList const & paragraphs = text.paragraphs();
438 ParagraphList::const_iterator par = paragraphs.begin();
439 ParagraphList::const_iterator pend = paragraphs.end();
441 OutputParams ourparams = runparams;
442 while (par != pend) {
443 Layout const & style = par->layout();
444 ParagraphList::const_iterator lastpar = par;
445 ParagraphList::const_iterator send;
447 switch (style.latextype) {
448 case LATEX_COMMAND: {
449 // The files with which we are working never have more than
450 // one paragraph in a command structure.
452 // if (ourparams.html_in_par)
453 // fix it so we don't get sections inside standard, e.g.
454 // note that we may then need to make runparams not const, so we
455 // can communicate that back.
456 // FIXME Maybe this fix should be in the routines themselves, in case
457 // they are called from elsewhere.
458 makeCommand(buf, os, ourparams, text, par);
462 case LATEX_ENVIRONMENT:
463 case LATEX_LIST_ENVIRONMENT:
464 case LATEX_ITEM_ENVIRONMENT: {
465 // FIXME Same fix here.
466 send = searchEnvironmentHtml(par, pend);
467 par = makeEnvironmentHtml(buf, os, ourparams, text, par, send);
470 case LATEX_BIB_ENVIRONMENT: {
471 // FIXME Same fix here.
472 send = searchEnvironmentHtml(par, pend);
473 par = makeBibliography(buf, os, ourparams, text, par, send);
476 case LATEX_PARAGRAPH:
477 send = searchParagraphHtml(par, pend);
478 par = makeParagraphs(buf, os, ourparams, text, par, send);
482 // makeEnvironment may process more than one paragraphs and bypass pend
483 if (distance(lastpar, par) >= distance(lastpar, pend))