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<<(char c)
296 os_ << html::escapeChar(c);
301 XHTMLStream & XHTMLStream::operator<<(int i)
310 XHTMLStream & XHTMLStream::operator<<(NextRaw const &)
317 XHTMLStream & XHTMLStream::operator<<(html::StartTag const & tag)
319 if (tag.tag_.empty())
321 pending_tags_.push_back(tag);
328 XHTMLStream & XHTMLStream::operator<<(html::CompTag const & tag)
330 if (tag.tag_.empty())
340 bool XHTMLStream::isTagOpen(string const & stag)
342 TagStack::const_iterator sit = tag_stack_.begin();
343 TagStack::const_iterator const sen = tag_stack_.end();
344 for (; sit != sen; ++sit)
345 if (sit->tag_ == stag)
351 // this is complicated, because we want to make sure that
352 // everything is properly nested. the code ought to make
353 // sure of that, but we won't assert (yet) if we run into
354 // a problem. we'll just output error messages and try our
355 // best to make things work.
356 XHTMLStream & XHTMLStream::operator<<(html::EndTag const & etag)
358 if (etag.tag_.empty())
361 // make sure there are tags to be closed
362 if (tag_stack_.empty()) {
363 writeError("Tried to close `" + etag.tag_
364 + "' when no tags were open!");
368 // first make sure we're not closing an empty tag
369 if (!pending_tags_.empty()) {
370 html::StartTag const & stag = pending_tags_.back();
371 if (etag.tag_ == stag.tag_) {
372 // we have <tag></tag>, so we discard it and remove it
373 // from the pending_tags_.
374 pending_tags_.pop_back();
377 // there is a pending tag that isn't the one we are trying
379 // is this tag itself pending?
380 // non-const iterators because we may call erase().
381 TagDeque::iterator dit = pending_tags_.begin();
382 TagDeque::iterator const den = pending_tags_.end();
383 for (; dit != den; ++dit) {
384 if (dit->tag_ == etag.tag_) {
385 // it was pending, so we just erase it
386 writeError("Tried to close pending tag `" + etag.tag_
387 + "' when other tags were pending. Last pending tag is `"
388 + pending_tags_.back().tag_ + "'. Tag discarded.");
389 pending_tags_.erase(dit);
393 // so etag isn't itself pending. is it even open?
394 if (!isTagOpen(etag.tag_)) {
395 writeError("Tried to close `" + etag.tag_
396 + "' when tag was not open. Tag discarded.");
399 // ok, so etag is open.
400 // our strategy will be as below: we will do what we need to
401 // do to close this tag.
402 string estr = "Closing tag `" + etag.tag_
403 + "' when other tags are pending. Discarded pending tags:\n";
404 for (dit = pending_tags_.begin(); dit != den; ++dit)
405 estr += dit->tag_ + "\n";
407 // clear the pending tags...
408 pending_tags_.clear();
409 // ...and then just fall through.
412 // is the tag we are closing the last one we opened?
413 if (etag.tag_ == tag_stack_.back().tag_) {
415 os_ << etag.asEndTag();
416 // ...and forget about it
417 tag_stack_.pop_back();
421 // we are trying to close a tag other than the one last opened.
422 // let's first see if this particular tag is still open somehow.
423 if (!isTagOpen(etag.tag_)) {
424 writeError("Tried to close `" + etag.tag_
425 + "' when tag was not open. Tag discarded.");
429 // so the tag was opened, but other tags have been opened since
430 // and not yet closed.
431 // if it's a font tag, though...
432 if (html::isFontTag(etag.tag_)) {
433 // it won't be a problem if the other tags open since this one
434 // are also font tags.
435 TagStack::const_reverse_iterator rit = tag_stack_.rbegin();
436 TagStack::const_reverse_iterator ren = tag_stack_.rend();
437 for (; rit != ren; ++rit) {
438 if (rit->tag_ == etag.tag_)
440 if (!html::isFontTag(rit->tag_)) {
441 // we'll just leave it and, presumably, have to close it later.
442 writeError("Unable to close font tag `" + etag.tag_
443 + "' due to open non-font tag `" + rit->tag_ + "'.");
449 // <em>this is <strong>bold
450 // and are being asked to closed em. we want:
451 // <em>this is <strong>bold</strong></em><strong>
452 // first, we close the intervening tags...
453 html::StartTag curtag = tag_stack_.back();
454 // ...remembering them in a stack.
456 while (curtag.tag_ != etag.tag_) {
457 os_ << curtag.asEndTag();
458 fontstack.push_back(curtag);
459 tag_stack_.pop_back();
460 curtag = tag_stack_.back();
462 // now close our tag...
463 os_ << etag.asEndTag();
464 tag_stack_.pop_back();
466 // ...and restore the other tags.
467 rit = fontstack.rbegin();
468 ren = fontstack.rend();
469 for (; rit != ren; ++rit)
470 pending_tags_.push_back(*rit);
474 // it wasn't a font tag.
475 // so other tags were opened before this one and not properly closed.
476 // so we'll close them, too. that may cause other issues later, but it
477 // at least guarantees proper nesting.
478 writeError("Closing tag `" + etag.tag_
479 + "' when other tags are open, namely:");
480 html::StartTag curtag = tag_stack_.back();
481 while (curtag.tag_ != etag.tag_) {
482 writeError(curtag.tag_);
483 os_ << curtag.asEndTag();
484 tag_stack_.pop_back();
485 curtag = tag_stack_.back();
487 // curtag is now the one we actually want.
488 os_ << curtag.asEndTag();
489 tag_stack_.pop_back();
494 // End code for XHTMLStream
498 // convenience functions
500 inline void openTag(XHTMLStream & xs, Layout const & lay)
502 xs << html::StartTag(lay.htmltag(), lay.htmlattr());
506 void openTag(XHTMLStream & xs, Layout const & lay, ParagraphParameters const & params)
508 // FIXME Are there other things we should handle here?
509 string const align = alignmentToCSS(params.align());
514 string attrs = lay.htmlattr() + " style='text-align: " + align + ";'";
515 xs << html::StartTag(lay.htmltag(), attrs);
519 inline void closeTag(XHTMLStream & xs, Layout const & lay)
521 xs << html::EndTag(lay.htmltag());
525 inline void openLabelTag(XHTMLStream & xs, Layout const & lay)
527 xs << html::StartTag(lay.htmllabeltag(), lay.htmllabelattr());
531 inline void closeLabelTag(XHTMLStream & xs, Layout const & lay)
533 xs << html::EndTag(lay.htmllabeltag());
537 inline void openItemTag(XHTMLStream & xs, Layout const & lay)
539 xs << html::StartTag(lay.htmlitemtag(), lay.htmlitemattr(), true);
543 inline void closeItemTag(XHTMLStream & xs, Layout const & lay)
545 xs << html::EndTag(lay.htmlitemtag());
548 // end of convenience functions
550 ParagraphList::const_iterator searchParagraphHtml(
551 ParagraphList::const_iterator p,
552 ParagraphList::const_iterator const & pend)
554 for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
561 ParagraphList::const_iterator searchEnvironmentHtml(
562 ParagraphList::const_iterator const pstart,
563 ParagraphList::const_iterator const & pend)
565 ParagraphList::const_iterator p = pstart;
566 Layout const & bstyle = p->layout();
567 size_t const depth = p->params().depth();
568 for (++p; p != pend; ++p) {
569 Layout const & style = p->layout();
570 // It shouldn't happen that e.g. a section command occurs inside
571 // a quotation environment, at a higher depth, but as of 6/2009,
572 // it can happen. We pretend that it's just at lowest depth.
573 if (style.latextype == LATEX_COMMAND)
575 // If depth is down, we're done
576 if (p->params().depth() < depth)
578 // If depth is up, we're not done
579 if (p->params().depth() > depth)
581 // Now we know we are at the same depth
582 if (style.latextype == LATEX_PARAGRAPH
583 || style.latexname() != bstyle.latexname())
590 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
592 OutputParams const & runparams,
594 ParagraphList::const_iterator const & pbegin,
595 ParagraphList::const_iterator const & pend)
597 ParagraphList::const_iterator const begin = text.paragraphs().begin();
598 ParagraphList::const_iterator par = pbegin;
599 for (; par != pend; ++par) {
600 Layout const & lay = par->layout();
601 if (!lay.counter.empty())
602 buf.params().documentClass().counters().step(lay.counter, OutputUpdate);
603 // FIXME We should see if there's a label to be output and
604 // do something with it.
608 // If we are already in a paragraph, and this is the first one, then we
609 // do not want to open the paragraph tag.
610 // we also do not want to open it if the current layout does not permit
611 // multiple paragraphs.
612 bool const opened = runparams.html_make_pars &&
613 (par != pbegin || !runparams.html_in_par);
615 openTag(xs, lay, par->params());
616 docstring const deferred =
617 par->simpleLyXHTMLOnePar(buf, xs, runparams, text.outerFont(distance(begin, par)));
619 // We want to issue the closing tag if either:
620 // (i) We opened it, and either html_in_par is false,
621 // or we're not in the last paragraph, anyway.
622 // (ii) We didn't open it and html_in_par is true,
623 // but we are in the first par, and there is a next par.
624 ParagraphList::const_iterator nextpar = par;
626 bool const needclose =
627 (opened && (!runparams.html_in_par || nextpar != pend))
628 || (!opened && runparams.html_in_par && par == pbegin && nextpar != pend);
633 if (!deferred.empty()) {
634 xs << XHTMLStream::NextRaw() << deferred;
642 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
644 OutputParams const & runparams,
646 ParagraphList::const_iterator const & pbegin,
647 ParagraphList::const_iterator const & pend)
650 // Use TextClass::htmlTOCLayout() to figure out how we should look.
651 xs << html::StartTag("h2", "class='bibliography'");
652 xs << pbegin->layout().labelstring(false);
653 xs << html::EndTag("h2");
655 xs << html::StartTag("div", "class='bibliography'");
657 makeParagraphs(buf, xs, runparams, text, pbegin, pend);
658 xs << html::EndTag("div");
663 bool isNormalEnv(Layout const & lay)
665 return lay.latextype == LATEX_ENVIRONMENT
666 || lay.latextype == LATEX_BIB_ENVIRONMENT;
670 ParagraphList::const_iterator makeEnvironmentHtml(Buffer const & buf,
672 OutputParams const & runparams,
674 ParagraphList::const_iterator const & pbegin,
675 ParagraphList::const_iterator const & pend)
677 ParagraphList::const_iterator const begin = text.paragraphs().begin();
678 ParagraphList::const_iterator par = pbegin;
679 Layout const & bstyle = par->layout();
680 depth_type const origdepth = pbegin->params().depth();
682 // open tag for this environment
686 // we will on occasion need to remember a layout from before.
687 Layout const * lastlay = 0;
689 while (par != pend) {
690 Layout const & style = par->layout();
691 // the counter only gets stepped if we're in some kind of list,
692 // or if it's the first time through.
693 // note that enum, etc, are handled automatically.
694 // FIXME There may be a bug here about user defined enumeration
695 // types. If so, then we'll need to take the counter and add "i",
696 // "ii", etc, as with enum.
697 Counters & cnts = buf.params().documentClass().counters();
698 docstring const & cntr = style.counter;
699 if (!style.counter.empty()
700 && (par == pbegin || !isNormalEnv(style))
701 && cnts.hasCounter(cntr)
703 cnts.step(cntr, OutputUpdate);
704 ParagraphList::const_iterator send;
705 // this will be positive, if we want to skip the initial word
706 // (if it's been taken for the label).
709 switch (style.latextype) {
710 case LATEX_ENVIRONMENT:
711 case LATEX_LIST_ENVIRONMENT:
712 case LATEX_ITEM_ENVIRONMENT: {
713 // There are two possiblities in this case.
714 // One is that we are still in the environment in which we
715 // started---which we will be if the depth is the same.
716 if (par->params().depth() == origdepth) {
717 LASSERT(bstyle == style, /* */);
719 closeItemTag(xs, *lastlay);
723 bool const labelfirst = style.htmllabelfirst();
725 openItemTag(xs, style);
728 if (style.labeltype != LABEL_NO_LABEL &&
729 style.htmllabeltag() != "NONE") {
730 if (isNormalEnv(style)) {
731 // in this case, we print the label only for the first
732 // paragraph (as in a theorem).
734 docstring const lbl =
735 pbegin->params().labelString();
737 openLabelTag(xs, style);
739 closeLabelTag(xs, style);
743 } else { // some kind of list
744 if (style.labeltype == LABEL_MANUAL) {
745 openLabelTag(xs, style);
746 sep = par->firstWordLyXHTML(xs, runparams);
747 closeLabelTag(xs, style);
751 openLabelTag(xs, style);
752 xs << par->params().labelString();
753 closeLabelTag(xs, style);
757 } // end label output
760 openItemTag(xs, style);
762 par->simpleLyXHTMLOnePar(buf, xs, runparams,
763 text.outerFont(distance(begin, par)), sep);
766 // We may not want to close the tag yet, in particular:
767 // If we're not at the end...
769 // and are doing items...
770 && !isNormalEnv(style)
771 // and if the depth has changed...
772 && par->params().depth() != origdepth) {
773 // then we'll save this layout for later, and close it when
774 // we get another item.
777 closeItemTag(xs, style);
780 // The other possibility is that the depth has increased, in which
781 // case we need to recurse.
783 send = searchEnvironmentHtml(par, pend);
784 par = makeEnvironmentHtml(buf, xs, runparams, text, par, send);
788 case LATEX_PARAGRAPH:
789 send = searchParagraphHtml(par, pend);
790 par = makeParagraphs(buf, xs, runparams, text, par, send);
793 case LATEX_BIB_ENVIRONMENT:
796 par = makeParagraphs(buf, xs, runparams, text, par, send);
806 closeItemTag(xs, *lastlay);
807 closeTag(xs, bstyle);
813 void makeCommand(Buffer const & buf,
815 OutputParams const & runparams,
817 ParagraphList::const_iterator const & pbegin)
819 Layout const & style = pbegin->layout();
820 if (!style.counter.empty())
821 buf.params().documentClass().counters().step(style.counter, OutputUpdate);
825 // Label around sectioning number:
826 // FIXME Probably need to account for LABEL_MANUAL
827 if (style.labeltype != LABEL_NO_LABEL) {
828 openLabelTag(xs, style);
829 xs << pbegin->params().labelString();
830 closeLabelTag(xs, style);
831 // Otherwise the label might run together with the text
832 xs << from_ascii(" ");
835 ParagraphList::const_iterator const begin = text.paragraphs().begin();
836 pbegin->simpleLyXHTMLOnePar(buf, xs, runparams,
837 text.outerFont(distance(begin, pbegin)));
842 } // end anonymous namespace
845 void xhtmlParagraphs(Text const & text,
848 OutputParams const & runparams)
850 ParagraphList const & paragraphs = text.paragraphs();
851 ParagraphList::const_iterator par = paragraphs.begin();
852 ParagraphList::const_iterator pend = paragraphs.end();
854 OutputParams ourparams = runparams;
855 while (par != pend) {
856 if (par->params().startOfAppendix()) {
857 // FIXME: only the counter corresponding to toplevel
858 // sectioning should be reset
859 Counters & cnts = buf.masterBuffer()->params().documentClass().counters();
863 Layout const & style = par->layout();
864 ParagraphList::const_iterator lastpar = par;
865 ParagraphList::const_iterator send;
867 switch (style.latextype) {
868 case LATEX_COMMAND: {
869 // The files with which we are working never have more than
870 // one paragraph in a command structure.
872 // if (ourparams.html_in_par)
873 // fix it so we don't get sections inside standard, e.g.
874 // note that we may then need to make runparams not const, so we
875 // can communicate that back.
876 // FIXME Maybe this fix should be in the routines themselves, in case
877 // they are called from elsewhere.
878 makeCommand(buf, xs, ourparams, text, par);
882 case LATEX_ENVIRONMENT:
883 case LATEX_LIST_ENVIRONMENT:
884 case LATEX_ITEM_ENVIRONMENT: {
885 // FIXME Same fix here.
886 send = searchEnvironmentHtml(par, pend);
887 par = makeEnvironmentHtml(buf, xs, ourparams, text, par, send);
890 case LATEX_BIB_ENVIRONMENT: {
891 // FIXME Same fix here.
892 send = searchEnvironmentHtml(par, pend);
893 par = makeBibliography(buf, xs, ourparams, text, par, send);
896 case LATEX_PARAGRAPH:
897 send = searchParagraphHtml(par, pend);
898 par = makeParagraphs(buf, xs, ourparams, text, par, send);
902 // makeEnvironment may process more than one paragraphs and bypass pend
903 if (distance(lastpar, par) >= distance(lastpar, pend))
909 string alignmentToCSS(LyXAlignment align) {
911 case LYX_ALIGN_BLOCK:
912 // we are NOT going to use text-align: justify!!
915 case LYX_ALIGN_RIGHT:
917 case LYX_ALIGN_CENTER: