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"
23 #include "OutputParams.h"
24 #include "Paragraph.h"
25 #include "ParagraphList.h"
26 #include "ParagraphParameters.h"
29 #include "TextClass.h"
31 #include "support/convert.h"
32 #include "support/debug.h"
33 #include "support/lassert.h"
34 #include "support/lstrings.h"
39 using namespace lyx::support;
45 docstring escapeChar(char_type c)
69 // escape what needs escaping
70 docstring htmlize(docstring const & str) {
72 docstring::const_iterator it = str.begin();
73 docstring::const_iterator en = str.end();
74 for (; it != en; ++it)
80 string escapeChar(char c)
104 // escape what needs escaping
105 string htmlize(string const & str) {
107 string::const_iterator it = str.begin();
108 string::const_iterator en = str.end();
109 for (; it != en; ++it)
110 d << escapeChar(*it);
115 string cleanAttr(string const & str)
118 string::const_iterator it = str.begin();
119 string::const_iterator en = str.end();
120 for (; it != en; ++it)
121 newname += isalnum(*it) ? *it : '_';
126 docstring cleanAttr(docstring const & str)
129 docstring::const_iterator it = str.begin();
130 docstring::const_iterator en = str.end();
131 for (; it != en; ++it)
140 bool isFontTag(string const & s)
142 return s == "em" || s == "strong"; // others?
146 docstring StartTag::asTag() const
148 string output = "<" + tag_;
150 output += " " + html::htmlize(attr_);
152 return from_utf8(output);
156 docstring StartTag::asEndTag() const
158 string output = "</" + tag_ + ">";
159 return from_utf8(output);
163 docstring EndTag::asEndTag() const
165 string output = "</" + tag_ + ">";
166 return from_utf8(output);
170 docstring CompTag::asTag() const
172 string output = "<" + tag_;
174 output += " " + html::htmlize(attr_);
176 return from_utf8(output);
183 ////////////////////////////////////////////////////////////////
187 ////////////////////////////////////////////////////////////////
189 XHTMLStream::XHTMLStream(odocstream & os)
190 : os_(os), nextraw_(false)
194 void XHTMLStream::cr()
197 os_ << from_ascii("\n");
201 void XHTMLStream::writeError(std::string const & s)
204 os_ << from_utf8("<!-- Output Error: " + s + " -->");
208 bool XHTMLStream::closeFontTags()
210 if (tag_stack_.empty())
212 // first, we close any open font tags we can close
213 html::StartTag curtag = tag_stack_.back();
214 while (html::isFontTag(curtag.tag_)) {
215 os_ << curtag.asEndTag();
216 tag_stack_.pop_back();
217 if (tag_stack_.empty())
218 // this probably shouldn't happen, since then the
219 // font tags weren't in any other tag. but that
220 // problem will likely be caught elsewhere.
222 curtag = tag_stack_.back();
224 // so we've hit a non-font tag. let's see if any of the
225 // remaining tags are font tags.
226 TagStack::const_iterator it = tag_stack_.begin();
227 TagStack::const_iterator en = tag_stack_.end();
228 bool noFontTags = true;
229 for (; it != en; ++it) {
230 if (html::isFontTag(it->tag_)) {
231 writeError("Font tag `" + it->tag_ + "' still open in closeFontTags().\n"
232 "This is likely not a problem, but you might want to check.");
240 void XHTMLStream::clearTagDeque()
242 while (!pending_tags_.empty()) {
243 html::StartTag const & tag = pending_tags_.front();
246 tag_stack_.push_back(tag);
247 pending_tags_.pop_front();
252 XHTMLStream & XHTMLStream::operator<<(docstring const & d)
259 os_ << html::htmlize(d);
264 XHTMLStream & XHTMLStream::operator<<(const char * s)
267 docstring const d = from_ascii(s);
272 os_ << html::htmlize(d);
277 XHTMLStream & XHTMLStream::operator<<(char_type c)
284 os_ << html::escapeChar(c);
289 XHTMLStream & XHTMLStream::operator<<(int i)
298 XHTMLStream & XHTMLStream::operator<<(NextRaw const &)
305 XHTMLStream & XHTMLStream::operator<<(html::StartTag const & tag)
307 if (tag.tag_.empty())
309 pending_tags_.push_back(tag);
316 XHTMLStream & XHTMLStream::operator<<(html::CompTag const & tag)
318 if (tag.tag_.empty())
328 bool XHTMLStream::isTagOpen(string const & stag)
330 TagStack::const_iterator sit = tag_stack_.begin();
331 TagStack::const_iterator const sen = tag_stack_.end();
332 for (; sit != sen; ++sit)
333 if (sit->tag_ == stag)
339 // this is complicated, because we want to make sure that
340 // everything is properly nested. the code ought to make
341 // sure of that, but we won't assert (yet) if we run into
342 // a problem. we'll just output error messages and try our
343 // best to make things work.
344 XHTMLStream & XHTMLStream::operator<<(html::EndTag const & etag)
346 if (etag.tag_.empty())
349 // make sure there are tags to be closed
350 if (tag_stack_.empty()) {
351 writeError("Tried to close `" + etag.tag_
352 + "' when no tags were open!");
356 // first make sure we're not closing an empty tag
357 if (!pending_tags_.empty()) {
358 html::StartTag const & stag = pending_tags_.back();
359 if (etag.tag_ == stag.tag_) {
360 // we have <tag></tag>, so we discard it and remove it
361 // from the pending_tags_.
362 pending_tags_.pop_back();
365 // there is a pending tag that isn't the one we are trying
367 // is this tag itself pending?
368 // non-const iterators because we may call erase().
369 TagDeque::iterator dit = pending_tags_.begin();
370 TagDeque::iterator const den = pending_tags_.end();
371 for (; dit != den; ++dit) {
372 if (dit->tag_ == etag.tag_) {
373 // it was pending, so we just erase it
374 writeError("Tried to close pending tag `" + etag.tag_
375 + "' when other tags were pending. Last pending tag is `"
376 + pending_tags_.back().tag_ + "'. Tag discarded.");
377 pending_tags_.erase(dit);
381 // so etag isn't itself pending. is it even open?
382 if (!isTagOpen(etag.tag_)) {
383 writeError("Tried to close `" + etag.tag_
384 + "' when tag was not open. Tag discarded.");
387 // ok, so etag is open.
388 // our strategy will be as below: we will do what we need to
389 // do to close this tag.
390 string estr = "Closing tag `" + etag.tag_
391 + "' when other tags are pending. Discarded pending tags:\n";
392 for (dit = pending_tags_.begin(); dit != den; ++dit)
393 estr += dit->tag_ + "\n";
395 // clear the pending tags...
396 pending_tags_.clear();
397 // ...and then just fall through.
400 // is the tag we are closing the last one we opened?
401 if (etag.tag_ == tag_stack_.back().tag_) {
403 os_ << etag.asEndTag();
404 // ...and forget about it
405 tag_stack_.pop_back();
409 // we are trying to close a tag other than the one last opened.
410 // let's first see if this particular tag is still open somehow.
411 if (!isTagOpen(etag.tag_)) {
412 writeError("Tried to close `" + etag.tag_
413 + "' when tag was not open. Tag discarded.");
417 // so the tag was opened, but other tags have been opened since
418 // and not yet closed.
419 // if it's a font tag, though...
420 if (html::isFontTag(etag.tag_)) {
421 // it won't be a problem if the other tags open since this one
422 // are also font tags.
423 TagStack::const_reverse_iterator rit = tag_stack_.rbegin();
424 TagStack::const_reverse_iterator ren = tag_stack_.rend();
425 for (; rit != ren; ++rit) {
426 if (rit->tag_ == etag.tag_)
428 if (!html::isFontTag(rit->tag_)) {
429 // we'll just leave it and, presumably, have to close it later.
430 writeError("Unable to close font tag `" + etag.tag_
431 + "' due to open non-font tag `" + rit->tag_ + "'.");
437 // <em>this is <strong>bold
438 // and are being asked to closed em. we want:
439 // <em>this is <strong>bold</strong></em><strong>
440 // first, we close the intervening tags...
441 html::StartTag curtag = tag_stack_.back();
442 // ...remembering them in a stack.
444 while (curtag.tag_ != etag.tag_) {
445 os_ << curtag.asEndTag();
446 fontstack.push_back(curtag);
447 tag_stack_.pop_back();
448 curtag = tag_stack_.back();
450 // now close our tag...
451 os_ << etag.asEndTag();
452 tag_stack_.pop_back();
454 // ...and restore the other tags.
455 rit = fontstack.rbegin();
456 ren = fontstack.rend();
457 for (; rit != ren; ++rit)
458 pending_tags_.push_back(*rit);
462 // it wasn't a font tag.
463 // so other tags were opened before this one and not properly closed.
464 // so we'll close them, too. that may cause other issues later, but it
465 // at least guarantees proper nesting.
466 writeError("Closing tag `" + etag.tag_
467 + "' when other tags are open, namely:");
468 html::StartTag curtag = tag_stack_.back();
469 while (curtag.tag_ != etag.tag_) {
470 writeError(curtag.tag_);
471 os_ << curtag.asEndTag();
472 tag_stack_.pop_back();
473 curtag = tag_stack_.back();
475 // curtag is now the one we actually want.
476 os_ << curtag.asEndTag();
477 tag_stack_.pop_back();
482 // End code for XHTMLStream
486 // convenience functions
488 inline void openTag(XHTMLStream & xs, Layout const & lay)
490 xs << html::StartTag(lay.htmltag(), lay.htmlattr());
494 inline void closeTag(XHTMLStream & xs, Layout const & lay)
496 xs << html::EndTag(lay.htmltag());
500 inline void openLabelTag(XHTMLStream & xs, Layout const & lay)
502 xs << html::StartTag(lay.htmllabeltag(), lay.htmllabelattr());
506 inline void closeLabelTag(XHTMLStream & xs, Layout const & lay)
508 xs << html::EndTag(lay.htmllabeltag());
512 inline void openItemTag(XHTMLStream & xs, Layout const & lay)
514 xs << html::StartTag(lay.htmlitemtag(), lay.htmlitemattr(), true);
518 inline void closeItemTag(XHTMLStream & xs, Layout const & lay)
520 xs << html::EndTag(lay.htmlitemtag());
523 // end of convenience functions
525 ParagraphList::const_iterator searchParagraphHtml(
526 ParagraphList::const_iterator p,
527 ParagraphList::const_iterator const & pend)
529 for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
536 ParagraphList::const_iterator searchEnvironmentHtml(
537 ParagraphList::const_iterator const pstart,
538 ParagraphList::const_iterator const & pend)
540 ParagraphList::const_iterator p = pstart;
541 Layout const & bstyle = p->layout();
542 size_t const depth = p->params().depth();
543 for (++p; p != pend; ++p) {
544 Layout const & style = p->layout();
545 // It shouldn't happen that e.g. a section command occurs inside
546 // a quotation environment, at a higher depth, but as of 6/2009,
547 // it can happen. We pretend that it's just at lowest depth.
548 if (style.latextype == LATEX_COMMAND)
550 // If depth is down, we're done
551 if (p->params().depth() < depth)
553 // If depth is up, we're not done
554 if (p->params().depth() > depth)
556 // Now we know we are at the same depth
557 if (style.latextype == LATEX_PARAGRAPH
558 || style.latexname() != bstyle.latexname())
565 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
567 OutputParams const & runparams,
569 ParagraphList::const_iterator const & pbegin,
570 ParagraphList::const_iterator const & pend)
572 ParagraphList::const_iterator const begin = text.paragraphs().begin();
573 ParagraphList::const_iterator par = pbegin;
574 for (; par != pend; ++par) {
575 Layout const & lay = par->layout();
576 if (!lay.counter.empty())
577 buf.params().documentClass().counters().step(lay.counter, OutputUpdate);
578 // FIXME We should see if there's a label to be output and
579 // do something with it.
583 // If we are already in a paragraph, and this is the first one, then we
584 // do not want to open the paragraph tag.
585 // we also do not want to open it if the current layout does not permit
586 // multiple paragraphs.
587 bool const opened = runparams.html_make_pars &&
588 (par != pbegin || !runparams.html_in_par);
591 docstring const deferred =
592 par->simpleLyXHTMLOnePar(buf, xs, runparams, text.outerFont(distance(begin, par)));
594 // We want to issue the closing tag if either:
595 // (i) We opened it, and either html_in_par is false,
596 // or we're not in the last paragraph, anyway.
597 // (ii) We didn't open it and html_in_par is true,
598 // but we are in the first par, and there is a next par.
599 ParagraphList::const_iterator nextpar = par;
601 bool const needclose =
602 (opened && (!runparams.html_in_par || nextpar != pend))
603 || (!opened && runparams.html_in_par && par == pbegin && nextpar != pend);
608 if (!deferred.empty()) {
609 xs << XHTMLStream::NextRaw() << deferred;
617 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
619 OutputParams const & runparams,
621 ParagraphList::const_iterator const & pbegin,
622 ParagraphList::const_iterator const & pend)
625 // Use TextClass::htmlTOCLayout() to figure out how we should look.
626 xs << html::StartTag("h2", "class='bibliography'");
627 xs << pbegin->layout().labelstring(false);
628 xs << html::EndTag("h2");
630 xs << html::StartTag("div", "class='bibliography'");
632 makeParagraphs(buf, xs, runparams, text, pbegin, pend);
633 xs << html::EndTag("div");
638 bool isNormalEnv(Layout const & lay)
640 return lay.latextype == LATEX_ENVIRONMENT
641 || lay.latextype == LATEX_BIB_ENVIRONMENT;
645 ParagraphList::const_iterator makeEnvironmentHtml(Buffer const & buf,
647 OutputParams const & runparams,
649 ParagraphList::const_iterator const & pbegin,
650 ParagraphList::const_iterator const & pend)
652 ParagraphList::const_iterator const begin = text.paragraphs().begin();
653 ParagraphList::const_iterator par = pbegin;
654 Layout const & bstyle = par->layout();
655 depth_type const origdepth = pbegin->params().depth();
657 // open tag for this environment
661 // we will on occasion need to remember a layout from before.
662 Layout const * lastlay = 0;
664 while (par != pend) {
665 Layout const & style = par->layout();
666 // the counter only gets stepped if we're in some kind of list,
667 // or if it's the first time through.
668 // note that enum, etc, are handled automatically.
669 // FIXME There may be a bug here about user defined enumeration
670 // types. If so, then we'll need to take the counter and add "i",
671 // "ii", etc, as with enum.
672 Counters & cnts = buf.params().documentClass().counters();
673 docstring const & cntr = style.counter;
674 if (!style.counter.empty()
675 && (par == pbegin || !isNormalEnv(style))
676 && cnts.hasCounter(cntr)
678 cnts.step(cntr, OutputUpdate);
679 ParagraphList::const_iterator send;
680 // this will be positive, if we want to skip the initial word
681 // (if it's been taken for the label).
684 switch (style.latextype) {
685 case LATEX_ENVIRONMENT:
686 case LATEX_LIST_ENVIRONMENT:
687 case LATEX_ITEM_ENVIRONMENT: {
688 // There are two possiblities in this case.
689 // One is that we are still in the environment in which we
690 // started---which we will be if the depth is the same.
691 if (par->params().depth() == origdepth) {
692 LASSERT(bstyle == style, /* */);
694 closeItemTag(xs, *lastlay);
697 if (isNormalEnv(style)) {
698 // in this case, we print the label only for the first
699 // paragraph (as in a theorem).
700 openItemTag(xs, style);
701 if (par == pbegin && style.htmllabeltag() != "NONE") {
702 docstring const lbl =
703 pbegin->params().labelString();
705 openLabelTag(xs, style);
707 closeLabelTag(xs, style);
711 } else { // some kind of list
712 bool const labelfirst = style.htmllabelfirst();
714 openItemTag(xs, style);
715 if (style.labeltype == LABEL_MANUAL
716 && style.htmllabeltag() != "NONE") {
717 openLabelTag(xs, style);
718 sep = par->firstWordLyXHTML(xs, runparams);
719 closeLabelTag(xs, style);
722 else if (style.labeltype != LABEL_NO_LABEL
723 && style.htmllabeltag() != "NONE") {
724 openLabelTag(xs, style);
725 xs << par->params().labelString();
726 closeLabelTag(xs, style);
730 openItemTag(xs, style);
732 par->simpleLyXHTMLOnePar(buf, xs, runparams,
733 text.outerFont(distance(begin, par)), sep);
735 // We may not want to close the tag yet, in particular,
736 // if we're not at the end...
738 // and are doing items...
739 && !isNormalEnv(style)
740 // and if the depth has changed...
741 && par->params().depth() != origdepth) {
742 // then we'll save this layout for later, and close it when
743 // we get another item.
746 closeItemTag(xs, style);
749 // The other possibility is that the depth has increased, in which
750 // case we need to recurse.
752 send = searchEnvironmentHtml(par, pend);
753 par = makeEnvironmentHtml(buf, xs, runparams, text, par, send);
757 case LATEX_PARAGRAPH:
758 send = searchParagraphHtml(par, pend);
759 par = makeParagraphs(buf, xs, runparams, text, par, send);
762 case LATEX_BIB_ENVIRONMENT:
765 par = makeParagraphs(buf, xs, runparams, text, par, send);
775 closeItemTag(xs, *lastlay);
776 closeTag(xs, bstyle);
782 void makeCommand(Buffer const & buf,
784 OutputParams const & runparams,
786 ParagraphList::const_iterator const & pbegin)
788 Layout const & style = pbegin->layout();
789 if (!style.counter.empty())
790 buf.params().documentClass().counters().step(style.counter, OutputUpdate);
794 // Label around sectioning number:
795 // FIXME Probably need to account for LABEL_MANUAL
796 if (style.labeltype != LABEL_NO_LABEL) {
797 openLabelTag(xs, style);
798 xs << pbegin->params().labelString();
799 closeLabelTag(xs, style);
800 // Otherwise the label might run together with the text
801 xs << from_ascii(" ");
804 ParagraphList::const_iterator const begin = text.paragraphs().begin();
805 pbegin->simpleLyXHTMLOnePar(buf, xs, runparams,
806 text.outerFont(distance(begin, pbegin)));
811 } // end anonymous namespace
814 void xhtmlParagraphs(Text const & text,
817 OutputParams const & runparams)
819 ParagraphList const & paragraphs = text.paragraphs();
820 ParagraphList::const_iterator par = paragraphs.begin();
821 ParagraphList::const_iterator pend = paragraphs.end();
823 OutputParams ourparams = runparams;
824 while (par != pend) {
825 if (par->params().startOfAppendix()) {
826 // FIXME: only the counter corresponding to toplevel
827 // sectioning should be reset
828 Counters & cnts = buf.masterBuffer()->params().documentClass().counters();
832 Layout const & style = par->layout();
833 ParagraphList::const_iterator lastpar = par;
834 ParagraphList::const_iterator send;
836 switch (style.latextype) {
837 case LATEX_COMMAND: {
838 // The files with which we are working never have more than
839 // one paragraph in a command structure.
841 // if (ourparams.html_in_par)
842 // fix it so we don't get sections inside standard, e.g.
843 // note that we may then need to make runparams not const, so we
844 // can communicate that back.
845 // FIXME Maybe this fix should be in the routines themselves, in case
846 // they are called from elsewhere.
847 makeCommand(buf, xs, ourparams, text, par);
851 case LATEX_ENVIRONMENT:
852 case LATEX_LIST_ENVIRONMENT:
853 case LATEX_ITEM_ENVIRONMENT: {
854 // FIXME Same fix here.
855 send = searchEnvironmentHtml(par, pend);
856 par = makeEnvironmentHtml(buf, xs, ourparams, text, par, send);
859 case LATEX_BIB_ENVIRONMENT: {
860 // FIXME Same fix here.
861 send = searchEnvironmentHtml(par, pend);
862 par = makeBibliography(buf, xs, ourparams, text, par, send);
865 case LATEX_PARAGRAPH:
866 send = searchParagraphHtml(par, pend);
867 par = makeParagraphs(buf, xs, ourparams, text, par, send);
871 // makeEnvironment may process more than one paragraphs and bypass pend
872 if (distance(lastpar, par) >= distance(lastpar, pend))