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 if (tag.tag_.empty())
206 pending_tags_.push_back(tag);
213 XHTMLStream & XHTMLStream::operator<<(CompTag const & tag)
215 if (tag.tag_.empty())
224 bool XHTMLStream::isTagOpen(string const & stag)
226 TagStack::const_iterator sit = tag_stack_.begin();
227 TagStack::const_iterator const sen = tag_stack_.end();
228 for (; sit != sen; ++sit)
229 // we could check for the
230 if (sit->tag_ == stag)
236 // this is complicated, because we want to make sure that
237 // everything is properly nested. the code ought to make
238 // sure of that, but we won't assert (yet) if we run into
239 // a problem. we'll just output error messages and try our
240 // best to make things work.
241 XHTMLStream & XHTMLStream::operator<<(EndTag const & etag)
243 if (etag.tag_.empty())
245 // first make sure we're not closing an empty tag
246 if (!pending_tags_.empty()) {
247 StartTag const & stag = pending_tags_.back();
248 if (etag.tag_ == stag.tag_) {
249 // we have <tag></tag>, so we discard it and remove it
250 // from the pending_tags_.
251 pending_tags_.pop_back();
254 // there is a pending tag that isn't the one we are trying
256 // is this tag itself pending?
257 // non-const iterators because we may call erase().
258 TagDeque::iterator dit = pending_tags_.begin();
259 TagDeque::iterator const den = pending_tags_.end();
260 for (; dit != den; ++dit) {
261 if (dit->tag_ == etag.tag_) {
262 // it was pending, so we just erase it
263 LYXERR0("Tried to close pending tag `" << etag.tag_
264 << "' when other tags were pending. Tag discarded.");
265 pending_tags_.erase(dit);
269 // so etag isn't itself pending. is it even open?
270 if (!isTagOpen(etag.tag_)) {
271 LYXERR0("Tried to close `" << etag.tag_
272 << "' when tag was not open. Tag discarded.");
275 // ok, so etag is open.
276 // our strategy will be as below: we will do what we need to
277 // do to close this tag.
278 LYXERR0("Closing tag `" << etag.tag_
279 << "' when other tags are pending. Discarded pending tags:");
280 for (dit = pending_tags_.begin(); dit != den; ++dit)
282 // clear the pending tags...
283 pending_tags_.clear();
284 // ...and then just fall through.
287 // is the tag we are closing the last one we opened?
288 if (etag.tag_ == tag_stack_.back().tag_) {
290 os_ << etag.asEndTag();
291 // ...and forget about it
292 tag_stack_.pop_back();
296 // we are trying to close a tag other than the one last opened.
297 // let's first see if this particular tag is still open somehow.
298 if (!isTagOpen(etag.tag_)) {
299 LYXERR0("Tried to close `" << etag.tag_
300 << "' when tag was not open. Tag discarded.");
304 // so the tag was opened, but other tags have been opened since
305 // and not yet closed.
306 // if it's a font tag, though...
307 if (html::isFontTag(etag.tag_)) {
308 // it won't be a problem if the other tags open since this one
309 // are also font tags.
310 TagStack::const_reverse_iterator rit = tag_stack_.rbegin();
311 TagStack::const_reverse_iterator ren = tag_stack_.rend();
312 for (; rit != ren; ++rit) {
313 if (rit->tag_ == etag.tag_)
315 if (!html::isFontTag(rit->tag_)) {
316 // we'll just leave it and, presumably, have to close it later.
317 LYXERR0("Unable to close font tag `" << etag.tag_
318 << "' due to open non-font tag `" << rit->tag_ << "'.");
324 // <em>this is <strong>bold
325 // and are being asked to closed em. we want:
326 // <em>this is <strong>bold</strong></em><strong>
327 // first, we close the intervening tags...
328 StartTag curtag = tag_stack_.back();
329 // ...remembering them in a stack.
331 while (curtag.tag_ != etag.tag_) {
332 os_ << curtag.asEndTag();
333 fontstack.push_back(curtag);
334 tag_stack_.pop_back();
335 curtag = tag_stack_.back();
337 // now close our tag...
338 os_ << etag.asEndTag();
339 // ...and restore the other tags.
340 rit = fontstack.rbegin();
341 ren = fontstack.rend();
342 for (; rit != ren; ++rit)
343 pending_tags_.push_back(*rit);
347 // it wasn't a font tag.
348 // so other tags were opened before this one and not properly closed.
349 // so we'll close them, too. that may cause other issues later, but it
350 // at least guarantees proper nesting.
351 LYXERR0("Closing tag `" << etag.tag_
352 << "' when other tags are open, namely:");
353 StartTag curtag = tag_stack_.back();
354 while (curtag.tag_ != etag.tag_) {
355 LYXERR0(curtag.tag_);
356 os_ << curtag.asEndTag();
357 tag_stack_.pop_back();
358 curtag = tag_stack_.back();
360 // curtag is now the one we actually want.
361 os_ << curtag.asEndTag();
362 tag_stack_.pop_back();
367 // End code for XHTMLStream
371 // convenience functions
373 inline void openTag(XHTMLStream & xs, Layout const & lay)
375 xs << StartTag(lay.htmltag(), lay.htmlattr());
379 inline void closeTag(XHTMLStream & xs, Layout const & lay)
381 xs << EndTag(lay.htmltag());
385 inline void openLabelTag(XHTMLStream & xs, Layout const & lay)
387 xs << StartTag(lay.htmllabeltag(), lay.htmllabelattr());
391 inline void closeLabelTag(XHTMLStream & xs, Layout const & lay)
393 xs << EndTag(lay.htmllabeltag());
397 inline void openItemTag(XHTMLStream & xs, Layout const & lay)
399 xs << StartTag(lay.htmlitemtag(), lay.htmlitemattr(), true);
403 inline void closeItemTag(XHTMLStream & xs, Layout const & lay)
405 xs << EndTag(lay.htmlitemtag());
408 // end of convenience functions
410 ParagraphList::const_iterator searchParagraphHtml(
411 ParagraphList::const_iterator p,
412 ParagraphList::const_iterator const & pend)
414 for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
421 ParagraphList::const_iterator searchEnvironmentHtml(
422 ParagraphList::const_iterator const pstart,
423 ParagraphList::const_iterator const & pend)
425 ParagraphList::const_iterator p = pstart;
426 Layout const & bstyle = p->layout();
427 size_t const depth = p->params().depth();
428 for (++p; p != pend; ++p) {
429 Layout const & style = p->layout();
430 // It shouldn't happen that e.g. a section command occurs inside
431 // a quotation environment, at a higher depth, but as of 6/2009,
432 // it can happen. We pretend that it's just at lowest depth.
433 if (style.latextype == LATEX_COMMAND)
435 // If depth is down, we're done
436 if (p->params().depth() < depth)
438 // If depth is up, we're not done
439 if (p->params().depth() > depth)
441 // Now we know we are at the same depth
442 if (style.latextype == LATEX_PARAGRAPH
443 || style.latexname() != bstyle.latexname())
450 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
452 OutputParams const & runparams,
454 ParagraphList::const_iterator const & pbegin,
455 ParagraphList::const_iterator const & pend)
457 ParagraphList::const_iterator const begin = text.paragraphs().begin();
458 ParagraphList::const_iterator par = pbegin;
459 for (; par != pend; ++par) {
460 Layout const & lay = par->layout();
461 if (!lay.counter.empty())
462 buf.params().documentClass().counters().step(lay.counter);
463 // FIXME We should see if there's a label to be output and
464 // do something with it.
468 // FIXME Should we really allow anything other than 'p' here?
470 // If we are already in a paragraph, and this is the first one, then we
471 // do not want to open the paragraph tag.
473 (par == pbegin && runparams.html_in_par) ? false : true;
476 docstring const deferred =
477 par->simpleLyXHTMLOnePar(buf, xs, runparams, text.outerFont(distance(begin, par)));
479 // We want to issue the closing tag if either:
480 // (i) We opened it, and either html_in_par is false,
481 // or we're not in the last paragraph, anyway.
482 // (ii) We didn't open it and html_in_par is true,
483 // but we are in the first par, and there is a next par.
484 ParagraphList::const_iterator nextpar = par;
486 bool const needclose =
487 (opened && (!runparams.html_in_par || nextpar != pend))
488 || (!opened && runparams.html_in_par && par == pbegin && nextpar != pend);
493 if (!deferred.empty()) {
502 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
504 OutputParams const & runparams,
506 ParagraphList::const_iterator const & pbegin,
507 ParagraphList::const_iterator const & pend)
509 xs << StartTag("h2", "class='bibliography'");
510 xs << pbegin->layout().labelstring(false);
513 xs << StartTag("div", "class='bibliography'");
515 makeParagraphs(buf, xs, runparams, text, pbegin, pend);
521 bool isNormalEnv(Layout const & lay)
523 return lay.latextype == LATEX_ENVIRONMENT;
527 ParagraphList::const_iterator makeEnvironmentHtml(Buffer const & buf,
529 OutputParams const & runparams,
531 ParagraphList::const_iterator const & pbegin,
532 ParagraphList::const_iterator const & pend)
534 ParagraphList::const_iterator const begin = text.paragraphs().begin();
535 ParagraphList::const_iterator par = pbegin;
536 Layout const & bstyle = par->layout();
537 depth_type const origdepth = pbegin->params().depth();
539 // open tag for this environment
543 // we will on occasion need to remember a layout from before.
544 Layout const * lastlay = 0;
546 while (par != pend) {
547 Layout const & style = par->layout();
548 // the counter only gets stepped if we're in some kind of list,
549 // or if it's the first time through.
550 if (!style.counter.empty() && (par == pbegin || !isNormalEnv(style)))
551 buf.params().documentClass().counters().step(style.counter);
552 ParagraphList::const_iterator send;
553 // this will be positive, if we want to skip the initial word
554 // (if it's been taken for the label).
557 switch (style.latextype) {
558 case LATEX_ENVIRONMENT:
559 case LATEX_LIST_ENVIRONMENT:
560 case LATEX_ITEM_ENVIRONMENT: {
561 // There are two possiblities in this case.
562 // One is that we are still in the environment in which we
563 // started---which we will be if the depth is the same.
564 if (par->params().depth() == origdepth) {
565 LASSERT(bstyle == style, /* */);
567 closeItemTag(xs, *lastlay);
570 bool const labelfirst = style.htmllabelfirst();
571 if (isNormalEnv(style)) {
572 // in this case, we print the label only for the first
573 // paragraph (as in a theorem).
574 openItemTag(xs, style);
575 if (par == pbegin && style.htmllabeltag() != "NONE") {
576 docstring const lbl =
577 pbegin->expandLabel(style, buf.params(), false);
579 openLabelTag(xs, style);
581 closeLabelTag(xs, style);
585 } else { // some kind of list
587 openItemTag(xs, style);
588 if (style.labeltype == LABEL_MANUAL
589 && style.htmllabeltag() != "NONE") {
590 openLabelTag(xs, style);
591 // sep = par->firstWordLyXHTML(xs, runparams);
592 closeLabelTag(xs, style);
595 else if (style.labeltype != LABEL_NO_LABEL
596 && style.htmllabeltag() != "NONE") {
597 openLabelTag(xs, style);
598 xs << par->expandLabel(style, buf.params(), false);
599 closeLabelTag(xs, style);
603 openItemTag(xs, style);
605 xs << StartTag("span", "class='" + to_utf8(style.name()) + " inneritem'");
607 par->simpleLyXHTMLOnePar(buf, xs, runparams,
608 text.outerFont(distance(begin, par)), sep);
609 if (!isNormalEnv(style) && !labelfirst)
610 xs << EndTag("span");
612 // We may not want to close the tag yet, in particular,
613 // if we're not at the end...
615 // and are doing items...
616 && style.latextype == LATEX_ITEM_ENVIRONMENT
617 // and if the depth has changed...
618 && par->params().depth() != origdepth) {
619 // then we'll save this layout for later, and close it when
620 // we get another item.
623 closeItemTag(xs, style);
626 // The other possibility is that the depth has increased, in which
627 // case we need to recurse.
629 send = searchEnvironmentHtml(par, pend);
630 par = makeEnvironmentHtml(buf, xs, runparams, text, par, send);
634 case LATEX_PARAGRAPH:
635 send = searchParagraphHtml(par, pend);
636 par = makeParagraphs(buf, xs, runparams, text, par, send);
639 case LATEX_BIB_ENVIRONMENT:
642 par = makeParagraphs(buf, xs, runparams, text, par, send);
652 closeItemTag(xs, *lastlay);
653 closeTag(xs, bstyle);
659 void makeCommand(Buffer const & buf,
661 OutputParams const & runparams,
663 ParagraphList::const_iterator const & pbegin)
665 Layout const & style = pbegin->layout();
666 if (!style.counter.empty())
667 buf.params().documentClass().counters().step(style.counter);
671 // Label around sectioning number:
672 // FIXME Probably need to account for LABEL_MANUAL
673 if (style.labeltype != LABEL_NO_LABEL) {
674 openLabelTag(xs, style);
675 xs << pbegin->expandLabel(style, buf.params(), false);
676 closeLabelTag(xs, style);
677 // Otherwise the label might run together with the text
678 xs << from_ascii(" ");
681 ParagraphList::const_iterator const begin = text.paragraphs().begin();
682 pbegin->simpleLyXHTMLOnePar(buf, xs, runparams,
683 text.outerFont(distance(begin, pbegin)));
688 } // end anonymous namespace
691 void xhtmlParagraphs(Text const & text,
694 OutputParams const & runparams)
696 ParagraphList const & paragraphs = text.paragraphs();
697 ParagraphList::const_iterator par = paragraphs.begin();
698 ParagraphList::const_iterator pend = paragraphs.end();
700 OutputParams ourparams = runparams;
701 while (par != pend) {
702 Layout const & style = par->layout();
703 ParagraphList::const_iterator lastpar = par;
704 ParagraphList::const_iterator send;
706 switch (style.latextype) {
707 case LATEX_COMMAND: {
708 // The files with which we are working never have more than
709 // one paragraph in a command structure.
711 // if (ourparams.html_in_par)
712 // fix it so we don't get sections inside standard, e.g.
713 // note that we may then need to make runparams not const, so we
714 // can communicate that back.
715 // FIXME Maybe this fix should be in the routines themselves, in case
716 // they are called from elsewhere.
717 makeCommand(buf, xs, ourparams, text, par);
721 case LATEX_ENVIRONMENT:
722 case LATEX_LIST_ENVIRONMENT:
723 case LATEX_ITEM_ENVIRONMENT: {
724 // FIXME Same fix here.
725 send = searchEnvironmentHtml(par, pend);
726 par = makeEnvironmentHtml(buf, xs, ourparams, text, par, send);
729 case LATEX_BIB_ENVIRONMENT: {
730 // FIXME Same fix here.
731 send = searchEnvironmentHtml(par, pend);
732 par = makeBibliography(buf, xs, ourparams, text, par, send);
735 case LATEX_PARAGRAPH:
736 send = searchParagraphHtml(par, pend);
737 par = makeParagraphs(buf, xs, ourparams, text, par, send);
741 // makeEnvironment may process more than one paragraphs and bypass pend
742 if (distance(lastpar, par) >= distance(lastpar, pend))