]> git.lyx.org Git - features.git/blob - src/output_xhtml.cpp
Fix bug #8598. We need to use the masterBuffer's counters in the
[features.git] / src / output_xhtml.cpp
1 /**
2  * \file output_xhtml.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Richard Heck
7  * 
8  * This code is based upon output_docbook.cpp
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "output_xhtml.h"
16
17 #include "Buffer.h"
18 #include "buffer_funcs.h"
19 #include "BufferParams.h"
20 #include "Counters.h"
21 #include "Font.h"
22 #include "Layout.h"
23 #include "OutputParams.h"
24 #include "Paragraph.h"
25 #include "ParagraphList.h"
26 #include "ParagraphParameters.h"
27 #include "sgml.h"
28 #include "Text.h"
29 #include "TextClass.h"
30
31 #include "support/convert.h"
32 #include "support/debug.h"
33 #include "support/lassert.h"
34 #include "support/lstrings.h"
35 #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.masterBuffer()->params().
710                             documentClass().counters().step(lay.counter, OutputUpdate);
711                 // FIXME We should see if there's a label to be output and
712                 // do something with it.
713                 if (par != pbegin)
714                         xs << html::CR();
715
716                 // If we are already in a paragraph, and this is the first one, then we
717                 // do not want to open the paragraph tag.
718                 // we also do not want to open it if the current layout does not permit
719                 // multiple paragraphs.
720                 bool const opened = runparams.html_make_pars &&
721                         (par != pbegin || !runparams.html_in_par);
722                 if (opened)
723                         openTag(xs, lay, par->params());
724                 docstring const deferred = 
725                         par->simpleLyXHTMLOnePar(buf, xs, runparams, text.outerFont(distance(begin, par)));
726
727                 // We want to issue the closing tag if either:
728                 //   (i)  We opened it, and either html_in_par is false,
729                 //        or we're not in the last paragraph, anyway.
730                 //   (ii) We didn't open it and html_in_par is true, 
731                 //        but we are in the first par, and there is a next par.
732                 ParagraphList::const_iterator nextpar = par;
733                 ++nextpar;
734                 bool const needclose = 
735                         (opened && (!runparams.html_in_par || nextpar != pend))
736                         || (!opened && runparams.html_in_par && par == pbegin && nextpar != pend);
737                 if (needclose) {
738                         closeTag(xs, lay);
739                         xs << html::CR();
740                 }
741                 if (!deferred.empty()) {
742                         xs << XHTMLStream::ESCAPE_NONE << deferred << html::CR();
743                 }
744         }
745         return pend;
746 }
747
748
749 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
750                                 XHTMLStream & xs,
751                                 OutputParams const & runparams,
752                                 Text const & text,
753                                 ParagraphList::const_iterator const & pbegin,
754                                 ParagraphList::const_iterator const & pend) 
755 {
756         // FIXME XHTML
757         // Use TextClass::htmlTOCLayout() to figure out how we should look.
758         xs << html::StartTag("h2", "class='bibliography'")
759            << pbegin->layout().labelstring(false)
760            << html::EndTag("h2")
761            << html::CR()
762            << html::StartTag("div", "class='bibliography'")
763            << html::CR();
764         makeParagraphs(buf, xs, runparams, text, pbegin, pend);
765         xs << html::EndTag("div");
766         return pend;
767 }
768
769
770 bool isNormalEnv(Layout const & lay)
771 {
772         return lay.latextype == LATEX_ENVIRONMENT
773             || lay.latextype == LATEX_BIB_ENVIRONMENT;
774 }
775
776         
777 ParagraphList::const_iterator makeEnvironmentHtml(Buffer const & buf,
778                                               XHTMLStream & xs,
779                                               OutputParams const & runparams,
780                                               Text const & text,
781                                               ParagraphList::const_iterator const & pbegin,
782                                               ParagraphList::const_iterator const & pend) 
783 {
784         ParagraphList::const_iterator const begin = text.paragraphs().begin();
785         ParagraphList::const_iterator par = pbegin;
786         Layout const & bstyle = par->layout();
787         depth_type const origdepth = pbegin->params().depth();
788
789         // open tag for this environment
790         openTag(xs, bstyle);
791         xs << html::CR();
792
793         // we will on occasion need to remember a layout from before.
794         Layout const * lastlay = 0;
795
796         while (par != pend) {
797                 Layout const & style = par->layout();
798                 // the counter only gets stepped if we're in some kind of list,
799                 // or if it's the first time through.
800                 // note that enum, etc, are handled automatically.
801                 // FIXME There may be a bug here about user defined enumeration
802                 // types. If so, then we'll need to take the counter and add "i",
803                 // "ii", etc, as with enum.
804                 Counters & cnts = buf.masterBuffer()->params().documentClass().counters();
805                 docstring const & cntr = style.counter;
806                 if (!style.counter.empty() 
807                     && (par == pbegin || !isNormalEnv(style)) 
808                                 && cnts.hasCounter(cntr)
809                 )
810                         cnts.step(cntr, OutputUpdate);
811                 ParagraphList::const_iterator send;
812                 // this will be positive, if we want to skip the initial word
813                 // (if it's been taken for the label).
814                 pos_type sep = 0;
815
816                 switch (style.latextype) {
817                 case LATEX_ENVIRONMENT:
818                 case LATEX_LIST_ENVIRONMENT:
819                 case LATEX_ITEM_ENVIRONMENT: {
820                         // There are two possiblities in this case. 
821                         // One is that we are still in the environment in which we 
822                         // started---which we will be if the depth is the same.
823                         if (par->params().depth() == origdepth) {
824                                 LASSERT(bstyle == style, /* */);
825                                 if (lastlay != 0) {
826                                         closeItemTag(xs, *lastlay);
827                                         lastlay = 0;
828                                 }
829                                 
830                                 bool const labelfirst = style.htmllabelfirst();
831                                 if (!labelfirst)
832                                         openItemTag(xs, style, par->params());
833                                 
834                                 // label output
835                                 if (style.labeltype != LABEL_NO_LABEL && 
836                                     style.htmllabeltag() != "NONE") {
837                                         if (isNormalEnv(style)) {
838                                                 // in this case, we print the label only for the first 
839                                                 // paragraph (as in a theorem).
840                                                 if (par == pbegin) {
841                                                         docstring const lbl = 
842                                                                         pbegin->params().labelString();
843                                                         if (!lbl.empty()) {
844                                                                 openLabelTag(xs, style);
845                                                                 xs << lbl;
846                                                                 closeLabelTag(xs, style);
847                                                         }
848                                                         xs << html::CR();
849                                                 }
850                                         }       else { // some kind of list
851                                                 if (style.labeltype == LABEL_MANUAL) {
852                                                         openLabelTag(xs, style);
853                                                         sep = par->firstWordLyXHTML(xs, runparams);
854                                                         closeLabelTag(xs, style);
855                                                         xs << html::CR();
856                                                 }
857                                                 else {
858                                                         openLabelTag(xs, style);
859                                                         xs << par->params().labelString();
860                                                         closeLabelTag(xs, style);
861                                                         xs << html::CR();
862                                                 }
863                                         }
864                                 } // end label output
865
866                                 if (labelfirst)
867                                         openItemTag(xs, style, par->params());
868
869                                 par->simpleLyXHTMLOnePar(buf, xs, runparams, 
870                                         text.outerFont(distance(begin, par)), sep);
871                                 ++par;
872
873                                 // We may not want to close the tag yet, in particular:
874                                 // If we're not at the end...
875                                 if (par != pend 
876                                         //  and are doing items...
877                                          && !isNormalEnv(style)
878                                          // and if the depth has changed...
879                                          && par->params().depth() != origdepth) {
880                                          // then we'll save this layout for later, and close it when
881                                          // we get another item.
882                                         lastlay = &style;
883                                 } else
884                                         closeItemTag(xs, style);
885                                 xs << html::CR();
886                         }
887                         // The other possibility is that the depth has increased, in which
888                         // case we need to recurse.
889                         else {
890                                 send = searchEnvironmentHtml(par, pend);
891                                 par = makeEnvironmentHtml(buf, xs, runparams, text, par, send);
892                         }
893                         break;
894                 }
895                 case LATEX_PARAGRAPH:
896                         send = searchParagraphHtml(par, pend);
897                         par = makeParagraphs(buf, xs, runparams, text, par, send);
898                         break;
899                 // Shouldn't happen
900                 case LATEX_BIB_ENVIRONMENT:
901                         send = par;
902                         ++send;
903                         par = makeParagraphs(buf, xs, runparams, text, par, send);
904                         break;
905                 // Shouldn't happen
906                 case LATEX_COMMAND:
907                         ++par;
908                         break;
909                 }
910         }
911
912         if (lastlay != 0)
913                 closeItemTag(xs, *lastlay);
914         closeTag(xs, bstyle);
915         xs << html::CR();
916         return pend;
917 }
918
919
920 void makeCommand(Buffer const & buf,
921                  XHTMLStream & xs,
922                  OutputParams const & runparams,
923                  Text const & text,
924                  ParagraphList::const_iterator const & pbegin)
925 {
926         Layout const & style = pbegin->layout();
927         if (!style.counter.empty())
928                 buf.masterBuffer()->params().
929                     documentClass().counters().step(style.counter, OutputUpdate);
930
931         openTag(xs, style, pbegin->params());
932
933         // Label around sectioning number:
934         // FIXME Probably need to account for LABEL_MANUAL
935         // FIXME Probably also need now to account for labels ABOVE and CENTERED.
936         if (style.labeltype != LABEL_NO_LABEL) {
937                 openLabelTag(xs, style);
938                 xs << pbegin->params().labelString();
939                 closeLabelTag(xs, style);
940                 // Otherwise the label might run together with the text
941                 xs << from_ascii(" ");
942         }
943
944         ParagraphList::const_iterator const begin = text.paragraphs().begin();
945         pbegin->simpleLyXHTMLOnePar(buf, xs, runparams,
946                         text.outerFont(distance(begin, pbegin)));
947         closeTag(xs, style);
948         xs << html::CR();
949 }
950
951 } // end anonymous namespace
952
953
954 void xhtmlParagraphs(Text const & text,
955                        Buffer const & buf,
956                        XHTMLStream & xs,
957                        OutputParams const & runparams)
958 {
959         ParagraphList const & paragraphs = text.paragraphs();
960         if (runparams.par_begin == runparams.par_end) {
961                 runparams.par_begin = 0;
962                 runparams.par_end = paragraphs.size();
963         }
964         pit_type bpit = runparams.par_begin;
965         pit_type const epit = runparams.par_end;
966         LASSERT(bpit < epit, /* */);
967
968         OutputParams ourparams = runparams;
969         ParagraphList::const_iterator const pend =
970                 (epit == (int) paragraphs.size()) ?
971                         paragraphs.end() : paragraphs.constIterator(epit);
972         while (bpit < epit) {
973                 ParagraphList::const_iterator par = paragraphs.constIterator(bpit);
974                 if (par->params().startOfAppendix()) {
975                         // We want to reset the counter corresponding to toplevel sectioning
976                         Layout const & lay =
977                                 buf.masterBuffer()->params().documentClass().getTOCLayout();
978                         docstring const cnt = lay.counter;
979                         if (!cnt.empty()) {
980                                 Counters & cnts =
981                                         buf.masterBuffer()->params().documentClass().counters();
982                                 cnts.reset(cnt);
983                         }
984                 }
985                 Layout const & style = par->layout();
986                 ParagraphList::const_iterator const lastpar = par;
987                 ParagraphList::const_iterator send;
988
989                 switch (style.latextype) {
990                 case LATEX_COMMAND: {
991                         // The files with which we are working never have more than
992                         // one paragraph in a command structure.
993                         // FIXME 
994                         // if (ourparams.html_in_par)
995                         //   fix it so we don't get sections inside standard, e.g.
996                         // note that we may then need to make runparams not const, so we
997                         // can communicate that back.
998                         // FIXME Maybe this fix should be in the routines themselves, in case
999                         // they are called from elsewhere.
1000                         makeCommand(buf, xs, ourparams, text, par);
1001                         ++par;
1002                         break;
1003                 }
1004                 case LATEX_ENVIRONMENT:
1005                 case LATEX_LIST_ENVIRONMENT:
1006                 case LATEX_ITEM_ENVIRONMENT: {
1007                         // FIXME Same fix here.
1008                         send = searchEnvironmentHtml(par, pend);
1009                         par = makeEnvironmentHtml(buf, xs, ourparams, text, par, send);
1010                         break;
1011                 }
1012                 case LATEX_BIB_ENVIRONMENT: {
1013                         // FIXME Same fix here.
1014                         send = searchEnvironmentHtml(par, pend);
1015                         par = makeBibliography(buf, xs, ourparams, text, par, send);
1016                         break;
1017                 }
1018                 case LATEX_PARAGRAPH:
1019                         send = searchParagraphHtml(par, pend);
1020                         par = makeParagraphs(buf, xs, ourparams, text, par, send);
1021                         break;
1022                 }
1023                 bpit += distance(lastpar, par);
1024         }
1025 }
1026
1027
1028 string alignmentToCSS(LyXAlignment align)
1029 {
1030         switch (align) {
1031         case LYX_ALIGN_BLOCK:
1032                 // we are NOT going to use text-align: justify!!
1033         case LYX_ALIGN_LEFT:
1034                 return "left";
1035         case LYX_ALIGN_RIGHT:
1036                 return "right";
1037         case LYX_ALIGN_CENTER:
1038                 return "center";
1039         default:
1040                 break;
1041         }
1042         return "";
1043 }
1044
1045 } // namespace lyx