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