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"
37 using namespace lyx::support;
43 docstring escapeChar(char_type c)
67 // escape what needs escaping
68 docstring htmlize(docstring const & str) {
70 docstring::const_iterator it = str.begin();
71 docstring::const_iterator en = str.end();
72 for (; it != en; ++it)
78 bool isFontTag(string const & s)
80 return s == "em" || s == "strong"; // others?
84 ////////////////////////////////////////////////////////////////
88 ////////////////////////////////////////////////////////////////
90 XHTMLStream::XHTMLStream(odocstream & os)
95 void XHTMLStream::cr()
102 bool XHTMLStream::closeFontTags()
104 // first, we close any open font tags we can close
105 StartTag curtag = tag_stack_.back();
106 while (isFontTag(curtag.tag_)) {
107 os_ << "</" << curtag.tag_ << ">";
108 tag_stack_.pop_back();
109 if (tag_stack_.empty())
110 // this probably shouldn't happen, since then the
111 // font tags weren't in any other tag. but that
112 // problem will likely be caught elsewhere.
114 curtag = tag_stack_.back();
116 // so we've hit a non-font tag. let's see if any of the
117 // remaining tags are font tags.
118 TagStack::const_iterator it = tag_stack_.begin();
119 TagStack::const_iterator en = tag_stack_.end();
120 bool noFontTags = true;
121 for (; it != en; ++it) {
122 if (isFontTag(it->tag_)) {
123 LYXERR0("Font tag `" << it->tag_ << "' still open in closeFontTags().");
131 void XHTMLStream::clearTagDeque()
133 while (!pending_tags_.empty()) {
134 StartTag const & tag = pending_tags_.front();
136 os_ << "<" << tag.tag_ << " " << tag.attr_ << ">";
137 tag_stack_.push_back(tag);
138 pending_tags_.pop_front();
142 XHTMLStream & XHTMLStream::operator<<(docstring const & d)
144 // I'm tempted to make sure here that there are no tags in the input
152 XHTMLStream & XHTMLStream::operator<<(char_type c)
155 os_ << escapeChar(c);
161 XHTMLStream & XHTMLStream::operator<<(StartTag const & tag)
163 pending_tags_.push_back(tag);
170 XHTMLStream & XHTMLStream::operator<<(CompTag const & tag)
174 os_ << "<" << tag.tag_ << " " << tag.attr_ << " />";
179 bool XHTMLStream::isTagOpen(string const & stag)
181 TagStack::const_iterator sit = tag_stack_.begin();
182 TagStack::const_iterator const sen = tag_stack_.end();
183 for (; sit != sen; ++sit)
184 // we could check for the
185 if (sit->tag_ == stag)
191 // this is complicated, because we want to make sure that
192 // everything is properly nested. the code ought to make
193 // sure of that, but we won't assert (yet) if we run into
194 // a problem. we'll just output error messages and try our
195 // best to make things work.
196 XHTMLStream & XHTMLStream::operator<<(EndTag const & etag)
198 // first make sure we're not closing an empty tag
199 if (!pending_tags_.empty()) {
200 StartTag const & stag = pending_tags_.back();
201 if (etag.tag_ == stag.tag_) {
202 // we have <tag></tag>, so we discard it and remove it
203 // from the pending_tags_.
204 pending_tags_.pop_back();
207 // there is a pending tag that isn't the one we are trying
209 // is this tag itself pending?
210 // non-const iterators because we may call erase().
211 TagDeque::iterator dit = pending_tags_.begin();
212 TagDeque::iterator const den = pending_tags_.end();
213 for (; dit != den; ++dit) {
214 if (dit->tag_ == etag.tag_) {
215 // it was pending, so we just erase it
216 LYXERR0("Tried to close pending tag `" << etag.tag_
217 << "' when other tags were pending. Tag discarded.");
218 pending_tags_.erase(dit);
222 // so etag isn't itself pending. is it even open?
223 if (!isTagOpen(etag.tag_)) {
224 LYXERR0("Tried to close `" << etag.tag_
225 << "' when tag was not open. Tag discarded.");
228 // ok, so etag is open.
229 // our strategy will be as below: we will do what we need to
230 // do to close this tag.
231 LYXERR0("Closing tag `" << etag.tag_
232 << "' when other tags are pending. Discarded pending tags:");
233 for (dit = pending_tags_.begin(); dit != den; ++dit)
235 // clear the pending tags...
236 pending_tags_.clear();
237 // ...and then just fall through.
240 // is the tag we are closing the last one we opened?
241 if (etag.tag_ == tag_stack_.back().tag_) {
243 os_ << "</" << etag.tag_ << ">";
244 // ...and forget about it
245 tag_stack_.pop_back();
249 // we are trying to close a tag other than the one last opened.
250 // let's first see if this particular tag is still open somehow.
251 if (!isTagOpen(etag.tag_)) {
252 LYXERR0("Tried to close `" << etag.tag_
253 << "' when tag was not open. Tag discarded.");
257 // so the tag was opened, but other tags have been opened since
258 // and not yet closed.
259 // if it's a font tag, though...
260 if (isFontTag(etag.tag_)) {
261 // it won't be a problem if the other tags open since this one
262 // are also font tags.
263 TagStack::const_reverse_iterator rit = tag_stack_.rbegin();
264 TagStack::const_reverse_iterator ren = tag_stack_.rend();
265 for (; rit != ren; ++rit) {
266 if (!isFontTag(rit->tag_)) {
267 // we'll just leave it and, presumably, have to close it later.
268 LYXERR0("Unable to close font tag `" << etag.tag_
269 << "' due to open non-font tags.");
275 // <em>this is <strong>bold
276 // and are being asked to closed em. we want:
277 // <em>this is <strong>bold</strong></em><strong>
278 // first, we close the intervening tags...
279 StartTag curtag = tag_stack_.back();
280 // ...remembering them in a stack.
282 while (curtag.tag_ != etag.tag_) {
283 os_ << "</" << curtag.tag_ << ">";
284 fontstack.push_back(curtag);
285 tag_stack_.pop_back();
286 curtag = tag_stack_.back();
288 // now close our tag...
289 os_ << "</" << etag.tag_ << ">";
290 // ...and restore the other tags.
291 rit = fontstack.rbegin();
292 ren = fontstack.rend();
293 for (; rit != ren; ++rit)
294 pending_tags_.push_back(*rit);
298 // it wasn't a font tag.
299 // so other tags were opened before this one and not properly closed.
300 // so we'll close them, too. that may cause other issues later, but it
301 // at least guarantees proper nesting.
302 LYXERR0("Closing tag `" << etag.tag_
303 << "' when other tags are open, namely:");
304 StartTag curtag = tag_stack_.back();
305 while (curtag.tag_ != etag.tag_) {
306 LYXERR0(curtag.tag_);
307 os_ << "</" << curtag.tag_ << ">";
308 tag_stack_.pop_back();
309 curtag = tag_stack_.back();
311 // curtag is now the one we actually want.
312 os_ << "</" << curtag.tag_ << ">";
313 tag_stack_.pop_back();
319 ///////////////////////////////////////////////////////////////
320 // OLD STUFF to be replaced
322 // FIXME This needs to be protected somehow.
323 static vector<string> taglist;
325 bool openTag(odocstream & os, string const & tag, string const & attr)
329 os << from_ascii("<" + tag + (attr.empty() ? "" : " " + attr) + ">");
330 taglist.push_back(tag);
335 bool closeTag(odocstream & os, string const & tag)
339 // FIXME Check for proper nesting
340 if (taglist.empty()){
341 LYXERR0("Last tag not found when closing `" << tag << "'!");
344 string const & lasttag = taglist.back();
345 if (lasttag != tag) {
346 LYXERR0("Last tag was `" << lasttag << "' when closing `" << tag << "'!");
350 os << from_ascii("</" + tag + ">");
360 bool openTag(odocstream & os, Layout const & lay)
362 return html::openTag(os, lay.htmltag(), lay.htmlattr());
366 bool closeTag(odocstream & os, Layout const & lay)
368 return html::closeTag(os, lay.htmltag());
372 bool openLabelTag(odocstream & os, Layout const & lay)
374 return html::openTag(os, lay.htmllabeltag(), lay.htmllabelattr());
378 bool closeLabelTag(odocstream & os, Layout const & lay)
380 return html::closeTag(os, lay.htmllabeltag());
384 bool openItemTag(odocstream & os, Layout const & lay)
386 return html::openTag(os, lay.htmlitemtag(), lay.htmlitemattr());
390 bool closeItemTag(odocstream & os, Layout const & lay)
392 return html::closeTag(os, lay.htmlitemtag());
395 // end of old stuff to be replaced
396 ///////////////////////////////////////////////////////////////
398 ParagraphList::const_iterator searchParagraphHtml(
399 ParagraphList::const_iterator p,
400 ParagraphList::const_iterator const & pend)
402 for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
409 ParagraphList::const_iterator searchEnvironmentHtml(
410 ParagraphList::const_iterator const pstart,
411 ParagraphList::const_iterator const & pend)
413 ParagraphList::const_iterator p = pstart;
414 Layout const & bstyle = p->layout();
415 size_t const depth = p->params().depth();
416 for (++p; p != pend; ++p) {
417 Layout const & style = p->layout();
418 // It shouldn't happen that e.g. a section command occurs inside
419 // a quotation environment, at a higher depth, but as of 6/2009,
420 // it can happen. We pretend that it's just at lowest depth.
421 if (style.latextype == LATEX_COMMAND)
423 // If depth is down, we're done
424 if (p->params().depth() < depth)
426 // If depth is up, we're not done
427 if (p->params().depth() > depth)
429 // Now we know we are at the same depth
430 if (style.latextype == LATEX_PARAGRAPH
431 || style.latexname() != bstyle.latexname())
438 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
440 OutputParams const & runparams,
442 ParagraphList::const_iterator const & pbegin,
443 ParagraphList::const_iterator const & pend)
445 ParagraphList::const_iterator const begin = text.paragraphs().begin();
446 ParagraphList::const_iterator par = pbegin;
447 for (; par != pend; ++par) {
448 Layout const & lay = par->layout();
449 if (!lay.counter.empty())
450 buf.params().documentClass().counters().step(lay.counter);
451 // FIXME We should see if there's a label to be output and
452 // do something with it.
456 // FIXME Should we really allow anything other than 'p' here?
458 // If we are already in a paragraph, and this is the first one, then we
459 // do not want to open the paragraph tag.
461 (par == pbegin && runparams.html_in_par) ? false : openTag(os, lay);
462 docstring const deferred = par->simpleLyXHTMLOnePar(buf, os, runparams,
463 text.outerFont(distance(begin, par)));
465 // We want to issue the closing tag if either:
466 // (i) We opened it, and either html_in_par is false,
467 // or we're not in the last paragraph, anyway.
468 // (ii) We didn't open it and html_in_par is true,
469 // but we are in the first par, and there is a next par.
470 ParagraphList::const_iterator nextpar = par;
472 bool const needClose =
473 (opened && (!runparams.html_in_par || nextpar != pend))
474 || (!opened && runparams.html_in_par && par == pbegin && nextpar != pend);
479 if (!deferred.empty())
480 os << deferred << '\n';
486 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
488 OutputParams const & runparams,
490 ParagraphList::const_iterator const & pbegin,
491 ParagraphList::const_iterator const & pend)
493 os << "<h2 class='bibliography'>"
494 << pbegin->layout().labelstring(false)
496 << "<div class='bibliography'>\n";
497 makeParagraphs(buf, os, runparams, text, pbegin, pend);
504 bool isNormalEnv(Layout const & lay)
506 return lay.latextype == LATEX_ENVIRONMENT;
510 ParagraphList::const_iterator makeEnvironmentHtml(Buffer const & buf,
512 OutputParams const & runparams,
514 ParagraphList::const_iterator const & pbegin,
515 ParagraphList::const_iterator const & pend)
517 ParagraphList::const_iterator const begin = text.paragraphs().begin();
518 ParagraphList::const_iterator par = pbegin;
519 Layout const & bstyle = par->layout();
520 depth_type const origdepth = pbegin->params().depth();
522 // Open tag for this environment
523 bool const main_tag_opened = openTag(os, bstyle);
526 // we will on occasion need to remember a layout from before.
527 Layout const * lastlay = 0;
529 while (par != pend) {
530 Layout const & style = par->layout();
531 // the counter only gets stepped if we're in some kind of list,
532 // or if it's the first time through.
533 if (!style.counter.empty() && (par == pbegin || !isNormalEnv(style)))
534 buf.params().documentClass().counters().step(style.counter);
535 ParagraphList::const_iterator send;
536 // this will be positive, if we want to skip the initial word
537 // (if it's been taken for the label).
540 switch (style.latextype) {
541 case LATEX_ENVIRONMENT:
542 case LATEX_LIST_ENVIRONMENT:
543 case LATEX_ITEM_ENVIRONMENT: {
544 // There are two possiblities in this case.
545 // One is that we are still in the environment in which we
546 // started---which we will be if the depth is the same.
547 if (par->params().depth() == origdepth) {
548 LASSERT(bstyle == style, /* */);
550 closeItemTag(os, *lastlay);
553 bool item_tag_opened = false;
554 bool const labelfirst = style.htmllabelfirst();
555 bool madelabel = false;
556 if (isNormalEnv(style)) {
557 // in this case, we print the label only for the first
558 // paragraph (as in a theorem).
559 item_tag_opened = openItemTag(os, style);
560 if (par == pbegin && style.htmllabeltag() != "NONE") {
561 docstring const lbl =
562 pbegin->expandLabel(style, buf.params(), false);
564 bool const label_tag_opened = openLabelTag(os, style);
566 if (label_tag_opened)
567 closeLabelTag(os, style);
571 } else { // some kind of list
573 item_tag_opened = openItemTag(os, style);
574 if (style.labeltype == LABEL_MANUAL
575 && style.htmllabeltag() != "NONE") {
576 madelabel = openLabelTag(os, style);
577 sep = par->firstWordLyXHTML(os, runparams);
579 closeLabelTag(os, style);
582 else if (style.labeltype != LABEL_NO_LABEL
583 && style.htmllabeltag() != "NONE") {
584 madelabel = openLabelTag(os, style);
585 os << par->expandLabel(style, buf.params(), false);
587 closeLabelTag(os, style);
591 item_tag_opened = openItemTag(os, style);
593 os << "<span class='" << style.name() << "inneritem'>";
595 par->simpleLyXHTMLOnePar(buf, os, runparams,
596 text.outerFont(distance(begin, par)), sep);
597 if (!isNormalEnv(style) && !labelfirst && madelabel)
600 if (item_tag_opened) {
601 // We may not want to close the tag yet, in particular,
602 // if we're not at the end...
604 // and are doing items...
605 && style.latextype == LATEX_ITEM_ENVIRONMENT
606 // and if the depth has changed...
607 && par->params().depth() != origdepth) {
608 // then we'll save this layout for later, and close it when
609 // we get another item.
612 closeItemTag(os, style);
616 // The other possibility is that the depth has increased, in which
617 // case we need to recurse.
619 send = searchEnvironmentHtml(par, pend);
620 par = makeEnvironmentHtml(buf, os, runparams, text, par, send);
624 case LATEX_PARAGRAPH:
625 send = searchParagraphHtml(par, pend);
626 par = makeParagraphs(buf, os, runparams, text, par, send);
629 case LATEX_BIB_ENVIRONMENT:
632 par = makeParagraphs(buf, os, runparams, text, par, send);
642 closeItemTag(os, *lastlay);
644 closeTag(os, bstyle);
650 void makeCommand(Buffer const & buf,
652 OutputParams const & runparams,
654 ParagraphList::const_iterator const & pbegin)
656 Layout const & style = pbegin->layout();
657 if (!style.counter.empty())
658 buf.params().documentClass().counters().step(style.counter);
660 bool const main_tag_opened = openTag(os, style);
662 // Label around sectioning number:
663 // FIXME Probably need to account for LABEL_MANUAL
664 if (style.labeltype != LABEL_NO_LABEL) {
665 bool const label_tag_opened = openLabelTag(os, style);
666 os << pbegin->expandLabel(style, buf.params(), false);
667 if (label_tag_opened)
668 closeLabelTag(os, style);
669 // Otherwise the label might run together with the text
673 ParagraphList::const_iterator const begin = text.paragraphs().begin();
674 pbegin->simpleLyXHTMLOnePar(buf, os, runparams,
675 text.outerFont(distance(begin, pbegin)));
681 } // end anonymous namespace
684 void xhtmlParagraphs(Text const & text,
687 OutputParams const & runparams)
689 ParagraphList const & paragraphs = text.paragraphs();
690 ParagraphList::const_iterator par = paragraphs.begin();
691 ParagraphList::const_iterator pend = paragraphs.end();
693 OutputParams ourparams = runparams;
694 while (par != pend) {
695 Layout const & style = par->layout();
696 ParagraphList::const_iterator lastpar = par;
697 ParagraphList::const_iterator send;
699 switch (style.latextype) {
700 case LATEX_COMMAND: {
701 // The files with which we are working never have more than
702 // one paragraph in a command structure.
704 // if (ourparams.html_in_par)
705 // fix it so we don't get sections inside standard, e.g.
706 // note that we may then need to make runparams not const, so we
707 // can communicate that back.
708 // FIXME Maybe this fix should be in the routines themselves, in case
709 // they are called from elsewhere.
710 makeCommand(buf, os, ourparams, text, par);
714 case LATEX_ENVIRONMENT:
715 case LATEX_LIST_ENVIRONMENT:
716 case LATEX_ITEM_ENVIRONMENT: {
717 // FIXME Same fix here.
718 send = searchEnvironmentHtml(par, pend);
719 par = makeEnvironmentHtml(buf, os, ourparams, text, par, send);
722 case LATEX_BIB_ENVIRONMENT: {
723 // FIXME Same fix here.
724 send = searchEnvironmentHtml(par, pend);
725 par = makeBibliography(buf, os, ourparams, text, par, send);
728 case LATEX_PARAGRAPH:
729 send = searchParagraphHtml(par, pend);
730 par = makeParagraphs(buf, os, ourparams, text, par, send);
734 // makeEnvironment may process more than one paragraphs and bypass pend
735 if (distance(lastpar, par) >= distance(lastpar, pend))