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