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