]> git.lyx.org Git - lyx.git/blob - src/output_xhtml.cpp
InsetTabular.cpp: multirows inherit the width and the alignment from the column;...
[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
36 #include <vector>
37
38 using namespace std;
39 using namespace lyx::support;
40
41 namespace lyx {
42
43 namespace html {
44
45 docstring escapeChar(char_type c)
46 {
47         docstring str;
48         switch (c) {
49         case ' ':
50                 str += " ";
51                 break;
52         case '&':
53                 str += "&amp;";
54                 break;
55         case '<':
56                 str += "&lt;";
57                 break;
58         case '>':
59                 str += "&gt;";
60                 break;
61         default:
62                 str += c;
63                 break;
64         }
65         return str;
66 }
67
68
69 // escape what needs escaping
70 docstring htmlize(docstring const & str) {
71         odocstringstream d;
72         docstring::const_iterator it = str.begin();
73         docstring::const_iterator en = str.end();
74         for (; it != en; ++it)
75                 d << escapeChar(*it);
76         return d.str();
77 }
78
79
80 string escapeChar(char c)
81 {
82         string str;
83         switch (c) {
84         case ' ':
85                 str += " ";
86                 break;
87         case '&':
88                 str += "&amp;";
89                 break;
90         case '<':
91                 str += "&lt;";
92                 break;
93         case '>':
94                 str += "&gt;";
95                 break;
96         default:
97                 str += c;
98                 break;
99         }
100         return str;
101 }
102
103
104 // escape what needs escaping
105 string htmlize(string const & str) {
106         ostringstream d;
107         string::const_iterator it = str.begin();
108         string::const_iterator en = str.end();
109         for (; it != en; ++it)
110                 d << escapeChar(*it);
111         return d.str();
112 }
113
114
115 string cleanAttr(string const & str)
116 {
117         string newname;
118         string::const_iterator it = str.begin();
119         string::const_iterator en = str.end();
120         for (; it != en; ++it)
121                 newname += isalnum(*it) ? *it : '_';
122         return newname; 
123 }
124
125
126 docstring cleanAttr(docstring const & str)
127 {
128         docstring newname;
129         docstring::const_iterator it = str.begin();
130         docstring::const_iterator en = str.end();
131         for (; it != en; ++it)
132                 if (isalnum(*it))
133                         newname += *it;
134                 else
135                         newname += '_';
136         return newname; 
137 }
138
139
140 bool isFontTag(string const & s)
141 {
142         return s == "em" || s == "strong"; // others?
143 }
144
145
146 docstring StartTag::asTag() const
147 {
148         string output = "<" + tag_;
149         if (!attr_.empty())
150                 output += " " + html::htmlize(attr_);
151         output += ">";
152         return from_utf8(output);
153 }
154
155
156 docstring StartTag::asEndTag() const
157 {
158         string output = "</" + tag_ + ">";
159         return from_utf8(output);
160 }
161
162
163 docstring EndTag::asEndTag() const
164 {
165         string output = "</" + tag_ + ">";
166         return from_utf8(output);
167 }
168
169
170 docstring CompTag::asTag() const
171 {
172         string output = "<" + tag_;
173         if (!attr_.empty())
174                 output += " " + html::htmlize(attr_);
175         output += " />";
176         return from_utf8(output);
177 }
178
179 } // namespace html
180
181
182
183 ////////////////////////////////////////////////////////////////
184 ///
185 /// XHTMLStream
186 ///
187 ////////////////////////////////////////////////////////////////
188
189 XHTMLStream::XHTMLStream(odocstream & os) 
190                 : os_(os), nextraw_(false)
191 {}
192
193
194 void XHTMLStream::cr() 
195 {
196         // tabs?
197         os_ << from_ascii("\n");
198 }
199
200
201 void XHTMLStream::writeError(std::string const & s)
202 {
203         LYXERR0(s);
204         os_ << from_utf8("<!-- Output Error: " + s + " -->");
205 }
206
207
208 bool XHTMLStream::closeFontTags()
209 {
210         if (tag_stack_.empty())
211                 return true;
212         // first, we close any open font tags we can close
213         html::StartTag curtag = tag_stack_.back();
214         while (html::isFontTag(curtag.tag_)) {
215                 os_ << curtag.asEndTag();
216                 tag_stack_.pop_back();
217                 if (tag_stack_.empty())
218                         // this probably shouldn't happen, since then the
219                         // font tags weren't in any other tag. but that
220                         // problem will likely be caught elsewhere.
221                         return true;
222                 curtag = tag_stack_.back();
223         }
224         // so we've hit a non-font tag. let's see if any of the
225         // remaining tags are font tags.
226         TagStack::const_iterator it = tag_stack_.begin();
227         TagStack::const_iterator en = tag_stack_.end();
228         bool noFontTags = true;
229         for (; it != en; ++it) {
230                 if (html::isFontTag(it->tag_)) {
231                         writeError("Font tag `" + it->tag_ + "' still open in closeFontTags().\n"
232                                 "This is likely not a problem, but you might want to check.");
233                         noFontTags = false;
234                 }
235         }
236         return noFontTags;
237 }
238
239
240 void XHTMLStream::clearTagDeque()
241 {
242         while (!pending_tags_.empty()) {
243                 html::StartTag const & tag = pending_tags_.front();
244                 // tabs?
245                 os_ << tag.asTag();
246                 tag_stack_.push_back(tag);
247                 pending_tags_.pop_front();
248         }
249 }
250
251
252 XHTMLStream & XHTMLStream::operator<<(docstring const & d)
253 {
254         clearTagDeque();
255         if (nextraw_) {
256                 os_ << d;
257                 nextraw_ = false;
258         } else
259                 os_ << html::htmlize(d);
260         return *this;
261 }
262
263
264 XHTMLStream & XHTMLStream::operator<<(const char * s)
265 {
266         clearTagDeque();
267         docstring const d = from_ascii(s);
268         if (nextraw_) {
269                 os_ << d;
270                 nextraw_ = false;
271         } else
272                 os_ << html::htmlize(d);
273         return *this;
274 }
275
276
277 XHTMLStream & XHTMLStream::operator<<(char_type c)
278 {
279         clearTagDeque();
280         if (nextraw_) {
281                 os_ << c;
282                 nextraw_ = false;
283         } else
284                 os_ << html::escapeChar(c);
285         return *this;
286 }
287
288
289 XHTMLStream & XHTMLStream::operator<<(char c)
290 {
291         clearTagDeque();
292         if (nextraw_) {
293                 os_ << c;
294                 nextraw_ = false;
295         } else {
296                 string const d = html::escapeChar(c);
297                 os_ << from_ascii(d);
298         }
299         return *this;
300 }
301
302
303 XHTMLStream & XHTMLStream::operator<<(int i)
304 {
305         clearTagDeque();
306         os_ << i;
307         nextraw_ = false;
308         return *this;
309 }
310
311
312 XHTMLStream & XHTMLStream::operator<<(NextRaw const &) 
313
314         nextraw_ = true; 
315         return *this;
316 }
317
318
319 XHTMLStream & XHTMLStream::operator<<(html::StartTag const & tag) 
320 {
321         if (tag.tag_.empty())
322                 return *this;
323         pending_tags_.push_back(tag);
324         if (tag.keepempty_)
325                 clearTagDeque();
326         return *this;
327 }
328
329
330 XHTMLStream & XHTMLStream::operator<<(html::CompTag const & tag) 
331 {
332         if (tag.tag_.empty())
333                 return *this;
334         clearTagDeque();
335         // tabs?
336         os_ << tag.asTag();
337         cr();
338         return *this;
339 }
340
341
342 bool XHTMLStream::isTagOpen(string const & stag)
343 {
344         TagStack::const_iterator sit = tag_stack_.begin();
345         TagStack::const_iterator const sen = tag_stack_.end();
346         for (; sit != sen; ++sit)
347                 if (sit->tag_ == stag) 
348                         return true;
349         return false;
350 }
351
352
353 // this is complicated, because we want to make sure that
354 // everything is properly nested. the code ought to make 
355 // sure of that, but we won't assert (yet) if we run into
356 // a problem. we'll just output error messages and try our
357 // best to make things work.
358 XHTMLStream & XHTMLStream::operator<<(html::EndTag const & etag)
359 {
360         if (etag.tag_.empty())
361                 return *this;
362
363         // make sure there are tags to be closed
364         if (tag_stack_.empty()) {
365                 writeError("Tried to close `" + etag.tag_
366                          + "' when no tags were open!");
367                 return *this;           
368         }
369
370         // first make sure we're not closing an empty tag
371         if (!pending_tags_.empty()) {
372                 html::StartTag const & stag = pending_tags_.back();
373                 if (etag.tag_ == stag.tag_)  {
374                         // we have <tag></tag>, so we discard it and remove it 
375                         // from the pending_tags_.
376                         pending_tags_.pop_back();
377                         return *this;
378                 }
379                 // there is a pending tag that isn't the one we are trying
380                 // to close. 
381                 // is this tag itself pending?
382                 // non-const iterators because we may call erase().
383                 TagDeque::iterator dit = pending_tags_.begin();
384                 TagDeque::iterator const den = pending_tags_.end();
385                 for (; dit != den; ++dit) {
386                         if (dit->tag_ == etag.tag_) {
387                                 // it was pending, so we just erase it
388                                 writeError("Tried to close pending tag `" + etag.tag_ 
389                                         + "' when other tags were pending. Last pending tag is `"
390                                         + pending_tags_.back().tag_ + "'. Tag discarded.");
391                                 pending_tags_.erase(dit);
392                                 return *this;
393                         }
394                 }
395                 // so etag isn't itself pending. is it even open?
396                 if (!isTagOpen(etag.tag_)) {
397                         writeError("Tried to close `" + etag.tag_ 
398                                  + "' when tag was not open. Tag discarded.");
399                         return *this;
400                 }
401                 // ok, so etag is open.
402                 // our strategy will be as below: we will do what we need to 
403                 // do to close this tag.
404                 string estr = "Closing tag `" + etag.tag_ 
405                         + "' when other tags are pending. Discarded pending tags:\n";
406                 for (dit = pending_tags_.begin(); dit != den; ++dit)
407                         estr += dit->tag_ + "\n";
408                 writeError(estr);
409                 // clear the pending tags...
410                 pending_tags_.clear();
411                 // ...and then just fall through.
412         }
413
414         // is the tag we are closing the last one we opened?
415         if (etag.tag_ == tag_stack_.back().tag_) {
416                 // output it...
417                 os_ << etag.asEndTag();
418                 // ...and forget about it
419                 tag_stack_.pop_back();
420                 return *this;
421         } 
422         
423         // we are trying to close a tag other than the one last opened. 
424         // let's first see if this particular tag is still open somehow.
425         if (!isTagOpen(etag.tag_)) {
426                 writeError("Tried to close `" + etag.tag_ 
427                         + "' when tag was not open. Tag discarded.");
428                 return *this;
429         }
430         
431         // so the tag was opened, but other tags have been opened since
432         // and not yet closed.
433         // if it's a font tag, though...
434         if (html::isFontTag(etag.tag_)) {
435                 // it won't be a problem if the other tags open since this one
436                 // are also font tags.
437                 TagStack::const_reverse_iterator rit = tag_stack_.rbegin();
438                 TagStack::const_reverse_iterator ren = tag_stack_.rend();
439                 for (; rit != ren; ++rit) {
440                         if (rit->tag_ == etag.tag_)
441                                 break;
442                         if (!html::isFontTag(rit->tag_)) {
443                                 // we'll just leave it and, presumably, have to close it later.
444                                 writeError("Unable to close font tag `" + etag.tag_ 
445                                         + "' due to open non-font tag `" + rit->tag_ + "'.");
446                                 return *this;
447                         }
448                 }
449                 
450                 // so we have e.g.:
451                 //    <em>this is <strong>bold
452                 // and are being asked to closed em. we want:
453                 //    <em>this is <strong>bold</strong></em><strong>
454                 // first, we close the intervening tags...
455                 html::StartTag curtag = tag_stack_.back();
456                 // ...remembering them in a stack.
457                 TagStack fontstack;
458                 while (curtag.tag_ != etag.tag_) {
459                         os_ << curtag.asEndTag();
460                         fontstack.push_back(curtag);
461                         tag_stack_.pop_back();
462                         curtag = tag_stack_.back();
463                 }
464                 // now close our tag...
465                 os_ << etag.asEndTag();
466                 tag_stack_.pop_back();
467
468                 // ...and restore the other tags.
469                 rit = fontstack.rbegin();
470                 ren = fontstack.rend();
471                 for (; rit != ren; ++rit)
472                         pending_tags_.push_back(*rit);
473                 return *this;
474         }
475         
476         // it wasn't a font tag.
477         // so other tags were opened before this one and not properly closed. 
478         // so we'll close them, too. that may cause other issues later, but it 
479         // at least guarantees proper nesting.
480         writeError("Closing tag `" + etag.tag_ 
481                 + "' when other tags are open, namely:");
482         html::StartTag curtag = tag_stack_.back();
483         while (curtag.tag_ != etag.tag_) {
484                 writeError(curtag.tag_);
485                 os_ << curtag.asEndTag();
486                 tag_stack_.pop_back();
487                 curtag = tag_stack_.back();
488         }
489         // curtag is now the one we actually want.
490         os_ << curtag.asEndTag();
491         tag_stack_.pop_back();
492         
493         return *this;
494 }
495
496 // End code for XHTMLStream
497
498 namespace {
499         
500 // convenience functions
501
502 inline void openTag(XHTMLStream & xs, Layout const & lay)
503 {
504         xs << html::StartTag(lay.htmltag(), lay.htmlattr());
505 }
506
507
508 void openTag(XHTMLStream & xs, Layout const & lay, 
509              ParagraphParameters const & params)
510 {
511         // FIXME Are there other things we should handle here?
512         string const align = alignmentToCSS(params.align());
513         if (align.empty()) {
514                 openTag(xs, lay);
515                 return;
516         }
517         string attrs = lay.htmlattr() + " style='text-align: " + align + ";'";
518         xs << html::StartTag(lay.htmltag(), attrs);
519 }
520
521
522 inline void closeTag(XHTMLStream & xs, Layout const & lay)
523 {
524         xs << html::EndTag(lay.htmltag());
525 }
526
527
528 inline void openLabelTag(XHTMLStream & xs, Layout const & lay)
529 {
530         xs << html::StartTag(lay.htmllabeltag(), lay.htmllabelattr());
531 }
532
533
534 inline void closeLabelTag(XHTMLStream & xs, Layout const & lay)
535 {
536         xs << html::EndTag(lay.htmllabeltag());
537 }
538
539
540 inline void openItemTag(XHTMLStream & xs, Layout const & lay)
541 {
542         xs << html::StartTag(lay.htmlitemtag(), lay.htmlitemattr(), true);
543 }
544
545
546 void openItemTag(XHTMLStream & xs, Layout const & lay, 
547              ParagraphParameters const & params)
548 {
549         // FIXME Are there other things we should handle here?
550         string const align = alignmentToCSS(params.align());
551         if (align.empty()) {
552                 openItemTag(xs, lay);
553                 return;
554         }
555         string attrs = lay.htmlattr() + " style='text-align: " + align + ";'";
556         xs << html::StartTag(lay.htmlitemtag(), attrs);
557 }
558
559
560 inline void closeItemTag(XHTMLStream & xs, Layout const & lay)
561 {
562         xs << html::EndTag(lay.htmlitemtag());
563 }
564
565 // end of convenience functions
566
567 ParagraphList::const_iterator searchParagraphHtml(
568         ParagraphList::const_iterator p,
569         ParagraphList::const_iterator const & pend)
570 {
571         for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
572                 ;
573
574         return p;
575 }
576
577
578 ParagraphList::const_iterator searchEnvironmentHtml(
579                 ParagraphList::const_iterator const pstart,
580                 ParagraphList::const_iterator const & pend)
581 {
582         ParagraphList::const_iterator p = pstart;
583         Layout const & bstyle = p->layout();
584         size_t const depth = p->params().depth();
585         for (++p; p != pend; ++p) {
586                 Layout const & style = p->layout();
587                 // It shouldn't happen that e.g. a section command occurs inside
588                 // a quotation environment, at a higher depth, but as of 6/2009,
589                 // it can happen. We pretend that it's just at lowest depth.
590                 if (style.latextype == LATEX_COMMAND)
591                         return p;
592                 // If depth is down, we're done
593                 if (p->params().depth() < depth)
594                         return p;
595                 // If depth is up, we're not done
596                 if (p->params().depth() > depth)
597                         continue;
598                 // Now we know we are at the same depth
599                 if (style.latextype == LATEX_PARAGRAPH
600                     || style.latexname() != bstyle.latexname())
601                         return p;
602         }
603         return pend;
604 }
605
606
607 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
608                                             XHTMLStream & xs,
609                                             OutputParams const & runparams,
610                                             Text const & text,
611                                             ParagraphList::const_iterator const & pbegin,
612                                             ParagraphList::const_iterator const & pend)
613 {
614         ParagraphList::const_iterator const begin = text.paragraphs().begin();
615         ParagraphList::const_iterator par = pbegin;
616         for (; par != pend; ++par) {
617                 Layout const & lay = par->layout();
618                 if (!lay.counter.empty())
619                         buf.params().documentClass().counters().step(lay.counter, OutputUpdate);
620                 // FIXME We should see if there's a label to be output and
621                 // do something with it.
622                 if (par != pbegin)
623                         xs.cr();
624
625                 // If we are already in a paragraph, and this is the first one, then we
626                 // do not want to open the paragraph tag.
627                 // we also do not want to open it if the current layout does not permit
628                 // multiple paragraphs.
629                 bool const opened = runparams.html_make_pars &&
630                         (par != pbegin || !runparams.html_in_par);
631                 if (opened)
632                         openTag(xs, lay, par->params());
633                 docstring const deferred = 
634                         par->simpleLyXHTMLOnePar(buf, xs, runparams, text.outerFont(distance(begin, par)));
635
636                 // We want to issue the closing tag if either:
637                 //   (i)  We opened it, and either html_in_par is false,
638                 //        or we're not in the last paragraph, anyway.
639                 //   (ii) We didn't open it and html_in_par is true, 
640                 //        but we are in the first par, and there is a next par.
641                 ParagraphList::const_iterator nextpar = par;
642                 nextpar++;
643                 bool const needclose = 
644                         (opened && (!runparams.html_in_par || nextpar != pend))
645                         || (!opened && runparams.html_in_par && par == pbegin && nextpar != pend);
646                 if (needclose) {
647                         closeTag(xs, lay);
648                         xs.cr();
649                 }
650                 if (!deferred.empty()) {
651                         xs << XHTMLStream::NextRaw() << deferred;
652                         xs.cr();
653                 }
654         }
655         return pend;
656 }
657
658
659 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
660                                 XHTMLStream & xs,
661                                 OutputParams const & runparams,
662                                 Text const & text,
663                                 ParagraphList::const_iterator const & pbegin,
664                                 ParagraphList::const_iterator const & pend) 
665 {
666         // FIXME XHTML
667         // Use TextClass::htmlTOCLayout() to figure out how we should look.
668         xs << html::StartTag("h2", "class='bibliography'");
669         xs << pbegin->layout().labelstring(false);
670         xs << html::EndTag("h2");
671         xs.cr();
672         xs << html::StartTag("div", "class='bibliography'");
673         xs.cr();
674         makeParagraphs(buf, xs, runparams, text, pbegin, pend);
675         xs << html::EndTag("div");
676         return pend;
677 }
678
679
680 bool isNormalEnv(Layout const & lay)
681 {
682         return lay.latextype == LATEX_ENVIRONMENT
683             || lay.latextype == LATEX_BIB_ENVIRONMENT;
684 }
685
686         
687 ParagraphList::const_iterator makeEnvironmentHtml(Buffer const & buf,
688                                               XHTMLStream & xs,
689                                               OutputParams const & runparams,
690                                               Text const & text,
691                                               ParagraphList::const_iterator const & pbegin,
692                                               ParagraphList::const_iterator const & pend) 
693 {
694         ParagraphList::const_iterator const begin = text.paragraphs().begin();
695         ParagraphList::const_iterator par = pbegin;
696         Layout const & bstyle = par->layout();
697         depth_type const origdepth = pbegin->params().depth();
698
699         // open tag for this environment
700         openTag(xs, bstyle);
701         xs.cr();
702
703         // we will on occasion need to remember a layout from before.
704         Layout const * lastlay = 0;
705
706         while (par != pend) {
707                 Layout const & style = par->layout();
708                 // the counter only gets stepped if we're in some kind of list,
709                 // or if it's the first time through.
710                 // note that enum, etc, are handled automatically.
711                 // FIXME There may be a bug here about user defined enumeration
712                 // types. If so, then we'll need to take the counter and add "i",
713                 // "ii", etc, as with enum.
714                 Counters & cnts = buf.params().documentClass().counters();
715                 docstring const & cntr = style.counter;
716                 if (!style.counter.empty() 
717                     && (par == pbegin || !isNormalEnv(style)) 
718                                 && cnts.hasCounter(cntr)
719                 )
720                         cnts.step(cntr, OutputUpdate);
721                 ParagraphList::const_iterator send;
722                 // this will be positive, if we want to skip the initial word
723                 // (if it's been taken for the label).
724                 pos_type sep = 0;
725
726                 switch (style.latextype) {
727                 case LATEX_ENVIRONMENT:
728                 case LATEX_LIST_ENVIRONMENT:
729                 case LATEX_ITEM_ENVIRONMENT: {
730                         // There are two possiblities in this case. 
731                         // One is that we are still in the environment in which we 
732                         // started---which we will be if the depth is the same.
733                         if (par->params().depth() == origdepth) {
734                                 LASSERT(bstyle == style, /* */);
735                                 if (lastlay != 0) {
736                                         closeItemTag(xs, *lastlay);
737                                         lastlay = 0;
738                                 }
739                                 
740                                 bool const labelfirst = style.htmllabelfirst();
741                                 if (!labelfirst)
742                                         openItemTag(xs, style, par->params());
743                                 
744                                 // label output
745                                 if (style.labeltype != LABEL_NO_LABEL && 
746                                     style.htmllabeltag() != "NONE") {
747                                         if (isNormalEnv(style)) {
748                                                 // in this case, we print the label only for the first 
749                                                 // paragraph (as in a theorem).
750                                                 if (par == pbegin) {
751                                                         docstring const lbl = 
752                                                                         pbegin->params().labelString();
753                                                         if (!lbl.empty()) {
754                                                                 openLabelTag(xs, style);
755                                                                 xs << lbl;
756                                                                 closeLabelTag(xs, style);
757                                                         }
758                                                         xs.cr();
759                                                 }
760                                         }       else { // some kind of list
761                                                 if (style.labeltype == LABEL_MANUAL) {
762                                                         openLabelTag(xs, style);
763                                                         sep = par->firstWordLyXHTML(xs, runparams);
764                                                         closeLabelTag(xs, style);
765                                                         xs.cr();
766                                                 }
767                                                 else {
768                                                         openLabelTag(xs, style);
769                                                         xs << par->params().labelString();
770                                                         closeLabelTag(xs, style);
771                                                         xs.cr();
772                                                 }
773                                         }
774                                 } // end label output
775
776                                 if (labelfirst)
777                                         openItemTag(xs, style, par->params());
778
779                                 par->simpleLyXHTMLOnePar(buf, xs, runparams, 
780                                         text.outerFont(distance(begin, par)), sep);
781                                 ++par;
782
783                                 // We may not want to close the tag yet, in particular:
784                                 // If we're not at the end...
785                                 if (par != pend 
786                                         //  and are doing items...
787                                          && !isNormalEnv(style)
788                                          // and if the depth has changed...
789                                          && par->params().depth() != origdepth) {
790                                          // then we'll save this layout for later, and close it when
791                                          // we get another item.
792                                         lastlay = &style;
793                                 } else
794                                         closeItemTag(xs, style);
795                                 xs.cr();
796                         }
797                         // The other possibility is that the depth has increased, in which
798                         // case we need to recurse.
799                         else {
800                                 send = searchEnvironmentHtml(par, pend);
801                                 par = makeEnvironmentHtml(buf, xs, runparams, text, par, send);
802                         }
803                         break;
804                 }
805                 case LATEX_PARAGRAPH:
806                         send = searchParagraphHtml(par, pend);
807                         par = makeParagraphs(buf, xs, runparams, text, par, send);
808                         break;
809                 // Shouldn't happen
810                 case LATEX_BIB_ENVIRONMENT:
811                         send = par;
812                         ++send;
813                         par = makeParagraphs(buf, xs, runparams, text, par, send);
814                         break;
815                 // Shouldn't happen
816                 case LATEX_COMMAND:
817                         ++par;
818                         break;
819                 }
820         }
821
822         if (lastlay != 0)
823                 closeItemTag(xs, *lastlay);
824         closeTag(xs, bstyle);
825         xs.cr();
826         return pend;
827 }
828
829
830 void makeCommand(Buffer const & buf,
831                                           XHTMLStream & xs,
832                                           OutputParams const & runparams,
833                                           Text const & text,
834                                           ParagraphList::const_iterator const & pbegin)
835 {
836         Layout const & style = pbegin->layout();
837         if (!style.counter.empty())
838                 buf.params().documentClass().counters().step(style.counter, OutputUpdate);
839
840         openTag(xs, style, pbegin->params());
841
842         // Label around sectioning number:
843         // FIXME Probably need to account for LABEL_MANUAL
844         if (style.labeltype != LABEL_NO_LABEL) {
845                 openLabelTag(xs, style);
846                 xs << pbegin->params().labelString();
847                 closeLabelTag(xs, style);
848                 // Otherwise the label might run together with the text
849                 xs << from_ascii(" ");
850         }
851
852         ParagraphList::const_iterator const begin = text.paragraphs().begin();
853         pbegin->simpleLyXHTMLOnePar(buf, xs, runparams,
854                         text.outerFont(distance(begin, pbegin)));
855         closeTag(xs, style);
856         xs.cr();
857 }
858
859 } // end anonymous namespace
860
861
862 void xhtmlParagraphs(Text const & text,
863                        Buffer const & buf,
864                        XHTMLStream & xs,
865                        OutputParams const & runparams)
866 {
867         ParagraphList const & paragraphs = text.paragraphs();
868         ParagraphList::const_iterator par = paragraphs.begin();
869         ParagraphList::const_iterator pend = paragraphs.end();
870
871         OutputParams ourparams = runparams;
872         while (par != pend) {
873                 if (par->params().startOfAppendix()) {
874                         // FIXME: only the counter corresponding to toplevel
875                         // sectioning should be reset
876                         Counters & cnts = buf.masterBuffer()->params().documentClass().counters();
877                         cnts.reset();
878                         cnts.appendix(true);
879                 }
880                 Layout const & style = par->layout();
881                 ParagraphList::const_iterator lastpar = par;
882                 ParagraphList::const_iterator send;
883
884                 switch (style.latextype) {
885                 case LATEX_COMMAND: {
886                         // The files with which we are working never have more than
887                         // one paragraph in a command structure.
888                         // FIXME 
889                         // if (ourparams.html_in_par)
890                         //   fix it so we don't get sections inside standard, e.g.
891                         // note that we may then need to make runparams not const, so we
892                         // can communicate that back.
893                         // FIXME Maybe this fix should be in the routines themselves, in case
894                         // they are called from elsewhere.
895                         makeCommand(buf, xs, ourparams, text, par);
896                         ++par;
897                         break;
898                 }
899                 case LATEX_ENVIRONMENT:
900                 case LATEX_LIST_ENVIRONMENT:
901                 case LATEX_ITEM_ENVIRONMENT: {
902                         // FIXME Same fix here.
903                         send = searchEnvironmentHtml(par, pend);
904                         par = makeEnvironmentHtml(buf, xs, ourparams, text, par, send);
905                         break;
906                 }
907                 case LATEX_BIB_ENVIRONMENT: {
908                         // FIXME Same fix here.
909                         send = searchEnvironmentHtml(par, pend);
910                         par = makeBibliography(buf, xs, ourparams, text, par, send);
911                         break;
912                 }
913                 case LATEX_PARAGRAPH:
914                         send = searchParagraphHtml(par, pend);
915                         par = makeParagraphs(buf, xs, ourparams, text, par, send);
916                         break;
917                 }
918                 // FIXME??
919                 // makeEnvironment may process more than one paragraphs and bypass pend
920                 if (distance(lastpar, par) >= distance(lastpar, pend))
921                         break;
922         }
923 }
924
925
926 string alignmentToCSS(LyXAlignment align) {
927         switch (align) {
928         case LYX_ALIGN_BLOCK:
929                 // we are NOT going to use text-align: justify!!
930         case LYX_ALIGN_LEFT:
931                 return "left";
932         case LYX_ALIGN_RIGHT:
933                 return "right";
934         case LYX_ALIGN_CENTER:
935                 return "center";
936         default:
937                 break;
938         }
939         return "";
940 }
941
942 } // namespace lyx