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 string escapeChar(char c)
102 // escape what needs escaping
103 string htmlize(string const & str) {
105 string::const_iterator it = str.begin();
106 string::const_iterator en = str.end();
107 for (; it != en; ++it)
108 d << escapeChar(*it);
113 string cleanAttr(string const & str)
116 string::const_iterator it = str.begin();
117 string::const_iterator en = str.end();
118 for (; it != en; ++it)
119 newname += isalnum(*it) ? *it : '_';
124 docstring cleanAttr(docstring const & str)
127 docstring::const_iterator it = str.begin();
128 docstring::const_iterator en = str.end();
129 for (; it != en; ++it)
138 bool isFontTag(string const & s)
140 return s == "em" || s == "strong"; // others?
145 docstring StartTag::asTag() const
147 string output = "<" + tag_;
149 output += " " + html::htmlize(attr_);
151 return from_utf8(output);
155 docstring StartTag::asEndTag() const
157 string output = "</" + tag_ + ">";
158 return from_utf8(output);
162 docstring EndTag::asEndTag() const
164 string output = "</" + tag_ + ">";
165 return from_utf8(output);
169 docstring CompTag::asTag() const
171 string output = "<" + tag_;
173 output += " " + html::htmlize(attr_);
175 return from_utf8(output);
179 ////////////////////////////////////////////////////////////////
183 ////////////////////////////////////////////////////////////////
185 XHTMLStream::XHTMLStream(odocstream & os)
186 : os_(os), nextraw_(false)
190 void XHTMLStream::cr()
193 os_ << from_ascii("\n");
197 void XHTMLStream::writeError(std::string const & s)
200 os_ << from_utf8("<!-- Output Error: " + s + " -->");
204 bool XHTMLStream::closeFontTags()
206 if (tag_stack_.empty())
208 // first, we close any open font tags we can close
209 StartTag curtag = tag_stack_.back();
210 while (html::isFontTag(curtag.tag_)) {
211 os_ << curtag.asEndTag();
212 tag_stack_.pop_back();
213 if (tag_stack_.empty())
214 // this probably shouldn't happen, since then the
215 // font tags weren't in any other tag. but that
216 // problem will likely be caught elsewhere.
218 curtag = tag_stack_.back();
220 // so we've hit a non-font tag. let's see if any of the
221 // remaining tags are font tags.
222 TagStack::const_iterator it = tag_stack_.begin();
223 TagStack::const_iterator en = tag_stack_.end();
224 bool noFontTags = true;
225 for (; it != en; ++it) {
226 if (html::isFontTag(it->tag_)) {
227 writeError("Font tag `" + it->tag_ + "' still open in closeFontTags().");
235 void XHTMLStream::clearTagDeque()
237 while (!pending_tags_.empty()) {
238 StartTag const & tag = pending_tags_.front();
242 tag_stack_.push_back(tag);
243 pending_tags_.pop_front();
248 XHTMLStream & XHTMLStream::operator<<(docstring const & d)
255 os_ << html::htmlize(d);
260 XHTMLStream & XHTMLStream::operator<<(const char * s)
263 docstring const d = from_ascii(s);
268 os_ << html::htmlize(d);
273 XHTMLStream & XHTMLStream::operator<<(char_type c)
280 os_ << html::escapeChar(c);
285 XHTMLStream & XHTMLStream::operator<<(NextRaw const &)
292 XHTMLStream & XHTMLStream::operator<<(StartTag const & tag)
294 if (tag.tag_.empty())
296 pending_tags_.push_back(tag);
303 XHTMLStream & XHTMLStream::operator<<(CompTag const & tag)
305 if (tag.tag_.empty())
315 bool XHTMLStream::isTagOpen(string const & stag)
317 TagStack::const_iterator sit = tag_stack_.begin();
318 TagStack::const_iterator const sen = tag_stack_.end();
319 for (; sit != sen; ++sit)
320 // we could check for the
321 if (sit->tag_ == stag)
327 // this is complicated, because we want to make sure that
328 // everything is properly nested. the code ought to make
329 // sure of that, but we won't assert (yet) if we run into
330 // a problem. we'll just output error messages and try our
331 // best to make things work.
332 XHTMLStream & XHTMLStream::operator<<(EndTag const & etag)
334 if (etag.tag_.empty())
336 // first make sure we're not closing an empty tag
337 if (!pending_tags_.empty()) {
338 StartTag const & stag = pending_tags_.back();
339 if (etag.tag_ == stag.tag_) {
340 // we have <tag></tag>, so we discard it and remove it
341 // from the pending_tags_.
342 pending_tags_.pop_back();
345 // there is a pending tag that isn't the one we are trying
347 // is this tag itself pending?
348 // non-const iterators because we may call erase().
349 TagDeque::iterator dit = pending_tags_.begin();
350 TagDeque::iterator const den = pending_tags_.end();
351 for (; dit != den; ++dit) {
352 if (dit->tag_ == etag.tag_) {
353 // it was pending, so we just erase it
354 writeError("Tried to close pending tag `" + etag.tag_
355 + "' when other tags were pending. Last pending tag is `"
356 + pending_tags_.back().tag_ + "'. Tag discarded.");
357 pending_tags_.erase(dit);
361 // so etag isn't itself pending. is it even open?
362 if (!isTagOpen(etag.tag_)) {
363 writeError("Tried to close `" + etag.tag_
364 + "' when tag was not open. Tag discarded.");
367 // ok, so etag is open.
368 // our strategy will be as below: we will do what we need to
369 // do to close this tag.
370 string estr = "Closing tag `" + etag.tag_
371 + "' when other tags are pending. Discarded pending tags:\n";
372 for (dit = pending_tags_.begin(); dit != den; ++dit)
373 estr += dit->tag_ + "\n";
375 // clear the pending tags...
376 pending_tags_.clear();
377 // ...and then just fall through.
380 // is the tag we are closing the last one we opened?
381 if (etag.tag_ == tag_stack_.back().tag_) {
383 os_ << etag.asEndTag();
385 // ...and forget about it
386 tag_stack_.pop_back();
390 // we are trying to close a tag other than the one last opened.
391 // let's first see if this particular tag is still open somehow.
392 if (!isTagOpen(etag.tag_)) {
393 writeError("Tried to close `" + etag.tag_
394 + "' when tag was not open. Tag discarded.");
398 // so the tag was opened, but other tags have been opened since
399 // and not yet closed.
400 // if it's a font tag, though...
401 if (html::isFontTag(etag.tag_)) {
402 // it won't be a problem if the other tags open since this one
403 // are also font tags.
404 TagStack::const_reverse_iterator rit = tag_stack_.rbegin();
405 TagStack::const_reverse_iterator ren = tag_stack_.rend();
406 for (; rit != ren; ++rit) {
407 if (rit->tag_ == etag.tag_)
409 if (!html::isFontTag(rit->tag_)) {
410 // we'll just leave it and, presumably, have to close it later.
411 writeError("Unable to close font tag `" + etag.tag_
412 + "' due to open non-font tag `" + rit->tag_ + "'.");
418 // <em>this is <strong>bold
419 // and are being asked to closed em. we want:
420 // <em>this is <strong>bold</strong></em><strong>
421 // first, we close the intervening tags...
422 StartTag curtag = tag_stack_.back();
423 // ...remembering them in a stack.
425 while (curtag.tag_ != etag.tag_) {
426 os_ << curtag.asEndTag();
428 fontstack.push_back(curtag);
429 tag_stack_.pop_back();
430 curtag = tag_stack_.back();
432 // now close our tag...
433 os_ << etag.asEndTag();
435 tag_stack_.pop_back();
437 // ...and restore the other tags.
438 rit = fontstack.rbegin();
439 ren = fontstack.rend();
440 for (; rit != ren; ++rit)
441 pending_tags_.push_back(*rit);
445 // it wasn't a font tag.
446 // so other tags were opened before this one and not properly closed.
447 // so we'll close them, too. that may cause other issues later, but it
448 // at least guarantees proper nesting.
449 writeError("Closing tag `" + etag.tag_
450 + "' when other tags are open, namely:");
451 StartTag curtag = tag_stack_.back();
452 while (curtag.tag_ != etag.tag_) {
453 writeError(curtag.tag_);
454 os_ << curtag.asEndTag();
455 tag_stack_.pop_back();
456 curtag = tag_stack_.back();
458 // curtag is now the one we actually want.
459 os_ << curtag.asEndTag();
460 tag_stack_.pop_back();
465 // End code for XHTMLStream
469 // convenience functions
471 inline void openTag(XHTMLStream & xs, Layout const & lay)
473 xs << StartTag(lay.htmltag(), lay.htmlattr());
477 inline void closeTag(XHTMLStream & xs, Layout const & lay)
479 xs << EndTag(lay.htmltag());
483 inline void openLabelTag(XHTMLStream & xs, Layout const & lay)
485 xs << StartTag(lay.htmllabeltag(), lay.htmllabelattr());
489 inline void closeLabelTag(XHTMLStream & xs, Layout const & lay)
491 xs << EndTag(lay.htmllabeltag());
495 inline void openItemTag(XHTMLStream & xs, Layout const & lay)
497 xs << StartTag(lay.htmlitemtag(), lay.htmlitemattr(), true);
501 inline void closeItemTag(XHTMLStream & xs, Layout const & lay)
503 xs << EndTag(lay.htmlitemtag());
506 // end of convenience functions
508 ParagraphList::const_iterator searchParagraphHtml(
509 ParagraphList::const_iterator p,
510 ParagraphList::const_iterator const & pend)
512 for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
519 ParagraphList::const_iterator searchEnvironmentHtml(
520 ParagraphList::const_iterator const pstart,
521 ParagraphList::const_iterator const & pend)
523 ParagraphList::const_iterator p = pstart;
524 Layout const & bstyle = p->layout();
525 size_t const depth = p->params().depth();
526 for (++p; p != pend; ++p) {
527 Layout const & style = p->layout();
528 // It shouldn't happen that e.g. a section command occurs inside
529 // a quotation environment, at a higher depth, but as of 6/2009,
530 // it can happen. We pretend that it's just at lowest depth.
531 if (style.latextype == LATEX_COMMAND)
533 // If depth is down, we're done
534 if (p->params().depth() < depth)
536 // If depth is up, we're not done
537 if (p->params().depth() > depth)
539 // Now we know we are at the same depth
540 if (style.latextype == LATEX_PARAGRAPH
541 || style.latexname() != bstyle.latexname())
548 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
550 OutputParams const & runparams,
552 ParagraphList::const_iterator const & pbegin,
553 ParagraphList::const_iterator const & pend)
555 ParagraphList::const_iterator const begin = text.paragraphs().begin();
556 ParagraphList::const_iterator par = pbegin;
557 for (; par != pend; ++par) {
558 Layout const & lay = par->layout();
559 if (!lay.counter.empty())
560 buf.params().documentClass().counters().step(lay.counter);
561 // FIXME We should see if there's a label to be output and
562 // do something with it.
566 // If we are already in a paragraph, and this is the first one, then we
567 // do not want to open the paragraph tag.
568 // we also do not want to open it if the current layout does not permit
569 // multiple paragraphs.
570 bool const opened = runparams.html_make_pars &&
571 (par != pbegin || !runparams.html_in_par);
574 docstring const deferred =
575 par->simpleLyXHTMLOnePar(buf, xs, runparams, text.outerFont(distance(begin, par)));
577 // We want to issue the closing tag if either:
578 // (i) We opened it, and either html_in_par is false,
579 // or we're not in the last paragraph, anyway.
580 // (ii) We didn't open it and html_in_par is true,
581 // but we are in the first par, and there is a next par.
582 ParagraphList::const_iterator nextpar = par;
584 bool const needclose =
585 (opened && (!runparams.html_in_par || nextpar != pend))
586 || (!opened && runparams.html_in_par && par == pbegin && nextpar != pend);
591 if (!deferred.empty()) {
592 xs << XHTMLStream::NextRaw() << deferred;
600 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
602 OutputParams const & runparams,
604 ParagraphList::const_iterator const & pbegin,
605 ParagraphList::const_iterator const & pend)
607 xs << StartTag("h2", "class='bibliography'");
608 xs << pbegin->layout().labelstring(false);
611 xs << StartTag("div", "class='bibliography'");
613 makeParagraphs(buf, xs, runparams, text, pbegin, pend);
619 bool isNormalEnv(Layout const & lay)
621 return lay.latextype == LATEX_ENVIRONMENT
622 || lay.latextype == LATEX_BIB_ENVIRONMENT;
626 ParagraphList::const_iterator makeEnvironmentHtml(Buffer const & buf,
628 OutputParams const & runparams,
630 ParagraphList::const_iterator const & pbegin,
631 ParagraphList::const_iterator const & pend)
633 ParagraphList::const_iterator const begin = text.paragraphs().begin();
634 ParagraphList::const_iterator par = pbegin;
635 Layout const & bstyle = par->layout();
636 depth_type const origdepth = pbegin->params().depth();
638 // open tag for this environment
642 // we will on occasion need to remember a layout from before.
643 Layout const * lastlay = 0;
645 while (par != pend) {
646 Layout const & style = par->layout();
647 // the counter only gets stepped if we're in some kind of list,
648 // or if it's the first time through.
649 // note that enum, etc, are handled automatically.
650 // FIXME There may be a bug here about user defined enumeration
651 // types. If so, then we'll need to take the counter and add "i",
652 // "ii", etc, as with enum.
653 Counters & cnts = buf.params().documentClass().counters();
654 docstring const & cntr = style.counter;
655 if (!style.counter.empty()
656 && (par == pbegin || !isNormalEnv(style))
657 && cnts.hasCounter(cntr)
660 ParagraphList::const_iterator send;
661 // this will be positive, if we want to skip the initial word
662 // (if it's been taken for the label).
665 switch (style.latextype) {
666 case LATEX_ENVIRONMENT:
667 case LATEX_LIST_ENVIRONMENT:
668 case LATEX_ITEM_ENVIRONMENT: {
669 // There are two possiblities in this case.
670 // One is that we are still in the environment in which we
671 // started---which we will be if the depth is the same.
672 if (par->params().depth() == origdepth) {
673 LASSERT(bstyle == style, /* */);
675 closeItemTag(xs, *lastlay);
678 bool const labelfirst = style.htmllabelfirst();
679 if (isNormalEnv(style)) {
680 // in this case, we print the label only for the first
681 // paragraph (as in a theorem).
682 openItemTag(xs, style);
683 if (par == pbegin && style.htmllabeltag() != "NONE") {
684 docstring const lbl =
685 pbegin->expandLabel(style, buf.params(), false);
687 openLabelTag(xs, style);
689 closeLabelTag(xs, style);
693 } else { // some kind of list
695 openItemTag(xs, style);
696 if (style.labeltype == LABEL_MANUAL
697 && style.htmllabeltag() != "NONE") {
698 openLabelTag(xs, style);
699 sep = par->firstWordLyXHTML(xs, runparams);
700 closeLabelTag(xs, style);
703 else if (style.labeltype != LABEL_NO_LABEL
704 && style.htmllabeltag() != "NONE") {
705 openLabelTag(xs, style);
706 xs << par->expandLabel(style, buf.params(), false);
707 closeLabelTag(xs, style);
711 openItemTag(xs, style);
713 par->simpleLyXHTMLOnePar(buf, xs, runparams,
714 text.outerFont(distance(begin, par)), false, sep);
716 // We may not want to close the tag yet, in particular,
717 // if we're not at the end...
719 // and are doing items...
720 && !isNormalEnv(style)
721 // and if the depth has changed...
722 && par->params().depth() != origdepth) {
723 // then we'll save this layout for later, and close it when
724 // we get another item.
727 closeItemTag(xs, style);
730 // The other possibility is that the depth has increased, in which
731 // case we need to recurse.
733 send = searchEnvironmentHtml(par, pend);
734 par = makeEnvironmentHtml(buf, xs, runparams, text, par, send);
738 case LATEX_PARAGRAPH:
739 send = searchParagraphHtml(par, pend);
740 par = makeParagraphs(buf, xs, runparams, text, par, send);
743 case LATEX_BIB_ENVIRONMENT:
746 par = makeParagraphs(buf, xs, runparams, text, par, send);
756 closeItemTag(xs, *lastlay);
757 closeTag(xs, bstyle);
763 void makeCommand(Buffer const & buf,
765 OutputParams const & runparams,
767 ParagraphList::const_iterator const & pbegin)
769 Layout const & style = pbegin->layout();
770 if (!style.counter.empty())
771 buf.params().documentClass().counters().step(style.counter);
775 // Label around sectioning number:
776 // FIXME Probably need to account for LABEL_MANUAL
777 if (style.labeltype != LABEL_NO_LABEL) {
778 openLabelTag(xs, style);
779 xs << pbegin->expandLabel(style, buf.params(), false);
780 closeLabelTag(xs, style);
781 // Otherwise the label might run together with the text
782 xs << from_ascii(" ");
785 ParagraphList::const_iterator const begin = text.paragraphs().begin();
786 pbegin->simpleLyXHTMLOnePar(buf, xs, runparams,
787 text.outerFont(distance(begin, pbegin)));
792 } // end anonymous namespace
795 void xhtmlParagraphs(Text const & text,
798 OutputParams const & runparams)
800 ParagraphList const & paragraphs = text.paragraphs();
801 ParagraphList::const_iterator par = paragraphs.begin();
802 ParagraphList::const_iterator pend = paragraphs.end();
804 OutputParams ourparams = runparams;
805 while (par != pend) {
806 Layout const & style = par->layout();
807 ParagraphList::const_iterator lastpar = par;
808 ParagraphList::const_iterator send;
810 switch (style.latextype) {
811 case LATEX_COMMAND: {
812 // The files with which we are working never have more than
813 // one paragraph in a command structure.
815 // if (ourparams.html_in_par)
816 // fix it so we don't get sections inside standard, e.g.
817 // note that we may then need to make runparams not const, so we
818 // can communicate that back.
819 // FIXME Maybe this fix should be in the routines themselves, in case
820 // they are called from elsewhere.
821 makeCommand(buf, xs, ourparams, text, par);
825 case LATEX_ENVIRONMENT:
826 case LATEX_LIST_ENVIRONMENT:
827 case LATEX_ITEM_ENVIRONMENT: {
828 // FIXME Same fix here.
829 send = searchEnvironmentHtml(par, pend);
830 par = makeEnvironmentHtml(buf, xs, ourparams, text, par, send);
833 case LATEX_BIB_ENVIRONMENT: {
834 // FIXME Same fix here.
835 send = searchEnvironmentHtml(par, pend);
836 par = makeBibliography(buf, xs, ourparams, text, par, send);
839 case LATEX_PARAGRAPH:
840 send = searchParagraphHtml(par, pend);
841 par = makeParagraphs(buf, xs, ourparams, text, par, send);
845 // makeEnvironment may process more than one paragraphs and bypass pend
846 if (distance(lastpar, par) >= distance(lastpar, pend))