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 bool XHTMLStream::closeFontTags()
139 // first, we close any open font tags we can close
140 StartTag curtag = tag_stack_.back();
141 while (html::isFontTag(curtag.tag_)) {
142 os_ << curtag.asEndTag();
143 tag_stack_.pop_back();
144 if (tag_stack_.empty())
145 // this probably shouldn't happen, since then the
146 // font tags weren't in any other tag. but that
147 // problem will likely be caught elsewhere.
149 curtag = tag_stack_.back();
151 // so we've hit a non-font tag. let's see if any of the
152 // remaining tags are font tags.
153 TagStack::const_iterator it = tag_stack_.begin();
154 TagStack::const_iterator en = tag_stack_.end();
155 bool noFontTags = true;
156 for (; it != en; ++it) {
157 if (html::isFontTag(it->tag_)) {
158 LYXERR0("Font tag `" << it->tag_ << "' still open in closeFontTags().");
166 void XHTMLStream::clearTagDeque()
168 while (!pending_tags_.empty()) {
169 StartTag const & tag = pending_tags_.front();
172 tag_stack_.push_back(tag);
173 pending_tags_.pop_front();
178 XHTMLStream & XHTMLStream::operator<<(docstring const & d)
181 os_ << html::htmlize(d);
186 XHTMLStream & XHTMLStream::operator<<(const char * s)
189 os_ << html::htmlize(from_ascii(s));
194 XHTMLStream & XHTMLStream::operator<<(char_type c)
197 os_ << html::escapeChar(c);
202 XHTMLStream & XHTMLStream::operator<<(StartTag const & tag)
204 pending_tags_.push_back(tag);
211 XHTMLStream & XHTMLStream::operator<<(CompTag const & tag)
220 bool XHTMLStream::isTagOpen(string const & stag)
222 TagStack::const_iterator sit = tag_stack_.begin();
223 TagStack::const_iterator const sen = tag_stack_.end();
224 for (; sit != sen; ++sit)
225 // we could check for the
226 if (sit->tag_ == stag)
232 // this is complicated, because we want to make sure that
233 // everything is properly nested. the code ought to make
234 // sure of that, but we won't assert (yet) if we run into
235 // a problem. we'll just output error messages and try our
236 // best to make things work.
237 XHTMLStream & XHTMLStream::operator<<(EndTag const & etag)
239 // first make sure we're not closing an empty tag
240 if (!pending_tags_.empty()) {
241 StartTag const & stag = pending_tags_.back();
242 if (etag.tag_ == stag.tag_) {
243 // we have <tag></tag>, so we discard it and remove it
244 // from the pending_tags_.
245 pending_tags_.pop_back();
248 // there is a pending tag that isn't the one we are trying
250 // is this tag itself pending?
251 // non-const iterators because we may call erase().
252 TagDeque::iterator dit = pending_tags_.begin();
253 TagDeque::iterator const den = pending_tags_.end();
254 for (; dit != den; ++dit) {
255 if (dit->tag_ == etag.tag_) {
256 // it was pending, so we just erase it
257 LYXERR0("Tried to close pending tag `" << etag.tag_
258 << "' when other tags were pending. Tag discarded.");
259 pending_tags_.erase(dit);
263 // so etag isn't itself pending. is it even open?
264 if (!isTagOpen(etag.tag_)) {
265 LYXERR0("Tried to close `" << etag.tag_
266 << "' when tag was not open. Tag discarded.");
269 // ok, so etag is open.
270 // our strategy will be as below: we will do what we need to
271 // do to close this tag.
272 LYXERR0("Closing tag `" << etag.tag_
273 << "' when other tags are pending. Discarded pending tags:");
274 for (dit = pending_tags_.begin(); dit != den; ++dit)
276 // clear the pending tags...
277 pending_tags_.clear();
278 // ...and then just fall through.
281 // is the tag we are closing the last one we opened?
282 if (etag.tag_ == tag_stack_.back().tag_) {
284 os_ << etag.asEndTag();
285 // ...and forget about it
286 tag_stack_.pop_back();
290 // we are trying to close a tag other than the one last opened.
291 // let's first see if this particular tag is still open somehow.
292 if (!isTagOpen(etag.tag_)) {
293 LYXERR0("Tried to close `" << etag.tag_
294 << "' when tag was not open. Tag discarded.");
298 // so the tag was opened, but other tags have been opened since
299 // and not yet closed.
300 // if it's a font tag, though...
301 if (html::isFontTag(etag.tag_)) {
302 // it won't be a problem if the other tags open since this one
303 // are also font tags.
304 TagStack::const_reverse_iterator rit = tag_stack_.rbegin();
305 TagStack::const_reverse_iterator ren = tag_stack_.rend();
306 for (; rit != ren; ++rit) {
307 if (rit->tag_ == etag.tag_)
309 if (!html::isFontTag(rit->tag_)) {
310 // we'll just leave it and, presumably, have to close it later.
311 LYXERR0("Unable to close font tag `" << etag.tag_
312 << "' due to open non-font tag `" << rit->tag_ << "'.");
318 // <em>this is <strong>bold
319 // and are being asked to closed em. we want:
320 // <em>this is <strong>bold</strong></em><strong>
321 // first, we close the intervening tags...
322 StartTag curtag = tag_stack_.back();
323 // ...remembering them in a stack.
325 while (curtag.tag_ != etag.tag_) {
326 os_ << curtag.asEndTag();
327 fontstack.push_back(curtag);
328 tag_stack_.pop_back();
329 curtag = tag_stack_.back();
331 // now close our tag...
332 os_ << etag.asEndTag();
333 // ...and restore the other tags.
334 rit = fontstack.rbegin();
335 ren = fontstack.rend();
336 for (; rit != ren; ++rit)
337 pending_tags_.push_back(*rit);
341 // it wasn't a font tag.
342 // so other tags were opened before this one and not properly closed.
343 // so we'll close them, too. that may cause other issues later, but it
344 // at least guarantees proper nesting.
345 LYXERR0("Closing tag `" << etag.tag_
346 << "' when other tags are open, namely:");
347 StartTag curtag = tag_stack_.back();
348 while (curtag.tag_ != etag.tag_) {
349 LYXERR0(curtag.tag_);
350 os_ << curtag.asEndTag();
351 tag_stack_.pop_back();
352 curtag = tag_stack_.back();
354 // curtag is now the one we actually want.
355 os_ << curtag.asEndTag();
356 tag_stack_.pop_back();
361 // End code for XHTMLStream
365 // convenience functions
367 inline void openTag(XHTMLStream & xs, Layout const & lay)
369 xs << StartTag(lay.htmltag(), lay.htmlattr());
373 inline void closeTag(XHTMLStream & xs, Layout const & lay)
375 xs << EndTag(lay.htmltag());
379 inline void openLabelTag(XHTMLStream & xs, Layout const & lay)
381 xs << StartTag(lay.htmllabeltag(), lay.htmllabelattr());
385 inline void closeLabelTag(XHTMLStream & xs, Layout const & lay)
387 xs << EndTag(lay.htmllabeltag());
391 inline void openItemTag(XHTMLStream & xs, Layout const & lay)
393 xs << StartTag(lay.htmlitemtag(), lay.htmlitemattr(), true);
397 inline void closeItemTag(XHTMLStream & xs, Layout const & lay)
399 xs << EndTag(lay.htmlitemtag());
402 // end of convenience functions
404 ParagraphList::const_iterator searchParagraphHtml(
405 ParagraphList::const_iterator p,
406 ParagraphList::const_iterator const & pend)
408 for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
415 ParagraphList::const_iterator searchEnvironmentHtml(
416 ParagraphList::const_iterator const pstart,
417 ParagraphList::const_iterator const & pend)
419 ParagraphList::const_iterator p = pstart;
420 Layout const & bstyle = p->layout();
421 size_t const depth = p->params().depth();
422 for (++p; p != pend; ++p) {
423 Layout const & style = p->layout();
424 // It shouldn't happen that e.g. a section command occurs inside
425 // a quotation environment, at a higher depth, but as of 6/2009,
426 // it can happen. We pretend that it's just at lowest depth.
427 if (style.latextype == LATEX_COMMAND)
429 // If depth is down, we're done
430 if (p->params().depth() < depth)
432 // If depth is up, we're not done
433 if (p->params().depth() > depth)
435 // Now we know we are at the same depth
436 if (style.latextype == LATEX_PARAGRAPH
437 || style.latexname() != bstyle.latexname())
444 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
446 OutputParams const & runparams,
448 ParagraphList::const_iterator const & pbegin,
449 ParagraphList::const_iterator const & pend)
451 ParagraphList::const_iterator const begin = text.paragraphs().begin();
452 ParagraphList::const_iterator par = pbegin;
453 for (; par != pend; ++par) {
454 Layout const & lay = par->layout();
455 if (!lay.counter.empty())
456 buf.params().documentClass().counters().step(lay.counter);
457 // FIXME We should see if there's a label to be output and
458 // do something with it.
462 // FIXME Should we really allow anything other than 'p' here?
464 // If we are already in a paragraph, and this is the first one, then we
465 // do not want to open the paragraph tag.
467 (par == pbegin && runparams.html_in_par) ? false : true;
470 docstring const deferred =
471 par->simpleLyXHTMLOnePar(buf, xs, runparams, text.outerFont(distance(begin, par)));
473 // We want to issue the closing tag if either:
474 // (i) We opened it, and either html_in_par is false,
475 // or we're not in the last paragraph, anyway.
476 // (ii) We didn't open it and html_in_par is true,
477 // but we are in the first par, and there is a next par.
478 ParagraphList::const_iterator nextpar = par;
480 bool const needclose =
481 (opened && (!runparams.html_in_par || nextpar != pend))
482 || (!opened && runparams.html_in_par && par == pbegin && nextpar != pend);
487 if (!deferred.empty()) {
496 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
498 OutputParams const & runparams,
500 ParagraphList::const_iterator const & pbegin,
501 ParagraphList::const_iterator const & pend)
503 xs << StartTag("h2", "class='bibliography'");
504 xs << pbegin->layout().labelstring(false);
507 xs << StartTag("div", "class='bibliography'");
509 makeParagraphs(buf, xs, runparams, text, pbegin, pend);
515 bool isNormalEnv(Layout const & lay)
517 return lay.latextype == LATEX_ENVIRONMENT;
521 ParagraphList::const_iterator makeEnvironmentHtml(Buffer const & buf,
523 OutputParams const & runparams,
525 ParagraphList::const_iterator const & pbegin,
526 ParagraphList::const_iterator const & pend)
528 ParagraphList::const_iterator const begin = text.paragraphs().begin();
529 ParagraphList::const_iterator par = pbegin;
530 Layout const & bstyle = par->layout();
531 depth_type const origdepth = pbegin->params().depth();
533 // open tag for this environment
537 // we will on occasion need to remember a layout from before.
538 Layout const * lastlay = 0;
540 while (par != pend) {
541 Layout const & style = par->layout();
542 // the counter only gets stepped if we're in some kind of list,
543 // or if it's the first time through.
544 if (!style.counter.empty() && (par == pbegin || !isNormalEnv(style)))
545 buf.params().documentClass().counters().step(style.counter);
546 ParagraphList::const_iterator send;
547 // this will be positive, if we want to skip the initial word
548 // (if it's been taken for the label).
551 switch (style.latextype) {
552 case LATEX_ENVIRONMENT:
553 case LATEX_LIST_ENVIRONMENT:
554 case LATEX_ITEM_ENVIRONMENT: {
555 // There are two possiblities in this case.
556 // One is that we are still in the environment in which we
557 // started---which we will be if the depth is the same.
558 if (par->params().depth() == origdepth) {
559 LASSERT(bstyle == style, /* */);
561 closeItemTag(xs, *lastlay);
564 bool const labelfirst = style.htmllabelfirst();
565 if (isNormalEnv(style)) {
566 // in this case, we print the label only for the first
567 // paragraph (as in a theorem).
568 openItemTag(xs, style);
569 if (par == pbegin && style.htmllabeltag() != "NONE") {
570 docstring const lbl =
571 pbegin->expandLabel(style, buf.params(), false);
573 openLabelTag(xs, style);
575 closeLabelTag(xs, style);
579 } else { // some kind of list
581 openItemTag(xs, style);
582 if (style.labeltype == LABEL_MANUAL
583 && style.htmllabeltag() != "NONE") {
584 openLabelTag(xs, style);
585 // sep = par->firstWordLyXHTML(xs, runparams);
586 closeLabelTag(xs, style);
589 else if (style.labeltype != LABEL_NO_LABEL
590 && style.htmllabeltag() != "NONE") {
591 openLabelTag(xs, style);
592 xs << par->expandLabel(style, buf.params(), false);
593 closeLabelTag(xs, style);
597 openItemTag(xs, style);
599 xs << StartTag("span", "class='" + to_utf8(style.name()) + " inneritem'");
601 par->simpleLyXHTMLOnePar(buf, xs, runparams,
602 text.outerFont(distance(begin, par)), sep);
603 if (!isNormalEnv(style) && !labelfirst)
604 xs << EndTag("span");
606 // We may not want to close the tag yet, in particular,
607 // if we're not at the end...
609 // and are doing items...
610 && style.latextype == LATEX_ITEM_ENVIRONMENT
611 // and if the depth has changed...
612 && par->params().depth() != origdepth) {
613 // then we'll save this layout for later, and close it when
614 // we get another item.
617 closeItemTag(xs, style);
620 // The other possibility is that the depth has increased, in which
621 // case we need to recurse.
623 send = searchEnvironmentHtml(par, pend);
624 par = makeEnvironmentHtml(buf, xs, runparams, text, par, send);
628 case LATEX_PARAGRAPH:
629 send = searchParagraphHtml(par, pend);
630 par = makeParagraphs(buf, xs, runparams, text, par, send);
633 case LATEX_BIB_ENVIRONMENT:
636 par = makeParagraphs(buf, xs, runparams, text, par, send);
646 closeItemTag(xs, *lastlay);
647 closeTag(xs, bstyle);
653 void makeCommand(Buffer const & buf,
655 OutputParams const & runparams,
657 ParagraphList::const_iterator const & pbegin)
659 Layout const & style = pbegin->layout();
660 if (!style.counter.empty())
661 buf.params().documentClass().counters().step(style.counter);
665 // Label around sectioning number:
666 // FIXME Probably need to account for LABEL_MANUAL
667 if (style.labeltype != LABEL_NO_LABEL) {
668 openLabelTag(xs, style);
669 xs << pbegin->expandLabel(style, buf.params(), false);
670 closeLabelTag(xs, style);
671 // Otherwise the label might run together with the text
672 xs << from_ascii(" ");
675 ParagraphList::const_iterator const begin = text.paragraphs().begin();
676 pbegin->simpleLyXHTMLOnePar(buf, xs, runparams,
677 text.outerFont(distance(begin, pbegin)));
682 } // end anonymous namespace
685 void xhtmlParagraphs(Text const & text,
688 OutputParams const & runparams)
690 ParagraphList const & paragraphs = text.paragraphs();
691 ParagraphList::const_iterator par = paragraphs.begin();
692 ParagraphList::const_iterator pend = paragraphs.end();
694 OutputParams ourparams = runparams;
695 while (par != pend) {
696 Layout const & style = par->layout();
697 ParagraphList::const_iterator lastpar = par;
698 ParagraphList::const_iterator send;
700 switch (style.latextype) {
701 case LATEX_COMMAND: {
702 // The files with which we are working never have more than
703 // one paragraph in a command structure.
705 // if (ourparams.html_in_par)
706 // fix it so we don't get sections inside standard, e.g.
707 // note that we may then need to make runparams not const, so we
708 // can communicate that back.
709 // FIXME Maybe this fix should be in the routines themselves, in case
710 // they are called from elsewhere.
711 makeCommand(buf, xs, ourparams, text, par);
715 case LATEX_ENVIRONMENT:
716 case LATEX_LIST_ENVIRONMENT:
717 case LATEX_ITEM_ENVIRONMENT: {
718 // FIXME Same fix here.
719 send = searchEnvironmentHtml(par, pend);
720 par = makeEnvironmentHtml(buf, xs, ourparams, text, par, send);
723 case LATEX_BIB_ENVIRONMENT: {
724 // FIXME Same fix here.
725 send = searchEnvironmentHtml(par, pend);
726 par = makeBibliography(buf, xs, ourparams, text, par, send);
729 case LATEX_PARAGRAPH:
730 send = searchParagraphHtml(par, pend);
731 par = makeParagraphs(buf, xs, ourparams, text, par, send);
735 // makeEnvironment may process more than one paragraphs and bypass pend
736 if (distance(lastpar, par) >= distance(lastpar, pend))