]> git.lyx.org Git - lyx.git/blob - src/output_xhtml.cpp
1c822a9bf529d4164462ff0e8d752ef4b74ea812
[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, ParagraphParameters const & params)
507 {
508         // FIXME Are there other things we should handle here?
509         string const align = alignmentToCSS(params.align());
510         if (align.empty()) {
511                 openTag(xs, lay);
512                 return;
513         }
514         string attrs = lay.htmlattr() + " style='text-align: " + align + ";'";
515         xs << html::StartTag(lay.htmltag(), attrs);
516 }
517
518
519 inline void closeTag(XHTMLStream & xs, Layout const & lay)
520 {
521         xs << html::EndTag(lay.htmltag());
522 }
523
524
525 inline void openLabelTag(XHTMLStream & xs, Layout const & lay)
526 {
527         xs << html::StartTag(lay.htmllabeltag(), lay.htmllabelattr());
528 }
529
530
531 inline void closeLabelTag(XHTMLStream & xs, Layout const & lay)
532 {
533         xs << html::EndTag(lay.htmllabeltag());
534 }
535
536
537 inline void openItemTag(XHTMLStream & xs, Layout const & lay)
538 {
539         xs << html::StartTag(lay.htmlitemtag(), lay.htmlitemattr(), true);
540 }
541
542
543 inline void closeItemTag(XHTMLStream & xs, Layout const & lay)
544 {
545         xs << html::EndTag(lay.htmlitemtag());
546 }
547
548 // end of convenience functions
549
550 ParagraphList::const_iterator searchParagraphHtml(
551         ParagraphList::const_iterator p,
552         ParagraphList::const_iterator const & pend)
553 {
554         for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
555                 ;
556
557         return p;
558 }
559
560
561 ParagraphList::const_iterator searchEnvironmentHtml(
562                 ParagraphList::const_iterator const pstart,
563                 ParagraphList::const_iterator const & pend)
564 {
565         ParagraphList::const_iterator p = pstart;
566         Layout const & bstyle = p->layout();
567         size_t const depth = p->params().depth();
568         for (++p; p != pend; ++p) {
569                 Layout const & style = p->layout();
570                 // It shouldn't happen that e.g. a section command occurs inside
571                 // a quotation environment, at a higher depth, but as of 6/2009,
572                 // it can happen. We pretend that it's just at lowest depth.
573                 if (style.latextype == LATEX_COMMAND)
574                         return p;
575                 // If depth is down, we're done
576                 if (p->params().depth() < depth)
577                         return p;
578                 // If depth is up, we're not done
579                 if (p->params().depth() > depth)
580                         continue;
581                 // Now we know we are at the same depth
582                 if (style.latextype == LATEX_PARAGRAPH
583                     || style.latexname() != bstyle.latexname())
584                         return p;
585         }
586         return pend;
587 }
588
589
590 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
591                                             XHTMLStream & xs,
592                                             OutputParams const & runparams,
593                                             Text const & text,
594                                             ParagraphList::const_iterator const & pbegin,
595                                             ParagraphList::const_iterator const & pend)
596 {
597         ParagraphList::const_iterator const begin = text.paragraphs().begin();
598         ParagraphList::const_iterator par = pbegin;
599         for (; par != pend; ++par) {
600                 Layout const & lay = par->layout();
601                 if (!lay.counter.empty())
602                         buf.params().documentClass().counters().step(lay.counter, OutputUpdate);
603                 // FIXME We should see if there's a label to be output and
604                 // do something with it.
605                 if (par != pbegin)
606                         xs.cr();
607
608                 // If we are already in a paragraph, and this is the first one, then we
609                 // do not want to open the paragraph tag.
610                 // we also do not want to open it if the current layout does not permit
611                 // multiple paragraphs.
612                 bool const opened = runparams.html_make_pars &&
613                         (par != pbegin || !runparams.html_in_par);
614                 if (opened)
615                         openTag(xs, lay, par->params());
616                 docstring const deferred = 
617                         par->simpleLyXHTMLOnePar(buf, xs, runparams, text.outerFont(distance(begin, par)));
618
619                 // We want to issue the closing tag if either:
620                 //   (i)  We opened it, and either html_in_par is false,
621                 //        or we're not in the last paragraph, anyway.
622                 //   (ii) We didn't open it and html_in_par is true, 
623                 //        but we are in the first par, and there is a next par.
624                 ParagraphList::const_iterator nextpar = par;
625                 nextpar++;
626                 bool const needclose = 
627                         (opened && (!runparams.html_in_par || nextpar != pend))
628                         || (!opened && runparams.html_in_par && par == pbegin && nextpar != pend);
629                 if (needclose) {
630                         closeTag(xs, lay);
631                         xs.cr();
632                 }
633                 if (!deferred.empty()) {
634                         xs << XHTMLStream::NextRaw() << deferred;
635                         xs.cr();
636                 }
637         }
638         return pend;
639 }
640
641
642 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
643                                 XHTMLStream & xs,
644                                 OutputParams const & runparams,
645                                 Text const & text,
646                                 ParagraphList::const_iterator const & pbegin,
647                                 ParagraphList::const_iterator const & pend) 
648 {
649         // FIXME XHTML
650         // Use TextClass::htmlTOCLayout() to figure out how we should look.
651         xs << html::StartTag("h2", "class='bibliography'");
652         xs << pbegin->layout().labelstring(false);
653         xs << html::EndTag("h2");
654         xs.cr();
655         xs << html::StartTag("div", "class='bibliography'");
656         xs.cr();
657         makeParagraphs(buf, xs, runparams, text, pbegin, pend);
658         xs << html::EndTag("div");
659         return pend;
660 }
661
662
663 bool isNormalEnv(Layout const & lay)
664 {
665         return lay.latextype == LATEX_ENVIRONMENT
666             || lay.latextype == LATEX_BIB_ENVIRONMENT;
667 }
668
669         
670 ParagraphList::const_iterator makeEnvironmentHtml(Buffer const & buf,
671                                               XHTMLStream & xs,
672                                               OutputParams const & runparams,
673                                               Text const & text,
674                                               ParagraphList::const_iterator const & pbegin,
675                                               ParagraphList::const_iterator const & pend) 
676 {
677         ParagraphList::const_iterator const begin = text.paragraphs().begin();
678         ParagraphList::const_iterator par = pbegin;
679         Layout const & bstyle = par->layout();
680         depth_type const origdepth = pbegin->params().depth();
681
682         // open tag for this environment
683         openTag(xs, bstyle);
684         xs.cr();
685
686         // we will on occasion need to remember a layout from before.
687         Layout const * lastlay = 0;
688
689         while (par != pend) {
690                 Layout const & style = par->layout();
691                 // the counter only gets stepped if we're in some kind of list,
692                 // or if it's the first time through.
693                 // note that enum, etc, are handled automatically.
694                 // FIXME There may be a bug here about user defined enumeration
695                 // types. If so, then we'll need to take the counter and add "i",
696                 // "ii", etc, as with enum.
697                 Counters & cnts = buf.params().documentClass().counters();
698                 docstring const & cntr = style.counter;
699                 if (!style.counter.empty() 
700                     && (par == pbegin || !isNormalEnv(style)) 
701                                 && cnts.hasCounter(cntr)
702                 )
703                         cnts.step(cntr, OutputUpdate);
704                 ParagraphList::const_iterator send;
705                 // this will be positive, if we want to skip the initial word
706                 // (if it's been taken for the label).
707                 pos_type sep = 0;
708
709                 switch (style.latextype) {
710                 case LATEX_ENVIRONMENT:
711                 case LATEX_LIST_ENVIRONMENT:
712                 case LATEX_ITEM_ENVIRONMENT: {
713                         // There are two possiblities in this case. 
714                         // One is that we are still in the environment in which we 
715                         // started---which we will be if the depth is the same.
716                         if (par->params().depth() == origdepth) {
717                                 LASSERT(bstyle == style, /* */);
718                                 if (lastlay != 0) {
719                                         closeItemTag(xs, *lastlay);
720                                         lastlay = 0;
721                                 }
722                                 
723                                 bool const labelfirst = style.htmllabelfirst();
724                                 if (!labelfirst)
725                                         openItemTag(xs, style);
726                                 
727                                 // label output
728                                 if (style.labeltype != LABEL_NO_LABEL && 
729                                     style.htmllabeltag() != "NONE") {
730                                         if (isNormalEnv(style)) {
731                                                 // in this case, we print the label only for the first 
732                                                 // paragraph (as in a theorem).
733                                                 if (par == pbegin) {
734                                                         docstring const lbl = 
735                                                                         pbegin->params().labelString();
736                                                         if (!lbl.empty()) {
737                                                                 openLabelTag(xs, style);
738                                                                 xs << lbl;
739                                                                 closeLabelTag(xs, style);
740                                                         }
741                                                         xs.cr();
742                                                 }
743                                         }       else { // some kind of list
744                                                 if (style.labeltype == LABEL_MANUAL) {
745                                                         openLabelTag(xs, style);
746                                                         sep = par->firstWordLyXHTML(xs, runparams);
747                                                         closeLabelTag(xs, style);
748                                                         xs.cr();
749                                                 }
750                                                 else {
751                                                         openLabelTag(xs, style);
752                                                         xs << par->params().labelString();
753                                                         closeLabelTag(xs, style);
754                                                         xs.cr();
755                                                 }
756                                         }
757                                 } // end label output
758
759                                 if (labelfirst)
760                                         openItemTag(xs, style);
761
762                                 par->simpleLyXHTMLOnePar(buf, xs, runparams, 
763                                         text.outerFont(distance(begin, par)), sep);
764                                 ++par;
765
766                                 // We may not want to close the tag yet, in particular:
767                                 // If we're not at the end...
768                                 if (par != pend 
769                                         //  and are doing items...
770                                          && !isNormalEnv(style)
771                                          // and if the depth has changed...
772                                          && par->params().depth() != origdepth) {
773                                          // then we'll save this layout for later, and close it when
774                                          // we get another item.
775                                         lastlay = &style;
776                                 } else
777                                         closeItemTag(xs, style);
778                                 xs.cr();
779                         }
780                         // The other possibility is that the depth has increased, in which
781                         // case we need to recurse.
782                         else {
783                                 send = searchEnvironmentHtml(par, pend);
784                                 par = makeEnvironmentHtml(buf, xs, runparams, text, par, send);
785                         }
786                         break;
787                 }
788                 case LATEX_PARAGRAPH:
789                         send = searchParagraphHtml(par, pend);
790                         par = makeParagraphs(buf, xs, runparams, text, par, send);
791                         break;
792                 // Shouldn't happen
793                 case LATEX_BIB_ENVIRONMENT:
794                         send = par;
795                         ++send;
796                         par = makeParagraphs(buf, xs, runparams, text, par, send);
797                         break;
798                 // Shouldn't happen
799                 case LATEX_COMMAND:
800                         ++par;
801                         break;
802                 }
803         }
804
805         if (lastlay != 0)
806                 closeItemTag(xs, *lastlay);
807         closeTag(xs, bstyle);
808         xs.cr();
809         return pend;
810 }
811
812
813 void makeCommand(Buffer const & buf,
814                                           XHTMLStream & xs,
815                                           OutputParams const & runparams,
816                                           Text const & text,
817                                           ParagraphList::const_iterator const & pbegin)
818 {
819         Layout const & style = pbegin->layout();
820         if (!style.counter.empty())
821                 buf.params().documentClass().counters().step(style.counter, OutputUpdate);
822
823         openTag(xs, style);
824
825         // Label around sectioning number:
826         // FIXME Probably need to account for LABEL_MANUAL
827         if (style.labeltype != LABEL_NO_LABEL) {
828                 openLabelTag(xs, style);
829                 xs << pbegin->params().labelString();
830                 closeLabelTag(xs, style);
831                 // Otherwise the label might run together with the text
832                 xs << from_ascii(" ");
833         }
834
835         ParagraphList::const_iterator const begin = text.paragraphs().begin();
836         pbegin->simpleLyXHTMLOnePar(buf, xs, runparams,
837                         text.outerFont(distance(begin, pbegin)));
838         closeTag(xs, style);
839         xs.cr();
840 }
841
842 } // end anonymous namespace
843
844
845 void xhtmlParagraphs(Text const & text,
846                        Buffer const & buf,
847                        XHTMLStream & xs,
848                        OutputParams const & runparams)
849 {
850         ParagraphList const & paragraphs = text.paragraphs();
851         ParagraphList::const_iterator par = paragraphs.begin();
852         ParagraphList::const_iterator pend = paragraphs.end();
853
854         OutputParams ourparams = runparams;
855         while (par != pend) {
856                 if (par->params().startOfAppendix()) {
857                         // FIXME: only the counter corresponding to toplevel
858                         // sectioning should be reset
859                         Counters & cnts = buf.masterBuffer()->params().documentClass().counters();
860                         cnts.reset();
861                         cnts.appendix(true);
862                 }
863                 Layout const & style = par->layout();
864                 ParagraphList::const_iterator lastpar = par;
865                 ParagraphList::const_iterator send;
866
867                 switch (style.latextype) {
868                 case LATEX_COMMAND: {
869                         // The files with which we are working never have more than
870                         // one paragraph in a command structure.
871                         // FIXME 
872                         // if (ourparams.html_in_par)
873                         //   fix it so we don't get sections inside standard, e.g.
874                         // note that we may then need to make runparams not const, so we
875                         // can communicate that back.
876                         // FIXME Maybe this fix should be in the routines themselves, in case
877                         // they are called from elsewhere.
878                         makeCommand(buf, xs, ourparams, text, par);
879                         ++par;
880                         break;
881                 }
882                 case LATEX_ENVIRONMENT:
883                 case LATEX_LIST_ENVIRONMENT:
884                 case LATEX_ITEM_ENVIRONMENT: {
885                         // FIXME Same fix here.
886                         send = searchEnvironmentHtml(par, pend);
887                         par = makeEnvironmentHtml(buf, xs, ourparams, text, par, send);
888                         break;
889                 }
890                 case LATEX_BIB_ENVIRONMENT: {
891                         // FIXME Same fix here.
892                         send = searchEnvironmentHtml(par, pend);
893                         par = makeBibliography(buf, xs, ourparams, text, par, send);
894                         break;
895                 }
896                 case LATEX_PARAGRAPH:
897                         send = searchParagraphHtml(par, pend);
898                         par = makeParagraphs(buf, xs, ourparams, text, par, send);
899                         break;
900                 }
901                 // FIXME??
902                 // makeEnvironment may process more than one paragraphs and bypass pend
903                 if (distance(lastpar, par) >= distance(lastpar, pend))
904                         break;
905         }
906 }
907
908
909 string alignmentToCSS(LyXAlignment align) {
910         switch (align) {
911         case LYX_ALIGN_BLOCK:
912                 // we are NOT going to use text-align: justify!!
913         case LYX_ALIGN_LEFT:
914                 return "left";
915         case LYX_ALIGN_RIGHT:
916                 return "right";
917         case LYX_ALIGN_CENTER:
918                 return "center";
919         default:
920                 break;
921         }
922         return "";
923 }
924
925 } // namespace lyx