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,
507 ParagraphParameters const & params)
509 // FIXME Are there other things we should handle here?
510 string const align = alignmentToCSS(params.align());
515 string attrs = lay.htmlattr() + " style='text-align: " + align + ";'";
516 xs << html::StartTag(lay.htmltag(), attrs);
520 inline void closeTag(XHTMLStream & xs, Layout const & lay)
522 xs << html::EndTag(lay.htmltag());
526 inline void openLabelTag(XHTMLStream & xs, Layout const & lay)
528 xs << html::StartTag(lay.htmllabeltag(), lay.htmllabelattr());
532 inline void closeLabelTag(XHTMLStream & xs, Layout const & lay)
534 xs << html::EndTag(lay.htmllabeltag());
538 inline void openItemTag(XHTMLStream & xs, Layout const & lay)
540 xs << html::StartTag(lay.htmlitemtag(), lay.htmlitemattr(), true);
544 void openItemTag(XHTMLStream & xs, Layout const & lay,
545 ParagraphParameters const & params)
547 // FIXME Are there other things we should handle here?
548 string const align = alignmentToCSS(params.align());
550 openItemTag(xs, lay);
553 string attrs = lay.htmlattr() + " style='text-align: " + align + ";'";
554 xs << html::StartTag(lay.htmlitemtag(), attrs);
558 inline void closeItemTag(XHTMLStream & xs, Layout const & lay)
560 xs << html::EndTag(lay.htmlitemtag());
563 // end of convenience functions
565 ParagraphList::const_iterator searchParagraphHtml(
566 ParagraphList::const_iterator p,
567 ParagraphList::const_iterator const & pend)
569 for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
576 ParagraphList::const_iterator searchEnvironmentHtml(
577 ParagraphList::const_iterator const pstart,
578 ParagraphList::const_iterator const & pend)
580 ParagraphList::const_iterator p = pstart;
581 Layout const & bstyle = p->layout();
582 size_t const depth = p->params().depth();
583 for (++p; p != pend; ++p) {
584 Layout const & style = p->layout();
585 // It shouldn't happen that e.g. a section command occurs inside
586 // a quotation environment, at a higher depth, but as of 6/2009,
587 // it can happen. We pretend that it's just at lowest depth.
588 if (style.latextype == LATEX_COMMAND)
590 // If depth is down, we're done
591 if (p->params().depth() < depth)
593 // If depth is up, we're not done
594 if (p->params().depth() > depth)
596 // Now we know we are at the same depth
597 if (style.latextype == LATEX_PARAGRAPH
598 || style.latexname() != bstyle.latexname())
605 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
607 OutputParams const & runparams,
609 ParagraphList::const_iterator const & pbegin,
610 ParagraphList::const_iterator const & pend)
612 ParagraphList::const_iterator const begin = text.paragraphs().begin();
613 ParagraphList::const_iterator par = pbegin;
614 for (; par != pend; ++par) {
615 Layout const & lay = par->layout();
616 if (!lay.counter.empty())
617 buf.params().documentClass().counters().step(lay.counter, OutputUpdate);
618 // FIXME We should see if there's a label to be output and
619 // do something with it.
623 // If we are already in a paragraph, and this is the first one, then we
624 // do not want to open the paragraph tag.
625 // we also do not want to open it if the current layout does not permit
626 // multiple paragraphs.
627 bool const opened = runparams.html_make_pars &&
628 (par != pbegin || !runparams.html_in_par);
630 openTag(xs, lay, par->params());
631 docstring const deferred =
632 par->simpleLyXHTMLOnePar(buf, xs, runparams, text.outerFont(distance(begin, par)));
634 // We want to issue the closing tag if either:
635 // (i) We opened it, and either html_in_par is false,
636 // or we're not in the last paragraph, anyway.
637 // (ii) We didn't open it and html_in_par is true,
638 // but we are in the first par, and there is a next par.
639 ParagraphList::const_iterator nextpar = par;
641 bool const needclose =
642 (opened && (!runparams.html_in_par || nextpar != pend))
643 || (!opened && runparams.html_in_par && par == pbegin && nextpar != pend);
648 if (!deferred.empty()) {
649 xs << XHTMLStream::NextRaw() << deferred;
657 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
659 OutputParams const & runparams,
661 ParagraphList::const_iterator const & pbegin,
662 ParagraphList::const_iterator const & pend)
665 // Use TextClass::htmlTOCLayout() to figure out how we should look.
666 xs << html::StartTag("h2", "class='bibliography'");
667 xs << pbegin->layout().labelstring(false);
668 xs << html::EndTag("h2");
670 xs << html::StartTag("div", "class='bibliography'");
672 makeParagraphs(buf, xs, runparams, text, pbegin, pend);
673 xs << html::EndTag("div");
678 bool isNormalEnv(Layout const & lay)
680 return lay.latextype == LATEX_ENVIRONMENT
681 || lay.latextype == LATEX_BIB_ENVIRONMENT;
685 ParagraphList::const_iterator makeEnvironmentHtml(Buffer const & buf,
687 OutputParams const & runparams,
689 ParagraphList::const_iterator const & pbegin,
690 ParagraphList::const_iterator const & pend)
692 ParagraphList::const_iterator const begin = text.paragraphs().begin();
693 ParagraphList::const_iterator par = pbegin;
694 Layout const & bstyle = par->layout();
695 depth_type const origdepth = pbegin->params().depth();
697 // open tag for this environment
701 // we will on occasion need to remember a layout from before.
702 Layout const * lastlay = 0;
704 while (par != pend) {
705 Layout const & style = par->layout();
706 // the counter only gets stepped if we're in some kind of list,
707 // or if it's the first time through.
708 // note that enum, etc, are handled automatically.
709 // FIXME There may be a bug here about user defined enumeration
710 // types. If so, then we'll need to take the counter and add "i",
711 // "ii", etc, as with enum.
712 Counters & cnts = buf.params().documentClass().counters();
713 docstring const & cntr = style.counter;
714 if (!style.counter.empty()
715 && (par == pbegin || !isNormalEnv(style))
716 && cnts.hasCounter(cntr)
718 cnts.step(cntr, OutputUpdate);
719 ParagraphList::const_iterator send;
720 // this will be positive, if we want to skip the initial word
721 // (if it's been taken for the label).
724 switch (style.latextype) {
725 case LATEX_ENVIRONMENT:
726 case LATEX_LIST_ENVIRONMENT:
727 case LATEX_ITEM_ENVIRONMENT: {
728 // There are two possiblities in this case.
729 // One is that we are still in the environment in which we
730 // started---which we will be if the depth is the same.
731 if (par->params().depth() == origdepth) {
732 LASSERT(bstyle == style, /* */);
734 closeItemTag(xs, *lastlay);
738 bool const labelfirst = style.htmllabelfirst();
740 openItemTag(xs, style, par->params());
743 if (style.labeltype != LABEL_NO_LABEL &&
744 style.htmllabeltag() != "NONE") {
745 if (isNormalEnv(style)) {
746 // in this case, we print the label only for the first
747 // paragraph (as in a theorem).
749 docstring const lbl =
750 pbegin->params().labelString();
752 openLabelTag(xs, style);
754 closeLabelTag(xs, style);
758 } else { // some kind of list
759 if (style.labeltype == LABEL_MANUAL) {
760 openLabelTag(xs, style);
761 sep = par->firstWordLyXHTML(xs, runparams);
762 closeLabelTag(xs, style);
766 openLabelTag(xs, style);
767 xs << par->params().labelString();
768 closeLabelTag(xs, style);
772 } // end label output
775 openItemTag(xs, style, par->params());
777 par->simpleLyXHTMLOnePar(buf, xs, runparams,
778 text.outerFont(distance(begin, par)), sep);
781 // We may not want to close the tag yet, in particular:
782 // If we're not at the end...
784 // and are doing items...
785 && !isNormalEnv(style)
786 // and if the depth has changed...
787 && par->params().depth() != origdepth) {
788 // then we'll save this layout for later, and close it when
789 // we get another item.
792 closeItemTag(xs, style);
795 // The other possibility is that the depth has increased, in which
796 // case we need to recurse.
798 send = searchEnvironmentHtml(par, pend);
799 par = makeEnvironmentHtml(buf, xs, runparams, text, par, send);
803 case LATEX_PARAGRAPH:
804 send = searchParagraphHtml(par, pend);
805 par = makeParagraphs(buf, xs, runparams, text, par, send);
808 case LATEX_BIB_ENVIRONMENT:
811 par = makeParagraphs(buf, xs, runparams, text, par, send);
821 closeItemTag(xs, *lastlay);
822 closeTag(xs, bstyle);
828 void makeCommand(Buffer const & buf,
830 OutputParams const & runparams,
832 ParagraphList::const_iterator const & pbegin)
834 Layout const & style = pbegin->layout();
835 if (!style.counter.empty())
836 buf.params().documentClass().counters().step(style.counter, OutputUpdate);
840 // Label around sectioning number:
841 // FIXME Probably need to account for LABEL_MANUAL
842 if (style.labeltype != LABEL_NO_LABEL) {
843 openLabelTag(xs, style);
844 xs << pbegin->params().labelString();
845 closeLabelTag(xs, style);
846 // Otherwise the label might run together with the text
847 xs << from_ascii(" ");
850 ParagraphList::const_iterator const begin = text.paragraphs().begin();
851 pbegin->simpleLyXHTMLOnePar(buf, xs, runparams,
852 text.outerFont(distance(begin, pbegin)));
857 } // end anonymous namespace
860 void xhtmlParagraphs(Text const & text,
863 OutputParams const & runparams)
865 ParagraphList const & paragraphs = text.paragraphs();
866 ParagraphList::const_iterator par = paragraphs.begin();
867 ParagraphList::const_iterator pend = paragraphs.end();
869 OutputParams ourparams = runparams;
870 while (par != pend) {
871 if (par->params().startOfAppendix()) {
872 // FIXME: only the counter corresponding to toplevel
873 // sectioning should be reset
874 Counters & cnts = buf.masterBuffer()->params().documentClass().counters();
878 Layout const & style = par->layout();
879 ParagraphList::const_iterator lastpar = par;
880 ParagraphList::const_iterator send;
882 switch (style.latextype) {
883 case LATEX_COMMAND: {
884 // The files with which we are working never have more than
885 // one paragraph in a command structure.
887 // if (ourparams.html_in_par)
888 // fix it so we don't get sections inside standard, e.g.
889 // note that we may then need to make runparams not const, so we
890 // can communicate that back.
891 // FIXME Maybe this fix should be in the routines themselves, in case
892 // they are called from elsewhere.
893 makeCommand(buf, xs, ourparams, text, par);
897 case LATEX_ENVIRONMENT:
898 case LATEX_LIST_ENVIRONMENT:
899 case LATEX_ITEM_ENVIRONMENT: {
900 // FIXME Same fix here.
901 send = searchEnvironmentHtml(par, pend);
902 par = makeEnvironmentHtml(buf, xs, ourparams, text, par, send);
905 case LATEX_BIB_ENVIRONMENT: {
906 // FIXME Same fix here.
907 send = searchEnvironmentHtml(par, pend);
908 par = makeBibliography(buf, xs, ourparams, text, par, send);
911 case LATEX_PARAGRAPH:
912 send = searchParagraphHtml(par, pend);
913 par = makeParagraphs(buf, xs, ourparams, text, par, send);
917 // makeEnvironment may process more than one paragraphs and bypass pend
918 if (distance(lastpar, par) >= distance(lastpar, pend))
924 string alignmentToCSS(LyXAlignment align) {
926 case LYX_ALIGN_BLOCK:
927 // we are NOT going to use text-align: justify!!
930 case LYX_ALIGN_RIGHT:
932 case LYX_ALIGN_CENTER: