]> git.lyx.org Git - lyx.git/blob - src/output_xhtml.cpp
This commit does two things.
[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 "Layout.h"
22 #include "OutputParams.h"
23 #include "Paragraph.h"
24 #include "ParagraphList.h"
25 #include "ParagraphParameters.h"
26 #include "sgml.h"
27 #include "Text.h"
28 #include "TextClass.h"
29
30 #include "support/lassert.h"
31 #include "support/debug.h"
32 #include "support/lstrings.h"
33
34 #include <boost/next_prior.hpp>
35 #include <vector>
36
37 using namespace std;
38 using namespace lyx::support;
39
40 namespace lyx {
41
42 namespace html {
43
44 docstring escapeChar(char_type c)
45 {
46         docstring str;
47         switch (c) {
48         case ' ':
49                 str += " ";
50                 break;
51         case '&':
52                 str += "&amp;";
53                 break;
54         case '<':
55                 str += "&lt;";
56                 break;
57         case '>':
58                 str += "&gt;";
59                 break;
60         default:
61                 str += c;
62                 break;
63         }
64         return str;
65 }
66
67
68 // FIXME do something here.
69 docstring htmlize(docstring const & str) {
70         return str;
71 }
72
73 // FIXME This needs to be protected somehow.
74 static vector<string> taglist;
75
76 bool openTag(odocstream & os, string const & tag, string const & attr)
77 {
78         if (tag.empty())
79                 return false;
80         // FIXME This is completely primitive. We need something
81         // a lot better.
82         // Now do some checks on nesting of tags.
83         if (tag == "p")
84                 if (find(taglist.begin(), taglist.end(), "p") != taglist.end())
85                         return false;
86         os << from_ascii("<" + tag + (attr.empty() ? "" : " " + attr) + ">");
87         taglist.push_back(tag);
88         return true;
89 }
90
91
92 bool closeTag(odocstream & os, string const & tag)
93 {
94         if (tag.empty())
95                 return false;
96         // FIXME Check for proper nesting
97         if (taglist.empty()){
98                 LYXERR0("Last tag not found when closing `" << tag << "'!");
99                 return false;
100         }
101         string const & lasttag = taglist.back();
102         if (lasttag != tag)  {
103                 LYXERR0("Last tag was `" << lasttag << "' when closing `" << tag << "'!");
104                 return false;
105         }
106         taglist.pop_back();
107         os << from_ascii("</" + tag + ">");
108         return true;
109 }
110
111
112
113 } // html
114
115 namespace {
116
117 bool openTag(odocstream & os, Layout const & lay)
118 {
119         string const tag = lay.htmltag().empty() 
120                         ? "div" : lay.htmltag();
121         string const attr = lay.htmlattr().empty()
122                         ? "class=\"" + to_utf8(lay.name()) + "\"" : lay.htmlattr();
123         return html::openTag(os, tag, attr);
124 }
125
126
127 bool closeTag(odocstream & os, Layout const & lay)
128 {
129         string const tag = lay.htmltag().empty() 
130                         ? "div" : lay.htmltag();
131         return html::closeTag(os, tag);
132 }
133
134
135 bool openLabelTag(odocstream & os, Layout const & lay)
136 {
137         string const tag = lay.htmllabel().empty() 
138                         ? "span" : lay.htmllabel();
139         string const attr = lay.htmlattr().empty()
140                         ? "class=\"" + to_utf8(lay.name()) + "label\"" : lay.htmllabelattr();
141         return html::openTag(os, tag, attr);
142 }
143
144
145 bool closeLabelTag(odocstream & os, Layout const & lay)
146 {
147         string const tag = lay.htmllabel().empty() 
148                         ? "span" : lay.htmllabel();
149         return html::closeTag(os, tag);
150 }
151
152
153 bool openItemTag(odocstream & os, Layout const & lay)
154 {
155         string const tag = lay.htmlitem().empty() 
156                         ? "div" : lay.htmlitem();
157         string const attr = lay.htmlitemattr().empty()
158                         ? "class=\"" + to_utf8(lay.name()) + "item\"" : lay.htmllabelattr();
159         return html::openTag(os, tag, attr);
160 }
161
162
163 bool closeItemTag(odocstream & os, Layout const & lay)
164 {
165         string const tag = lay.htmlitem().empty() 
166                         ? "div" : lay.htmlitem();
167         return html::closeTag(os, tag);
168 }
169
170 ParagraphList::const_iterator searchParagraphHtml(
171         ParagraphList::const_iterator p,
172   ParagraphList::const_iterator const & pend)
173 {
174         for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
175                 ;
176
177         return p;
178 }
179
180
181 ParagraphList::const_iterator searchEnvironmentHtml(
182                 ParagraphList::const_iterator const pstart,
183                 ParagraphList::const_iterator const & pend)
184 {
185         ParagraphList::const_iterator p = pstart;
186         Layout const & bstyle = p->layout();
187         size_t const depth = p->params().depth();
188         for (++p; p != pend; ++p) {
189                 Layout const & style = p->layout();
190                 // It shouldn't happen that e.g. a section command occurs inside
191                 // a quotation environment, at a higher depth, but as of 6/2009,
192                 // it can happen. We pretend that it's just at lowest depth.
193                 if (style.latextype == LATEX_COMMAND)
194                         return p;
195                 // If depth is down, we're done
196                 if (p->params().depth() < depth)
197                         return p;
198                 // If depth is up, we're not done
199                 if (p->params().depth() > depth)
200                         continue;
201                 // Now we know we are at the same depth
202                 if (style.latextype == LATEX_PARAGRAPH
203                     || style.latexname() != bstyle.latexname())
204                         return p;
205         }
206         return pend;
207 }
208
209
210 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
211                                             odocstream & os,
212                                             OutputParams const & runparams,
213                                             Text const & text,
214                                             ParagraphList::const_iterator const & pbegin,
215                                             ParagraphList::const_iterator const & pend)
216 {
217         ParagraphList::const_iterator const begin = text.paragraphs().begin();
218         ParagraphList::const_iterator par = pbegin;
219         for (; par != pend; ++par) {
220                 Layout const & lay = par->layout();
221                 if (!lay.counter.empty())
222                         buf.params().documentClass().counters().step(lay.counter);
223                 // FIXME We should see if there's a label to be output and
224                 // do something with it.
225                 if (par != pbegin)
226                         os << '\n';
227                 bool const opened = openTag(os, lay);
228                 docstring const deferred = par->simpleLyXHTMLOnePar(buf, os, runparams,
229                                 text.outerFont(distance(begin, par)));
230                 if (opened) {
231                         closeTag(os, lay);
232                         os << '\n';
233                 }
234                 if (!deferred.empty())
235                         os << deferred << '\n';
236         }
237         return pend;
238 }
239
240
241 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
242                                 odocstream & os,
243                                 OutputParams const & runparams,
244                                 Text const & text,
245                                 ParagraphList::const_iterator const & pbegin,
246                                 ParagraphList::const_iterator const & pend) 
247 {
248         os << "<h2 class='bibliography'>" 
249            << pbegin->layout().labelstring(false) 
250            << "</h2>\n"
251            << "<div class='bibliography'>\n";
252                         makeParagraphs(buf, os, runparams, text, pbegin, pend);
253         os << "</div>";
254         return pend;
255 }
256
257
258 namespace {
259         bool isNormalEnv(Layout const & lay)
260         {
261                 return lay.latextype == LATEX_ENVIRONMENT;
262         }
263 }
264
265 ParagraphList::const_iterator makeEnvironmentHtml(Buffer const & buf,
266                                               odocstream & os,
267                                               OutputParams const & runparams,
268                                               Text const & text,
269                                               ParagraphList::const_iterator const & pbegin,
270                                               ParagraphList::const_iterator const & pend) 
271 {
272         ParagraphList::const_iterator const begin = text.paragraphs().begin();
273         ParagraphList::const_iterator par = pbegin;
274         Layout const & bstyle = par->layout();
275         depth_type const origdepth = pbegin->params().depth();
276
277         // Open tag for this environment
278         bool const main_tag_opened = openTag(os, bstyle);
279         os << '\n';
280
281         // we will on occasion need to remember a layout from before.
282         Layout const * lastlay = 0;
283
284         while (par != pend) {
285                 Layout const & style = par->layout();
286                 // the counter only gets stepped if we're in some kind of list,
287                 // or if it's the first time through.
288                 if (!style.counter.empty() && (par == pbegin || !isNormalEnv(style)))
289                         buf.params().documentClass().counters().step(style.counter);
290                 ParagraphList::const_iterator send;
291                 // this will be positive, if we want to skip the initial word
292                 // (if it's been taken for the label).
293                 pos_type sep = 0;
294
295                 switch (style.latextype) {
296                 case LATEX_ENVIRONMENT:
297                 case LATEX_LIST_ENVIRONMENT:
298                 case LATEX_ITEM_ENVIRONMENT: {
299                         // There are two possiblities in this case. 
300                         // One is that we are still in the environment in which we 
301                         // started---which we will be if the depth is the same.
302                         if (par->params().depth() == origdepth) {
303                                 LASSERT(bstyle == style, /* */);
304                                 if (lastlay != 0) {
305                                         closeItemTag(os, *lastlay);
306                                         lastlay = 0;
307                                 }
308                                 bool item_tag_opened = false;
309                                 bool const labelfirst = style.htmllabelfirst();
310                                 if (isNormalEnv(style)) {
311                                         // in this case, we print the label only for the first 
312                                         // paragraph (as in a theorem).
313                                         item_tag_opened = openItemTag(os, style);
314                                         if (par == pbegin) {
315                                                 bool const label_tag_opened = openLabelTag(os, style);
316                                                 os << pbegin->expandLabel(style, buf.params(), false);
317                                                 if (label_tag_opened)
318                                                         closeLabelTag(os, style);
319                                                 os << '\n';
320                                         }
321                                 }       else { // some kind of list
322                                         if (!labelfirst)
323                                                 item_tag_opened = openItemTag(os, style);
324                                         if (style.labeltype == LABEL_MANUAL) {
325                                                 bool const label_tag_opened = openLabelTag(os, style);
326                                                 sep = par->firstWordLyXHTML(os, runparams);
327                                                 if (label_tag_opened)
328                                                         closeLabelTag(os, style);
329                                                 os << '\n';
330                                         }
331                                         else if (style.labeltype != LABEL_NO_LABEL) {
332                                                 bool const label_tag_opened = openLabelTag(os, style);
333                                                 os << par->expandLabel(style, buf.params(), false);
334                                                 if (label_tag_opened)
335                                                         closeLabelTag(os, style);
336                                                 os << '\n';
337                                         }
338                                         if (labelfirst)
339                                                 item_tag_opened = openItemTag(os, style);
340                                         else
341                                                 os << "<span class='item'>";
342                                 }
343                                 par->simpleLyXHTMLOnePar(buf, os, runparams, 
344                                         text.outerFont(distance(begin, par)), sep);
345                                 if (!isNormalEnv(style) && labelfirst)
346                                         os << "</span>";
347                                 ++par;
348                                 if (item_tag_opened) {
349                                         // We may not want to close the tag yet, in particular,
350                                         // if we're not at the end...
351                                         if (par != pend 
352                                     //  and are doing items...
353                                      && style.latextype == LATEX_ITEM_ENVIRONMENT
354                                      // and if the depth has changed...
355                                      && par->params().depth() != origdepth) {
356                                      // then we'll save this layout for later, and close it when
357                                      // we get another item.
358                                                 lastlay = &style;
359                                         } else
360                                                 closeItemTag(os, style);
361                                         os << '\n';
362                                 }
363                         }
364                         // The other possibility is that the depth has increased, in which
365                         // case we need to recurse.
366                         else {
367                                 send = searchEnvironmentHtml(par, pend);
368                                 par = makeEnvironmentHtml(buf, os, runparams, text, par, send);
369                         }
370                         break;
371                 }
372                 case LATEX_PARAGRAPH:
373                         send = searchParagraphHtml(par, pend);
374                         par = makeParagraphs(buf, os, runparams, text, par, send);
375                         break;
376                 // Shouldn't happen
377                 case LATEX_BIB_ENVIRONMENT:
378                         send = par;
379                         ++send;
380                         par = makeParagraphs(buf, os, runparams, text, par, send);
381                         break;
382                 // Shouldn't happen
383                 case LATEX_COMMAND:
384                         ++par;
385                         break;
386                 }
387         }
388
389         if (lastlay != 0)
390                 closeItemTag(os, *lastlay);
391         if (main_tag_opened)
392                 closeTag(os, bstyle);
393         os << '\n';
394         return pend;
395 }
396
397
398 void makeCommand(Buffer const & buf,
399                                           odocstream & os,
400                                           OutputParams const & runparams,
401                                           Text const & text,
402                                           ParagraphList::const_iterator const & pbegin)
403 {
404         Layout const & style = pbegin->layout();
405         if (!style.counter.empty())
406                 buf.params().documentClass().counters().step(style.counter);
407
408         bool const main_tag_opened = openTag(os, style);
409
410         // Label around sectioning number:
411         // FIXME Probably need to account for LABEL_MANUAL
412         if (style.labeltype != LABEL_NO_LABEL) {
413                 bool const label_tag_opened = openLabelTag(os, style);
414                 os << pbegin->expandLabel(style, buf.params(), false);
415                 if (label_tag_opened)
416                         closeLabelTag(os, style);
417                 // Otherwise the label might run together with the text
418                 os << ' ';
419         }
420
421         ParagraphList::const_iterator const begin = text.paragraphs().begin();
422         pbegin->simpleLyXHTMLOnePar(buf, os, runparams,
423                         text.outerFont(distance(begin, pbegin)));
424         if (main_tag_opened)
425                 closeTag(os, style);
426         os << '\n';
427 }
428
429 } // end anonymous namespace
430
431
432 void xhtmlParagraphs(Text const & text,
433                        Buffer const & buf,
434                        odocstream & os,
435                        OutputParams const & runparams)
436 {
437         ParagraphList const & paragraphs = text.paragraphs();
438         ParagraphList::const_iterator par = paragraphs.begin();
439         ParagraphList::const_iterator pend = paragraphs.end();
440
441         while (par != pend) {
442                 Layout const & style = par->layout();
443                 ParagraphList::const_iterator lastpar = par;
444                 ParagraphList::const_iterator send;
445
446                 switch (style.latextype) {
447                 case LATEX_COMMAND: {
448                         // The files with which we are working never have more than
449                         // one paragraph in a command structure.
450                         makeCommand(buf, os, runparams, text, par);
451                         ++par;
452                         break;
453                 }
454                 case LATEX_ENVIRONMENT:
455                 case LATEX_LIST_ENVIRONMENT:
456                 case LATEX_ITEM_ENVIRONMENT: {
457                         send = searchEnvironmentHtml(par, pend);
458                         par = makeEnvironmentHtml(buf, os, runparams, text, par, send);
459                         break;
460                 }
461                 case LATEX_BIB_ENVIRONMENT: {
462                         send = searchEnvironmentHtml(par, pend);
463                         par = makeBibliography(buf, os, runparams, text, par, send);
464                         break;
465                 }
466                 case LATEX_PARAGRAPH:
467                         send = searchParagraphHtml(par, pend);
468                         par = makeParagraphs(buf, os, runparams, text, par, send);
469                         break;
470                 }
471                 // FIXME??
472                 // makeEnvironment may process more than one paragraphs and bypass pend
473                 if (distance(lastpar, par) >= distance(lastpar, pend))
474                         break;
475         }
476 }
477
478
479 } // namespace lyx