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