]> git.lyx.org Git - lyx.git/blob - src/output_xhtml.cpp
Fix comparing a pointer with a char
[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 openTag(XHTMLStream & xs, Layout const & lay)
756 {
757         xs << html::StartTag(lay.htmltag(), lay.htmlattr());
758 }
759
760
761 void openTag(XHTMLStream & xs, Layout const & lay, 
762              ParagraphParameters const & params)
763 {
764         // FIXME Are there other things we should handle here?
765         string const align = alignmentToCSS(params.align());
766         if (align.empty()) {
767                 openTag(xs, lay);
768                 return;
769         }
770         string attrs = lay.htmlattr() + " style='text-align: " + align + ";'";
771         xs << html::StartTag(lay.htmltag(), attrs);
772 }
773
774
775 inline void openParTag(XHTMLStream & xs, Layout const & lay,
776                        std::string parlabel)
777 {
778         xs << html::ParTag(lay.htmltag(), lay.htmlattr(), parlabel);
779 }
780
781
782 void openParTag(XHTMLStream & xs, Layout const & lay,
783                 ParagraphParameters const & params,
784                 std::string parlabel)
785 {
786         // FIXME Are there other things we should handle here?
787         string const align = alignmentToCSS(params.align());
788         if (align.empty()) {
789                 openParTag(xs, lay, parlabel);
790                 return;
791         }
792         string attrs = lay.htmlattr() + " style='text-align: " + align + ";'";
793         xs << html::ParTag(lay.htmltag(), attrs, parlabel);
794 }
795
796
797 inline void closeTag(XHTMLStream & xs, Layout const & lay)
798 {
799         xs << html::EndTag(lay.htmltag());
800 }
801
802
803 inline void openLabelTag(XHTMLStream & xs, Layout const & lay)
804 {
805         xs << html::StartTag(lay.htmllabeltag(), lay.htmllabelattr());
806 }
807
808
809 inline void closeLabelTag(XHTMLStream & xs, Layout const & lay)
810 {
811         xs << html::EndTag(lay.htmllabeltag());
812 }
813
814
815 inline void openItemTag(XHTMLStream & xs, Layout const & lay)
816 {
817         xs << html::StartTag(lay.htmlitemtag(), lay.htmlitemattr(), true);
818 }
819
820
821 void openItemTag(XHTMLStream & xs, Layout const & lay, 
822              ParagraphParameters const & params)
823 {
824         // FIXME Are there other things we should handle here?
825         string const align = alignmentToCSS(params.align());
826         if (align.empty()) {
827                 openItemTag(xs, lay);
828                 return;
829         }
830         string attrs = lay.htmlattr() + " style='text-align: " + align + ";'";
831         xs << html::StartTag(lay.htmlitemtag(), attrs);
832 }
833
834
835 inline void closeItemTag(XHTMLStream & xs, Layout const & lay)
836 {
837         xs << html::EndTag(lay.htmlitemtag());
838 }
839
840 // end of convenience functions
841
842 ParagraphList::const_iterator findLastParagraph(
843         ParagraphList::const_iterator p,
844         ParagraphList::const_iterator const & pend)
845 {
846         for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
847                 ;
848
849         return p;
850 }
851
852
853 ParagraphList::const_iterator findEndOfEnvironment(
854                 ParagraphList::const_iterator const pstart,
855                 ParagraphList::const_iterator const & pend)
856 {
857         ParagraphList::const_iterator p = pstart;
858         Layout const & bstyle = p->layout();
859         size_t const depth = p->params().depth();
860         for (++p; p != pend; ++p) {
861                 Layout const & style = p->layout();
862                 // It shouldn't happen that e.g. a section command occurs inside
863                 // a quotation environment, at a higher depth, but as of 6/2009,
864                 // it can happen. We pretend that it's just at lowest depth.
865                 if (style.latextype == LATEX_COMMAND)
866                         return p;
867
868                 // If depth is down, we're done
869                 if (p->params().depth() < depth)
870                         return p;
871
872                 // If depth is up, we're not done
873                 if (p->params().depth() > depth)
874                         continue;
875
876                 // FIXME I am not sure about the first check.
877                 // Surely we *could* have different layouts that count as
878                 // LATEX_PARAGRAPH, right? 
879                 if (style.latextype == LATEX_PARAGRAPH || style != bstyle)
880                         return p;
881         }
882         return pend;
883 }
884
885
886 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
887                                             XHTMLStream & xs,
888                                             OutputParams const & runparams,
889                                             Text const & text,
890                                             ParagraphList::const_iterator const & pbegin,
891                                             ParagraphList::const_iterator const & pend)
892 {
893         ParagraphList::const_iterator const begin = text.paragraphs().begin();
894         ParagraphList::const_iterator par = pbegin;
895         for (; par != pend; ++par) {
896                 Layout const & lay = par->layout();
897                 if (!lay.counter.empty())
898                         buf.masterBuffer()->params().
899                             documentClass().counters().step(lay.counter, OutputUpdate);
900                 // FIXME We should see if there's a label to be output and
901                 // do something with it.
902                 if (par != pbegin)
903                         xs << html::CR();
904
905                 // If we are already in a paragraph, and this is the first one, then we
906                 // do not want to open the paragraph tag.
907                 // we also do not want to open it if the current layout does not permit
908                 // multiple paragraphs.
909                 bool const opened = runparams.html_make_pars &&
910                         (par != pbegin || !runparams.html_in_par);
911                 bool const make_parid = !runparams.for_toc && runparams.html_make_pars;
912
913                 if (opened)
914                         openParTag(xs, lay, par->params(),
915                                    make_parid ? par->magicLabel() : "");
916
917                 docstring const deferred = 
918                         par->simpleLyXHTMLOnePar(buf, xs, runparams, text.outerFont(distance(begin, par)));
919
920                 // We want to issue the closing tag if either:
921                 //   (i)  We opened it, and either html_in_par is false,
922                 //        or we're not in the last paragraph, anyway.
923                 //   (ii) We didn't open it and html_in_par is true, 
924                 //        but we are in the first par, and there is a next par.
925                 ParagraphList::const_iterator nextpar = par;
926                 ++nextpar;
927                 bool const needclose = 
928                         (opened && (!runparams.html_in_par || nextpar != pend))
929                         || (!opened && runparams.html_in_par && par == pbegin && nextpar != pend);
930                 if (needclose) {
931                         closeTag(xs, lay);
932                         xs << html::CR();
933                 }
934                 if (!deferred.empty()) {
935                         xs << XHTMLStream::ESCAPE_NONE << deferred << html::CR();
936                 }
937         }
938         return pend;
939 }
940
941
942 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
943                                 XHTMLStream & xs,
944                                 OutputParams const & runparams,
945                                 Text const & text,
946                                 ParagraphList::const_iterator const & pbegin,
947                                 ParagraphList::const_iterator const & pend) 
948 {
949         // FIXME XHTML
950         // Use TextClass::htmlTOCLayout() to figure out how we should look.
951         xs << html::StartTag("h2", "class='bibliography'")
952            << pbegin->layout().labelstring(false)
953            << html::EndTag("h2")
954            << html::CR()
955            << html::StartTag("div", "class='bibliography'")
956            << html::CR();
957         makeParagraphs(buf, xs, runparams, text, pbegin, pend);
958         xs << html::EndTag("div");
959         return pend;
960 }
961
962
963 bool isNormalEnv(Layout const & lay)
964 {
965         return lay.latextype == LATEX_ENVIRONMENT
966             || lay.latextype == LATEX_BIB_ENVIRONMENT;
967 }
968
969         
970 ParagraphList::const_iterator makeEnvironment(Buffer const & buf,
971                                               XHTMLStream & xs,
972                                               OutputParams const & runparams,
973                                               Text const & text,
974                                               ParagraphList::const_iterator const & pbegin,
975                                               ParagraphList::const_iterator const & pend) 
976 {
977         ParagraphList::const_iterator const begin = text.paragraphs().begin();
978         ParagraphList::const_iterator par = pbegin;
979         Layout const & bstyle = par->layout();
980         depth_type const origdepth = pbegin->params().depth();
981
982         // open tag for this environment
983         openParTag(xs, bstyle, pbegin->magicLabel());
984         xs << html::CR();
985
986         // we will on occasion need to remember a layout from before.
987         Layout const * lastlay = 0;
988
989         while (par != pend) {
990                 Layout const & style = par->layout();
991                 // the counter only gets stepped if we're in some kind of list,
992                 // or if it's the first time through.
993                 // note that enum, etc, are handled automatically.
994                 // FIXME There may be a bug here about user defined enumeration
995                 // types. If so, then we'll need to take the counter and add "i",
996                 // "ii", etc, as with enum.
997                 Counters & cnts = buf.masterBuffer()->params().documentClass().counters();
998                 docstring const & cntr = style.counter;
999                 if (!style.counter.empty() 
1000                     && (par == pbegin || !isNormalEnv(style)) 
1001                                 && cnts.hasCounter(cntr)
1002                 )
1003                         cnts.step(cntr, OutputUpdate);
1004                 ParagraphList::const_iterator send;
1005                 // this will be positive, if we want to skip the initial word
1006                 // (if it's been taken for the label).
1007                 pos_type sep = 0;
1008
1009                 switch (style.latextype) {
1010                 case LATEX_ENVIRONMENT:
1011                 case LATEX_LIST_ENVIRONMENT:
1012                 case LATEX_ITEM_ENVIRONMENT: {
1013                         // There are two possiblities in this case. 
1014                         // One is that we are still in the environment in which we 
1015                         // started---which we will be if the depth is the same.
1016                         if (par->params().depth() == origdepth) {
1017                                 LATTEST(bstyle == style);
1018                                 if (lastlay != 0) {
1019                                         closeItemTag(xs, *lastlay);
1020                                         lastlay = 0;
1021                                 }
1022                                 
1023                                 bool const labelfirst = style.htmllabelfirst();
1024                                 if (!labelfirst)
1025                                         openItemTag(xs, style, par->params());
1026                                 
1027                                 // label output
1028                                 if (style.labeltype != LABEL_NO_LABEL && 
1029                                     style.htmllabeltag() != "NONE") {
1030                                         if (isNormalEnv(style)) {
1031                                                 // in this case, we print the label only for the first 
1032                                                 // paragraph (as in a theorem).
1033                                                 if (par == pbegin) {
1034                                                         docstring const lbl = 
1035                                                                         pbegin->params().labelString();
1036                                                         if (!lbl.empty()) {
1037                                                                 openLabelTag(xs, style);
1038                                                                 xs << lbl;
1039                                                                 closeLabelTag(xs, style);
1040                                                         }
1041                                                         xs << html::CR();
1042                                                 }
1043                                         }       else { // some kind of list
1044                                                 if (style.labeltype == LABEL_MANUAL) {
1045                                                         openLabelTag(xs, style);
1046                                                         sep = par->firstWordLyXHTML(xs, runparams);
1047                                                         closeLabelTag(xs, style);
1048                                                         xs << html::CR();
1049                                                 }
1050                                                 else {
1051                                                         openLabelTag(xs, style);
1052                                                         xs << par->params().labelString();
1053                                                         closeLabelTag(xs, style);
1054                                                         xs << html::CR();
1055                                                 }
1056                                         }
1057                                 } // end label output
1058
1059                                 if (labelfirst)
1060                                         openItemTag(xs, style, par->params());
1061
1062                                 par->simpleLyXHTMLOnePar(buf, xs, runparams, 
1063                                         text.outerFont(distance(begin, par)), sep);
1064                                 ++par;
1065
1066                                 // We may not want to close the tag yet, in particular:
1067                                 // If we're not at the end...
1068                                 if (par != pend 
1069                                         //  and are doing items...
1070                                          && !isNormalEnv(style)
1071                                          // and if the depth has changed...
1072                                          && par->params().depth() != origdepth) {
1073                                          // then we'll save this layout for later, and close it when
1074                                          // we get another item.
1075                                         lastlay = &style;
1076                                 } else
1077                                         closeItemTag(xs, style);
1078                                 xs << html::CR();
1079                         }
1080                         // The other possibility is that the depth has increased, in which
1081                         // case we need to recurse.
1082                         else {
1083                                 send = findEndOfEnvironment(par, pend);
1084                                 par = makeEnvironment(buf, xs, runparams, text, par, send);
1085                         }
1086                         break;
1087                 }
1088                 case LATEX_PARAGRAPH:
1089                         send = findLastParagraph(par, pend);
1090                         par = makeParagraphs(buf, xs, runparams, text, par, send);
1091                         break;
1092                 // Shouldn't happen
1093                 case LATEX_BIB_ENVIRONMENT:
1094                         send = par;
1095                         ++send;
1096                         par = makeParagraphs(buf, xs, runparams, text, par, send);
1097                         break;
1098                 // Shouldn't happen
1099                 case LATEX_COMMAND:
1100                         ++par;
1101                         break;
1102                 }
1103         }
1104
1105         if (lastlay != 0)
1106                 closeItemTag(xs, *lastlay);
1107         closeTag(xs, bstyle);
1108         xs << html::CR();
1109         return pend;
1110 }
1111
1112
1113 void makeCommand(Buffer const & buf,
1114                  XHTMLStream & xs,
1115                  OutputParams const & runparams,
1116                  Text const & text,
1117                  ParagraphList::const_iterator const & pbegin)
1118 {
1119         Layout const & style = pbegin->layout();
1120         if (!style.counter.empty())
1121                 buf.masterBuffer()->params().
1122                     documentClass().counters().step(style.counter, OutputUpdate);
1123
1124         bool const make_parid = !runparams.for_toc && runparams.html_make_pars;
1125
1126         openParTag(xs, style, pbegin->params(),
1127                    make_parid ? pbegin->magicLabel() : "");
1128
1129         // Label around sectioning number:
1130         // FIXME Probably need to account for LABEL_MANUAL
1131         // FIXME Probably also need now to account for labels ABOVE and CENTERED.
1132         if (style.labeltype != LABEL_NO_LABEL) {
1133                 openLabelTag(xs, style);
1134                 xs << pbegin->params().labelString();
1135                 closeLabelTag(xs, style);
1136                 // Otherwise the label might run together with the text
1137                 xs << from_ascii(" ");
1138         }
1139
1140         ParagraphList::const_iterator const begin = text.paragraphs().begin();
1141         pbegin->simpleLyXHTMLOnePar(buf, xs, runparams,
1142                         text.outerFont(distance(begin, pbegin)));
1143         closeTag(xs, style);
1144         xs << html::CR();
1145 }
1146
1147 } // end anonymous namespace
1148
1149
1150 void xhtmlParagraphs(Text const & text,
1151                        Buffer const & buf,
1152                        XHTMLStream & xs,
1153                        OutputParams const & runparams)
1154 {
1155         ParagraphList const & paragraphs = text.paragraphs();
1156         if (runparams.par_begin == runparams.par_end) {
1157                 runparams.par_begin = 0;
1158                 runparams.par_end = paragraphs.size();
1159         }
1160         pit_type bpit = runparams.par_begin;
1161         pit_type const epit = runparams.par_end;
1162         LASSERT(bpit < epit,
1163                 { xs << XHTMLStream::ESCAPE_NONE << "<!-- XHTML output error! -->\n"; return; });
1164
1165         OutputParams ourparams = runparams;
1166         ParagraphList::const_iterator const pend =
1167                 (epit == (int) paragraphs.size()) ?
1168                         paragraphs.end() : paragraphs.constIterator(epit);
1169         while (bpit < epit) {
1170                 ParagraphList::const_iterator par = paragraphs.constIterator(bpit);
1171                 if (par->params().startOfAppendix()) {
1172                         // We want to reset the counter corresponding to toplevel sectioning
1173                         Layout const & lay =
1174                                 buf.masterBuffer()->params().documentClass().getTOCLayout();
1175                         docstring const cnt = lay.counter;
1176                         if (!cnt.empty()) {
1177                                 Counters & cnts =
1178                                         buf.masterBuffer()->params().documentClass().counters();
1179                                 cnts.reset(cnt);
1180                         }
1181                 }
1182                 Layout const & style = par->layout();
1183                 ParagraphList::const_iterator const lastpar = par;
1184                 ParagraphList::const_iterator send;
1185
1186                 switch (style.latextype) {
1187                 case LATEX_COMMAND: {
1188                         // The files with which we are working never have more than
1189                         // one paragraph in a command structure.
1190                         // FIXME 
1191                         // if (ourparams.html_in_par)
1192                         //   fix it so we don't get sections inside standard, e.g.
1193                         // note that we may then need to make runparams not const, so we
1194                         // can communicate that back.
1195                         // FIXME Maybe this fix should be in the routines themselves, in case
1196                         // they are called from elsewhere.
1197                         makeCommand(buf, xs, ourparams, text, par);
1198                         ++par;
1199                         break;
1200                 }
1201                 case LATEX_ENVIRONMENT:
1202                 case LATEX_LIST_ENVIRONMENT:
1203                 case LATEX_ITEM_ENVIRONMENT: {
1204                         // FIXME Same fix here.
1205                         send = findEndOfEnvironment(par, pend);
1206                         par = makeEnvironment(buf, xs, ourparams, text, par, send);
1207                         break;
1208                 }
1209                 case LATEX_BIB_ENVIRONMENT: {
1210                         // FIXME Same fix here.
1211                         send = findEndOfEnvironment(par, pend);
1212                         par = makeBibliography(buf, xs, ourparams, text, par, send);
1213                         break;
1214                 }
1215                 case LATEX_PARAGRAPH:
1216                         send = findLastParagraph(par, pend);
1217                         par = makeParagraphs(buf, xs, ourparams, text, par, send);
1218                         break;
1219                 }
1220                 bpit += distance(lastpar, par);
1221         }
1222 }
1223
1224
1225 string alignmentToCSS(LyXAlignment align)
1226 {
1227         switch (align) {
1228         case LYX_ALIGN_BLOCK:
1229                 // we are NOT going to use text-align: justify!!
1230         case LYX_ALIGN_LEFT:
1231                 return "left";
1232         case LYX_ALIGN_RIGHT:
1233                 return "right";
1234         case LYX_ALIGN_CENTER:
1235                 return "center";
1236         default:
1237                 break;
1238         }
1239         return "";
1240 }
1241
1242 } // namespace lyx