]> git.lyx.org Git - lyx.git/blob - src/output_xhtml.cpp
3e212ae9452c911f78fb5b30806b471565f2ba78
[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 "paragraph_funcs.h"
25 #include "ParagraphList.h"
26 #include "ParagraphParameters.h"
27 #include "sgml.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 This needs to be protected somehow.
69 static vector<string> taglist;
70
71 bool openTag(odocstream & os, string const & tag, string const & attr)
72 {
73         if (tag.empty())
74                 return false;
75         // FIXME This is completely primitive. We need something
76         // a lot better.
77         // Now do some checks on nesting of tags.
78         if (tag == "p")
79                 if (find(taglist.begin(), taglist.end(), "p") != taglist.end())
80                         return false;
81         os << from_ascii("<" + tag + (attr.empty() ? "" : " " + attr) + ">");
82         taglist.push_back(tag);
83         return true;
84 }
85
86
87 bool closeTag(odocstream & os, string const & tag)
88 {
89         if (tag.empty())
90                 return false;
91         // FIXME Check for proper nesting
92         if (taglist.empty()){
93                 LYXERR0("Last tag not found when closing `" << tag << "'!");
94                 return false;
95         }
96         string const & lasttag = taglist.back();
97         if (lasttag != tag)  {
98                 LYXERR0("Last tag was `" << lasttag << "' when closing `" << tag << "'!");
99                 return false;
100         }
101         taglist.pop_back();
102         os << from_ascii("</" + tag + ">");
103         return true;
104 }
105
106
107
108 } // html
109
110 namespace {
111
112 bool openTag(odocstream & os, Layout const & lay)
113 {
114         return html::openTag(os, lay.htmltag(), lay.htmlattr());
115 }
116
117
118 bool closeTag(odocstream & os, Layout const & lay)
119 {
120         return html::closeTag(os, lay.htmltag());
121 }
122
123
124 bool openLabelTag(odocstream & os, Layout const & lay)
125 {
126         return html::openTag(os, lay.htmllabel(), lay.htmllabelattr());
127 }
128
129
130 bool closeLabelTag(odocstream & os, Layout const & lay)
131 {
132         return html::closeTag(os, lay.htmllabel());
133 }
134
135
136 bool openItemTag(odocstream & os, Layout const & lay)
137 {
138         return html::openTag(os, lay.htmlitem(), lay.htmlitemattr());
139 }
140
141
142 bool closeItemTag(odocstream & os, Layout const & lay)
143 {
144         return html::closeTag(os, lay.htmlitem());
145 }
146
147 ParagraphList::const_iterator searchParagraph(
148         ParagraphList::const_iterator p,
149   ParagraphList::const_iterator const & pend)
150 {
151         for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
152                 ;
153
154         return p;
155 }
156
157
158 ParagraphList::const_iterator searchEnvironment(
159                 ParagraphList::const_iterator p,
160                 ParagraphList::const_iterator const & pend)
161 {
162         Layout const & bstyle = p->layout();
163         size_t const depth = p->params().depth();
164         for (++p; p != pend; ++p) {
165                 Layout const & style = p->layout();
166                 // It shouldn't happen that e.g. a section command occurs inside
167                 // a quotation environment, at a higher depth, but as of 6/2009,
168                 // it can happen. We pretend that it's just at lowest depth.
169                 if (style.latextype == LATEX_COMMAND)
170                         return p;
171                 // If depth is down, we're done
172                 if (p->params().depth() < depth)
173                         return p;
174                 // If depth is up, we're not done
175                 if (p->params().depth() > depth)
176                         continue;
177                 // Now we know we are at the same depth
178                 if (style.latextype == LATEX_PARAGRAPH
179                     || style.latexname() != bstyle.latexname())
180                         return p;
181
182         }
183         return pend;
184 }
185
186
187 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
188                                             odocstream & os,
189                                             OutputParams const & runparams,
190                                             ParagraphList const & paragraphs,
191                                             ParagraphList::const_iterator const & pbegin,
192                                             ParagraphList::const_iterator const & pend)
193 {
194         ParagraphList::const_iterator par = pbegin;
195         for (; par != pend; ++par) {
196                 Layout const & lay = par->layout();
197                 if (!lay.counter.empty())
198                         buf.params().documentClass().counters().step(lay.counter);
199                 // FIXME We should see if there's a label to be output and
200                 // do something with it.
201                 if (par != pbegin)
202                         os << '\n';
203                 bool const opened = openTag(os, lay);
204                 par->simpleLyXHTMLOnePar(buf, os, runparams, 
205                                 outerFont(distance(paragraphs.begin(), par), paragraphs));
206                 if (opened) {
207                         closeTag(os, lay);
208                         os << '\n';
209                 }
210         }
211         return pend;
212 }
213
214
215 ParagraphList::const_iterator makeEnvironment(Buffer const & buf,
216                                               odocstream & os,
217                                               OutputParams const & runparams,
218                                               ParagraphList const & paragraphs,
219                                               ParagraphList::const_iterator const & pbegin,
220                                               ParagraphList::const_iterator const & pend) 
221 {
222         ParagraphList::const_iterator par = pbegin;
223         Layout const & bstyle = par->layout();
224         depth_type const origdepth = pbegin->params().depth();
225
226         // Open tag for this environment
227         bool const main_tag_opened = openTag(os, bstyle);
228         os << '\n';
229
230         // we will on occasion need to remember a layout from before.
231         Layout const * lastlay = 0;
232
233         while (par != pend) {
234                 Layout const & style = par->layout();
235                 if (!style.counter.empty())
236                         buf.params().documentClass().counters().step(style.counter);
237                 ParagraphList::const_iterator send;
238                 // this will be positive, if we want to skip the initial word
239                 // (if it's been taken for the label).
240                 pos_type sep = 0;
241
242                 switch (style.latextype) {
243                 case LATEX_ENVIRONMENT:
244                 case LATEX_LIST_ENVIRONMENT:
245                 case LATEX_ITEM_ENVIRONMENT: {
246                         // There are two possiblities in this case. 
247                         // One is that we are still in the environment in which we 
248                         // started---which we will be if the depth is the same.
249                         if (par->params().depth() == origdepth) {
250                                 Layout const & cstyle = par->layout();
251                                 if (lastlay != 0) {
252                                         closeItemTag(os, *lastlay);
253                                         lastlay = 0;
254                                 }
255                                 bool const item_tag_opened = openItemTag(os, cstyle);
256                                 if (cstyle.labeltype == LABEL_MANUAL) {
257                                         bool const label_tag_opened = openLabelTag(os, cstyle);
258                                         sep = par->firstWordLyXHTML(os, runparams);
259                                         if (label_tag_opened)
260                                                 closeLabelTag(os, cstyle);
261                                         os << '\n';
262                                 }
263                                 // FIXME Why did I put that first condition??
264                                 else if (style.latextype == LATEX_ENVIRONMENT 
265                                            && style.labeltype != LABEL_NO_LABEL) {
266                                         bool const label_tag_opened = openLabelTag(os, cstyle);
267                                         os << pbegin->expandLabel(style, buf.params(), false);
268                                         if (label_tag_opened)
269                                                 closeLabelTag(os, cstyle);
270                                         os << '\n';
271                                 }
272
273                                 par->simpleLyXHTMLOnePar(buf, os, runparams, 
274                                         outerFont(distance(paragraphs.begin(), par), paragraphs), sep);
275                                 ++par;
276                                 if (item_tag_opened) {
277                                         // We may not want to close the tag yet, in particular,
278                                         // if we're not at the end...
279                                         if (par != pend 
280                                     //  and are doing items...
281                                      && style.latextype == LATEX_ITEM_ENVIRONMENT
282                                      // and if the depth has changed...
283                                      && par->params().depth() != origdepth) {
284                                      // then we'll save this layout for later, and close it when
285                                      // we get another item.
286                                                 lastlay = &cstyle;
287                                         } else {
288                                                 closeItemTag(os, cstyle);
289                                         }
290                                         os << '\n';
291                                 }
292                         }
293                         // The other possibility is that the depth has increased, in which
294                         // case we need to recurse.
295                         else {
296                                 send = searchEnvironment(par, pend);
297                                 par = makeEnvironment(buf, os, runparams, paragraphs, par, send);
298                         }
299                         break;
300                 }
301                 case LATEX_PARAGRAPH:
302                         send = searchParagraph(par, pend);
303                         par = makeParagraphs(buf, os, runparams, paragraphs, par, send);
304                         break;
305                 // FIXME
306                 case LATEX_BIB_ENVIRONMENT:
307                 case LATEX_COMMAND:
308                         ++par;
309                         break;
310                 }
311         }
312
313         if (lastlay != 0)
314                 closeItemTag(os, *lastlay);
315         if (main_tag_opened)
316                 closeTag(os, bstyle);
317         os << '\n';
318         return pend;
319 }
320
321
322 void makeCommand(Buffer const & buf,
323                                           odocstream & os,
324                                           OutputParams const & runparams,
325                                           ParagraphList const & paragraphs,
326                                           ParagraphList::const_iterator const & pbegin)
327 {
328         Layout const & style = pbegin->layout();
329         if (!style.counter.empty())
330                 buf.params().documentClass().counters().step(style.counter);
331
332         bool const main_tag_opened = openTag(os, style);
333
334         // Label around sectioning number:
335         // FIXME Probably need to account for LABEL_MANUAL
336         if (style.labeltype != LABEL_NO_LABEL) {
337                 bool const label_tag_opened = openLabelTag(os, style);
338                 os << pbegin->expandLabel(style, buf.params(), false);
339                 if (label_tag_opened)
340                         closeLabelTag(os, style);
341                 // Otherwise the label might run together with the text
342                 os << ' ';
343         }
344
345         pbegin->simpleLyXHTMLOnePar(buf, os, runparams,
346                         outerFont(distance(paragraphs.begin(), pbegin), paragraphs));
347         if (main_tag_opened)
348                 closeTag(os, style);
349         os << '\n';
350 }
351
352 } // end anonymous namespace
353
354
355 void xhtmlParagraphs(ParagraphList const & paragraphs,
356                        Buffer const & buf,
357                        odocstream & os,
358                        OutputParams const & runparams)
359 {
360         ParagraphList::const_iterator par = paragraphs.begin();
361         ParagraphList::const_iterator pend = paragraphs.end();
362
363         while (par != pend) {
364                 LYXERR0(par->id());
365                 Layout const & style = par->layout();
366                 ParagraphList::const_iterator lastpar = par;
367                 ParagraphList::const_iterator send;
368
369                 switch (style.latextype) {
370                 case LATEX_COMMAND: {
371                         // The files with which we are working never have more than
372                         // one paragraph in a command structure.
373                         makeCommand(buf, os, runparams, paragraphs, par);
374                         ++par;
375                         break;
376                 }
377                 case LATEX_ENVIRONMENT:
378                 case LATEX_LIST_ENVIRONMENT:
379                 case LATEX_ITEM_ENVIRONMENT: {
380                         send = searchEnvironment(par, pend);
381                         par = makeEnvironment(buf, os, runparams, paragraphs, par,send);
382                         break;
383                 }
384                 case LATEX_PARAGRAPH:
385                         send = searchParagraph(par, pend);
386                         par = makeParagraphs(buf, os, runparams, paragraphs, par,send);
387                         break;
388                 case LATEX_BIB_ENVIRONMENT:
389                         // FIXME
390                         ++par;
391                         break;
392                 }
393                 // FIXME??
394                 // makeEnvironment may process more than one paragraphs and bypass pend
395                 if (distance(lastpar, par) >= distance(lastpar, pend))
396                         break;
397         }
398 }
399
400
401 } // namespace lyx