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