]> git.lyx.org Git - lyx.git/blob - src/output_xhtml.cpp
7b6a2dc80a960e1ceceb5443cf17ff101db8d5c6
[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         return html::openTag(os, lay.htmltag(), lay.htmlattr());
120 }
121
122
123 bool closeTag(odocstream & os, Layout const & lay)
124 {
125         return html::closeTag(os, lay.htmltag());
126 }
127
128
129 bool openLabelTag(odocstream & os, Layout const & lay)
130 {
131         return html::openTag(os, lay.htmllabel(), lay.htmllabelattr());
132 }
133
134
135 bool closeLabelTag(odocstream & os, Layout const & lay)
136 {
137         return html::closeTag(os, lay.htmllabel());
138 }
139
140
141 bool openItemTag(odocstream & os, Layout const & lay)
142 {
143         return html::openTag(os, lay.htmlitem(), lay.htmlitemattr());
144 }
145
146
147 bool closeItemTag(odocstream & os, Layout const & lay)
148 {
149         return html::closeTag(os, lay.htmlitem());
150 }
151
152 ParagraphList::const_iterator searchParagraphHtml(
153         ParagraphList::const_iterator p,
154         ParagraphList::const_iterator const & pend)
155 {
156         for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
157                 ;
158
159         return p;
160 }
161
162
163 ParagraphList::const_iterator searchEnvironmentHtml(
164                 ParagraphList::const_iterator const pstart,
165                 ParagraphList::const_iterator const & pend)
166 {
167         ParagraphList::const_iterator p = pstart;
168         Layout const & bstyle = p->layout();
169         size_t const depth = p->params().depth();
170         for (++p; p != pend; ++p) {
171                 Layout const & style = p->layout();
172                 // It shouldn't happen that e.g. a section command occurs inside
173                 // a quotation environment, at a higher depth, but as of 6/2009,
174                 // it can happen. We pretend that it's just at lowest depth.
175                 if (style.latextype == LATEX_COMMAND)
176                         return p;
177                 // If depth is down, we're done
178                 if (p->params().depth() < depth)
179                         return p;
180                 // If depth is up, we're not done
181                 if (p->params().depth() > depth)
182                         continue;
183                 // Now we know we are at the same depth
184                 if (style.latextype == LATEX_PARAGRAPH
185                     || style.latexname() != bstyle.latexname())
186                         return p;
187         }
188         return pend;
189 }
190
191
192 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
193                                             odocstream & os,
194                                             OutputParams const & runparams,
195                                             Text const & text,
196                                             ParagraphList::const_iterator const & pbegin,
197                                             ParagraphList::const_iterator const & pend)
198 {
199         ParagraphList::const_iterator const begin = text.paragraphs().begin();
200         ParagraphList::const_iterator par = pbegin;
201         for (; par != pend; ++par) {
202                 Layout const & lay = par->layout();
203                 if (!lay.counter.empty())
204                         buf.params().documentClass().counters().step(lay.counter);
205                 // FIXME We should see if there's a label to be output and
206                 // do something with it.
207                 if (par != pbegin)
208                         os << '\n';
209                 bool const opened = openTag(os, lay);
210                 docstring const deferred = par->simpleLyXHTMLOnePar(buf, os, runparams,
211                                 text.outerFont(distance(begin, par)));
212                 if (opened) {
213                         closeTag(os, lay);
214                         os << '\n';
215                 }
216                 if (!deferred.empty())
217                         os << deferred << '\n';
218         }
219         return pend;
220 }
221
222
223 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
224                                 odocstream & os,
225                                 OutputParams const & runparams,
226                                 Text const & text,
227                                 ParagraphList::const_iterator const & pbegin,
228                                 ParagraphList::const_iterator const & pend) 
229 {
230         os << "<h2 class='bibliography'>" 
231            << pbegin->layout().labelstring(false) 
232            << "</h2>\n"
233            << "<div class='bibliography'>\n";
234                         makeParagraphs(buf, os, runparams, text, pbegin, pend);
235         os << "</div>";
236         return pend;
237 }
238
239
240 namespace {
241         bool isNormalEnv(Layout const & lay)
242         {
243                 return lay.latextype == LATEX_ENVIRONMENT;
244         }
245 }
246
247 ParagraphList::const_iterator makeEnvironmentHtml(Buffer const & buf,
248                                               odocstream & os,
249                                               OutputParams const & runparams,
250                                               Text const & text,
251                                               ParagraphList::const_iterator const & pbegin,
252                                               ParagraphList::const_iterator const & pend) 
253 {
254         ParagraphList::const_iterator const begin = text.paragraphs().begin();
255         ParagraphList::const_iterator par = pbegin;
256         Layout const & bstyle = par->layout();
257         depth_type const origdepth = pbegin->params().depth();
258
259         // Open tag for this environment
260         bool const main_tag_opened = openTag(os, bstyle);
261         os << '\n';
262
263         // we will on occasion need to remember a layout from before.
264         Layout const * lastlay = 0;
265
266         while (par != pend) {
267                 Layout const & style = par->layout();
268                 // the counter only gets stepped if we're in some kind of list,
269                 // or if it's the first time through.
270                 if (!style.counter.empty() && (par == pbegin || !isNormalEnv(style)))
271                         buf.params().documentClass().counters().step(style.counter);
272                 ParagraphList::const_iterator send;
273                 // this will be positive, if we want to skip the initial word
274                 // (if it's been taken for the label).
275                 pos_type sep = 0;
276
277                 switch (style.latextype) {
278                 case LATEX_ENVIRONMENT:
279                 case LATEX_LIST_ENVIRONMENT:
280                 case LATEX_ITEM_ENVIRONMENT: {
281                         // There are two possiblities in this case. 
282                         // One is that we are still in the environment in which we 
283                         // started---which we will be if the depth is the same.
284                         if (par->params().depth() == origdepth) {
285                                 LASSERT(bstyle == style, /* */);
286                                 if (lastlay != 0) {
287                                         closeItemTag(os, *lastlay);
288                                         lastlay = 0;
289                                 }
290                                 bool item_tag_opened = false;
291                                 bool const labelfirst = style.htmllabelfirst();
292                                 bool madelabel = false;
293                                 if (isNormalEnv(style)) {
294                                         // in this case, we print the label only for the first 
295                                         // paragraph (as in a theorem).
296                                         item_tag_opened = openItemTag(os, style);
297                                         if (par == pbegin && style.htmllabel() != "NONE") {
298                                                 docstring const lbl = 
299                                                                 pbegin->expandLabel(style, buf.params(), false);
300                                                 if (!lbl.empty()) {
301                                                         bool const label_tag_opened = openLabelTag(os, style);
302                                                         os << lbl;
303                                                         if (label_tag_opened)
304                                                                 closeLabelTag(os, style);
305                                                 }
306                                                 os << '\n';
307                                         }
308                                 }       else { // some kind of list
309                                         if (!labelfirst)
310                                                 item_tag_opened = openItemTag(os, style);
311                                         if (style.labeltype == LABEL_MANUAL
312                                             && style.htmllabel() != "NONE") {
313                                                 madelabel = openLabelTag(os, style);
314                                                 sep = par->firstWordLyXHTML(os, runparams);
315                                                 if (madelabel)
316                                                         closeLabelTag(os, style);
317                                                 os << '\n';
318                                         }
319                                         else if (style.labeltype != LABEL_NO_LABEL
320                                                  && style.htmllabel() != "NONE") {
321                                                 madelabel = openLabelTag(os, style);
322                                                 os << par->expandLabel(style, buf.params(), false);
323                                                 if (madelabel)
324                                                         closeLabelTag(os, style);
325                                                 os << '\n';
326                                         }
327                                         if (labelfirst)
328                                                 item_tag_opened = openItemTag(os, style);
329                                         else if (madelabel)
330                                                 os << "<span class='" << style.name() << "inneritem'>";
331                                 }
332                                 par->simpleLyXHTMLOnePar(buf, os, runparams, 
333                                         text.outerFont(distance(begin, par)), sep);
334                                 if (!isNormalEnv(style) && !labelfirst && madelabel)
335                                         os << "</span>";
336                                 ++par;
337                                 if (item_tag_opened) {
338                                         // We may not want to close the tag yet, in particular,
339                                         // if we're not at the end...
340                                         if (par != pend 
341                                     //  and are doing items...
342                                      && style.latextype == LATEX_ITEM_ENVIRONMENT
343                                      // and if the depth has changed...
344                                      && par->params().depth() != origdepth) {
345                                      // then we'll save this layout for later, and close it when
346                                      // we get another item.
347                                                 lastlay = &style;
348                                         } else
349                                                 closeItemTag(os, style);
350                                         os << '\n';
351                                 }
352                         }
353                         // The other possibility is that the depth has increased, in which
354                         // case we need to recurse.
355                         else {
356                                 send = searchEnvironmentHtml(par, pend);
357                                 par = makeEnvironmentHtml(buf, os, runparams, text, par, send);
358                         }
359                         break;
360                 }
361                 case LATEX_PARAGRAPH:
362                         send = searchParagraphHtml(par, pend);
363                         par = makeParagraphs(buf, os, runparams, text, par, send);
364                         break;
365                 // Shouldn't happen
366                 case LATEX_BIB_ENVIRONMENT:
367                         send = par;
368                         ++send;
369                         par = makeParagraphs(buf, os, runparams, text, par, send);
370                         break;
371                 // Shouldn't happen
372                 case LATEX_COMMAND:
373                         ++par;
374                         break;
375                 }
376         }
377
378         if (lastlay != 0)
379                 closeItemTag(os, *lastlay);
380         if (main_tag_opened)
381                 closeTag(os, bstyle);
382         os << '\n';
383         return pend;
384 }
385
386
387 void makeCommand(Buffer const & buf,
388                                           odocstream & os,
389                                           OutputParams const & runparams,
390                                           Text const & text,
391                                           ParagraphList::const_iterator const & pbegin)
392 {
393         Layout const & style = pbegin->layout();
394         if (!style.counter.empty())
395                 buf.params().documentClass().counters().step(style.counter);
396
397         bool const main_tag_opened = openTag(os, style);
398
399         // Label around sectioning number:
400         // FIXME Probably need to account for LABEL_MANUAL
401         if (style.labeltype != LABEL_NO_LABEL) {
402                 bool const label_tag_opened = openLabelTag(os, style);
403                 os << pbegin->expandLabel(style, buf.params(), false);
404                 if (label_tag_opened)
405                         closeLabelTag(os, style);
406                 // Otherwise the label might run together with the text
407                 os << ' ';
408         }
409
410         ParagraphList::const_iterator const begin = text.paragraphs().begin();
411         pbegin->simpleLyXHTMLOnePar(buf, os, runparams,
412                         text.outerFont(distance(begin, pbegin)));
413         if (main_tag_opened)
414                 closeTag(os, style);
415         os << '\n';
416 }
417
418 } // end anonymous namespace
419
420
421 void xhtmlParagraphs(Text const & text,
422                        Buffer const & buf,
423                        odocstream & os,
424                        OutputParams const & runparams)
425 {
426         ParagraphList const & paragraphs = text.paragraphs();
427         ParagraphList::const_iterator par = paragraphs.begin();
428         ParagraphList::const_iterator pend = paragraphs.end();
429
430         while (par != pend) {
431                 Layout const & style = par->layout();
432                 ParagraphList::const_iterator lastpar = par;
433                 ParagraphList::const_iterator send;
434
435                 switch (style.latextype) {
436                 case LATEX_COMMAND: {
437                         // The files with which we are working never have more than
438                         // one paragraph in a command structure.
439                         makeCommand(buf, os, runparams, text, par);
440                         ++par;
441                         break;
442                 }
443                 case LATEX_ENVIRONMENT:
444                 case LATEX_LIST_ENVIRONMENT:
445                 case LATEX_ITEM_ENVIRONMENT: {
446                         send = searchEnvironmentHtml(par, pend);
447                         par = makeEnvironmentHtml(buf, os, runparams, text, par, send);
448                         break;
449                 }
450                 case LATEX_BIB_ENVIRONMENT: {
451                         send = searchEnvironmentHtml(par, pend);
452                         par = makeBibliography(buf, os, runparams, text, par, send);
453                         break;
454                 }
455                 case LATEX_PARAGRAPH:
456                         send = searchParagraphHtml(par, pend);
457                         par = makeParagraphs(buf, os, runparams, text, par, send);
458                         break;
459                 }
460                 // FIXME??
461                 // makeEnvironment may process more than one paragraphs and bypass pend
462                 if (distance(lastpar, par) >= distance(lastpar, pend))
463                         break;
464         }
465 }
466
467
468 } // namespace lyx