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/convert.h"
31 #include "support/debug.h"
32 #include "support/lassert.h"
33 #include "support/lstrings.h"
38 using namespace lyx::support;
44 docstring escapeChar(char_type c)
68 // escape what needs escaping
69 docstring htmlize(docstring const & str) {
71 docstring::const_iterator it = str.begin();
72 docstring::const_iterator en = str.end();
73 for (; it != en; ++it)
79 string escapeChar(char c)
103 // escape what needs escaping
104 string htmlize(string const & str) {
106 string::const_iterator it = str.begin();
107 string::const_iterator en = str.end();
108 for (; it != en; ++it)
109 d << escapeChar(*it);
114 string cleanAttr(string const & str)
117 string::const_iterator it = str.begin();
118 string::const_iterator en = str.end();
119 for (; it != en; ++it)
120 newname += isalnum(*it) ? *it : '_';
125 docstring cleanAttr(docstring const & str)
128 docstring::const_iterator it = str.begin();
129 docstring::const_iterator en = str.end();
130 for (; it != en; ++it)
139 bool isFontTag(string const & s)
141 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);
180 ////////////////////////////////////////////////////////////////
184 ////////////////////////////////////////////////////////////////
186 XHTMLStream::XHTMLStream(odocstream & os)
187 : os_(os), nextraw_(false)
191 void XHTMLStream::cr()
194 os_ << from_ascii("\n");
198 void XHTMLStream::writeError(std::string const & s)
201 os_ << from_utf8("<!-- Output Error: " + s + " -->");
205 bool XHTMLStream::closeFontTags()
207 if (tag_stack_.empty())
209 // first, we close any open font tags we can close
210 StartTag curtag = tag_stack_.back();
211 while (html::isFontTag(curtag.tag_)) {
212 os_ << curtag.asEndTag();
213 tag_stack_.pop_back();
214 if (tag_stack_.empty())
215 // this probably shouldn't happen, since then the
216 // font tags weren't in any other tag. but that
217 // problem will likely be caught elsewhere.
219 curtag = tag_stack_.back();
221 // so we've hit a non-font tag. let's see if any of the
222 // remaining tags are font tags.
223 TagStack::const_iterator it = tag_stack_.begin();
224 TagStack::const_iterator en = tag_stack_.end();
225 bool noFontTags = true;
226 for (; it != en; ++it) {
227 if (html::isFontTag(it->tag_)) {
228 writeError("Font tag `" + it->tag_ + "' still open in closeFontTags().");
236 void XHTMLStream::clearTagDeque()
238 while (!pending_tags_.empty()) {
239 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<<(int i)
294 XHTMLStream & XHTMLStream::operator<<(NextRaw const &)
301 XHTMLStream & XHTMLStream::operator<<(StartTag const & tag)
303 if (tag.tag_.empty())
305 pending_tags_.push_back(tag);
312 XHTMLStream & XHTMLStream::operator<<(CompTag const & tag)
314 if (tag.tag_.empty())
324 bool XHTMLStream::isTagOpen(string const & stag)
326 TagStack::const_iterator sit = tag_stack_.begin();
327 TagStack::const_iterator const sen = tag_stack_.end();
328 for (; sit != sen; ++sit)
329 // we could check for the
330 if (sit->tag_ == stag)
336 // this is complicated, because we want to make sure that
337 // everything is properly nested. the code ought to make
338 // sure of that, but we won't assert (yet) if we run into
339 // a problem. we'll just output error messages and try our
340 // best to make things work.
341 XHTMLStream & XHTMLStream::operator<<(EndTag const & etag)
343 if (etag.tag_.empty())
345 // first make sure we're not closing an empty tag
346 if (!pending_tags_.empty()) {
347 StartTag const & stag = pending_tags_.back();
348 if (etag.tag_ == stag.tag_) {
349 // we have <tag></tag>, so we discard it and remove it
350 // from the pending_tags_.
351 pending_tags_.pop_back();
354 // there is a pending tag that isn't the one we are trying
356 // is this tag itself pending?
357 // non-const iterators because we may call erase().
358 TagDeque::iterator dit = pending_tags_.begin();
359 TagDeque::iterator const den = pending_tags_.end();
360 for (; dit != den; ++dit) {
361 if (dit->tag_ == etag.tag_) {
362 // it was pending, so we just erase it
363 writeError("Tried to close pending tag `" + etag.tag_
364 + "' when other tags were pending. Last pending tag is `"
365 + pending_tags_.back().tag_ + "'. Tag discarded.");
366 pending_tags_.erase(dit);
370 // so etag isn't itself pending. is it even open?
371 if (!isTagOpen(etag.tag_)) {
372 writeError("Tried to close `" + etag.tag_
373 + "' when tag was not open. Tag discarded.");
376 // ok, so etag is open.
377 // our strategy will be as below: we will do what we need to
378 // do to close this tag.
379 string estr = "Closing tag `" + etag.tag_
380 + "' when other tags are pending. Discarded pending tags:\n";
381 for (dit = pending_tags_.begin(); dit != den; ++dit)
382 estr += dit->tag_ + "\n";
384 // clear the pending tags...
385 pending_tags_.clear();
386 // ...and then just fall through.
389 // is the tag we are closing the last one we opened?
390 if (etag.tag_ == tag_stack_.back().tag_) {
392 os_ << etag.asEndTag();
393 // ...and forget about it
394 tag_stack_.pop_back();
398 // we are trying to close a tag other than the one last opened.
399 // let's first see if this particular tag is still open somehow.
400 if (!isTagOpen(etag.tag_)) {
401 writeError("Tried to close `" + etag.tag_
402 + "' when tag was not open. Tag discarded.");
406 // so the tag was opened, but other tags have been opened since
407 // and not yet closed.
408 // if it's a font tag, though...
409 if (html::isFontTag(etag.tag_)) {
410 // it won't be a problem if the other tags open since this one
411 // are also font tags.
412 TagStack::const_reverse_iterator rit = tag_stack_.rbegin();
413 TagStack::const_reverse_iterator ren = tag_stack_.rend();
414 for (; rit != ren; ++rit) {
415 if (rit->tag_ == etag.tag_)
417 if (!html::isFontTag(rit->tag_)) {
418 // we'll just leave it and, presumably, have to close it later.
419 writeError("Unable to close font tag `" + etag.tag_
420 + "' due to open non-font tag `" + rit->tag_ + "'.");
426 // <em>this is <strong>bold
427 // and are being asked to closed em. we want:
428 // <em>this is <strong>bold</strong></em><strong>
429 // first, we close the intervening tags...
430 StartTag curtag = tag_stack_.back();
431 // ...remembering them in a stack.
433 while (curtag.tag_ != etag.tag_) {
434 os_ << curtag.asEndTag();
435 fontstack.push_back(curtag);
436 tag_stack_.pop_back();
437 curtag = tag_stack_.back();
439 // now close our tag...
440 os_ << etag.asEndTag();
441 tag_stack_.pop_back();
443 // ...and restore the other tags.
444 rit = fontstack.rbegin();
445 ren = fontstack.rend();
446 for (; rit != ren; ++rit)
447 pending_tags_.push_back(*rit);
451 // it wasn't a font tag.
452 // so other tags were opened before this one and not properly closed.
453 // so we'll close them, too. that may cause other issues later, but it
454 // at least guarantees proper nesting.
455 writeError("Closing tag `" + etag.tag_
456 + "' when other tags are open, namely:");
457 StartTag curtag = tag_stack_.back();
458 while (curtag.tag_ != etag.tag_) {
459 writeError(curtag.tag_);
460 os_ << curtag.asEndTag();
461 tag_stack_.pop_back();
462 curtag = tag_stack_.back();
464 // curtag is now the one we actually want.
465 os_ << curtag.asEndTag();
466 tag_stack_.pop_back();
471 // End code for XHTMLStream
475 // convenience functions
477 inline void openTag(XHTMLStream & xs, Layout const & lay)
479 xs << StartTag(lay.htmltag(), lay.htmlattr());
483 inline void closeTag(XHTMLStream & xs, Layout const & lay)
485 xs << EndTag(lay.htmltag());
489 inline void openLabelTag(XHTMLStream & xs, Layout const & lay)
491 xs << StartTag(lay.htmllabeltag(), lay.htmllabelattr());
495 inline void closeLabelTag(XHTMLStream & xs, Layout const & lay)
497 xs << EndTag(lay.htmllabeltag());
501 inline void openItemTag(XHTMLStream & xs, Layout const & lay)
503 xs << StartTag(lay.htmlitemtag(), lay.htmlitemattr(), true);
507 inline void closeItemTag(XHTMLStream & xs, Layout const & lay)
509 xs << EndTag(lay.htmlitemtag());
512 // end of convenience functions
514 ParagraphList::const_iterator searchParagraphHtml(
515 ParagraphList::const_iterator p,
516 ParagraphList::const_iterator const & pend)
518 for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
525 ParagraphList::const_iterator searchEnvironmentHtml(
526 ParagraphList::const_iterator const pstart,
527 ParagraphList::const_iterator const & pend)
529 ParagraphList::const_iterator p = pstart;
530 Layout const & bstyle = p->layout();
531 size_t const depth = p->params().depth();
532 for (++p; p != pend; ++p) {
533 Layout const & style = p->layout();
534 // It shouldn't happen that e.g. a section command occurs inside
535 // a quotation environment, at a higher depth, but as of 6/2009,
536 // it can happen. We pretend that it's just at lowest depth.
537 if (style.latextype == LATEX_COMMAND)
539 // If depth is down, we're done
540 if (p->params().depth() < depth)
542 // If depth is up, we're not done
543 if (p->params().depth() > depth)
545 // Now we know we are at the same depth
546 if (style.latextype == LATEX_PARAGRAPH
547 || style.latexname() != bstyle.latexname())
554 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
556 OutputParams const & runparams,
558 ParagraphList::const_iterator const & pbegin,
559 ParagraphList::const_iterator const & pend)
561 ParagraphList::const_iterator const begin = text.paragraphs().begin();
562 ParagraphList::const_iterator par = pbegin;
563 for (; par != pend; ++par) {
564 Layout const & lay = par->layout();
565 if (!lay.counter.empty())
566 buf.params().documentClass().counters().step(lay.counter);
567 // FIXME We should see if there's a label to be output and
568 // do something with it.
572 // If we are already in a paragraph, and this is the first one, then we
573 // do not want to open the paragraph tag.
574 // we also do not want to open it if the current layout does not permit
575 // multiple paragraphs.
576 bool const opened = runparams.html_make_pars &&
577 (par != pbegin || !runparams.html_in_par);
580 docstring const deferred =
581 par->simpleLyXHTMLOnePar(buf, xs, runparams, text.outerFont(distance(begin, par)));
583 // We want to issue the closing tag if either:
584 // (i) We opened it, and either html_in_par is false,
585 // or we're not in the last paragraph, anyway.
586 // (ii) We didn't open it and html_in_par is true,
587 // but we are in the first par, and there is a next par.
588 ParagraphList::const_iterator nextpar = par;
590 bool const needclose =
591 (opened && (!runparams.html_in_par || nextpar != pend))
592 || (!opened && runparams.html_in_par && par == pbegin && nextpar != pend);
597 if (!deferred.empty()) {
598 xs << XHTMLStream::NextRaw() << deferred;
606 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
608 OutputParams const & runparams,
610 ParagraphList::const_iterator const & pbegin,
611 ParagraphList::const_iterator const & pend)
613 xs << StartTag("h2", "class='bibliography'");
614 xs << pbegin->layout().labelstring(false);
617 xs << StartTag("div", "class='bibliography'");
619 makeParagraphs(buf, xs, runparams, text, pbegin, pend);
625 bool isNormalEnv(Layout const & lay)
627 return lay.latextype == LATEX_ENVIRONMENT
628 || lay.latextype == LATEX_BIB_ENVIRONMENT;
632 ParagraphList::const_iterator makeEnvironmentHtml(Buffer const & buf,
634 OutputParams const & runparams,
636 ParagraphList::const_iterator const & pbegin,
637 ParagraphList::const_iterator const & pend)
639 ParagraphList::const_iterator const begin = text.paragraphs().begin();
640 ParagraphList::const_iterator par = pbegin;
641 Layout const & bstyle = par->layout();
642 depth_type const origdepth = pbegin->params().depth();
644 // open tag for this environment
648 // we will on occasion need to remember a layout from before.
649 Layout const * lastlay = 0;
651 while (par != pend) {
652 Layout const & style = par->layout();
653 // the counter only gets stepped if we're in some kind of list,
654 // or if it's the first time through.
655 // note that enum, etc, are handled automatically.
656 // FIXME There may be a bug here about user defined enumeration
657 // types. If so, then we'll need to take the counter and add "i",
658 // "ii", etc, as with enum.
659 Counters & cnts = buf.params().documentClass().counters();
660 docstring const & cntr = style.counter;
661 if (!style.counter.empty()
662 && (par == pbegin || !isNormalEnv(style))
663 && cnts.hasCounter(cntr)
666 ParagraphList::const_iterator send;
667 // this will be positive, if we want to skip the initial word
668 // (if it's been taken for the label).
671 switch (style.latextype) {
672 case LATEX_ENVIRONMENT:
673 case LATEX_LIST_ENVIRONMENT:
674 case LATEX_ITEM_ENVIRONMENT: {
675 // There are two possiblities in this case.
676 // One is that we are still in the environment in which we
677 // started---which we will be if the depth is the same.
678 if (par->params().depth() == origdepth) {
679 LASSERT(bstyle == style, /* */);
681 closeItemTag(xs, *lastlay);
684 if (isNormalEnv(style)) {
685 // in this case, we print the label only for the first
686 // paragraph (as in a theorem).
687 openItemTag(xs, style);
688 if (par == pbegin && style.htmllabeltag() != "NONE") {
689 docstring const lbl =
690 pbegin->expandLabel(style, buf.params(), false);
692 openLabelTag(xs, style);
694 closeLabelTag(xs, style);
698 } else { // some kind of list
699 bool const labelfirst = style.htmllabelfirst();
701 openItemTag(xs, style);
702 if (style.labeltype == LABEL_MANUAL
703 && style.htmllabeltag() != "NONE") {
704 openLabelTag(xs, style);
705 sep = par->firstWordLyXHTML(xs, runparams);
706 closeLabelTag(xs, style);
709 else if (style.labeltype != LABEL_NO_LABEL
710 && style.htmllabeltag() != "NONE") {
711 openLabelTag(xs, style);
712 xs << par->expandLabel(style, buf.params(), false);
713 closeLabelTag(xs, style);
717 openItemTag(xs, style);
719 par->simpleLyXHTMLOnePar(buf, xs, runparams,
720 text.outerFont(distance(begin, par)), false, sep);
722 // We may not want to close the tag yet, in particular,
723 // if we're not at the end...
725 // and are doing items...
726 && !isNormalEnv(style)
727 // and if the depth has changed...
728 && par->params().depth() != origdepth) {
729 // then we'll save this layout for later, and close it when
730 // we get another item.
733 closeItemTag(xs, style);
736 // The other possibility is that the depth has increased, in which
737 // case we need to recurse.
739 send = searchEnvironmentHtml(par, pend);
740 par = makeEnvironmentHtml(buf, xs, runparams, text, par, send);
744 case LATEX_PARAGRAPH:
745 send = searchParagraphHtml(par, pend);
746 par = makeParagraphs(buf, xs, runparams, text, par, send);
749 case LATEX_BIB_ENVIRONMENT:
752 par = makeParagraphs(buf, xs, runparams, text, par, send);
762 closeItemTag(xs, *lastlay);
763 closeTag(xs, bstyle);
769 void makeCommand(Buffer const & buf,
771 OutputParams const & runparams,
773 ParagraphList::const_iterator const & pbegin)
775 Layout const & style = pbegin->layout();
776 if (!style.counter.empty())
777 buf.params().documentClass().counters().step(style.counter);
781 // Label around sectioning number:
782 // FIXME Probably need to account for LABEL_MANUAL
783 if (style.labeltype != LABEL_NO_LABEL) {
784 openLabelTag(xs, style);
785 xs << pbegin->expandLabel(style, buf.params(), false);
786 closeLabelTag(xs, style);
787 // Otherwise the label might run together with the text
788 xs << from_ascii(" ");
791 ParagraphList::const_iterator const begin = text.paragraphs().begin();
792 pbegin->simpleLyXHTMLOnePar(buf, xs, runparams,
793 text.outerFont(distance(begin, pbegin)));
798 } // end anonymous namespace
801 void xhtmlParagraphs(Text const & text,
804 OutputParams const & runparams)
806 ParagraphList const & paragraphs = text.paragraphs();
807 ParagraphList::const_iterator par = paragraphs.begin();
808 ParagraphList::const_iterator pend = paragraphs.end();
810 OutputParams ourparams = runparams;
811 while (par != pend) {
812 Layout const & style = par->layout();
813 ParagraphList::const_iterator lastpar = par;
814 ParagraphList::const_iterator send;
816 switch (style.latextype) {
817 case LATEX_COMMAND: {
818 // The files with which we are working never have more than
819 // one paragraph in a command structure.
821 // if (ourparams.html_in_par)
822 // fix it so we don't get sections inside standard, e.g.
823 // note that we may then need to make runparams not const, so we
824 // can communicate that back.
825 // FIXME Maybe this fix should be in the routines themselves, in case
826 // they are called from elsewhere.
827 makeCommand(buf, xs, ourparams, text, par);
831 case LATEX_ENVIRONMENT:
832 case LATEX_LIST_ENVIRONMENT:
833 case LATEX_ITEM_ENVIRONMENT: {
834 // FIXME Same fix here.
835 send = searchEnvironmentHtml(par, pend);
836 par = makeEnvironmentHtml(buf, xs, ourparams, text, par, send);
839 case LATEX_BIB_ENVIRONMENT: {
840 // FIXME Same fix here.
841 send = searchEnvironmentHtml(par, pend);
842 par = makeBibliography(buf, xs, ourparams, text, par, send);
845 case LATEX_PARAGRAPH:
846 send = searchParagraphHtml(par, pend);
847 par = makeParagraphs(buf, xs, ourparams, text, par, send);
851 // makeEnvironment may process more than one paragraphs and bypass pend
852 if (distance(lastpar, par) >= distance(lastpar, pend))