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