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