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