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