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