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