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 bool isFontTag(string const & s)
80 return s == "em" || s == "strong"; // others?
85 docstring StartTag::asTag() const
87 string output = "<" + tag_;
89 output += " " + attr_;
91 return from_utf8(output);
95 docstring StartTag::asEndTag() const
97 string output = "</" + tag_ + ">";
98 return from_utf8(output);
102 docstring EndTag::asEndTag() const
104 string output = "</" + tag_ + ">";
105 return from_utf8(output);
109 docstring CompTag::asTag() const
111 string output = "<" + tag_;
113 output += " " + attr_;
115 return from_utf8(output);
119 ////////////////////////////////////////////////////////////////
123 ////////////////////////////////////////////////////////////////
125 XHTMLStream::XHTMLStream(odocstream & os)
130 void XHTMLStream::cr()
133 os_ << from_ascii("\n");
137 void XHTMLStream::writeError(std::string const & s)
140 os_ << from_utf8("<!-- Output Error: " + s + " -->");
144 bool XHTMLStream::closeFontTags()
146 if (tag_stack_.empty())
148 // first, we close any open font tags we can close
149 StartTag curtag = tag_stack_.back();
150 while (html::isFontTag(curtag.tag_)) {
151 os_ << curtag.asEndTag();
152 tag_stack_.pop_back();
153 if (tag_stack_.empty())
154 // this probably shouldn't happen, since then the
155 // font tags weren't in any other tag. but that
156 // problem will likely be caught elsewhere.
158 curtag = tag_stack_.back();
160 // so we've hit a non-font tag. let's see if any of the
161 // remaining tags are font tags.
162 TagStack::const_iterator it = tag_stack_.begin();
163 TagStack::const_iterator en = tag_stack_.end();
164 bool noFontTags = true;
165 for (; it != en; ++it) {
166 if (html::isFontTag(it->tag_)) {
167 writeError("Font tag `" + it->tag_ + "' still open in closeFontTags().");
175 void XHTMLStream::clearTagDeque()
177 while (!pending_tags_.empty()) {
178 StartTag const & tag = pending_tags_.front();
181 tag_stack_.push_back(tag);
182 pending_tags_.pop_front();
187 XHTMLStream & XHTMLStream::operator<<(docstring const & d)
190 os_ << html::htmlize(d);
195 XHTMLStream & XHTMLStream::operator<<(const char * s)
198 os_ << html::htmlize(from_ascii(s));
203 XHTMLStream & XHTMLStream::operator<<(char_type c)
206 os_ << html::escapeChar(c);
211 XHTMLStream & XHTMLStream::operator<<(StartTag const & tag)
213 if (tag.tag_.empty())
215 pending_tags_.push_back(tag);
222 XHTMLStream & XHTMLStream::operator<<(CompTag const & tag)
224 if (tag.tag_.empty())
233 bool XHTMLStream::isTagOpen(string const & stag)
235 TagStack::const_iterator sit = tag_stack_.begin();
236 TagStack::const_iterator const sen = tag_stack_.end();
237 for (; sit != sen; ++sit)
238 // we could check for the
239 if (sit->tag_ == stag)
245 // this is complicated, because we want to make sure that
246 // everything is properly nested. the code ought to make
247 // sure of that, but we won't assert (yet) if we run into
248 // a problem. we'll just output error messages and try our
249 // best to make things work.
250 XHTMLStream & XHTMLStream::operator<<(EndTag const & etag)
252 if (etag.tag_.empty())
254 // first make sure we're not closing an empty tag
255 if (!pending_tags_.empty()) {
256 StartTag const & stag = pending_tags_.back();
257 if (etag.tag_ == stag.tag_) {
258 // we have <tag></tag>, so we discard it and remove it
259 // from the pending_tags_.
260 pending_tags_.pop_back();
263 // there is a pending tag that isn't the one we are trying
265 // is this tag itself pending?
266 // non-const iterators because we may call erase().
267 TagDeque::iterator dit = pending_tags_.begin();
268 TagDeque::iterator const den = pending_tags_.end();
269 for (; dit != den; ++dit) {
270 if (dit->tag_ == etag.tag_) {
271 // it was pending, so we just erase it
272 writeError("Tried to close pending tag `" + etag.tag_
273 + "' when other tags were pending. Tag discarded.");
274 pending_tags_.erase(dit);
278 // so etag isn't itself pending. is it even open?
279 if (!isTagOpen(etag.tag_)) {
280 writeError("Tried to close `" + etag.tag_
281 + "' when tag was not open. Tag discarded.");
284 // ok, so etag is open.
285 // our strategy will be as below: we will do what we need to
286 // do to close this tag.
287 string estr = "Closing tag `" + etag.tag_
288 + "' when other tags are pending. Discarded pending tags:\n";
289 for (dit = pending_tags_.begin(); dit != den; ++dit)
290 estr += dit->tag_ + "\n";
292 // clear the pending tags...
293 pending_tags_.clear();
294 // ...and then just fall through.
297 // is the tag we are closing the last one we opened?
298 if (etag.tag_ == tag_stack_.back().tag_) {
300 os_ << etag.asEndTag();
301 // ...and forget about it
302 tag_stack_.pop_back();
306 // we are trying to close a tag other than the one last opened.
307 // let's first see if this particular tag is still open somehow.
308 if (!isTagOpen(etag.tag_)) {
309 writeError("Tried to close `" + etag.tag_
310 + "' when tag was not open. Tag discarded.");
314 // so the tag was opened, but other tags have been opened since
315 // and not yet closed.
316 // if it's a font tag, though...
317 if (html::isFontTag(etag.tag_)) {
318 // it won't be a problem if the other tags open since this one
319 // are also font tags.
320 TagStack::const_reverse_iterator rit = tag_stack_.rbegin();
321 TagStack::const_reverse_iterator ren = tag_stack_.rend();
322 for (; rit != ren; ++rit) {
323 if (rit->tag_ == etag.tag_)
325 if (!html::isFontTag(rit->tag_)) {
326 // we'll just leave it and, presumably, have to close it later.
327 writeError("Unable to close font tag `" + etag.tag_
328 + "' due to open non-font tag `" + rit->tag_ + "'.");
334 // <em>this is <strong>bold
335 // and are being asked to closed em. we want:
336 // <em>this is <strong>bold</strong></em><strong>
337 // first, we close the intervening tags...
338 StartTag curtag = tag_stack_.back();
339 // ...remembering them in a stack.
341 while (curtag.tag_ != etag.tag_) {
342 os_ << curtag.asEndTag();
343 fontstack.push_back(curtag);
344 tag_stack_.pop_back();
345 curtag = tag_stack_.back();
347 // now close our tag...
348 os_ << etag.asEndTag();
349 tag_stack_.pop_back();
351 // ...and restore the other tags.
352 rit = fontstack.rbegin();
353 ren = fontstack.rend();
354 for (; rit != ren; ++rit)
355 pending_tags_.push_back(*rit);
359 // it wasn't a font tag.
360 // so other tags were opened before this one and not properly closed.
361 // so we'll close them, too. that may cause other issues later, but it
362 // at least guarantees proper nesting.
363 writeError("Closing tag `" + etag.tag_
364 + "' when other tags are open, namely:");
365 StartTag curtag = tag_stack_.back();
366 while (curtag.tag_ != etag.tag_) {
367 writeError(curtag.tag_);
368 os_ << curtag.asEndTag();
369 tag_stack_.pop_back();
370 curtag = tag_stack_.back();
372 // curtag is now the one we actually want.
373 os_ << curtag.asEndTag();
374 tag_stack_.pop_back();
379 // End code for XHTMLStream
383 // convenience functions
385 inline void openTag(XHTMLStream & xs, Layout const & lay)
387 xs << StartTag(lay.htmltag(), lay.htmlattr());
391 inline void closeTag(XHTMLStream & xs, Layout const & lay)
393 xs << EndTag(lay.htmltag());
397 inline void openLabelTag(XHTMLStream & xs, Layout const & lay)
399 xs << StartTag(lay.htmllabeltag(), lay.htmllabelattr());
403 inline void closeLabelTag(XHTMLStream & xs, Layout const & lay)
405 xs << EndTag(lay.htmllabeltag());
409 inline void openItemTag(XHTMLStream & xs, Layout const & lay)
411 xs << StartTag(lay.htmlitemtag(), lay.htmlitemattr(), true);
415 inline void closeItemTag(XHTMLStream & xs, Layout const & lay)
417 xs << EndTag(lay.htmlitemtag());
420 // end of convenience functions
422 ParagraphList::const_iterator searchParagraphHtml(
423 ParagraphList::const_iterator p,
424 ParagraphList::const_iterator const & pend)
426 for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
433 ParagraphList::const_iterator searchEnvironmentHtml(
434 ParagraphList::const_iterator const pstart,
435 ParagraphList::const_iterator const & pend)
437 ParagraphList::const_iterator p = pstart;
438 Layout const & bstyle = p->layout();
439 size_t const depth = p->params().depth();
440 for (++p; p != pend; ++p) {
441 Layout const & style = p->layout();
442 // It shouldn't happen that e.g. a section command occurs inside
443 // a quotation environment, at a higher depth, but as of 6/2009,
444 // it can happen. We pretend that it's just at lowest depth.
445 if (style.latextype == LATEX_COMMAND)
447 // If depth is down, we're done
448 if (p->params().depth() < depth)
450 // If depth is up, we're not done
451 if (p->params().depth() > depth)
453 // Now we know we are at the same depth
454 if (style.latextype == LATEX_PARAGRAPH
455 || style.latexname() != bstyle.latexname())
462 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
464 OutputParams const & runparams,
466 ParagraphList::const_iterator const & pbegin,
467 ParagraphList::const_iterator const & pend)
469 ParagraphList::const_iterator const begin = text.paragraphs().begin();
470 ParagraphList::const_iterator par = pbegin;
471 for (; par != pend; ++par) {
472 Layout const & lay = par->layout();
473 if (!lay.counter.empty())
474 buf.params().documentClass().counters().step(lay.counter);
475 // FIXME We should see if there's a label to be output and
476 // do something with it.
480 // If we are already in a paragraph, and this is the first one, then we
481 // do not want to open the paragraph tag.
482 // we also do not want to open it if the current layout does not permit
483 // multiple paragraphs.
484 bool const opened = runparams.html_make_pars &&
485 (par != pbegin || !runparams.html_in_par);
488 docstring const deferred =
489 par->simpleLyXHTMLOnePar(buf, xs, runparams, text.outerFont(distance(begin, par)));
491 // We want to issue the closing tag if either:
492 // (i) We opened it, and either html_in_par is false,
493 // or we're not in the last paragraph, anyway.
494 // (ii) We didn't open it and html_in_par is true,
495 // but we are in the first par, and there is a next par.
496 ParagraphList::const_iterator nextpar = par;
498 bool const needclose =
499 (opened && (!runparams.html_in_par || nextpar != pend))
500 || (!opened && runparams.html_in_par && par == pbegin && nextpar != pend);
505 if (!deferred.empty()) {
514 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
516 OutputParams const & runparams,
518 ParagraphList::const_iterator const & pbegin,
519 ParagraphList::const_iterator const & pend)
521 xs << StartTag("h2", "class='bibliography'");
522 xs << pbegin->layout().labelstring(false);
525 xs << StartTag("div", "class='bibliography'");
527 makeParagraphs(buf, xs, runparams, text, pbegin, pend);
533 bool isNormalEnv(Layout const & lay)
535 return lay.latextype == LATEX_ENVIRONMENT;
539 ParagraphList::const_iterator makeEnvironmentHtml(Buffer const & buf,
541 OutputParams const & runparams,
543 ParagraphList::const_iterator const & pbegin,
544 ParagraphList::const_iterator const & pend)
546 ParagraphList::const_iterator const begin = text.paragraphs().begin();
547 ParagraphList::const_iterator par = pbegin;
548 Layout const & bstyle = par->layout();
549 depth_type const origdepth = pbegin->params().depth();
551 // open tag for this environment
555 // we will on occasion need to remember a layout from before.
556 Layout const * lastlay = 0;
558 while (par != pend) {
559 Layout const & style = par->layout();
560 // the counter only gets stepped if we're in some kind of list,
561 // or if it's the first time through.
562 // note that enum, etc, are handled automatically.
563 // FIXME There may be a bug here about user defined enumeration
564 // types. If so, then we'll need to take the counter and add "i",
565 // "ii", etc, as with enum.
566 if (!style.counter.empty() &&
567 (par == pbegin || !isNormalEnv(style))
568 && style.latextype == LATEX_LIST_ENVIRONMENT)
569 buf.params().documentClass().counters().step(style.counter);
570 ParagraphList::const_iterator send;
571 // this will be positive, if we want to skip the initial word
572 // (if it's been taken for the label).
575 switch (style.latextype) {
576 case LATEX_ENVIRONMENT:
577 case LATEX_LIST_ENVIRONMENT:
578 case LATEX_ITEM_ENVIRONMENT: {
579 // There are two possiblities in this case.
580 // One is that we are still in the environment in which we
581 // started---which we will be if the depth is the same.
582 if (par->params().depth() == origdepth) {
583 LASSERT(bstyle == style, /* */);
585 closeItemTag(xs, *lastlay);
588 bool const labelfirst = style.htmllabelfirst();
589 if (isNormalEnv(style)) {
590 // in this case, we print the label only for the first
591 // paragraph (as in a theorem).
592 openItemTag(xs, style);
593 if (par == pbegin && style.htmllabeltag() != "NONE") {
594 docstring const lbl =
595 pbegin->expandLabel(style, buf.params(), false);
597 openLabelTag(xs, style);
599 closeLabelTag(xs, style);
603 } else { // some kind of list
605 openItemTag(xs, style);
606 if (style.labeltype == LABEL_MANUAL
607 && style.htmllabeltag() != "NONE") {
608 openLabelTag(xs, style);
609 // sep = par->firstWordLyXHTML(xs, runparams);
610 closeLabelTag(xs, style);
613 else if (style.labeltype != LABEL_NO_LABEL
614 && style.htmllabeltag() != "NONE") {
615 openLabelTag(xs, style);
616 xs << par->expandLabel(style, buf.params(), false);
617 closeLabelTag(xs, style);
621 openItemTag(xs, style);
623 par->simpleLyXHTMLOnePar(buf, xs, runparams,
624 text.outerFont(distance(begin, par)), sep);
626 // We may not want to close the tag yet, in particular,
627 // if we're not at the end...
629 // and are doing items...
630 && !isNormalEnv(style)
631 // and if the depth has changed...
632 && par->params().depth() != origdepth) {
633 // then we'll save this layout for later, and close it when
634 // we get another item.
637 closeItemTag(xs, style);
640 // The other possibility is that the depth has increased, in which
641 // case we need to recurse.
643 send = searchEnvironmentHtml(par, pend);
644 par = makeEnvironmentHtml(buf, xs, runparams, text, par, send);
648 case LATEX_PARAGRAPH:
649 send = searchParagraphHtml(par, pend);
650 par = makeParagraphs(buf, xs, runparams, text, par, send);
653 case LATEX_BIB_ENVIRONMENT:
656 par = makeParagraphs(buf, xs, runparams, text, par, send);
666 closeItemTag(xs, *lastlay);
667 closeTag(xs, bstyle);
673 void makeCommand(Buffer const & buf,
675 OutputParams const & runparams,
677 ParagraphList::const_iterator const & pbegin)
679 Layout const & style = pbegin->layout();
680 if (!style.counter.empty())
681 buf.params().documentClass().counters().step(style.counter);
685 // Label around sectioning number:
686 // FIXME Probably need to account for LABEL_MANUAL
687 if (style.labeltype != LABEL_NO_LABEL) {
688 openLabelTag(xs, style);
689 xs << pbegin->expandLabel(style, buf.params(), false);
690 closeLabelTag(xs, style);
691 // Otherwise the label might run together with the text
692 xs << from_ascii(" ");
695 ParagraphList::const_iterator const begin = text.paragraphs().begin();
696 pbegin->simpleLyXHTMLOnePar(buf, xs, runparams,
697 text.outerFont(distance(begin, pbegin)));
702 } // end anonymous namespace
705 void xhtmlParagraphs(Text const & text,
708 OutputParams const & runparams)
710 ParagraphList const & paragraphs = text.paragraphs();
711 ParagraphList::const_iterator par = paragraphs.begin();
712 ParagraphList::const_iterator pend = paragraphs.end();
714 OutputParams ourparams = runparams;
715 while (par != pend) {
716 Layout const & style = par->layout();
717 ParagraphList::const_iterator lastpar = par;
718 ParagraphList::const_iterator send;
720 switch (style.latextype) {
721 case LATEX_COMMAND: {
722 // The files with which we are working never have more than
723 // one paragraph in a command structure.
725 // if (ourparams.html_in_par)
726 // fix it so we don't get sections inside standard, e.g.
727 // note that we may then need to make runparams not const, so we
728 // can communicate that back.
729 // FIXME Maybe this fix should be in the routines themselves, in case
730 // they are called from elsewhere.
731 makeCommand(buf, xs, ourparams, text, par);
735 case LATEX_ENVIRONMENT:
736 case LATEX_LIST_ENVIRONMENT:
737 case LATEX_ITEM_ENVIRONMENT: {
738 // FIXME Same fix here.
739 send = searchEnvironmentHtml(par, pend);
740 par = makeEnvironmentHtml(buf, xs, ourparams, text, par, send);
743 case LATEX_BIB_ENVIRONMENT: {
744 // FIXME Same fix here.
745 send = searchEnvironmentHtml(par, pend);
746 par = makeBibliography(buf, xs, ourparams, text, par, send);
749 case LATEX_PARAGRAPH:
750 send = searchParagraphHtml(par, pend);
751 par = makeParagraphs(buf, xs, ourparams, text, par, send);
755 // makeEnvironment may process more than one paragraphs and bypass pend
756 if (distance(lastpar, par) >= distance(lastpar, pend))