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 bool isFontTag(string const & s)
115 return s == "em" || s == "strong"; // others?
120 docstring StartTag::asTag() const
122 string output = "<" + tag_;
124 output += " " + html::htmlize(attr_);
126 return from_utf8(output);
130 docstring StartTag::asEndTag() const
132 string output = "</" + tag_ + ">";
133 return from_utf8(output);
137 docstring EndTag::asEndTag() const
139 string output = "</" + tag_ + ">";
140 return from_utf8(output);
144 docstring CompTag::asTag() const
146 string output = "<" + tag_;
148 output += " " + html::htmlize(attr_);
150 return from_utf8(output);
154 ////////////////////////////////////////////////////////////////
158 ////////////////////////////////////////////////////////////////
160 XHTMLStream::XHTMLStream(odocstream & os)
161 : os_(os), nextraw_(false)
165 void XHTMLStream::cr()
168 os_ << from_ascii("\n");
172 void XHTMLStream::writeError(std::string const & s)
175 os_ << from_utf8("<!-- Output Error: " + s + " -->");
179 bool XHTMLStream::closeFontTags()
181 if (tag_stack_.empty())
183 // first, we close any open font tags we can close
184 StartTag curtag = tag_stack_.back();
185 while (html::isFontTag(curtag.tag_)) {
186 os_ << curtag.asEndTag();
187 tag_stack_.pop_back();
188 if (tag_stack_.empty())
189 // this probably shouldn't happen, since then the
190 // font tags weren't in any other tag. but that
191 // problem will likely be caught elsewhere.
193 curtag = tag_stack_.back();
195 // so we've hit a non-font tag. let's see if any of the
196 // remaining tags are font tags.
197 TagStack::const_iterator it = tag_stack_.begin();
198 TagStack::const_iterator en = tag_stack_.end();
199 bool noFontTags = true;
200 for (; it != en; ++it) {
201 if (html::isFontTag(it->tag_)) {
202 writeError("Font tag `" + it->tag_ + "' still open in closeFontTags().");
210 void XHTMLStream::clearTagDeque()
212 while (!pending_tags_.empty()) {
213 StartTag const & tag = pending_tags_.front();
216 tag_stack_.push_back(tag);
217 pending_tags_.pop_front();
222 XHTMLStream & XHTMLStream::operator<<(docstring const & d)
229 os_ << html::htmlize(d);
234 XHTMLStream & XHTMLStream::operator<<(const char * s)
237 docstring const d = from_ascii(s);
242 os_ << html::htmlize(d);
247 XHTMLStream & XHTMLStream::operator<<(char_type c)
254 os_ << html::escapeChar(c);
259 XHTMLStream & XHTMLStream::operator<<(NextRaw const &)
266 XHTMLStream & XHTMLStream::operator<<(StartTag const & tag)
268 if (tag.tag_.empty())
270 pending_tags_.push_back(tag);
277 XHTMLStream & XHTMLStream::operator<<(CompTag const & tag)
279 if (tag.tag_.empty())
288 bool XHTMLStream::isTagOpen(string const & stag)
290 TagStack::const_iterator sit = tag_stack_.begin();
291 TagStack::const_iterator const sen = tag_stack_.end();
292 for (; sit != sen; ++sit)
293 // we could check for the
294 if (sit->tag_ == stag)
300 // this is complicated, because we want to make sure that
301 // everything is properly nested. the code ought to make
302 // sure of that, but we won't assert (yet) if we run into
303 // a problem. we'll just output error messages and try our
304 // best to make things work.
305 XHTMLStream & XHTMLStream::operator<<(EndTag const & etag)
307 if (etag.tag_.empty())
309 // first make sure we're not closing an empty tag
310 if (!pending_tags_.empty()) {
311 StartTag const & stag = pending_tags_.back();
312 if (etag.tag_ == stag.tag_) {
313 // we have <tag></tag>, so we discard it and remove it
314 // from the pending_tags_.
315 pending_tags_.pop_back();
318 // there is a pending tag that isn't the one we are trying
320 // is this tag itself pending?
321 // non-const iterators because we may call erase().
322 TagDeque::iterator dit = pending_tags_.begin();
323 TagDeque::iterator const den = pending_tags_.end();
324 for (; dit != den; ++dit) {
325 if (dit->tag_ == etag.tag_) {
326 // it was pending, so we just erase it
327 writeError("Tried to close pending tag `" + etag.tag_
328 + "' when other tags were pending. Tag discarded.");
329 pending_tags_.erase(dit);
333 // so etag isn't itself pending. is it even open?
334 if (!isTagOpen(etag.tag_)) {
335 writeError("Tried to close `" + etag.tag_
336 + "' when tag was not open. Tag discarded.");
339 // ok, so etag is open.
340 // our strategy will be as below: we will do what we need to
341 // do to close this tag.
342 string estr = "Closing tag `" + etag.tag_
343 + "' when other tags are pending. Discarded pending tags:\n";
344 for (dit = pending_tags_.begin(); dit != den; ++dit)
345 estr += dit->tag_ + "\n";
347 // clear the pending tags...
348 pending_tags_.clear();
349 // ...and then just fall through.
352 // is the tag we are closing the last one we opened?
353 if (etag.tag_ == tag_stack_.back().tag_) {
355 os_ << etag.asEndTag();
356 // ...and forget about it
357 tag_stack_.pop_back();
361 // we are trying to close a tag other than the one last opened.
362 // let's first see if this particular tag is still open somehow.
363 if (!isTagOpen(etag.tag_)) {
364 writeError("Tried to close `" + etag.tag_
365 + "' when tag was not open. Tag discarded.");
369 // so the tag was opened, but other tags have been opened since
370 // and not yet closed.
371 // if it's a font tag, though...
372 if (html::isFontTag(etag.tag_)) {
373 // it won't be a problem if the other tags open since this one
374 // are also font tags.
375 TagStack::const_reverse_iterator rit = tag_stack_.rbegin();
376 TagStack::const_reverse_iterator ren = tag_stack_.rend();
377 for (; rit != ren; ++rit) {
378 if (rit->tag_ == etag.tag_)
380 if (!html::isFontTag(rit->tag_)) {
381 // we'll just leave it and, presumably, have to close it later.
382 writeError("Unable to close font tag `" + etag.tag_
383 + "' due to open non-font tag `" + rit->tag_ + "'.");
389 // <em>this is <strong>bold
390 // and are being asked to closed em. we want:
391 // <em>this is <strong>bold</strong></em><strong>
392 // first, we close the intervening tags...
393 StartTag curtag = tag_stack_.back();
394 // ...remembering them in a stack.
396 while (curtag.tag_ != etag.tag_) {
397 os_ << curtag.asEndTag();
398 fontstack.push_back(curtag);
399 tag_stack_.pop_back();
400 curtag = tag_stack_.back();
402 // now close our tag...
403 os_ << etag.asEndTag();
404 tag_stack_.pop_back();
406 // ...and restore the other tags.
407 rit = fontstack.rbegin();
408 ren = fontstack.rend();
409 for (; rit != ren; ++rit)
410 pending_tags_.push_back(*rit);
414 // it wasn't a font tag.
415 // so other tags were opened before this one and not properly closed.
416 // so we'll close them, too. that may cause other issues later, but it
417 // at least guarantees proper nesting.
418 writeError("Closing tag `" + etag.tag_
419 + "' when other tags are open, namely:");
420 StartTag curtag = tag_stack_.back();
421 while (curtag.tag_ != etag.tag_) {
422 writeError(curtag.tag_);
423 os_ << curtag.asEndTag();
424 tag_stack_.pop_back();
425 curtag = tag_stack_.back();
427 // curtag is now the one we actually want.
428 os_ << curtag.asEndTag();
429 tag_stack_.pop_back();
434 // End code for XHTMLStream
438 // convenience functions
440 inline void openTag(XHTMLStream & xs, Layout const & lay)
442 xs << StartTag(lay.htmltag(), lay.htmlattr());
446 inline void closeTag(XHTMLStream & xs, Layout const & lay)
448 xs << EndTag(lay.htmltag());
452 inline void openLabelTag(XHTMLStream & xs, Layout const & lay)
454 xs << StartTag(lay.htmllabeltag(), lay.htmllabelattr());
458 inline void closeLabelTag(XHTMLStream & xs, Layout const & lay)
460 xs << EndTag(lay.htmllabeltag());
464 inline void openItemTag(XHTMLStream & xs, Layout const & lay)
466 xs << StartTag(lay.htmlitemtag(), lay.htmlitemattr(), true);
470 inline void closeItemTag(XHTMLStream & xs, Layout const & lay)
472 xs << EndTag(lay.htmlitemtag());
475 // end of convenience functions
477 ParagraphList::const_iterator searchParagraphHtml(
478 ParagraphList::const_iterator p,
479 ParagraphList::const_iterator const & pend)
481 for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
488 ParagraphList::const_iterator searchEnvironmentHtml(
489 ParagraphList::const_iterator const pstart,
490 ParagraphList::const_iterator const & pend)
492 ParagraphList::const_iterator p = pstart;
493 Layout const & bstyle = p->layout();
494 size_t const depth = p->params().depth();
495 for (++p; p != pend; ++p) {
496 Layout const & style = p->layout();
497 // It shouldn't happen that e.g. a section command occurs inside
498 // a quotation environment, at a higher depth, but as of 6/2009,
499 // it can happen. We pretend that it's just at lowest depth.
500 if (style.latextype == LATEX_COMMAND)
502 // If depth is down, we're done
503 if (p->params().depth() < depth)
505 // If depth is up, we're not done
506 if (p->params().depth() > depth)
508 // Now we know we are at the same depth
509 if (style.latextype == LATEX_PARAGRAPH
510 || style.latexname() != bstyle.latexname())
517 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
519 OutputParams const & runparams,
521 ParagraphList::const_iterator const & pbegin,
522 ParagraphList::const_iterator const & pend)
524 ParagraphList::const_iterator const begin = text.paragraphs().begin();
525 ParagraphList::const_iterator par = pbegin;
526 for (; par != pend; ++par) {
527 Layout const & lay = par->layout();
528 if (!lay.counter.empty())
529 buf.params().documentClass().counters().step(lay.counter);
530 // FIXME We should see if there's a label to be output and
531 // do something with it.
535 // If we are already in a paragraph, and this is the first one, then we
536 // do not want to open the paragraph tag.
537 // we also do not want to open it if the current layout does not permit
538 // multiple paragraphs.
539 bool const opened = runparams.html_make_pars &&
540 (par != pbegin || !runparams.html_in_par);
543 docstring const deferred =
544 par->simpleLyXHTMLOnePar(buf, xs, runparams, text.outerFont(distance(begin, par)));
546 // We want to issue the closing tag if either:
547 // (i) We opened it, and either html_in_par is false,
548 // or we're not in the last paragraph, anyway.
549 // (ii) We didn't open it and html_in_par is true,
550 // but we are in the first par, and there is a next par.
551 ParagraphList::const_iterator nextpar = par;
553 bool const needclose =
554 (opened && (!runparams.html_in_par || nextpar != pend))
555 || (!opened && runparams.html_in_par && par == pbegin && nextpar != pend);
560 if (!deferred.empty()) {
561 xs << XHTMLStream::NextRaw() << deferred;
569 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
571 OutputParams const & runparams,
573 ParagraphList::const_iterator const & pbegin,
574 ParagraphList::const_iterator const & pend)
576 xs << StartTag("h2", "class='bibliography'");
577 xs << pbegin->layout().labelstring(false);
580 xs << StartTag("div", "class='bibliography'");
582 makeParagraphs(buf, xs, runparams, text, pbegin, pend);
588 bool isNormalEnv(Layout const & lay)
590 return lay.latextype == LATEX_ENVIRONMENT;
594 ParagraphList::const_iterator makeEnvironmentHtml(Buffer const & buf,
596 OutputParams const & runparams,
598 ParagraphList::const_iterator const & pbegin,
599 ParagraphList::const_iterator const & pend)
601 ParagraphList::const_iterator const begin = text.paragraphs().begin();
602 ParagraphList::const_iterator par = pbegin;
603 Layout const & bstyle = par->layout();
604 depth_type const origdepth = pbegin->params().depth();
606 // open tag for this environment
610 // we will on occasion need to remember a layout from before.
611 Layout const * lastlay = 0;
613 while (par != pend) {
614 Layout const & style = par->layout();
615 // the counter only gets stepped if we're in some kind of list,
616 // or if it's the first time through.
617 // note that enum, etc, are handled automatically.
618 // FIXME There may be a bug here about user defined enumeration
619 // types. If so, then we'll need to take the counter and add "i",
620 // "ii", etc, as with enum.
621 if (!style.counter.empty() &&
622 (par == pbegin || !isNormalEnv(style))
623 && style.latextype == LATEX_LIST_ENVIRONMENT)
624 buf.params().documentClass().counters().step(style.counter);
625 ParagraphList::const_iterator send;
626 // this will be positive, if we want to skip the initial word
627 // (if it's been taken for the label).
630 switch (style.latextype) {
631 case LATEX_ENVIRONMENT:
632 case LATEX_LIST_ENVIRONMENT:
633 case LATEX_ITEM_ENVIRONMENT: {
634 // There are two possiblities in this case.
635 // One is that we are still in the environment in which we
636 // started---which we will be if the depth is the same.
637 if (par->params().depth() == origdepth) {
638 LASSERT(bstyle == style, /* */);
640 closeItemTag(xs, *lastlay);
643 bool const labelfirst = style.htmllabelfirst();
644 if (isNormalEnv(style)) {
645 // in this case, we print the label only for the first
646 // paragraph (as in a theorem).
647 openItemTag(xs, style);
648 if (par == pbegin && style.htmllabeltag() != "NONE") {
649 docstring const lbl =
650 pbegin->expandLabel(style, buf.params(), false);
652 openLabelTag(xs, style);
654 closeLabelTag(xs, style);
658 } else { // some kind of list
660 openItemTag(xs, style);
661 if (style.labeltype == LABEL_MANUAL
662 && style.htmllabeltag() != "NONE") {
663 openLabelTag(xs, style);
664 // sep = par->firstWordLyXHTML(xs, runparams);
665 closeLabelTag(xs, style);
668 else if (style.labeltype != LABEL_NO_LABEL
669 && style.htmllabeltag() != "NONE") {
670 openLabelTag(xs, style);
671 xs << par->expandLabel(style, buf.params(), false);
672 closeLabelTag(xs, style);
676 openItemTag(xs, style);
678 par->simpleLyXHTMLOnePar(buf, xs, runparams,
679 text.outerFont(distance(begin, par)), sep);
681 // We may not want to close the tag yet, in particular,
682 // if we're not at the end...
684 // and are doing items...
685 && !isNormalEnv(style)
686 // and if the depth has changed...
687 && par->params().depth() != origdepth) {
688 // then we'll save this layout for later, and close it when
689 // we get another item.
692 closeItemTag(xs, style);
695 // The other possibility is that the depth has increased, in which
696 // case we need to recurse.
698 send = searchEnvironmentHtml(par, pend);
699 par = makeEnvironmentHtml(buf, xs, runparams, text, par, send);
703 case LATEX_PARAGRAPH:
704 send = searchParagraphHtml(par, pend);
705 par = makeParagraphs(buf, xs, runparams, text, par, send);
708 case LATEX_BIB_ENVIRONMENT:
711 par = makeParagraphs(buf, xs, runparams, text, par, send);
721 closeItemTag(xs, *lastlay);
722 closeTag(xs, bstyle);
728 void makeCommand(Buffer const & buf,
730 OutputParams const & runparams,
732 ParagraphList::const_iterator const & pbegin)
734 Layout const & style = pbegin->layout();
735 if (!style.counter.empty())
736 buf.params().documentClass().counters().step(style.counter);
740 // Label around sectioning number:
741 // FIXME Probably need to account for LABEL_MANUAL
742 if (style.labeltype != LABEL_NO_LABEL) {
743 openLabelTag(xs, style);
744 xs << pbegin->expandLabel(style, buf.params(), false);
745 closeLabelTag(xs, style);
746 // Otherwise the label might run together with the text
747 xs << from_ascii(" ");
750 ParagraphList::const_iterator const begin = text.paragraphs().begin();
751 pbegin->simpleLyXHTMLOnePar(buf, xs, runparams,
752 text.outerFont(distance(begin, pbegin)));
757 } // end anonymous namespace
760 void xhtmlParagraphs(Text const & text,
763 OutputParams const & runparams)
765 ParagraphList const & paragraphs = text.paragraphs();
766 ParagraphList::const_iterator par = paragraphs.begin();
767 ParagraphList::const_iterator pend = paragraphs.end();
769 OutputParams ourparams = runparams;
770 while (par != pend) {
771 Layout const & style = par->layout();
772 ParagraphList::const_iterator lastpar = par;
773 ParagraphList::const_iterator send;
775 switch (style.latextype) {
776 case LATEX_COMMAND: {
777 // The files with which we are working never have more than
778 // one paragraph in a command structure.
780 // if (ourparams.html_in_par)
781 // fix it so we don't get sections inside standard, e.g.
782 // note that we may then need to make runparams not const, so we
783 // can communicate that back.
784 // FIXME Maybe this fix should be in the routines themselves, in case
785 // they are called from elsewhere.
786 makeCommand(buf, xs, ourparams, text, par);
790 case LATEX_ENVIRONMENT:
791 case LATEX_LIST_ENVIRONMENT:
792 case LATEX_ITEM_ENVIRONMENT: {
793 // FIXME Same fix here.
794 send = searchEnvironmentHtml(par, pend);
795 par = makeEnvironmentHtml(buf, xs, ourparams, text, par, send);
798 case LATEX_BIB_ENVIRONMENT: {
799 // FIXME Same fix here.
800 send = searchEnvironmentHtml(par, pend);
801 par = makeBibliography(buf, xs, ourparams, text, par, send);
804 case LATEX_PARAGRAPH:
805 send = searchParagraphHtml(par, pend);
806 par = makeParagraphs(buf, xs, ourparams, text, par, send);
810 // makeEnvironment may process more than one paragraphs and bypass pend
811 if (distance(lastpar, par) >= distance(lastpar, pend))