]> git.lyx.org Git - lyx.git/blob - src/output_xhtml.cpp
a16f8ec7f983af869277dad6f4c81d75f86c595b
[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 p,
165                 ParagraphList::const_iterator const & pend)
166 {
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         }
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 ParagraphList::const_iterator makeEnvironmentHtml(Buffer const & buf,
241                                               odocstream & os,
242                                               OutputParams const & runparams,
243                                               Text const & text,
244                                               ParagraphList::const_iterator const & pbegin,
245                                               ParagraphList::const_iterator const & pend) 
246 {
247         ParagraphList::const_iterator const begin = text.paragraphs().begin();
248         ParagraphList::const_iterator par = pbegin;
249         Layout const & bstyle = par->layout();
250         depth_type const origdepth = pbegin->params().depth();
251
252         // Open tag for this environment
253         bool const main_tag_opened = openTag(os, bstyle);
254         os << '\n';
255
256         // we will on occasion need to remember a layout from before.
257         Layout const * lastlay = 0;
258
259         while (par != pend) {
260                 Layout const & style = par->layout();
261                 if (!style.counter.empty())
262                         buf.params().documentClass().counters().step(style.counter);
263                 ParagraphList::const_iterator send;
264                 // this will be positive, if we want to skip the initial word
265                 // (if it's been taken for the label).
266                 pos_type sep = 0;
267
268                 switch (style.latextype) {
269                 case LATEX_ENVIRONMENT:
270                 case LATEX_LIST_ENVIRONMENT:
271                 case LATEX_ITEM_ENVIRONMENT: {
272                         // There are two possiblities in this case. 
273                         // One is that we are still in the environment in which we 
274                         // started---which we will be if the depth is the same.
275                         if (par->params().depth() == origdepth) {
276                                 Layout const & cstyle = par->layout();
277                                 if (lastlay != 0) {
278                                         closeItemTag(os, *lastlay);
279                                         lastlay = 0;
280                                 }
281                                 bool const labelfirst = cstyle.htmllabelfirst();
282                                 bool item_tag_opened = false;
283                                 if (!labelfirst)
284                                         item_tag_opened = openItemTag(os, cstyle);
285                                 if (cstyle.labeltype == LABEL_MANUAL) {
286                                         bool const label_tag_opened = openLabelTag(os, cstyle);
287                                         sep = par->firstWordLyXHTML(os, runparams);
288                                         if (label_tag_opened)
289                                                 closeLabelTag(os, cstyle);
290                                         os << '\n';
291                                 }
292                                 // FIXME Why did I put that first condition??
293                                 else if (style.latextype == LATEX_ENVIRONMENT 
294                                            && style.labeltype != LABEL_NO_LABEL) {
295                                         bool const label_tag_opened = openLabelTag(os, cstyle);
296                                         os << pbegin->expandLabel(style, buf.params(), false);
297                                         if (label_tag_opened)
298                                                 closeLabelTag(os, cstyle);
299                                         os << '\n';
300                                 }
301                                 if (labelfirst)
302                                         item_tag_opened = openItemTag(os, cstyle);
303                                 else
304                                         os << "<span class='item'>";
305                                 par->simpleLyXHTMLOnePar(buf, os, runparams, 
306                                         text.outerFont(distance(begin, par)), sep);
307                                 if (!labelfirst)
308                                         os << "</span>";
309                                 ++par;
310                                 if (item_tag_opened) {
311                                         // We may not want to close the tag yet, in particular,
312                                         // if we're not at the end...
313                                         if (par != pend 
314                                     //  and are doing items...
315                                      && style.latextype == LATEX_ITEM_ENVIRONMENT
316                                      // and if the depth has changed...
317                                      && par->params().depth() != origdepth) {
318                                      // then we'll save this layout for later, and close it when
319                                      // we get another item.
320                                                 lastlay = &cstyle;
321                                         } else {
322                                                 closeItemTag(os, cstyle);
323                                         }
324                                         os << '\n';
325                                 }
326                         }
327                         // The other possibility is that the depth has increased, in which
328                         // case we need to recurse.
329                         else {
330                                 send = searchEnvironmentHtml(par, pend);
331                                 par = makeEnvironmentHtml(buf, os, runparams, text, par, send);
332                         }
333                         break;
334                 }
335                 case LATEX_PARAGRAPH:
336                         send = searchParagraphHtml(par, pend);
337                         par = makeParagraphs(buf, os, runparams, text, par, send);
338                         break;
339                 // Shouldn't happen
340                 case LATEX_BIB_ENVIRONMENT:
341                         send = par;
342                         ++send;
343                         par = makeParagraphs(buf, os, runparams, text, par, send);
344                         break;
345                 // Shouldn't happen
346                 case LATEX_COMMAND:
347                         ++par;
348                         break;
349                 }
350         }
351
352         if (lastlay != 0)
353                 closeItemTag(os, *lastlay);
354         if (main_tag_opened)
355                 closeTag(os, bstyle);
356         os << '\n';
357         return pend;
358 }
359
360
361 void makeCommand(Buffer const & buf,
362                                           odocstream & os,
363                                           OutputParams const & runparams,
364                                           Text const & text,
365                                           ParagraphList::const_iterator const & pbegin)
366 {
367         Layout const & style = pbegin->layout();
368         if (!style.counter.empty())
369                 buf.params().documentClass().counters().step(style.counter);
370
371         bool const main_tag_opened = openTag(os, style);
372
373         // Label around sectioning number:
374         // FIXME Probably need to account for LABEL_MANUAL
375         if (style.labeltype != LABEL_NO_LABEL) {
376                 bool const label_tag_opened = openLabelTag(os, style);
377                 os << pbegin->expandLabel(style, buf.params(), false);
378                 if (label_tag_opened)
379                         closeLabelTag(os, style);
380                 // Otherwise the label might run together with the text
381                 os << ' ';
382         }
383
384         ParagraphList::const_iterator const begin = text.paragraphs().begin();
385         pbegin->simpleLyXHTMLOnePar(buf, os, runparams,
386                         text.outerFont(distance(begin, pbegin)));
387         if (main_tag_opened)
388                 closeTag(os, style);
389         os << '\n';
390 }
391
392 } // end anonymous namespace
393
394
395 void xhtmlParagraphs(Text const & text,
396                        Buffer const & buf,
397                        odocstream & os,
398                        OutputParams const & runparams)
399 {
400         ParagraphList const & paragraphs = text.paragraphs();
401         ParagraphList::const_iterator par = paragraphs.begin();
402         ParagraphList::const_iterator pend = paragraphs.end();
403
404         while (par != pend) {
405                 Layout const & style = par->layout();
406                 ParagraphList::const_iterator lastpar = par;
407                 ParagraphList::const_iterator send;
408
409                 switch (style.latextype) {
410                 case LATEX_COMMAND: {
411                         // The files with which we are working never have more than
412                         // one paragraph in a command structure.
413                         makeCommand(buf, os, runparams, text, par);
414                         ++par;
415                         break;
416                 }
417                 case LATEX_ENVIRONMENT:
418                 case LATEX_LIST_ENVIRONMENT:
419                 case LATEX_ITEM_ENVIRONMENT: {
420                         send = searchEnvironmentHtml(par, pend);
421                         par = makeEnvironmentHtml(buf, os, runparams, text, par, send);
422                         break;
423                 }
424                 case LATEX_BIB_ENVIRONMENT: {
425                         send = searchEnvironmentHtml(par, pend);
426                         par = makeBibliography(buf, os, runparams, text, par, send);
427                         break;
428                 }
429                 case LATEX_PARAGRAPH:
430                         send = searchParagraphHtml(par, pend);
431                         par = makeParagraphs(buf, os, runparams, text, par, send);
432                         break;
433                 }
434                 // FIXME??
435                 // makeEnvironment may process more than one paragraphs and bypass pend
436                 if (distance(lastpar, par) >= distance(lastpar, pend))
437                         break;
438         }
439 }
440
441
442 } // namespace lyx