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.
8 * This code is based upon output_docbook.cpp
10 * Full author contact details are available in file CREDITS.
15 #include "output_xhtml.h"
18 #include "buffer_funcs.h"
19 #include "BufferParams.h"
22 #include "OutputParams.h"
23 #include "Paragraph.h"
24 #include "paragraph_funcs.h"
25 #include "ParagraphList.h"
26 #include "ParagraphParameters.h"
28 #include "TextClass.h"
30 #include "support/lassert.h"
31 #include "support/debug.h"
32 #include "support/lstrings.h"
34 #include <boost/next_prior.hpp>
38 using namespace lyx::support;
44 docstring escapeChar(char_type c)
70 static vector<string> taglist;
72 void openTag(odocstream & os, Layout const & lay)
74 string const & tag = lay.htmltag();
77 // Now do some checks on nesting of tags.
79 if (find(taglist.begin(), taglist.end(), "p") != taglist.end())
81 // FIXME More? Better?
82 string attr = lay.htmlattr();
85 os << from_ascii("<" + tag + attr + ">");
86 taglist.push_back(tag);
90 void closeTag(odocstream & os, Layout const & lay)
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.
96 if (find(taglist.begin(), taglist.end(), tag) == taglist.end())
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 << "'!");
109 void openLabelTag(odocstream & os, Layout const & lay)
111 string const & tag = lay.htmllabel();
114 string attr = lay.htmllabelattr();
117 os << from_ascii("<" + tag + attr + ">");
121 void closeLabelTag(odocstream & os, Layout const & lay)
123 string const & tag = lay.htmllabel();
126 os << from_ascii("</" + tag + ">");
130 void openItemTag(odocstream & os, Layout const & lay)
132 string const & tag = lay.htmlitem();
135 string attr = lay.htmlitemattr();
138 os << from_ascii("<" + tag + attr + ">");
142 void closeItemTag(odocstream & os, Layout const & lay)
144 string const & tag = lay.htmlitem();
147 os << from_ascii("</" + tag + ">");
151 ParagraphList::const_iterator searchParagraph(
152 ParagraphList::const_iterator p,
153 ParagraphList::const_iterator const & pend)
155 for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
162 ParagraphList::const_iterator searchEnvironment(
163 ParagraphList::const_iterator p,
164 ParagraphList::const_iterator const & pend)
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)
175 // If depth is down, we're done
176 if (p->params().depth() < depth)
178 // If depth is up, we're not done
179 if (p->params().depth() > depth)
181 // Now we know we are at the same depth
182 if (style.latextype == LATEX_PARAGRAPH
183 || style.latexname() != bstyle.latexname())
191 ParagraphList::const_iterator makeParagraph(Buffer const & buf,
193 OutputParams const & runparams,
194 ParagraphList const & paragraphs,
195 ParagraphList::const_iterator const & pbegin,
196 ParagraphList::const_iterator const & pend)
198 ParagraphList::const_iterator par = pbegin;
199 for (; par != pend; ++par) {
200 Layout const & lay = par->layout();
204 par->simpleLyXHTMLOnePar(buf, os, runparams,
205 outerFont(distance(paragraphs.begin(), par), paragraphs));
212 ParagraphList::const_iterator makeEnvironment(Buffer const & buf,
214 OutputParams const & runparams,
215 ParagraphList const & paragraphs,
216 ParagraphList::const_iterator const & pbegin,
217 ParagraphList::const_iterator const & pend) {
219 ParagraphList::const_iterator par = pbegin;
220 Layout const & bstyle = par->layout();
221 depth_type const origdepth = pbegin->params().depth();
223 // Open tag for this environment
227 // we will on occasion need to remember a layout from before.
228 Layout const * lastlay = 0;
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).
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) {
246 closeItemTag(os, *lastlay);
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);
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);
265 openItemTag(os, cstyle);
266 par->simpleLyXHTMLOnePar(buf, os, runparams,
267 outerFont(distance(paragraphs.begin(), par), paragraphs), sep);
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.
278 closeItemTag(os, cstyle); // FIXME
282 // The other possibility is that the depth has increased, in which
283 // case we need to recurse.
285 send = searchEnvironment(par, pend);
286 par = makeEnvironment(buf, os, runparams, paragraphs, par, send);
290 case LATEX_PARAGRAPH:
291 send = searchParagraph(par, pend);
292 par = makeParagraph(buf, os, runparams, paragraphs, par, send);
295 case LATEX_BIB_ENVIRONMENT:
302 closeItemTag(os, *lastlay);
303 closeTag(os, bstyle);
309 void makeCommand(Buffer const & buf,
311 OutputParams const & runparams,
312 ParagraphList const & paragraphs,
313 ParagraphList::const_iterator const & pbegin)
315 Layout const & style = pbegin->layout();
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
330 pbegin->simpleLyXHTMLOnePar(buf, os, runparams,
331 outerFont(distance(paragraphs.begin(), pbegin), paragraphs));
335 } // end anonymous namespace
338 void xhtmlParagraphs(ParagraphList const & paragraphs,
341 OutputParams const & runparams)
343 ParagraphList::const_iterator par = paragraphs.begin();
344 ParagraphList::const_iterator pend = paragraphs.end();
346 while (par != pend) {
347 Layout const & style = par->layout();
348 ParagraphList::const_iterator lastpar = par;
349 ParagraphList::const_iterator send;
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);
359 case LATEX_ENVIRONMENT:
360 case LATEX_ITEM_ENVIRONMENT: {
361 send = searchEnvironment(par, pend);
362 par = makeEnvironment(buf, os, runparams, paragraphs, par,send);
365 case LATEX_PARAGRAPH:
366 send = searchParagraph(par, pend);
367 par = makeParagraph(buf, os, runparams, paragraphs, par,send);
373 // makeEnvironment may process more than one paragraphs and bypass pend
374 if (distance(lastpar, par) >= distance(lastpar, pend))