1 /**
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.
5  *
6  * \author Richard Heck
7  *
8  * This code is based upon output_docbook.cpp
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12 
13 #include <config.h>
14 
15 #include "output_xhtml.h"
16 
17 #include "Buffer.h"
18 #include "buffer_funcs.h"
19 #include "BufferParams.h"
20 #include "Counters.h"
21 #include "Font.h"
22 #include "Layout.h"
23 #include "OutputParams.h"
24 #include "Paragraph.h"
25 #include "ParagraphList.h"
26 #include "ParagraphParameters.h"
27 #include "sgml.h"
28 #include "Text.h"
29 #include "TextClass.h"
30 
31 #include "support/convert.h"
32 #include "support/debug.h"
33 #include "support/lassert.h"
34 #include "support/lstrings.h"
35 #include "support/textutils.h"
36 
37 #include <vector>
38 
39 // Uncomment to activate debugging code.
40 // #define XHTML_DEBUG
41 
42 using namespace std;
43 using namespace lyx::support;
44 
45 namespace lyx {
46 
47 namespace html {
48 
escapeChar(char_type c,XHTMLStream::EscapeSettings e)49 docstring escapeChar(char_type c, XHTMLStream::EscapeSettings e)
50 {
51 	docstring str;
52 	switch (e) {
53 	case XHTMLStream::ESCAPE_NONE:
54 		str += c;
55 		break;
56 	case XHTMLStream::ESCAPE_ALL:
57 		if (c == '<') {
58 			str += "&lt;";
59 			break;
60 		} else if (c == '>') {
61 			str += "&gt;";
62 			break;
63 		}
64 	// fall through
65 	case XHTMLStream::ESCAPE_AND:
66 		if (c == '&')
67 			str += "&amp;";
68 		else
69 			str	+=c ;
70 		break;
71 	}
72 	return str;
73 }
74 
75 
76 // escape what needs escaping
htmlize(docstring const & str,XHTMLStream::EscapeSettings e)77 docstring htmlize(docstring const & str, XHTMLStream::EscapeSettings e)
78 {
79 	odocstringstream d;
80 	docstring::const_iterator it = str.begin();
81 	docstring::const_iterator en = str.end();
82 	for (; it != en; ++it)
83 		d << escapeChar(*it, e);
84 	return d.str();
85 }
86 
87 
escapeChar(char c,XHTMLStream::EscapeSettings e)88 docstring escapeChar(char c, XHTMLStream::EscapeSettings e)
89 {
90 	LATTEST(static_cast<unsigned char>(c) < 0x80);
91 	return escapeChar(static_cast<char_type>(c), e);
92 }
93 
94 
cleanAttr(docstring const & str)95 docstring cleanAttr(docstring const & str)
96 {
97 	docstring newname;
98 	docstring::const_iterator it = str.begin();
99 	docstring::const_iterator en = str.end();
100 	for (; it != en; ++it) {
101 		char_type const c = *it;
102 		newname += isAlnumASCII(c) ? c : char_type('_');
103 	}
104 	return newname;
105 }
106 
107 
writeTag() const108 docstring StartTag::writeTag() const
109 {
110 	docstring output = '<' + from_utf8(tag_);
111 	if (!attr_.empty())
112 		output += ' ' + html::htmlize(from_utf8(attr_), XHTMLStream::ESCAPE_NONE);
113 	output += ">";
114 	return output;
115 }
116 
117 
writeEndTag() const118 docstring StartTag::writeEndTag() const
119 {
120 	string output = "</" + tag_ + ">";
121 	return from_utf8(output);
122 }
123 
124 
operator ==(FontTag const & rhs) const125 bool StartTag::operator==(FontTag const & rhs) const
126 {
127 	return rhs == *this;
128 }
129 
130 
writeEndTag() const131 docstring EndTag::writeEndTag() const
132 {
133 	string output = "</" + tag_ + ">";
134 	return from_utf8(output);
135 }
136 
137 
ParTag(std::string const & tag,std::string attr,std::string const & parid)138 ParTag::ParTag(std::string const & tag, std::string attr,
139        std::string const & parid)
140   : StartTag(tag)
141 {
142 	if (!parid.empty())
143 		attr += " id='" + parid + "'";
144 	attr_ = attr;
145 }
146 
147 
writeTag() const148 docstring CompTag::writeTag() const
149 {
150 	docstring output = '<' + from_utf8(tag_);
151 	if (!attr_.empty())
152 		output += ' ' + html::htmlize(from_utf8(attr_), XHTMLStream::ESCAPE_NONE);
153 	output += " />";
154 	return output;
155 }
156 
157 
158 
159 namespace {
160 
fontToTag(html::FontTypes type)161 string fontToTag(html::FontTypes type)
162 {
163 	switch(type) {
164 	case FT_EMPH:
165 		return "em";
166 	case FT_BOLD:
167 		return "b";
168 	case FT_NOUN:
169 		return "dfn";
170 	case FT_UBAR:
171 	case FT_WAVE:
172 	case FT_DBAR:
173 		return "u";
174 	case FT_SOUT:
175 	case FT_XOUT:
176 		return "del";
177 	case FT_ITALIC:
178 		return "i";
179 	case FT_UPRIGHT:
180 	case FT_SLANTED:
181 	case FT_SMALLCAPS:
182 	case FT_ROMAN:
183 	case FT_SANS:
184 	case FT_TYPE:
185 	case FT_SIZE_TINY:
186 	case FT_SIZE_SCRIPT:
187 	case FT_SIZE_FOOTNOTE:
188 	case FT_SIZE_SMALL:
189 	case FT_SIZE_NORMAL:
190 	case FT_SIZE_LARGE:
191 	case FT_SIZE_LARGER:
192 	case FT_SIZE_LARGEST:
193 	case FT_SIZE_HUGE:
194 	case FT_SIZE_HUGER:
195 	case FT_SIZE_INCREASE:
196 	case FT_SIZE_DECREASE:
197 		return "span";
198 	}
199 	// kill warning
200 	return "";
201 }
202 
fontToAttribute(html::FontTypes type)203 string fontToAttribute(html::FontTypes type)
204 {
205 	switch(type) {
206 	case FT_EMPH:
207 	case FT_BOLD:
208 		return "";
209 	case FT_NOUN:
210 		return "class='lyxnoun'";
211 	case FT_UBAR:
212 		return "";
213 	case FT_DBAR:
214 		return "class='dline'";
215 	case FT_XOUT:
216 	case FT_SOUT:
217 		return "class='strikeout'";
218 	case FT_WAVE:
219 		return "class='wline'";
220 	case FT_ITALIC:
221 		return "";
222 	case FT_UPRIGHT:
223 		return "style='font-style:normal;'";
224 	case FT_SLANTED:
225 		return "style='font-style:oblique;'";
226 	case FT_SMALLCAPS:
227 		return "style='font-variant:small-caps;'";
228 	case FT_ROMAN:
229 		return "style='font-family:serif;'";
230 	case FT_SANS:
231 		return "style='font-family:sans-serif;'";
232 	case FT_TYPE:
233 		return "style='font-family:monospace;'";
234 	case FT_SIZE_TINY:
235 	case FT_SIZE_SCRIPT:
236 	case FT_SIZE_FOOTNOTE:
237 		return "style='font-size:x-small;'";
238 	case FT_SIZE_SMALL:
239 		return "style='font-size:small;'";
240 	case FT_SIZE_NORMAL:
241 		return "style='font-size:normal;'";
242 	case FT_SIZE_LARGE:
243 		return "style='font-size:large;'";
244 	case FT_SIZE_LARGER:
245 	case FT_SIZE_LARGEST:
246 		return "style='font-size:x-large;'";
247 	case FT_SIZE_HUGE:
248 	case FT_SIZE_HUGER:
249 		return "style='font-size:xx-large;'";
250 	case FT_SIZE_INCREASE:
251 		return "style='font-size:larger;'";
252 	case FT_SIZE_DECREASE:
253 		return "style='font-size:smaller;'";
254 	}
255 	// kill warning
256 	return "";
257 }
258 
259 } // end anonymous namespace
260 
261 
FontTag(FontTypes type)262 FontTag::FontTag(FontTypes type)
263   : StartTag(fontToTag(type), fontToAttribute(type)), font_type_(type)
264 {}
265 
266 
operator ==(StartTag const & tag) const267 bool FontTag::operator==(StartTag const & tag) const
268 {
269 	FontTag const * const ftag = tag.asFontTag();
270 	if (!ftag)
271 		return false;
272 	return (font_type_ == ftag->font_type_);
273 }
274 
275 
EndFontTag(FontTypes type)276 EndFontTag::EndFontTag(FontTypes type)
277 	  : EndTag(fontToTag(type)), font_type_(type)
278 {}
279 
280 } // namespace html
281 
282 
283 
284 ////////////////////////////////////////////////////////////////
285 ///
286 /// XHTMLStream
287 ///
288 ////////////////////////////////////////////////////////////////
289 
XHTMLStream(odocstream & os)290 XHTMLStream::XHTMLStream(odocstream & os)
291   : os_(os), escape_(ESCAPE_ALL)
292 {}
293 
294 
295 #ifdef XHTML_DEBUG
dumpTagStack(string const & msg)296 void XHTMLStream::dumpTagStack(string const & msg)
297 {
298 	*this << html::CR();
299 	writeError(msg);
300 	*this << html::CR();
301 	writeError("Tag Stack");
302 	TagDeque::const_reverse_iterator it = tag_stack_.rbegin();
303 	TagDeque::const_reverse_iterator en = tag_stack_.rend();
304 	for (; it != en; ++it) {
305 		writeError(it->get()->tag_);
306 	}
307 	writeError("End Tag Stack");
308 	*this << html::CR();
309 	writeError("Pending Tags");
310 	it = pending_tags_.rbegin();
311 	en = pending_tags_.rend();
312 	for (; it != en; ++it) {
313 		writeError(it->get()->tag_);
314 	}
315 	writeError("End Pending Tags");
316 	*this << html::CR();
317 }
318 #endif
319 
320 
writeError(std::string const & s) const321 void XHTMLStream::writeError(std::string const & s) const
322 {
323 	LYXERR0(s);
324 	os_ << from_utf8("<!-- Output Error: " + s + " -->\n");
325 }
326 
327 
328 namespace {
329 	// an illegal tag for internal use
330 	static html::StartTag const parsep_tag("&LyX_parsep_tag&");
331 } // namespace
332 
333 
closeFontTags()334 bool XHTMLStream::closeFontTags()
335 {
336 	if (isTagPending(parsep_tag))
337 		// we haven't had any content
338 		return true;
339 
340 #ifdef	XHTML_DEBUG
341 	dumpTagStack("Beging Close Font Tags");
342 #endif
343 
344 	// this may be a useless check, since we ought at least to have
345 	// the parsep_tag. but it can't hurt too much to be careful.
346 	if (tag_stack_.empty())
347 		return true;
348 
349 	// first, we close any open font tags we can close
350 	TagPtr curtag = tag_stack_.back();
351 	while (curtag->asFontTag()) {
352 		os_ << curtag->writeEndTag();
353 		tag_stack_.pop_back();
354 		// this shouldn't happen, since then the font tags
355 		// weren't in any other tag.
356 		LASSERT(!tag_stack_.empty(), return true);
357 		curtag = tag_stack_.back();
358 	}
359 
360 #ifdef	XHTML_DEBUG
361 	dumpTagStack("End Close Font Tags");
362 #endif
363 
364 	if (*curtag == parsep_tag)
365 		return true;
366 
367 	// so we've hit a non-font tag.
368 	writeError("Tags still open in closeFontTags(). Probably not a problem,\n"
369 	           "but you might want to check these tags:");
370 	TagDeque::const_reverse_iterator it = tag_stack_.rbegin();
371 	TagDeque::const_reverse_iterator const en = tag_stack_.rend();
372 	for (; it != en; ++it) {
373 		if (**it == parsep_tag)
374 			break;
375 		writeError((*it)->tag_);
376 	}
377 	return false;
378 }
379 
380 
startDivision(bool keep_empty)381 void XHTMLStream::startDivision(bool keep_empty)
382 {
383 	pending_tags_.push_back(makeTagPtr(html::StartTag(parsep_tag)));
384 	if (keep_empty)
385 		clearTagDeque();
386 #ifdef	XHTML_DEBUG
387 	dumpTagStack("StartDivision");
388 #endif
389 }
390 
391 
endDivision()392 void XHTMLStream::endDivision()
393 {
394 	if (isTagPending(parsep_tag)) {
395 		// this case is normal. it just means we didn't have content,
396 		// so the parsep_tag never got moved onto the tag stack.
397 		while (!pending_tags_.empty()) {
398 			// clear all pending tags up to and including the parsep tag.
399 			// note that we work from the back, because we want to get rid
400 			// of everything that hasn't been used.
401 			TagPtr const cur_tag = pending_tags_.back();
402 			pending_tags_.pop_back();
403 			if (*cur_tag == parsep_tag)
404 				break;
405 		}
406 
407 #ifdef	XHTML_DEBUG
408 		dumpTagStack("EndDivision");
409 #endif
410 
411 		return;
412 	}
413 
414 	if (!isTagOpen(parsep_tag)) {
415 		writeError("No division separation tag found in endDivision().");
416 		return;
417 	}
418 
419 	// this case is also normal, if the parsep tag is the last one
420 	// on the stack. otherwise, it's an error.
421 	while (!tag_stack_.empty()) {
422 		TagPtr const cur_tag = tag_stack_.back();
423 		tag_stack_.pop_back();
424 		if (*cur_tag == parsep_tag)
425 			break;
426 		writeError("Tag `" + cur_tag->tag_ + "' still open at end of paragraph. Closing.");
427 		os_ << cur_tag->writeEndTag();
428 	}
429 
430 #ifdef	XHTML_DEBUG
431 	dumpTagStack("EndDivision");
432 #endif
433 }
434 
435 
clearTagDeque()436 void XHTMLStream::clearTagDeque()
437 {
438 	while (!pending_tags_.empty()) {
439 		TagPtr const tag = pending_tags_.front();
440 		if (*tag != parsep_tag)
441 			// tabs?
442 			os_ << tag->writeTag();
443 		tag_stack_.push_back(tag);
444 		pending_tags_.pop_front();
445 	}
446 }
447 
448 
operator <<(docstring const & d)449 XHTMLStream & XHTMLStream::operator<<(docstring const & d)
450 {
451 	clearTagDeque();
452 	os_ << html::htmlize(d, escape_);
453 	escape_ = ESCAPE_ALL;
454 	return *this;
455 }
456 
457 
operator <<(const char * s)458 XHTMLStream & XHTMLStream::operator<<(const char * s)
459 {
460 	clearTagDeque();
461 	docstring const d = from_ascii(s);
462 	os_ << html::htmlize(d, escape_);
463 	escape_ = ESCAPE_ALL;
464 	return *this;
465 }
466 
467 
operator <<(char_type c)468 XHTMLStream & XHTMLStream::operator<<(char_type c)
469 {
470 	clearTagDeque();
471 	os_ << html::escapeChar(c, escape_);
472 	escape_ = ESCAPE_ALL;
473 	return *this;
474 }
475 
476 
operator <<(char c)477 XHTMLStream & XHTMLStream::operator<<(char c)
478 {
479 	clearTagDeque();
480 	os_ << html::escapeChar(c, escape_);
481 	escape_ = ESCAPE_ALL;
482 	return *this;
483 }
484 
485 
operator <<(int i)486 XHTMLStream & XHTMLStream::operator<<(int i)
487 {
488 	clearTagDeque();
489 	os_ << i;
490 	escape_ = ESCAPE_ALL;
491 	return *this;
492 }
493 
494 
operator <<(EscapeSettings e)495 XHTMLStream & XHTMLStream::operator<<(EscapeSettings e)
496 {
497 	escape_ = e;
498 	return *this;
499 }
500 
501 
operator <<(html::StartTag const & tag)502 XHTMLStream & XHTMLStream::operator<<(html::StartTag const & tag)
503 {
504 	if (tag.tag_.empty())
505 		return *this;
506 	pending_tags_.push_back(makeTagPtr(tag));
507 	if (tag.keepempty_)
508 		clearTagDeque();
509 	return *this;
510 }
511 
512 
operator <<(html::ParTag const & tag)513 XHTMLStream & XHTMLStream::operator<<(html::ParTag const & tag)
514 {
515 	if (tag.tag_.empty())
516 		return *this;
517 	pending_tags_.push_back(makeTagPtr(tag));
518 	return *this;
519 }
520 
521 
operator <<(html::CompTag const & tag)522 XHTMLStream & XHTMLStream::operator<<(html::CompTag const & tag)
523 {
524 	if (tag.tag_.empty())
525 		return *this;
526 	clearTagDeque();
527 	os_ << tag.writeTag();
528 	*this << html::CR();
529 	return *this;
530 }
531 
532 
operator <<(html::FontTag const & tag)533 XHTMLStream & XHTMLStream::operator<<(html::FontTag const & tag)
534 {
535 	if (tag.tag_.empty())
536 		return *this;
537 	pending_tags_.push_back(makeTagPtr(tag));
538 	return *this;
539 }
540 
541 
operator <<(html::CR const &)542 XHTMLStream & XHTMLStream::operator<<(html::CR const &)
543 {
544 	// tabs?
545 	os_ << from_ascii("\n");
546 	return *this;
547 }
548 
549 
isTagOpen(html::StartTag const & stag) const550 bool XHTMLStream::isTagOpen(html::StartTag const & stag) const
551 {
552 	TagDeque::const_iterator sit = tag_stack_.begin();
553 	TagDeque::const_iterator const sen = tag_stack_.end();
554 	for (; sit != sen; ++sit)
555 		if (**sit == stag)
556 			return true;
557 	return false;
558 }
559 
560 
isTagOpen(html::EndTag const & etag) const561 bool XHTMLStream::isTagOpen(html::EndTag const & etag) const
562 {
563 	TagDeque::const_iterator sit = tag_stack_.begin();
564 	TagDeque::const_iterator const sen = tag_stack_.end();
565 	for (; sit != sen; ++sit)
566 		if (etag == **sit)
567 			return true;
568 	return false;
569 }
570 
571 
isTagPending(html::StartTag const & stag) const572 bool XHTMLStream::isTagPending(html::StartTag const & stag) const
573 {
574 	TagDeque::const_iterator sit = pending_tags_.begin();
575 	TagDeque::const_iterator const sen = pending_tags_.end();
576 	for (; sit != sen; ++sit)
577 		if (**sit == stag)
578 			return true;
579 	return false;
580 }
581 
582 
583 // this is complicated, because we want to make sure that
584 // everything is properly nested. the code ought to make
585 // sure of that, but we won't assert (yet) if we run into
586 // a problem. we'll just output error messages and try our
587 // best to make things work.
operator <<(html::EndTag const & etag)588 XHTMLStream & XHTMLStream::operator<<(html::EndTag const & etag)
589 {
590 	if (etag.tag_.empty())
591 		return *this;
592 
593 	// if this tag is pending, we can simply discard it.
594 	if (!pending_tags_.empty()) {
595 
596 		if (etag == *pending_tags_.back()) {
597 			// we have <tag></tag>, so we discard it and remove it
598 			// from the pending_tags_.
599 			pending_tags_.pop_back();
600 			return *this;
601 		}
602 
603 		// there is a pending tag that isn't the one we are trying
604 		// to close.
605 
606 		// is this tag itself pending?
607 		// non-const iterators because we may call erase().
608 		TagDeque::iterator dit = pending_tags_.begin();
609 		TagDeque::iterator const den = pending_tags_.end();
610 		for (; dit != den; ++dit) {
611 			if (etag == **dit) {
612 				// it was pending, so we just erase it
613 				writeError("Tried to close pending tag `" + etag.tag_
614 				        + "' when other tags were pending. Last pending tag is `"
615 				        + to_utf8(pending_tags_.back()->writeTag())
616 				        + "'. Tag discarded.");
617 				pending_tags_.erase(dit);
618 				return *this;
619 			}
620 		}
621 		// so etag isn't itself pending. is it even open?
622 		if (!isTagOpen(etag)) {
623 			writeError("Tried to close `" + etag.tag_
624 			         + "' when tag was not open. Tag discarded.");
625 			return *this;
626 		}
627 		// ok, so etag is open.
628 		// our strategy will be as below: we will do what we need to
629 		// do to close this tag.
630 		string estr = "Closing tag `" + etag.tag_
631 		        + "' when other tags are pending. Discarded pending tags:\n";
632 		for (dit = pending_tags_.begin(); dit != den; ++dit)
633 			estr += to_utf8(html::htmlize((*dit)->writeTag(), XHTMLStream::ESCAPE_ALL)) + "\n";
634 		writeError(estr);
635 		// clear the pending tags...
636 		pending_tags_.clear();
637 		// ...and then just fall through.
638 	}
639 
640 	// make sure there are tags to be closed
641 	if (tag_stack_.empty()) {
642 		writeError("Tried to close `" + etag.tag_
643 		         + "' when no tags were open!");
644 		return *this;
645 	}
646 
647 	// is the tag we are closing the last one we opened?
648 	if (etag == *tag_stack_.back()) {
649 		// output it...
650 		os_ << etag.writeEndTag();
651 		// ...and forget about it
652 		tag_stack_.pop_back();
653 		return *this;
654 	}
655 
656 	// we are trying to close a tag other than the one last opened.
657 	// let's first see if this particular tag is still open somehow.
658 	if (!isTagOpen(etag)) {
659 		writeError("Tried to close `" + etag.tag_
660 		        + "' when tag was not open. Tag discarded.");
661 		return *this;
662 	}
663 
664 	// so the tag was opened, but other tags have been opened since
665 	// and not yet closed.
666 	// if it's a font tag, though...
667 	if (etag.asFontTag()) {
668 		// it won't be a problem if the other tags open since this one
669 		// are also font tags.
670 		TagDeque::const_reverse_iterator rit = tag_stack_.rbegin();
671 		TagDeque::const_reverse_iterator ren = tag_stack_.rend();
672 		for (; rit != ren; ++rit) {
673 			if (etag == **rit)
674 				break;
675 			if (!(*rit)->asFontTag()) {
676 				// we'll just leave it and, presumably, have to close it later.
677 				writeError("Unable to close font tag `" + etag.tag_
678 				        + "' due to open non-font tag `" + (*rit)->tag_ + "'.");
679 				return *this;
680 			}
681 		}
682 
683 		// so we have e.g.:
684 		//    <em>this is <strong>bold
685 		// and are being asked to closed em. we want:
686 		//    <em>this is <strong>bold</strong></em><strong>
687 		// first, we close the intervening tags...
688 		TagPtr curtag = tag_stack_.back();
689 		// ...remembering them in a stack.
690 		TagDeque fontstack;
691 		while (etag != *curtag) {
692 			os_ << curtag->writeEndTag();
693 			fontstack.push_back(curtag);
694 			tag_stack_.pop_back();
695 			curtag = tag_stack_.back();
696 		}
697 		os_ << etag.writeEndTag();
698 		tag_stack_.pop_back();
699 
700 		// ...and restore the other tags.
701 		rit = fontstack.rbegin();
702 		ren = fontstack.rend();
703 		for (; rit != ren; ++rit)
704 			pending_tags_.push_back(*rit);
705 		return *this;
706 	}
707 
708 	// it wasn't a font tag.
709 	// so other tags were opened before this one and not properly closed.
710 	// so we'll close them, too. that may cause other issues later, but it
711 	// at least guarantees proper nesting.
712 	writeError("Closing tag `" + etag.tag_
713 	        + "' when other tags are open, namely:");
714 	TagPtr curtag = tag_stack_.back();
715 	while (etag != *curtag) {
716 		writeError(curtag->tag_);
717 		if (*curtag != parsep_tag)
718 			os_ << curtag->writeEndTag();
719 		tag_stack_.pop_back();
720 		curtag = tag_stack_.back();
721 	}
722 	// curtag is now the one we actually want.
723 	os_ << curtag->writeEndTag();
724 	tag_stack_.pop_back();
725 
726 	return *this;
727 }
728 
729 // End code for XHTMLStream
730 
731 namespace {
732 
733 // convenience functions
734 
openParTag(XHTMLStream & xs,Layout const & lay,std::string parlabel)735 inline void openParTag(XHTMLStream & xs, Layout const & lay,
736                        std::string parlabel)
737 {
738 	xs << html::ParTag(lay.htmltag(), lay.htmlattr(), parlabel);
739 }
740 
741 
openParTag(XHTMLStream & xs,Layout const & lay,ParagraphParameters const & params,std::string parlabel)742 void openParTag(XHTMLStream & xs, Layout const & lay,
743                 ParagraphParameters const & params,
744                 std::string parlabel)
745 {
746 	// FIXME Are there other things we should handle here?
747 	string const align = alignmentToCSS(params.align());
748 	if (align.empty()) {
749 		openParTag(xs, lay, parlabel);
750 		return;
751 	}
752 	string attrs = lay.htmlattr() + " style='text-align: " + align + ";'";
753 	xs << html::ParTag(lay.htmltag(), attrs, parlabel);
754 }
755 
756 
closeTag(XHTMLStream & xs,Layout const & lay)757 inline void closeTag(XHTMLStream & xs, Layout const & lay)
758 {
759 	xs << html::EndTag(lay.htmltag());
760 }
761 
762 
openLabelTag(XHTMLStream & xs,Layout const & lay)763 inline void openLabelTag(XHTMLStream & xs, Layout const & lay)
764 {
765 	xs << html::StartTag(lay.htmllabeltag(), lay.htmllabelattr());
766 }
767 
768 
closeLabelTag(XHTMLStream & xs,Layout const & lay)769 inline void closeLabelTag(XHTMLStream & xs, Layout const & lay)
770 {
771 	xs << html::EndTag(lay.htmllabeltag());
772 }
773 
774 
openItemTag(XHTMLStream & xs,Layout const & lay)775 inline void openItemTag(XHTMLStream & xs, Layout const & lay)
776 {
777 	xs << html::StartTag(lay.htmlitemtag(), lay.htmlitemattr(), true);
778 }
779 
780 
openItemTag(XHTMLStream & xs,Layout const & lay,ParagraphParameters const & params)781 void openItemTag(XHTMLStream & xs, Layout const & lay,
782              ParagraphParameters const & params)
783 {
784 	// FIXME Are there other things we should handle here?
785 	string const align = alignmentToCSS(params.align());
786 	if (align.empty()) {
787 		openItemTag(xs, lay);
788 		return;
789 	}
790 	string attrs = lay.htmlattr() + " style='text-align: " + align + ";'";
791 	xs << html::StartTag(lay.htmlitemtag(), attrs);
792 }
793 
794 
closeItemTag(XHTMLStream & xs,Layout const & lay)795 inline void closeItemTag(XHTMLStream & xs, Layout const & lay)
796 {
797 	xs << html::EndTag(lay.htmlitemtag());
798 }
799 
800 // end of convenience functions
801 
findLastParagraph(ParagraphList::const_iterator p,ParagraphList::const_iterator const & pend)802 ParagraphList::const_iterator findLastParagraph(
803 	ParagraphList::const_iterator p,
804 	ParagraphList::const_iterator const & pend)
805 {
806 	for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
807 		;
808 
809 	return p;
810 }
811 
812 
findEndOfEnvironment(ParagraphList::const_iterator const & pstart,ParagraphList::const_iterator const & pend)813 ParagraphList::const_iterator findEndOfEnvironment(
814 		ParagraphList::const_iterator const & pstart,
815 		ParagraphList::const_iterator const & pend)
816 {
817 	ParagraphList::const_iterator p = pstart;
818 	Layout const & bstyle = p->layout();
819 	size_t const depth = p->params().depth();
820 	for (++p; p != pend; ++p) {
821 		Layout const & style = p->layout();
822 		// It shouldn't happen that e.g. a section command occurs inside
823 		// a quotation environment, at a higher depth, but as of 6/2009,
824 		// it can happen. We pretend that it's just at lowest depth.
825 		if (style.latextype == LATEX_COMMAND)
826 			return p;
827 
828 		// If depth is down, we're done
829 		if (p->params().depth() < depth)
830 			return p;
831 
832 		// If depth is up, we're not done
833 		if (p->params().depth() > depth)
834 			continue;
835 
836 		// FIXME I am not sure about the first check.
837 		// Surely we *could* have different layouts that count as
838 		// LATEX_PARAGRAPH, right?
839 		if (style.latextype == LATEX_PARAGRAPH || style != bstyle)
840 			return p;
841 	}
842 	return pend;
843 }
844 
845 
makeParagraphs(Buffer const & buf,XHTMLStream & xs,OutputParams const & runparams,Text const & text,ParagraphList::const_iterator const & pbegin,ParagraphList::const_iterator const & pend)846 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
847 					    XHTMLStream & xs,
848 					    OutputParams const & runparams,
849 					    Text const & text,
850 					    ParagraphList::const_iterator const & pbegin,
851 					    ParagraphList::const_iterator const & pend)
852 {
853 	ParagraphList::const_iterator const begin = text.paragraphs().begin();
854 	ParagraphList::const_iterator par = pbegin;
855 	for (; par != pend; ++par) {
856 		Layout const & lay = par->layout();
857 		if (!lay.counter.empty())
858 			buf.masterBuffer()->params().
859 			    documentClass().counters().step(lay.counter, OutputUpdate);
860 
861 		// FIXME We should see if there's a label to be output and
862 		// do something with it.
863 		if (par != pbegin)
864 			xs << html::CR();
865 
866 		// We want to open the paragraph tag if:
867 		//   (i) the current layout permits multiple paragraphs
868 		//  (ii) we are either not already inside a paragraph (HTMLIsBlock) OR
869 		//       we are, but this is not the first paragraph
870 		//
871 		// But there is also a special case, and we first see whether we are in it.
872 		// We do not want to open the paragraph tag if this paragraph contains
873 		// only one item, and that item is "inline", i.e., not HTMLIsBlock (such
874 		// as a branch). On the other hand, if that single item has a font change
875 		// applied to it, then we still do need to open the paragraph.
876 		//
877 		// Obviously, this is very fragile. The main reason we need to do this is
878 		// because of branches, e.g., a branch that contains an entire new section.
879 		// We do not really want to wrap that whole thing in a <div>...</div>.
880 		bool special_case = false;
881 		Inset const * specinset = par->size() == 1 ? par->getInset(0) : 0;
882 		if (specinset && !specinset->getLayout().htmlisblock()) {
883 			Layout const & style = par->layout();
884 			FontInfo const first_font = style.labeltype == LABEL_MANUAL ?
885 						style.labelfont : style.font;
886 			FontInfo const our_font =
887 				par->getFont(buf.masterBuffer()->params(), 0,
888 			               text.outerFont(distance(begin, par))).fontInfo();
889 			if (first_font == our_font)
890 				special_case = true;
891 		}
892 
893 		bool const open_par = runparams.html_make_pars
894 			&& (!runparams.html_in_par || par != pbegin)
895 			&& !special_case;
896 
897 		// We want to issue the closing tag if either:
898 		//   (i)  We opened it, and either html_in_par is false,
899 		//        or we're not in the last paragraph, anyway.
900 		//   (ii) We didn't open it and html_in_par is true,
901 		//        but we are in the first par, and there is a next par.
902 		ParagraphList::const_iterator nextpar = par;
903 		++nextpar;
904 		bool const close_par =
905 			(open_par && (!runparams.html_in_par || nextpar != pend))
906 			|| (!open_par && runparams.html_in_par && par == pbegin && nextpar != pend);
907 
908 		if (open_par) {
909 			// We do not issue the paragraph id if we are doing
910 			// this for the TOC (or some similar purpose)
911 			openParTag(xs, lay, par->params(),
912 			           runparams.for_toc ? "" : par->magicLabel());
913 		}
914 
915 		docstring const deferred = par->simpleLyXHTMLOnePar(buf, xs,
916 			runparams, text.outerFont(distance(begin, par)),
917 			open_par, close_par);
918 
919 		if (close_par) {
920 			closeTag(xs, lay);
921 			xs << html::CR();
922 		}
923 
924 		if (!deferred.empty()) {
925 			xs << XHTMLStream::ESCAPE_NONE << deferred << html::CR();
926 		}
927 	}
928 	return pend;
929 }
930 
931 
makeBibliography(Buffer const & buf,XHTMLStream & xs,OutputParams const & runparams,Text const & text,ParagraphList::const_iterator const & pbegin,ParagraphList::const_iterator const & pend)932 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
933 				XHTMLStream & xs,
934 				OutputParams const & runparams,
935 				Text const & text,
936 				ParagraphList::const_iterator const & pbegin,
937 				ParagraphList::const_iterator const & pend)
938 {
939 	// FIXME XHTML
940 	// Use TextClass::htmlTOCLayout() to figure out how we should look.
941 	xs << html::StartTag("h2", "class='bibliography'")
942 	   << pbegin->layout().labelstring(false)
943 	   << html::EndTag("h2")
944 	   << html::CR()
945 	   << html::StartTag("div", "class='bibliography'")
946 	   << html::CR();
947 	makeParagraphs(buf, xs, runparams, text, pbegin, pend);
948 	xs << html::EndTag("div");
949 	return pend;
950 }
951 
952 
isNormalEnv(Layout const & lay)953 bool isNormalEnv(Layout const & lay)
954 {
955 	return lay.latextype == LATEX_ENVIRONMENT
956 	    || lay.latextype == LATEX_BIB_ENVIRONMENT;
957 }
958 
959 
makeEnvironment(Buffer const & buf,XHTMLStream & xs,OutputParams const & runparams,Text const & text,ParagraphList::const_iterator const & pbegin,ParagraphList::const_iterator const & pend)960 ParagraphList::const_iterator makeEnvironment(Buffer const & buf,
961 					      XHTMLStream & xs,
962 					      OutputParams const & runparams,
963 					      Text const & text,
964 					      ParagraphList::const_iterator const & pbegin,
965 					      ParagraphList::const_iterator const & pend)
966 {
967 	ParagraphList::const_iterator const begin = text.paragraphs().begin();
968 	ParagraphList::const_iterator par = pbegin;
969 	Layout const & bstyle = par->layout();
970 	depth_type const origdepth = pbegin->params().depth();
971 
972 	// open tag for this environment
973 	openParTag(xs, bstyle, pbegin->magicLabel());
974 	xs << html::CR();
975 
976 	// we will on occasion need to remember a layout from before.
977 	Layout const * lastlay = 0;
978 
979 	while (par != pend) {
980 		Layout const & style = par->layout();
981 		// the counter only gets stepped if we're in some kind of list,
982 		// or if it's the first time through.
983 		// note that enum, etc, are handled automatically.
984 		// FIXME There may be a bug here about user defined enumeration
985 		// types. If so, then we'll need to take the counter and add "i",
986 		// "ii", etc, as with enum.
987 		Counters & cnts = buf.masterBuffer()->params().documentClass().counters();
988 		docstring const & cntr = style.counter;
989 		if (!style.counter.empty()
990 		    && (par == pbegin || !isNormalEnv(style))
991 				&& cnts.hasCounter(cntr)
992 		)
993 			cnts.step(cntr, OutputUpdate);
994 		ParagraphList::const_iterator send;
995 
996 		switch (style.latextype) {
997 		case LATEX_ENVIRONMENT:
998 		case LATEX_LIST_ENVIRONMENT:
999 		case LATEX_ITEM_ENVIRONMENT: {
1000 			// There are two possiblities in this case.
1001 			// One is that we are still in the environment in which we
1002 			// started---which we will be if the depth is the same.
1003 			if (par->params().depth() == origdepth) {
1004 				LATTEST(bstyle == style);
1005 				if (lastlay != 0) {
1006 					closeItemTag(xs, *lastlay);
1007 					lastlay = 0;
1008 				}
1009 
1010 				// this will be positive, if we want to skip the
1011 				// initial word (if it's been taken for the label).
1012 				pos_type sep = 0;
1013 				bool const labelfirst = style.htmllabelfirst();
1014 				if (!labelfirst)
1015 					openItemTag(xs, style, par->params());
1016 
1017 				// label output
1018 				if (style.labeltype != LABEL_NO_LABEL &&
1019 				    style.htmllabeltag() != "NONE") {
1020 					if (isNormalEnv(style)) {
1021 						// in this case, we print the label only for the first
1022 						// paragraph (as in a theorem).
1023 						if (par == pbegin) {
1024 							docstring const lbl =
1025 									pbegin->params().labelString();
1026 							if (!lbl.empty()) {
1027 								openLabelTag(xs, style);
1028 								xs << lbl;
1029 								closeLabelTag(xs, style);
1030 							}
1031 							xs << html::CR();
1032 						}
1033 					} else { // some kind of list
1034 						if (style.labeltype == LABEL_MANUAL) {
1035 							openLabelTag(xs, style);
1036 							sep = par->firstWordLyXHTML(xs, runparams);
1037 							closeLabelTag(xs, style);
1038 							xs << html::CR();
1039 						}
1040 						else {
1041 							openLabelTag(xs, style);
1042 							xs << par->params().labelString();
1043 							closeLabelTag(xs, style);
1044 							xs << html::CR();
1045 						}
1046 					}
1047 				} // end label output
1048 
1049 				if (labelfirst)
1050 					openItemTag(xs, style, par->params());
1051 
1052 				docstring deferred = par->simpleLyXHTMLOnePar(buf, xs, runparams,
1053 					text.outerFont(distance(begin, par)), true, true, sep);
1054 				xs << XHTMLStream::ESCAPE_NONE << deferred;
1055 				++par;
1056 
1057 				// We may not want to close the tag yet, in particular:
1058 				// If we're not at the end...
1059 				if (par != pend
1060 					//  and are doing items...
1061 					 && !isNormalEnv(style)
1062 					 // and if the depth has changed...
1063 					 && par->params().depth() != origdepth) {
1064 					 // then we'll save this layout for later, and close it when
1065 					 // we get another item.
1066 					lastlay = &style;
1067 				} else
1068 					closeItemTag(xs, style);
1069 				xs << html::CR();
1070 			}
1071 			// The other possibility is that the depth has increased, in which
1072 			// case we need to recurse.
1073 			else {
1074 				send = findEndOfEnvironment(par, pend);
1075 				par = makeEnvironment(buf, xs, runparams, text, par, send);
1076 			}
1077 			break;
1078 		}
1079 		case LATEX_PARAGRAPH:
1080 			send = findLastParagraph(par, pend);
1081 			par = makeParagraphs(buf, xs, runparams, text, par, send);
1082 			break;
1083 		// Shouldn't happen
1084 		case LATEX_BIB_ENVIRONMENT:
1085 			send = par;
1086 			++send;
1087 			par = makeParagraphs(buf, xs, runparams, text, par, send);
1088 			break;
1089 		// Shouldn't happen
1090 		case LATEX_COMMAND:
1091 			++par;
1092 			break;
1093 		}
1094 	}
1095 
1096 	if (lastlay != 0)
1097 		closeItemTag(xs, *lastlay);
1098 	closeTag(xs, bstyle);
1099 	xs << html::CR();
1100 	return pend;
1101 }
1102 
1103 
makeCommand(Buffer const & buf,XHTMLStream & xs,OutputParams const & runparams,Text const & text,ParagraphList::const_iterator const & pbegin)1104 void makeCommand(Buffer const & buf,
1105 		 XHTMLStream & xs,
1106 		 OutputParams const & runparams,
1107 		 Text const & text,
1108 		 ParagraphList::const_iterator const & pbegin)
1109 {
1110 	Layout const & style = pbegin->layout();
1111 	if (!style.counter.empty())
1112 		buf.masterBuffer()->params().
1113 		    documentClass().counters().step(style.counter, OutputUpdate);
1114 
1115 	bool const make_parid = !runparams.for_toc && runparams.html_make_pars;
1116 
1117 	openParTag(xs, style, pbegin->params(),
1118 	           make_parid ? pbegin->magicLabel() : "");
1119 
1120 	// Label around sectioning number:
1121 	// FIXME Probably need to account for LABEL_MANUAL
1122 	// FIXME Probably also need now to account for labels ABOVE and CENTERED.
1123 	if (style.labeltype != LABEL_NO_LABEL) {
1124 		openLabelTag(xs, style);
1125 		xs << pbegin->params().labelString();
1126 		closeLabelTag(xs, style);
1127 		// Otherwise the label might run together with the text
1128 		xs << from_ascii(" ");
1129 	}
1130 
1131 	ParagraphList::const_iterator const begin = text.paragraphs().begin();
1132 	pbegin->simpleLyXHTMLOnePar(buf, xs, runparams,
1133 			text.outerFont(distance(begin, pbegin)));
1134 	closeTag(xs, style);
1135 	xs << html::CR();
1136 }
1137 
1138 } // end anonymous namespace
1139 
1140 
xhtmlParagraphs(Text const & text,Buffer const & buf,XHTMLStream & xs,OutputParams const & runparams)1141 void xhtmlParagraphs(Text const & text,
1142 		       Buffer const & buf,
1143 		       XHTMLStream & xs,
1144 		       OutputParams const & runparams)
1145 {
1146 	ParagraphList const & paragraphs = text.paragraphs();
1147 	if (runparams.par_begin == runparams.par_end) {
1148 		runparams.par_begin = 0;
1149 		runparams.par_end = paragraphs.size();
1150 	}
1151 	pit_type bpit = runparams.par_begin;
1152 	pit_type const epit = runparams.par_end;
1153 	LASSERT(bpit < epit,
1154 		{ xs << XHTMLStream::ESCAPE_NONE << "<!-- XHTML output error! -->\n"; return; });
1155 
1156 	OutputParams ourparams = runparams;
1157 	ParagraphList::const_iterator const pend =
1158 		(epit == (int) paragraphs.size()) ?
1159 			paragraphs.end() : paragraphs.constIterator(epit);
1160 	while (bpit < epit) {
1161 		ParagraphList::const_iterator par = paragraphs.constIterator(bpit);
1162 		if (par->params().startOfAppendix()) {
1163 			// We want to reset the counter corresponding to toplevel sectioning
1164 			Layout const & lay =
1165 				buf.masterBuffer()->params().documentClass().getTOCLayout();
1166 			docstring const cnt = lay.counter;
1167 			if (!cnt.empty()) {
1168 				Counters & cnts =
1169 					buf.masterBuffer()->params().documentClass().counters();
1170 				cnts.reset(cnt);
1171 			}
1172 		}
1173 		Layout const & style = par->layout();
1174 		ParagraphList::const_iterator const lastpar = par;
1175 		ParagraphList::const_iterator send;
1176 
1177 		switch (style.latextype) {
1178 		case LATEX_COMMAND: {
1179 			// The files with which we are working never have more than
1180 			// one paragraph in a command structure.
1181 			// FIXME
1182 			// if (ourparams.html_in_par)
1183 			//   fix it so we don't get sections inside standard, e.g.
1184 			// note that we may then need to make runparams not const, so we
1185 			// can communicate that back.
1186 			// FIXME Maybe this fix should be in the routines themselves, in case
1187 			// they are called from elsewhere.
1188 			makeCommand(buf, xs, ourparams, text, par);
1189 			++par;
1190 			break;
1191 		}
1192 		case LATEX_ENVIRONMENT:
1193 		case LATEX_LIST_ENVIRONMENT:
1194 		case LATEX_ITEM_ENVIRONMENT: {
1195 			// FIXME Same fix here.
1196 			send = findEndOfEnvironment(par, pend);
1197 			par = makeEnvironment(buf, xs, ourparams, text, par, send);
1198 			break;
1199 		}
1200 		case LATEX_BIB_ENVIRONMENT: {
1201 			// FIXME Same fix here.
1202 			send = findEndOfEnvironment(par, pend);
1203 			par = makeBibliography(buf, xs, ourparams, text, par, send);
1204 			break;
1205 		}
1206 		case LATEX_PARAGRAPH:
1207 			send = findLastParagraph(par, pend);
1208 			par = makeParagraphs(buf, xs, ourparams, text, par, send);
1209 			break;
1210 		}
1211 		bpit += distance(lastpar, par);
1212 	}
1213 }
1214 
1215 
alignmentToCSS(LyXAlignment align)1216 string alignmentToCSS(LyXAlignment align)
1217 {
1218 	switch (align) {
1219 	case LYX_ALIGN_BLOCK:
1220 		// we are NOT going to use text-align: justify!!
1221 	case LYX_ALIGN_LEFT:
1222 		return "left";
1223 	case LYX_ALIGN_RIGHT:
1224 		return "right";
1225 	case LYX_ALIGN_CENTER:
1226 		return "center";
1227 	default:
1228 		break;
1229 	}
1230 	return "";
1231 }
1232 
1233 } // namespace lyx
1234