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