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