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