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