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 // first, we close any open font tags we can close
147 StartTag curtag = tag_stack_.back();
148 while (html::isFontTag(curtag.tag_)) {
149 os_ << curtag.asEndTag();
150 tag_stack_.pop_back();
151 if (tag_stack_.empty())
152 // this probably shouldn't happen, since then the
153 // font tags weren't in any other tag. but that
154 // problem will likely be caught elsewhere.
156 curtag = tag_stack_.back();
158 // so we've hit a non-font tag. let's see if any of the
159 // remaining tags are font tags.
160 TagStack::const_iterator it = tag_stack_.begin();
161 TagStack::const_iterator en = tag_stack_.end();
162 bool noFontTags = true;
163 for (; it != en; ++it) {
164 if (html::isFontTag(it->tag_)) {
165 writeError("Font tag `" + it->tag_ + "' still open in closeFontTags().");
173 void XHTMLStream::clearTagDeque()
175 while (!pending_tags_.empty()) {
176 StartTag const & tag = pending_tags_.front();
179 tag_stack_.push_back(tag);
180 pending_tags_.pop_front();
185 XHTMLStream & XHTMLStream::operator<<(docstring const & d)
188 os_ << html::htmlize(d);
193 XHTMLStream & XHTMLStream::operator<<(const char * s)
196 os_ << html::htmlize(from_ascii(s));
201 XHTMLStream & XHTMLStream::operator<<(char_type c)
204 os_ << html::escapeChar(c);
209 XHTMLStream & XHTMLStream::operator<<(StartTag const & tag)
211 if (tag.tag_.empty())
213 pending_tags_.push_back(tag);
220 XHTMLStream & XHTMLStream::operator<<(CompTag const & tag)
222 if (tag.tag_.empty())
231 bool XHTMLStream::isTagOpen(string const & stag)
233 TagStack::const_iterator sit = tag_stack_.begin();
234 TagStack::const_iterator const sen = tag_stack_.end();
235 for (; sit != sen; ++sit)
236 // we could check for the
237 if (sit->tag_ == stag)
243 // this is complicated, because we want to make sure that
244 // everything is properly nested. the code ought to make
245 // sure of that, but we won't assert (yet) if we run into
246 // a problem. we'll just output error messages and try our
247 // best to make things work.
248 XHTMLStream & XHTMLStream::operator<<(EndTag const & etag)
250 if (etag.tag_.empty())
252 // first make sure we're not closing an empty tag
253 if (!pending_tags_.empty()) {
254 StartTag const & stag = pending_tags_.back();
255 if (etag.tag_ == stag.tag_) {
256 // we have <tag></tag>, so we discard it and remove it
257 // from the pending_tags_.
258 pending_tags_.pop_back();
261 // there is a pending tag that isn't the one we are trying
263 // is this tag itself pending?
264 // non-const iterators because we may call erase().
265 TagDeque::iterator dit = pending_tags_.begin();
266 TagDeque::iterator const den = pending_tags_.end();
267 for (; dit != den; ++dit) {
268 if (dit->tag_ == etag.tag_) {
269 // it was pending, so we just erase it
270 writeError("Tried to close pending tag `" + etag.tag_
271 + "' when other tags were pending. Tag discarded.");
272 pending_tags_.erase(dit);
276 // so etag isn't itself pending. is it even open?
277 if (!isTagOpen(etag.tag_)) {
278 writeError("Tried to close `" + etag.tag_
279 + "' when tag was not open. Tag discarded.");
282 // ok, so etag is open.
283 // our strategy will be as below: we will do what we need to
284 // do to close this tag.
285 string estr = "Closing tag `" + etag.tag_
286 + "' when other tags are pending. Discarded pending tags:\n";
287 for (dit = pending_tags_.begin(); dit != den; ++dit)
288 estr += dit->tag_ + "\n";
290 // clear the pending tags...
291 pending_tags_.clear();
292 // ...and then just fall through.
295 // is the tag we are closing the last one we opened?
296 if (etag.tag_ == tag_stack_.back().tag_) {
298 os_ << etag.asEndTag();
299 // ...and forget about it
300 tag_stack_.pop_back();
304 // we are trying to close a tag other than the one last opened.
305 // let's first see if this particular tag is still open somehow.
306 if (!isTagOpen(etag.tag_)) {
307 writeError("Tried to close `" + etag.tag_
308 + "' when tag was not open. Tag discarded.");
312 // so the tag was opened, but other tags have been opened since
313 // and not yet closed.
314 // if it's a font tag, though...
315 if (html::isFontTag(etag.tag_)) {
316 // it won't be a problem if the other tags open since this one
317 // are also font tags.
318 TagStack::const_reverse_iterator rit = tag_stack_.rbegin();
319 TagStack::const_reverse_iterator ren = tag_stack_.rend();
320 for (; rit != ren; ++rit) {
321 if (rit->tag_ == etag.tag_)
323 if (!html::isFontTag(rit->tag_)) {
324 // we'll just leave it and, presumably, have to close it later.
325 writeError("Unable to close font tag `" + etag.tag_
326 + "' due to open non-font tag `" + rit->tag_ + "'.");
332 // <em>this is <strong>bold
333 // and are being asked to closed em. we want:
334 // <em>this is <strong>bold</strong></em><strong>
335 // first, we close the intervening tags...
336 StartTag curtag = tag_stack_.back();
337 // ...remembering them in a stack.
339 while (curtag.tag_ != etag.tag_) {
340 os_ << curtag.asEndTag();
341 fontstack.push_back(curtag);
342 tag_stack_.pop_back();
343 curtag = tag_stack_.back();
345 // now close our tag...
346 os_ << etag.asEndTag();
347 tag_stack_.pop_back();
349 // ...and restore the other tags.
350 rit = fontstack.rbegin();
351 ren = fontstack.rend();
352 for (; rit != ren; ++rit)
353 pending_tags_.push_back(*rit);
357 // it wasn't a font tag.
358 // so other tags were opened before this one and not properly closed.
359 // so we'll close them, too. that may cause other issues later, but it
360 // at least guarantees proper nesting.
361 writeError("Closing tag `" + etag.tag_
362 + "' when other tags are open, namely:");
363 StartTag curtag = tag_stack_.back();
364 while (curtag.tag_ != etag.tag_) {
365 writeError(curtag.tag_);
366 os_ << curtag.asEndTag();
367 tag_stack_.pop_back();
368 curtag = tag_stack_.back();
370 // curtag is now the one we actually want.
371 os_ << curtag.asEndTag();
372 tag_stack_.pop_back();
377 // End code for XHTMLStream
381 // convenience functions
383 inline void openTag(XHTMLStream & xs, Layout const & lay)
385 xs << StartTag(lay.htmltag(), lay.htmlattr());
389 inline void closeTag(XHTMLStream & xs, Layout const & lay)
391 xs << EndTag(lay.htmltag());
395 inline void openLabelTag(XHTMLStream & xs, Layout const & lay)
397 xs << StartTag(lay.htmllabeltag(), lay.htmllabelattr());
401 inline void closeLabelTag(XHTMLStream & xs, Layout const & lay)
403 xs << EndTag(lay.htmllabeltag());
407 inline void openItemTag(XHTMLStream & xs, Layout const & lay)
409 xs << StartTag(lay.htmlitemtag(), lay.htmlitemattr(), true);
413 inline void closeItemTag(XHTMLStream & xs, Layout const & lay)
415 xs << EndTag(lay.htmlitemtag());
418 // end of convenience functions
420 ParagraphList::const_iterator searchParagraphHtml(
421 ParagraphList::const_iterator p,
422 ParagraphList::const_iterator const & pend)
424 for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
431 ParagraphList::const_iterator searchEnvironmentHtml(
432 ParagraphList::const_iterator const pstart,
433 ParagraphList::const_iterator const & pend)
435 ParagraphList::const_iterator p = pstart;
436 Layout const & bstyle = p->layout();
437 size_t const depth = p->params().depth();
438 for (++p; p != pend; ++p) {
439 Layout const & style = p->layout();
440 // It shouldn't happen that e.g. a section command occurs inside
441 // a quotation environment, at a higher depth, but as of 6/2009,
442 // it can happen. We pretend that it's just at lowest depth.
443 if (style.latextype == LATEX_COMMAND)
445 // If depth is down, we're done
446 if (p->params().depth() < depth)
448 // If depth is up, we're not done
449 if (p->params().depth() > depth)
451 // Now we know we are at the same depth
452 if (style.latextype == LATEX_PARAGRAPH
453 || style.latexname() != bstyle.latexname())
460 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
462 OutputParams const & runparams,
464 ParagraphList::const_iterator const & pbegin,
465 ParagraphList::const_iterator const & pend)
467 ParagraphList::const_iterator const begin = text.paragraphs().begin();
468 ParagraphList::const_iterator par = pbegin;
469 for (; par != pend; ++par) {
470 Layout const & lay = par->layout();
471 if (!lay.counter.empty())
472 buf.params().documentClass().counters().step(lay.counter);
473 // FIXME We should see if there's a label to be output and
474 // do something with it.
478 // FIXME Should we really allow anything other than 'p' here?
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.
483 (par == pbegin && runparams.html_in_par) ? false : true;
486 docstring const deferred =
487 par->simpleLyXHTMLOnePar(buf, xs, runparams, text.outerFont(distance(begin, par)));
489 // We want to issue the closing tag if either:
490 // (i) We opened it, and either html_in_par is false,
491 // or we're not in the last paragraph, anyway.
492 // (ii) We didn't open it and html_in_par is true,
493 // but we are in the first par, and there is a next par.
494 ParagraphList::const_iterator nextpar = par;
496 bool const needclose =
497 (opened && (!runparams.html_in_par || nextpar != pend))
498 || (!opened && runparams.html_in_par && par == pbegin && nextpar != pend);
503 if (!deferred.empty()) {
512 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
514 OutputParams const & runparams,
516 ParagraphList::const_iterator const & pbegin,
517 ParagraphList::const_iterator const & pend)
519 xs << StartTag("h2", "class='bibliography'");
520 xs << pbegin->layout().labelstring(false);
523 xs << StartTag("div", "class='bibliography'");
525 makeParagraphs(buf, xs, runparams, text, pbegin, pend);
531 bool isNormalEnv(Layout const & lay)
533 return lay.latextype == LATEX_ENVIRONMENT;
537 ParagraphList::const_iterator makeEnvironmentHtml(Buffer const & buf,
539 OutputParams const & runparams,
541 ParagraphList::const_iterator const & pbegin,
542 ParagraphList::const_iterator const & pend)
544 ParagraphList::const_iterator const begin = text.paragraphs().begin();
545 ParagraphList::const_iterator par = pbegin;
546 Layout const & bstyle = par->layout();
547 depth_type const origdepth = pbegin->params().depth();
549 // open tag for this environment
553 // we will on occasion need to remember a layout from before.
554 Layout const * lastlay = 0;
556 while (par != pend) {
557 Layout const & style = par->layout();
558 // the counter only gets stepped if we're in some kind of list,
559 // or if it's the first time through.
560 // note that enum, etc, are handled automatically.
561 // FIXME There may be a bug here about user defined enumeration
562 // types. If so, then we'll need to take the counter and add "i",
563 // "ii", etc, as with enum.
564 if (!style.counter.empty() &&
565 (par == pbegin || !isNormalEnv(style))
566 && style.latextype == LATEX_LIST_ENVIRONMENT)
567 buf.params().documentClass().counters().step(style.counter);
568 ParagraphList::const_iterator send;
569 // this will be positive, if we want to skip the initial word
570 // (if it's been taken for the label).
573 switch (style.latextype) {
574 case LATEX_ENVIRONMENT:
575 case LATEX_LIST_ENVIRONMENT:
576 case LATEX_ITEM_ENVIRONMENT: {
577 // There are two possiblities in this case.
578 // One is that we are still in the environment in which we
579 // started---which we will be if the depth is the same.
580 if (par->params().depth() == origdepth) {
581 LASSERT(bstyle == style, /* */);
583 closeItemTag(xs, *lastlay);
586 bool const labelfirst = style.htmllabelfirst();
587 if (isNormalEnv(style)) {
588 // in this case, we print the label only for the first
589 // paragraph (as in a theorem).
590 openItemTag(xs, style);
591 if (par == pbegin && style.htmllabeltag() != "NONE") {
592 docstring const lbl =
593 pbegin->expandLabel(style, buf.params(), false);
595 openLabelTag(xs, style);
597 closeLabelTag(xs, style);
601 } else { // some kind of list
603 openItemTag(xs, style);
604 if (style.labeltype == LABEL_MANUAL
605 && style.htmllabeltag() != "NONE") {
606 openLabelTag(xs, style);
607 // sep = par->firstWordLyXHTML(xs, runparams);
608 closeLabelTag(xs, style);
611 else if (style.labeltype != LABEL_NO_LABEL
612 && style.htmllabeltag() != "NONE") {
613 openLabelTag(xs, style);
614 xs << par->expandLabel(style, buf.params(), false);
615 closeLabelTag(xs, style);
619 openItemTag(xs, style);
621 // FIXME This should probalby be put into the layout file rather
623 xs << StartTag("span", "class='" + to_utf8(style.name()) + "_inneritem'");
625 par->simpleLyXHTMLOnePar(buf, xs, runparams,
626 text.outerFont(distance(begin, par)), sep);
627 if (!isNormalEnv(style) && !labelfirst)
628 xs << EndTag("span");
630 // We may not want to close the tag yet, in particular,
631 // if we're not at the end...
633 // and are doing items...
634 && style.latextype == LATEX_ITEM_ENVIRONMENT
635 // and if the depth has changed...
636 && par->params().depth() != origdepth) {
637 // then we'll save this layout for later, and close it when
638 // we get another item.
641 closeItemTag(xs, style);
644 // The other possibility is that the depth has increased, in which
645 // case we need to recurse.
647 send = searchEnvironmentHtml(par, pend);
648 par = makeEnvironmentHtml(buf, xs, runparams, text, par, send);
652 case LATEX_PARAGRAPH:
653 send = searchParagraphHtml(par, pend);
654 par = makeParagraphs(buf, xs, runparams, text, par, send);
657 case LATEX_BIB_ENVIRONMENT:
660 par = makeParagraphs(buf, xs, runparams, text, par, send);
670 closeItemTag(xs, *lastlay);
671 closeTag(xs, bstyle);
677 void makeCommand(Buffer const & buf,
679 OutputParams const & runparams,
681 ParagraphList::const_iterator const & pbegin)
683 Layout const & style = pbegin->layout();
684 if (!style.counter.empty())
685 buf.params().documentClass().counters().step(style.counter);
689 // Label around sectioning number:
690 // FIXME Probably need to account for LABEL_MANUAL
691 if (style.labeltype != LABEL_NO_LABEL) {
692 openLabelTag(xs, style);
693 xs << pbegin->expandLabel(style, buf.params(), false);
694 closeLabelTag(xs, style);
695 // Otherwise the label might run together with the text
696 xs << from_ascii(" ");
699 ParagraphList::const_iterator const begin = text.paragraphs().begin();
700 pbegin->simpleLyXHTMLOnePar(buf, xs, runparams,
701 text.outerFont(distance(begin, pbegin)));
706 } // end anonymous namespace
709 void xhtmlParagraphs(Text const & text,
712 OutputParams const & runparams)
714 ParagraphList const & paragraphs = text.paragraphs();
715 ParagraphList::const_iterator par = paragraphs.begin();
716 ParagraphList::const_iterator pend = paragraphs.end();
718 OutputParams ourparams = runparams;
719 while (par != pend) {
720 Layout const & style = par->layout();
721 ParagraphList::const_iterator lastpar = par;
722 ParagraphList::const_iterator send;
724 switch (style.latextype) {
725 case LATEX_COMMAND: {
726 // The files with which we are working never have more than
727 // one paragraph in a command structure.
729 // if (ourparams.html_in_par)
730 // fix it so we don't get sections inside standard, e.g.
731 // note that we may then need to make runparams not const, so we
732 // can communicate that back.
733 // FIXME Maybe this fix should be in the routines themselves, in case
734 // they are called from elsewhere.
735 makeCommand(buf, xs, ourparams, text, par);
739 case LATEX_ENVIRONMENT:
740 case LATEX_LIST_ENVIRONMENT:
741 case LATEX_ITEM_ENVIRONMENT: {
742 // FIXME Same fix here.
743 send = searchEnvironmentHtml(par, pend);
744 par = makeEnvironmentHtml(buf, xs, ourparams, text, par, send);
747 case LATEX_BIB_ENVIRONMENT: {
748 // FIXME Same fix here.
749 send = searchEnvironmentHtml(par, pend);
750 par = makeBibliography(buf, xs, ourparams, text, par, send);
753 case LATEX_PARAGRAPH:
754 send = searchParagraphHtml(par, pend);
755 par = makeParagraphs(buf, xs, ourparams, text, par, send);
759 // makeEnvironment may process more than one paragraphs and bypass pend
760 if (distance(lastpar, par) >= distance(lastpar, pend))