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 // 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 string const tag = lay.htmltag().empty()
120 ? "div" : lay.htmltag();
121 string const attr = lay.htmlattr().empty()
122 ? "class=\"" + to_utf8(lay.name()) + "\"" : lay.htmlattr();
123 return html::openTag(os, tag, attr);
127 bool closeTag(odocstream & os, Layout const & lay)
129 string const tag = lay.htmltag().empty()
130 ? "div" : lay.htmltag();
131 return html::closeTag(os, tag);
135 bool openLabelTag(odocstream & os, Layout const & lay)
137 string const tag = lay.htmllabel().empty()
138 ? "span" : lay.htmllabel();
139 string const attr = lay.htmllabelattr().empty()
140 ? "class=\"" + to_utf8(lay.name()) + "label\"" : lay.htmllabelattr();
141 return html::openTag(os, tag, attr);
145 bool closeLabelTag(odocstream & os, Layout const & lay)
147 string const tag = lay.htmllabel().empty()
148 ? "span" : lay.htmllabel();
149 return html::closeTag(os, tag);
153 bool openItemTag(odocstream & os, Layout const & lay)
155 string const tag = lay.htmlitem().empty()
156 ? "div" : lay.htmlitem();
157 string const attr = lay.htmlitemattr().empty()
158 ? "class=\"" + to_utf8(lay.name()) + "item\"" : lay.htmllabelattr();
159 return html::openTag(os, tag, attr);
163 bool closeItemTag(odocstream & os, Layout const & lay)
165 string const tag = lay.htmlitem().empty()
166 ? "div" : lay.htmlitem();
167 return html::closeTag(os, tag);
170 ParagraphList::const_iterator searchParagraphHtml(
171 ParagraphList::const_iterator p,
172 ParagraphList::const_iterator const & pend)
174 for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
181 ParagraphList::const_iterator searchEnvironmentHtml(
182 ParagraphList::const_iterator const pstart,
183 ParagraphList::const_iterator const & pend)
185 ParagraphList::const_iterator p = pstart;
186 Layout const & bstyle = p->layout();
187 size_t const depth = p->params().depth();
188 for (++p; p != pend; ++p) {
189 Layout const & style = p->layout();
190 // It shouldn't happen that e.g. a section command occurs inside
191 // a quotation environment, at a higher depth, but as of 6/2009,
192 // it can happen. We pretend that it's just at lowest depth.
193 if (style.latextype == LATEX_COMMAND)
195 // If depth is down, we're done
196 if (p->params().depth() < depth)
198 // If depth is up, we're not done
199 if (p->params().depth() > depth)
201 // Now we know we are at the same depth
202 if (style.latextype == LATEX_PARAGRAPH
203 || style.latexname() != bstyle.latexname())
210 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
212 OutputParams const & runparams,
214 ParagraphList::const_iterator const & pbegin,
215 ParagraphList::const_iterator const & pend)
217 ParagraphList::const_iterator const begin = text.paragraphs().begin();
218 ParagraphList::const_iterator par = pbegin;
219 for (; par != pend; ++par) {
220 Layout const & lay = par->layout();
221 if (!lay.counter.empty())
222 buf.params().documentClass().counters().step(lay.counter);
223 // FIXME We should see if there's a label to be output and
224 // do something with it.
227 bool const opened = openTag(os, lay);
228 docstring const deferred = par->simpleLyXHTMLOnePar(buf, os, runparams,
229 text.outerFont(distance(begin, par)));
234 if (!deferred.empty())
235 os << deferred << '\n';
241 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
243 OutputParams const & runparams,
245 ParagraphList::const_iterator const & pbegin,
246 ParagraphList::const_iterator const & pend)
248 os << "<h2 class='bibliography'>"
249 << pbegin->layout().labelstring(false)
251 << "<div class='bibliography'>\n";
252 makeParagraphs(buf, os, runparams, text, pbegin, pend);
259 bool isNormalEnv(Layout const & lay)
261 return lay.latextype == LATEX_ENVIRONMENT;
265 ParagraphList::const_iterator makeEnvironmentHtml(Buffer const & buf,
267 OutputParams const & runparams,
269 ParagraphList::const_iterator const & pbegin,
270 ParagraphList::const_iterator const & pend)
272 ParagraphList::const_iterator const begin = text.paragraphs().begin();
273 ParagraphList::const_iterator par = pbegin;
274 Layout const & bstyle = par->layout();
275 depth_type const origdepth = pbegin->params().depth();
277 // Open tag for this environment
278 bool const main_tag_opened = openTag(os, bstyle);
281 // we will on occasion need to remember a layout from before.
282 Layout const * lastlay = 0;
284 while (par != pend) {
285 Layout const & style = par->layout();
286 // the counter only gets stepped if we're in some kind of list,
287 // or if it's the first time through.
288 if (!style.counter.empty() && (par == pbegin || !isNormalEnv(style)))
289 buf.params().documentClass().counters().step(style.counter);
290 ParagraphList::const_iterator send;
291 // this will be positive, if we want to skip the initial word
292 // (if it's been taken for the label).
295 switch (style.latextype) {
296 case LATEX_ENVIRONMENT:
297 case LATEX_LIST_ENVIRONMENT:
298 case LATEX_ITEM_ENVIRONMENT: {
299 // There are two possiblities in this case.
300 // One is that we are still in the environment in which we
301 // started---which we will be if the depth is the same.
302 if (par->params().depth() == origdepth) {
303 LASSERT(bstyle == style, /* */);
305 closeItemTag(os, *lastlay);
308 bool item_tag_opened = false;
309 bool const labelfirst = style.htmllabelfirst();
310 bool madelabel = false;
311 if (isNormalEnv(style)) {
312 // in this case, we print the label only for the first
313 // paragraph (as in a theorem).
314 item_tag_opened = openItemTag(os, style);
315 if (par == pbegin && style.htmllabel() != "NONE") {
316 docstring const lbl =
317 pbegin->expandLabel(style, buf.params(), false);
319 bool const label_tag_opened = openLabelTag(os, style);
321 if (label_tag_opened)
322 closeLabelTag(os, style);
326 } else { // some kind of list
328 item_tag_opened = openItemTag(os, style);
329 if (style.labeltype == LABEL_MANUAL
330 && style.htmllabel() != "NONE") {
331 madelabel = openLabelTag(os, style);
332 sep = par->firstWordLyXHTML(os, runparams);
334 closeLabelTag(os, style);
337 else if (style.labeltype != LABEL_NO_LABEL
338 && style.htmllabel() != "NONE") {
339 madelabel = openLabelTag(os, style);
340 os << par->expandLabel(style, buf.params(), false);
342 closeLabelTag(os, style);
346 item_tag_opened = openItemTag(os, style);
348 os << "<span class='" << style.name() << "inneritem'>";
350 par->simpleLyXHTMLOnePar(buf, os, runparams,
351 text.outerFont(distance(begin, par)), sep);
352 if (!isNormalEnv(style) && !labelfirst && madelabel)
355 if (item_tag_opened) {
356 // We may not want to close the tag yet, in particular,
357 // if we're not at the end...
359 // and are doing items...
360 && style.latextype == LATEX_ITEM_ENVIRONMENT
361 // and if the depth has changed...
362 && par->params().depth() != origdepth) {
363 // then we'll save this layout for later, and close it when
364 // we get another item.
367 closeItemTag(os, style);
371 // The other possibility is that the depth has increased, in which
372 // case we need to recurse.
374 send = searchEnvironmentHtml(par, pend);
375 par = makeEnvironmentHtml(buf, os, runparams, text, par, send);
379 case LATEX_PARAGRAPH:
380 send = searchParagraphHtml(par, pend);
381 par = makeParagraphs(buf, os, runparams, text, par, send);
384 case LATEX_BIB_ENVIRONMENT:
387 par = makeParagraphs(buf, os, runparams, text, par, send);
397 closeItemTag(os, *lastlay);
399 closeTag(os, bstyle);
405 void makeCommand(Buffer const & buf,
407 OutputParams const & runparams,
409 ParagraphList::const_iterator const & pbegin)
411 Layout const & style = pbegin->layout();
412 if (!style.counter.empty())
413 buf.params().documentClass().counters().step(style.counter);
415 bool const main_tag_opened = openTag(os, style);
417 // Label around sectioning number:
418 // FIXME Probably need to account for LABEL_MANUAL
419 if (style.labeltype != LABEL_NO_LABEL) {
420 bool const label_tag_opened = openLabelTag(os, style);
421 os << pbegin->expandLabel(style, buf.params(), false);
422 if (label_tag_opened)
423 closeLabelTag(os, style);
424 // Otherwise the label might run together with the text
428 ParagraphList::const_iterator const begin = text.paragraphs().begin();
429 pbegin->simpleLyXHTMLOnePar(buf, os, runparams,
430 text.outerFont(distance(begin, pbegin)));
436 } // end anonymous namespace
439 void xhtmlParagraphs(Text const & text,
442 OutputParams const & runparams)
444 ParagraphList const & paragraphs = text.paragraphs();
445 ParagraphList::const_iterator par = paragraphs.begin();
446 ParagraphList::const_iterator pend = paragraphs.end();
448 while (par != pend) {
449 Layout const & style = par->layout();
450 ParagraphList::const_iterator lastpar = par;
451 ParagraphList::const_iterator send;
453 switch (style.latextype) {
454 case LATEX_COMMAND: {
455 // The files with which we are working never have more than
456 // one paragraph in a command structure.
457 makeCommand(buf, os, runparams, text, par);
461 case LATEX_ENVIRONMENT:
462 case LATEX_LIST_ENVIRONMENT:
463 case LATEX_ITEM_ENVIRONMENT: {
464 send = searchEnvironmentHtml(par, pend);
465 par = makeEnvironmentHtml(buf, os, runparams, text, par, send);
468 case LATEX_BIB_ENVIRONMENT: {
469 send = searchEnvironmentHtml(par, pend);
470 par = makeBibliography(buf, os, runparams, text, par, send);
473 case LATEX_PARAGRAPH:
474 send = searchParagraphHtml(par, pend);
475 par = makeParagraphs(buf, os, runparams, text, par, send);
479 // makeEnvironment may process more than one paragraphs and bypass pend
480 if (distance(lastpar, par) >= distance(lastpar, pend))