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