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