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