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