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