]> git.lyx.org Git - lyx.git/blob - src/output_xhtml.cpp
typo
[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                 os_ << html::escapeChar(c);
297         return *this;
298 }
299
300
301 XHTMLStream & XHTMLStream::operator<<(int i)
302 {
303         clearTagDeque();
304         os_ << i;
305         nextraw_ = false;
306         return *this;
307 }
308
309
310 XHTMLStream & XHTMLStream::operator<<(NextRaw const &) 
311
312         nextraw_ = true; 
313         return *this;
314 }
315
316
317 XHTMLStream & XHTMLStream::operator<<(html::StartTag const & tag) 
318 {
319         if (tag.tag_.empty())
320                 return *this;
321         pending_tags_.push_back(tag);
322         if (tag.keepempty_)
323                 clearTagDeque();
324         return *this;
325 }
326
327
328 XHTMLStream & XHTMLStream::operator<<(html::CompTag const & tag) 
329 {
330         if (tag.tag_.empty())
331                 return *this;
332         clearTagDeque();
333         // tabs?
334         os_ << tag.asTag();
335         cr();
336         return *this;
337 }
338
339
340 bool XHTMLStream::isTagOpen(string const & stag)
341 {
342         TagStack::const_iterator sit = tag_stack_.begin();
343         TagStack::const_iterator const sen = tag_stack_.end();
344         for (; sit != sen; ++sit)
345                 if (sit->tag_ == stag) 
346                         return true;
347         return false;
348 }
349
350
351 // this is complicated, because we want to make sure that
352 // everything is properly nested. the code ought to make 
353 // sure of that, but we won't assert (yet) if we run into
354 // a problem. we'll just output error messages and try our
355 // best to make things work.
356 XHTMLStream & XHTMLStream::operator<<(html::EndTag const & etag)
357 {
358         if (etag.tag_.empty())
359                 return *this;
360
361         // make sure there are tags to be closed
362         if (tag_stack_.empty()) {
363                 writeError("Tried to close `" + etag.tag_
364                          + "' when no tags were open!");
365                 return *this;           
366         }
367
368         // first make sure we're not closing an empty tag
369         if (!pending_tags_.empty()) {
370                 html::StartTag const & stag = pending_tags_.back();
371                 if (etag.tag_ == stag.tag_)  {
372                         // we have <tag></tag>, so we discard it and remove it 
373                         // from the pending_tags_.
374                         pending_tags_.pop_back();
375                         return *this;
376                 }
377                 // there is a pending tag that isn't the one we are trying
378                 // to close. 
379                 // is this tag itself pending?
380                 // non-const iterators because we may call erase().
381                 TagDeque::iterator dit = pending_tags_.begin();
382                 TagDeque::iterator const den = pending_tags_.end();
383                 for (; dit != den; ++dit) {
384                         if (dit->tag_ == etag.tag_) {
385                                 // it was pending, so we just erase it
386                                 writeError("Tried to close pending tag `" + etag.tag_ 
387                                         + "' when other tags were pending. Last pending tag is `"
388                                         + pending_tags_.back().tag_ + "'. Tag discarded.");
389                                 pending_tags_.erase(dit);
390                                 return *this;
391                         }
392                 }
393                 // so etag isn't itself pending. is it even open?
394                 if (!isTagOpen(etag.tag_)) {
395                         writeError("Tried to close `" + etag.tag_ 
396                                  + "' when tag was not open. Tag discarded.");
397                         return *this;
398                 }
399                 // ok, so etag is open.
400                 // our strategy will be as below: we will do what we need to 
401                 // do to close this tag.
402                 string estr = "Closing tag `" + etag.tag_ 
403                         + "' when other tags are pending. Discarded pending tags:\n";
404                 for (dit = pending_tags_.begin(); dit != den; ++dit)
405                         estr += dit->tag_ + "\n";
406                 writeError(estr);
407                 // clear the pending tags...
408                 pending_tags_.clear();
409                 // ...and then just fall through.
410         }
411
412         // is the tag we are closing the last one we opened?
413         if (etag.tag_ == tag_stack_.back().tag_) {
414                 // output it...
415                 os_ << etag.asEndTag();
416                 // ...and forget about it
417                 tag_stack_.pop_back();
418                 return *this;
419         } 
420         
421         // we are trying to close a tag other than the one last opened. 
422         // let's first see if this particular tag is still open somehow.
423         if (!isTagOpen(etag.tag_)) {
424                 writeError("Tried to close `" + etag.tag_ 
425                         + "' when tag was not open. Tag discarded.");
426                 return *this;
427         }
428         
429         // so the tag was opened, but other tags have been opened since
430         // and not yet closed.
431         // if it's a font tag, though...
432         if (html::isFontTag(etag.tag_)) {
433                 // it won't be a problem if the other tags open since this one
434                 // are also font tags.
435                 TagStack::const_reverse_iterator rit = tag_stack_.rbegin();
436                 TagStack::const_reverse_iterator ren = tag_stack_.rend();
437                 for (; rit != ren; ++rit) {
438                         if (rit->tag_ == etag.tag_)
439                                 break;
440                         if (!html::isFontTag(rit->tag_)) {
441                                 // we'll just leave it and, presumably, have to close it later.
442                                 writeError("Unable to close font tag `" + etag.tag_ 
443                                         + "' due to open non-font tag `" + rit->tag_ + "'.");
444                                 return *this;
445                         }
446                 }
447                 
448                 // so we have e.g.:
449                 //    <em>this is <strong>bold
450                 // and are being asked to closed em. we want:
451                 //    <em>this is <strong>bold</strong></em><strong>
452                 // first, we close the intervening tags...
453                 html::StartTag curtag = tag_stack_.back();
454                 // ...remembering them in a stack.
455                 TagStack fontstack;
456                 while (curtag.tag_ != etag.tag_) {
457                         os_ << curtag.asEndTag();
458                         fontstack.push_back(curtag);
459                         tag_stack_.pop_back();
460                         curtag = tag_stack_.back();
461                 }
462                 // now close our tag...
463                 os_ << etag.asEndTag();
464                 tag_stack_.pop_back();
465
466                 // ...and restore the other tags.
467                 rit = fontstack.rbegin();
468                 ren = fontstack.rend();
469                 for (; rit != ren; ++rit)
470                         pending_tags_.push_back(*rit);
471                 return *this;
472         }
473         
474         // it wasn't a font tag.
475         // so other tags were opened before this one and not properly closed. 
476         // so we'll close them, too. that may cause other issues later, but it 
477         // at least guarantees proper nesting.
478         writeError("Closing tag `" + etag.tag_ 
479                 + "' when other tags are open, namely:");
480         html::StartTag curtag = tag_stack_.back();
481         while (curtag.tag_ != etag.tag_) {
482                 writeError(curtag.tag_);
483                 os_ << curtag.asEndTag();
484                 tag_stack_.pop_back();
485                 curtag = tag_stack_.back();
486         }
487         // curtag is now the one we actually want.
488         os_ << curtag.asEndTag();
489         tag_stack_.pop_back();
490         
491         return *this;
492 }
493
494 // End code for XHTMLStream
495
496 namespace {
497         
498 // convenience functions
499
500 inline void openTag(XHTMLStream & xs, Layout const & lay)
501 {
502         xs << html::StartTag(lay.htmltag(), lay.htmlattr());
503 }
504
505
506 void openTag(XHTMLStream & xs, Layout const & lay, 
507              ParagraphParameters const & params)
508 {
509         // FIXME Are there other things we should handle here?
510         string const align = alignmentToCSS(params.align());
511         if (align.empty()) {
512                 openTag(xs, lay);
513                 return;
514         }
515         string attrs = lay.htmlattr() + " style='text-align: " + align + ";'";
516         xs << html::StartTag(lay.htmltag(), attrs);
517 }
518
519
520 inline void closeTag(XHTMLStream & xs, Layout const & lay)
521 {
522         xs << html::EndTag(lay.htmltag());
523 }
524
525
526 inline void openLabelTag(XHTMLStream & xs, Layout const & lay)
527 {
528         xs << html::StartTag(lay.htmllabeltag(), lay.htmllabelattr());
529 }
530
531
532 inline void closeLabelTag(XHTMLStream & xs, Layout const & lay)
533 {
534         xs << html::EndTag(lay.htmllabeltag());
535 }
536
537
538 inline void openItemTag(XHTMLStream & xs, Layout const & lay)
539 {
540         xs << html::StartTag(lay.htmlitemtag(), lay.htmlitemattr(), true);
541 }
542
543
544 void openItemTag(XHTMLStream & xs, Layout const & lay, 
545              ParagraphParameters const & params)
546 {
547         // FIXME Are there other things we should handle here?
548         string const align = alignmentToCSS(params.align());
549         if (align.empty()) {
550                 openItemTag(xs, lay);
551                 return;
552         }
553         string attrs = lay.htmlattr() + " style='text-align: " + align + ";'";
554         xs << html::StartTag(lay.htmlitemtag(), attrs);
555 }
556
557
558 inline void closeItemTag(XHTMLStream & xs, Layout const & lay)
559 {
560         xs << html::EndTag(lay.htmlitemtag());
561 }
562
563 // end of convenience functions
564
565 ParagraphList::const_iterator searchParagraphHtml(
566         ParagraphList::const_iterator p,
567         ParagraphList::const_iterator const & pend)
568 {
569         for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
570                 ;
571
572         return p;
573 }
574
575
576 ParagraphList::const_iterator searchEnvironmentHtml(
577                 ParagraphList::const_iterator const pstart,
578                 ParagraphList::const_iterator const & pend)
579 {
580         ParagraphList::const_iterator p = pstart;
581         Layout const & bstyle = p->layout();
582         size_t const depth = p->params().depth();
583         for (++p; p != pend; ++p) {
584                 Layout const & style = p->layout();
585                 // It shouldn't happen that e.g. a section command occurs inside
586                 // a quotation environment, at a higher depth, but as of 6/2009,
587                 // it can happen. We pretend that it's just at lowest depth.
588                 if (style.latextype == LATEX_COMMAND)
589                         return p;
590                 // If depth is down, we're done
591                 if (p->params().depth() < depth)
592                         return p;
593                 // If depth is up, we're not done
594                 if (p->params().depth() > depth)
595                         continue;
596                 // Now we know we are at the same depth
597                 if (style.latextype == LATEX_PARAGRAPH
598                     || style.latexname() != bstyle.latexname())
599                         return p;
600         }
601         return pend;
602 }
603
604
605 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
606                                             XHTMLStream & xs,
607                                             OutputParams const & runparams,
608                                             Text const & text,
609                                             ParagraphList::const_iterator const & pbegin,
610                                             ParagraphList::const_iterator const & pend)
611 {
612         ParagraphList::const_iterator const begin = text.paragraphs().begin();
613         ParagraphList::const_iterator par = pbegin;
614         for (; par != pend; ++par) {
615                 Layout const & lay = par->layout();
616                 if (!lay.counter.empty())
617                         buf.params().documentClass().counters().step(lay.counter, OutputUpdate);
618                 // FIXME We should see if there's a label to be output and
619                 // do something with it.
620                 if (par != pbegin)
621                         xs.cr();
622
623                 // If we are already in a paragraph, and this is the first one, then we
624                 // do not want to open the paragraph tag.
625                 // we also do not want to open it if the current layout does not permit
626                 // multiple paragraphs.
627                 bool const opened = runparams.html_make_pars &&
628                         (par != pbegin || !runparams.html_in_par);
629                 if (opened)
630                         openTag(xs, lay, par->params());
631                 docstring const deferred = 
632                         par->simpleLyXHTMLOnePar(buf, xs, runparams, text.outerFont(distance(begin, par)));
633
634                 // We want to issue the closing tag if either:
635                 //   (i)  We opened it, and either html_in_par is false,
636                 //        or we're not in the last paragraph, anyway.
637                 //   (ii) We didn't open it and html_in_par is true, 
638                 //        but we are in the first par, and there is a next par.
639                 ParagraphList::const_iterator nextpar = par;
640                 nextpar++;
641                 bool const needclose = 
642                         (opened && (!runparams.html_in_par || nextpar != pend))
643                         || (!opened && runparams.html_in_par && par == pbegin && nextpar != pend);
644                 if (needclose) {
645                         closeTag(xs, lay);
646                         xs.cr();
647                 }
648                 if (!deferred.empty()) {
649                         xs << XHTMLStream::NextRaw() << deferred;
650                         xs.cr();
651                 }
652         }
653         return pend;
654 }
655
656
657 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
658                                 XHTMLStream & xs,
659                                 OutputParams const & runparams,
660                                 Text const & text,
661                                 ParagraphList::const_iterator const & pbegin,
662                                 ParagraphList::const_iterator const & pend) 
663 {
664         // FIXME XHTML
665         // Use TextClass::htmlTOCLayout() to figure out how we should look.
666         xs << html::StartTag("h2", "class='bibliography'");
667         xs << pbegin->layout().labelstring(false);
668         xs << html::EndTag("h2");
669         xs.cr();
670         xs << html::StartTag("div", "class='bibliography'");
671         xs.cr();
672         makeParagraphs(buf, xs, runparams, text, pbegin, pend);
673         xs << html::EndTag("div");
674         return pend;
675 }
676
677
678 bool isNormalEnv(Layout const & lay)
679 {
680         return lay.latextype == LATEX_ENVIRONMENT
681             || lay.latextype == LATEX_BIB_ENVIRONMENT;
682 }
683
684         
685 ParagraphList::const_iterator makeEnvironmentHtml(Buffer const & buf,
686                                               XHTMLStream & xs,
687                                               OutputParams const & runparams,
688                                               Text const & text,
689                                               ParagraphList::const_iterator const & pbegin,
690                                               ParagraphList::const_iterator const & pend) 
691 {
692         ParagraphList::const_iterator const begin = text.paragraphs().begin();
693         ParagraphList::const_iterator par = pbegin;
694         Layout const & bstyle = par->layout();
695         depth_type const origdepth = pbegin->params().depth();
696
697         // open tag for this environment
698         openTag(xs, bstyle);
699         xs.cr();
700
701         // we will on occasion need to remember a layout from before.
702         Layout const * lastlay = 0;
703
704         while (par != pend) {
705                 Layout const & style = par->layout();
706                 // the counter only gets stepped if we're in some kind of list,
707                 // or if it's the first time through.
708                 // note that enum, etc, are handled automatically.
709                 // FIXME There may be a bug here about user defined enumeration
710                 // types. If so, then we'll need to take the counter and add "i",
711                 // "ii", etc, as with enum.
712                 Counters & cnts = buf.params().documentClass().counters();
713                 docstring const & cntr = style.counter;
714                 if (!style.counter.empty() 
715                     && (par == pbegin || !isNormalEnv(style)) 
716                                 && cnts.hasCounter(cntr)
717                 )
718                         cnts.step(cntr, OutputUpdate);
719                 ParagraphList::const_iterator send;
720                 // this will be positive, if we want to skip the initial word
721                 // (if it's been taken for the label).
722                 pos_type sep = 0;
723
724                 switch (style.latextype) {
725                 case LATEX_ENVIRONMENT:
726                 case LATEX_LIST_ENVIRONMENT:
727                 case LATEX_ITEM_ENVIRONMENT: {
728                         // There are two possiblities in this case. 
729                         // One is that we are still in the environment in which we 
730                         // started---which we will be if the depth is the same.
731                         if (par->params().depth() == origdepth) {
732                                 LASSERT(bstyle == style, /* */);
733                                 if (lastlay != 0) {
734                                         closeItemTag(xs, *lastlay);
735                                         lastlay = 0;
736                                 }
737                                 
738                                 bool const labelfirst = style.htmllabelfirst();
739                                 if (!labelfirst)
740                                         openItemTag(xs, style, par->params());
741                                 
742                                 // label output
743                                 if (style.labeltype != LABEL_NO_LABEL && 
744                                     style.htmllabeltag() != "NONE") {
745                                         if (isNormalEnv(style)) {
746                                                 // in this case, we print the label only for the first 
747                                                 // paragraph (as in a theorem).
748                                                 if (par == pbegin) {
749                                                         docstring const lbl = 
750                                                                         pbegin->params().labelString();
751                                                         if (!lbl.empty()) {
752                                                                 openLabelTag(xs, style);
753                                                                 xs << lbl;
754                                                                 closeLabelTag(xs, style);
755                                                         }
756                                                         xs.cr();
757                                                 }
758                                         }       else { // some kind of list
759                                                 if (style.labeltype == LABEL_MANUAL) {
760                                                         openLabelTag(xs, style);
761                                                         sep = par->firstWordLyXHTML(xs, runparams);
762                                                         closeLabelTag(xs, style);
763                                                         xs.cr();
764                                                 }
765                                                 else {
766                                                         openLabelTag(xs, style);
767                                                         xs << par->params().labelString();
768                                                         closeLabelTag(xs, style);
769                                                         xs.cr();
770                                                 }
771                                         }
772                                 } // end label output
773
774                                 if (labelfirst)
775                                         openItemTag(xs, style, par->params());
776
777                                 par->simpleLyXHTMLOnePar(buf, xs, runparams, 
778                                         text.outerFont(distance(begin, par)), sep);
779                                 ++par;
780
781                                 // We may not want to close the tag yet, in particular:
782                                 // If we're not at the end...
783                                 if (par != pend 
784                                         //  and are doing items...
785                                          && !isNormalEnv(style)
786                                          // and if the depth has changed...
787                                          && par->params().depth() != origdepth) {
788                                          // then we'll save this layout for later, and close it when
789                                          // we get another item.
790                                         lastlay = &style;
791                                 } else
792                                         closeItemTag(xs, style);
793                                 xs.cr();
794                         }
795                         // The other possibility is that the depth has increased, in which
796                         // case we need to recurse.
797                         else {
798                                 send = searchEnvironmentHtml(par, pend);
799                                 par = makeEnvironmentHtml(buf, xs, runparams, text, par, send);
800                         }
801                         break;
802                 }
803                 case LATEX_PARAGRAPH:
804                         send = searchParagraphHtml(par, pend);
805                         par = makeParagraphs(buf, xs, runparams, text, par, send);
806                         break;
807                 // Shouldn't happen
808                 case LATEX_BIB_ENVIRONMENT:
809                         send = par;
810                         ++send;
811                         par = makeParagraphs(buf, xs, runparams, text, par, send);
812                         break;
813                 // Shouldn't happen
814                 case LATEX_COMMAND:
815                         ++par;
816                         break;
817                 }
818         }
819
820         if (lastlay != 0)
821                 closeItemTag(xs, *lastlay);
822         closeTag(xs, bstyle);
823         xs.cr();
824         return pend;
825 }
826
827
828 void makeCommand(Buffer const & buf,
829                                           XHTMLStream & xs,
830                                           OutputParams const & runparams,
831                                           Text const & text,
832                                           ParagraphList::const_iterator const & pbegin)
833 {
834         Layout const & style = pbegin->layout();
835         if (!style.counter.empty())
836                 buf.params().documentClass().counters().step(style.counter, OutputUpdate);
837
838         openTag(xs, style, pbegin->params());
839
840         // Label around sectioning number:
841         // FIXME Probably need to account for LABEL_MANUAL
842         if (style.labeltype != LABEL_NO_LABEL) {
843                 openLabelTag(xs, style);
844                 xs << pbegin->params().labelString();
845                 closeLabelTag(xs, style);
846                 // Otherwise the label might run together with the text
847                 xs << from_ascii(" ");
848         }
849
850         ParagraphList::const_iterator const begin = text.paragraphs().begin();
851         pbegin->simpleLyXHTMLOnePar(buf, xs, runparams,
852                         text.outerFont(distance(begin, pbegin)));
853         closeTag(xs, style);
854         xs.cr();
855 }
856
857 } // end anonymous namespace
858
859
860 void xhtmlParagraphs(Text const & text,
861                        Buffer const & buf,
862                        XHTMLStream & xs,
863                        OutputParams const & runparams)
864 {
865         ParagraphList const & paragraphs = text.paragraphs();
866         ParagraphList::const_iterator par = paragraphs.begin();
867         ParagraphList::const_iterator pend = paragraphs.end();
868
869         OutputParams ourparams = runparams;
870         while (par != pend) {
871                 if (par->params().startOfAppendix()) {
872                         // FIXME: only the counter corresponding to toplevel
873                         // sectioning should be reset
874                         Counters & cnts = buf.masterBuffer()->params().documentClass().counters();
875                         cnts.reset();
876                         cnts.appendix(true);
877                 }
878                 Layout const & style = par->layout();
879                 ParagraphList::const_iterator lastpar = par;
880                 ParagraphList::const_iterator send;
881
882                 switch (style.latextype) {
883                 case LATEX_COMMAND: {
884                         // The files with which we are working never have more than
885                         // one paragraph in a command structure.
886                         // FIXME 
887                         // if (ourparams.html_in_par)
888                         //   fix it so we don't get sections inside standard, e.g.
889                         // note that we may then need to make runparams not const, so we
890                         // can communicate that back.
891                         // FIXME Maybe this fix should be in the routines themselves, in case
892                         // they are called from elsewhere.
893                         makeCommand(buf, xs, ourparams, text, par);
894                         ++par;
895                         break;
896                 }
897                 case LATEX_ENVIRONMENT:
898                 case LATEX_LIST_ENVIRONMENT:
899                 case LATEX_ITEM_ENVIRONMENT: {
900                         // FIXME Same fix here.
901                         send = searchEnvironmentHtml(par, pend);
902                         par = makeEnvironmentHtml(buf, xs, ourparams, text, par, send);
903                         break;
904                 }
905                 case LATEX_BIB_ENVIRONMENT: {
906                         // FIXME Same fix here.
907                         send = searchEnvironmentHtml(par, pend);
908                         par = makeBibliography(buf, xs, ourparams, text, par, send);
909                         break;
910                 }
911                 case LATEX_PARAGRAPH:
912                         send = searchParagraphHtml(par, pend);
913                         par = makeParagraphs(buf, xs, ourparams, text, par, send);
914                         break;
915                 }
916                 // FIXME??
917                 // makeEnvironment may process more than one paragraphs and bypass pend
918                 if (distance(lastpar, par) >= distance(lastpar, pend))
919                         break;
920         }
921 }
922
923
924 string alignmentToCSS(LyXAlignment align) {
925         switch (align) {
926         case LYX_ALIGN_BLOCK:
927                 // we are NOT going to use text-align: justify!!
928         case LYX_ALIGN_LEFT:
929                 return "left";
930         case LYX_ALIGN_RIGHT:
931                 return "right";
932         case LYX_ALIGN_CENTER:
933                 return "center";
934         default:
935                 break;
936         }
937         return "";
938 }
939
940 } // namespace lyx