]> git.lyx.org Git - lyx.git/blob - src/output_xhtml.cpp
Avoid full metrics computation with Update:FitCursor
[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 Kimberly 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 "BufferParams.h"
19 #include "Counters.h"
20 #include "Font.h"
21 #include "Layout.h"
22 #include "LayoutEnums.h"
23 #include "Paragraph.h"
24 #include "ParagraphList.h"
25 #include "ParagraphParameters.h"
26 #include "xml.h"
27 #include "Text.h"
28 #include "TextClass.h"
29
30 #include "support/lassert.h"
31
32 #include <stack>
33 #include <iostream>
34
35 // Uncomment to activate debugging code.
36 // #define XHTML_DEBUG
37
38 using namespace std;
39 using namespace lyx::support;
40
41 namespace lyx {
42
43
44 docstring fontToHtmlTag(xml::FontTypes type)
45 {
46     switch(type) {
47         case xml::FontTypes::FT_EMPH:
48             return from_utf8("em");
49         case xml::FontTypes::FT_BOLD:
50             return from_utf8("b");
51         case xml::FontTypes::FT_NOUN:
52             return from_utf8("dfn");
53         case xml::FontTypes::FT_UBAR:
54         case xml::FontTypes::FT_WAVE:
55         case xml::FontTypes::FT_DBAR:
56             return from_utf8("u");
57         case xml::FontTypes::FT_SOUT:
58         case xml::FontTypes::FT_XOUT:
59             return from_utf8("del");
60         case xml::FontTypes::FT_ITALIC:
61             return from_utf8("i");
62         case xml::FontTypes::FT_UPRIGHT:
63         case xml::FontTypes::FT_SLANTED:
64         case xml::FontTypes::FT_SMALLCAPS:
65         case xml::FontTypes::FT_ROMAN:
66         case xml::FontTypes::FT_SANS:
67         case xml::FontTypes::FT_TYPE:
68         case xml::FontTypes::FT_SIZE_TINY:
69         case xml::FontTypes::FT_SIZE_SCRIPT:
70         case xml::FontTypes::FT_SIZE_FOOTNOTE:
71         case xml::FontTypes::FT_SIZE_SMALL:
72         case xml::FontTypes::FT_SIZE_NORMAL:
73         case xml::FontTypes::FT_SIZE_LARGE:
74         case xml::FontTypes::FT_SIZE_LARGER:
75         case xml::FontTypes::FT_SIZE_LARGEST:
76         case xml::FontTypes::FT_SIZE_HUGE:
77         case xml::FontTypes::FT_SIZE_HUGER:
78         case xml::FontTypes::FT_SIZE_INCREASE:
79         case xml::FontTypes::FT_SIZE_DECREASE:
80             return from_utf8("span");
81     }
82     // kill warning
83     return docstring();
84 }
85
86
87 docstring fontToHtmlAttribute(xml::FontTypes type)
88 {
89         switch(type) {
90         case xml::FontTypes::FT_EMPH:
91         case xml::FontTypes::FT_BOLD:
92                 return from_ascii("");
93         case xml::FontTypes::FT_NOUN:
94                 return from_ascii("class='lyxnoun'");
95         case xml::FontTypes::FT_UBAR:
96                 return from_ascii("");
97         case xml::FontTypes::FT_DBAR:
98                 return from_ascii("class='dline'");
99         case xml::FontTypes::FT_XOUT:
100         case xml::FontTypes::FT_SOUT:
101                 return from_ascii("class='strikeout'");
102         case xml::FontTypes::FT_WAVE:
103                 return from_ascii("class='wline'");
104         case xml::FontTypes::FT_ITALIC:
105                 return from_ascii("");
106         case xml::FontTypes::FT_UPRIGHT:
107                 return from_ascii("style='font-style:normal;'");
108         case xml::FontTypes::FT_SLANTED:
109                 return from_ascii("style='font-style:oblique;'");
110         case xml::FontTypes::FT_SMALLCAPS:
111                 return from_ascii("style='font-variant:small-caps;'");
112         case xml::FontTypes::FT_ROMAN:
113                 return from_ascii("style='font-family:serif;'");
114         case xml::FontTypes::FT_SANS:
115                 return from_ascii("style='font-family:sans-serif;'");
116         case xml::FontTypes::FT_TYPE:
117                 return from_ascii("style='font-family:monospace;'");
118         case xml::FontTypes::FT_SIZE_TINY:
119         case xml::FontTypes::FT_SIZE_SCRIPT:
120         case xml::FontTypes::FT_SIZE_FOOTNOTE:
121                 return from_ascii("style='font-size:x-small;'");
122         case xml::FontTypes::FT_SIZE_SMALL:
123                 return from_ascii("style='font-size:small;'");
124         case xml::FontTypes::FT_SIZE_NORMAL:
125                 return from_ascii("style='font-size:normal;'");
126         case xml::FontTypes::FT_SIZE_LARGE:
127                 return from_ascii("style='font-size:large;'");
128         case xml::FontTypes::FT_SIZE_LARGER:
129         case xml::FontTypes::FT_SIZE_LARGEST:
130                 return from_ascii("style='font-size:x-large;'");
131         case xml::FontTypes::FT_SIZE_HUGE:
132         case xml::FontTypes::FT_SIZE_HUGER:
133                 return from_ascii("style='font-size:xx-large;'");
134         case xml::FontTypes::FT_SIZE_INCREASE:
135                 return from_ascii("style='font-size:larger;'");
136         case xml::FontTypes::FT_SIZE_DECREASE:
137                 return from_ascii("style='font-size:smaller;'");
138         }
139         // kill warning
140         return from_ascii("");
141 }
142
143
144 xml::FontTag xhtmlStartFontTag(xml::FontTypes type)
145 {
146         return xml::FontTag(fontToHtmlTag(type), fontToHtmlAttribute(type), type);
147 }
148
149
150 xml::EndFontTag xhtmlEndFontTag(xml::FontTypes type)
151 {
152         return xml::EndFontTag(fontToHtmlTag(type), type);
153 }
154
155 namespace {
156
157 // convenience functions
158
159 inline void openParTag(XMLStream & xs, Layout const & lay,
160                        std::string const & parlabel)
161 {
162         string attrs = lay.htmlGetAttrString();
163         if (!parlabel.empty())
164                 attrs += " id='" + parlabel + "'";
165         xs << xml::ParTag(lay.htmltag(), attrs);
166 }
167
168
169 void openParTag(XMLStream & xs, Layout const & lay,
170                 std::string const & cssclass,
171                 std::string const & parlabel) {
172     string attrs = "class='" + cssclass + "'";
173     if (!parlabel.empty())
174         attrs += " id='" + parlabel + "'";
175     xs << xml::ParTag(lay.htmltag(), attrs);
176 }
177
178 void openParTag(XMLStream & xs, Layout const & lay,
179                 ParagraphParameters const & params,
180                 std::string const & parlabel)
181 {
182         // FIXME Are there other things we should handle here?
183         string const align = alignmentToCSS(params.align());
184         if (align.empty()) {
185                 openParTag(xs, lay, parlabel);
186                 return;
187         }
188         string attrs = lay.htmlGetAttrString() + " style='text-align: " + align + ";'";
189         if (!parlabel.empty())
190                 attrs += " id='" + parlabel + "'";
191         xs << xml::ParTag(lay.htmltag(), attrs);
192 }
193
194
195 inline void closeTag(XMLStream & xs, Layout const & lay)
196 {
197         xs << xml::EndTag(lay.htmltag());
198 }
199
200
201 inline void openLabelTag(XMLStream & xs, Layout const & lay)
202 {
203         xs << xml::StartTag(lay.htmllabeltag(), lay.htmllabelattr());
204 }
205
206
207 inline void closeLabelTag(XMLStream & xs, Layout const & lay)
208 {
209         xs << xml::EndTag(lay.htmllabeltag());
210 }
211
212
213 inline void openItemTag(XMLStream & xs, Layout const & lay)
214 {
215         if (lay.htmlitemtag() != "NONE") {
216                 xs << xml::StartTag(lay.htmlitemtag(), lay.htmlitemattr(), true);
217         }
218 }
219
220
221 void openItemTag(XMLStream & xs, Layout const & lay,
222              ParagraphParameters const & params)
223 {
224         if (lay.htmlitemtag() != "NONE") {
225                 // FIXME Are there other things we should handle here?
226                 string const align = alignmentToCSS(params.align());
227                 if (align.empty()) {
228                         openItemTag(xs, lay);
229                         return;
230                 }
231                 string attrs = lay.htmlGetAttrString() + " style='text-align: " + align + ";'";
232                 xs << xml::StartTag(lay.htmlitemtag(), attrs);
233         }
234 }
235
236
237 inline void closeItemTag(XMLStream & xs, Layout const & lay)
238 {
239         if (lay.htmlitemtag() != "NONE") {
240                 xs << xml::EndTag(lay.htmlitemtag());
241         }
242 }
243
244 // end of convenience functions
245
246 ParagraphList::const_iterator findLastParagraph(
247         ParagraphList::const_iterator p,
248         ParagraphList::const_iterator const & pend)
249 {
250         for (++p; p != pend && p->layout().latextype == LATEX_PARAGRAPH; ++p)
251                 ;
252
253         return p;
254 }
255
256
257 ParagraphList::const_iterator findEndOfEnvironment(
258                 ParagraphList::const_iterator const & pstart,
259                 ParagraphList::const_iterator const & pend)
260 {
261         ParagraphList::const_iterator p = pstart;
262         Layout const & bstyle = p->layout();
263         size_t const depth = p->params().depth();
264         for (++p; p != pend; ++p) {
265                 Layout const & style = p->layout();
266                 // It shouldn't happen that e.g. a section command occurs inside
267                 // a quotation environment, at a higher depth, but as of 6/2009,
268                 // it can happen. We pretend that it's just at lowest depth.
269                 if (style.latextype == LATEX_COMMAND)
270                         return p;
271
272                 // If depth is down, we're done
273                 if (p->params().depth() < depth)
274                         return p;
275
276                 // If depth is up, we're not done
277                 if (p->params().depth() > depth)
278                         continue;
279
280                 // FIXME I am not sure about the first check.
281                 // Surely we *could* have different layouts that count as
282                 // LATEX_PARAGRAPH, right?
283                 if (style.latextype == LATEX_PARAGRAPH || style != bstyle)
284                         return p;
285         }
286         return pend;
287 }
288
289
290 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
291                                             XMLStream & xs,
292                                             OutputParams const & runparams,
293                                             Text const & text,
294                                             ParagraphList::const_iterator const & pbegin,
295                                             ParagraphList::const_iterator const & pend)
296 {
297         ParagraphList::const_iterator const begin = text.paragraphs().begin();
298         ParagraphList::const_iterator par = pbegin;
299         for (; par != pend; ++par) {
300                 Layout const & lay = par->layout();
301                 if (!lay.counter.empty())
302                         buf.masterBuffer()->params().
303                             documentClass().counters().step(lay.counter, OutputUpdate);
304
305                 // FIXME We should see if there's a label to be output and
306                 // do something with it.
307                 if (par != pbegin)
308                         xs << xml::CR();
309
310                 // We want to open the paragraph tag if:
311                 //   (i) the current layout permits multiple paragraphs
312                 //  (ii) we are either not already inside a paragraph (HTMLIsBlock) OR
313                 //       we are, but this is not the first paragraph
314                 //
315                 // But there is also a special case, and we first see whether we are in it.
316                 // We do not want to open the paragraph tag if this paragraph contains
317                 // only one item, and that item is "inline", i.e., not HTMLIsBlock (such
318                 // as a branch). On the other hand, if that single item has a font change
319                 // applied to it, then we still do need to open the paragraph.
320                 //
321                 // Obviously, this is very fragile. The main reason we need to do this is
322                 // because of branches, e.g., a branch that contains an entire new section.
323                 // We do not really want to wrap that whole thing in a <div>...</div>.
324                 bool special_case = false;
325                 Inset const * specinset = par->size() == 1 ? par->getInset(0) : nullptr;
326                 if (specinset && !specinset->getLayout().htmlisblock()) {
327                         Layout const & style = par->layout();
328                         FontInfo const first_font = style.labeltype == LABEL_MANUAL ?
329                                                 style.labelfont : style.font;
330                         FontInfo const our_font =
331                                 par->getFont(buf.masterBuffer()->params(), 0,
332                                        text.outerFont(distance(begin, par))).fontInfo();
333                         if (first_font == our_font)
334                                 special_case = true;
335                 }
336
337                 bool const open_par = runparams.html_make_pars
338                         && (!runparams.html_in_par || par != pbegin)
339                         && !special_case;
340
341                 // We want to issue the closing tag if either:
342                 //   (i)  We opened it, and either html_in_par is false,
343                 //        or we're not in the last paragraph, anyway.
344                 //   (ii) We didn't open it and html_in_par is true,
345                 //        but we are in the first par, and there is a next par.
346                 ParagraphList::const_iterator nextpar = par;
347                 ++nextpar;
348                 bool const close_par =
349                         (open_par && (!runparams.html_in_par || nextpar != pend))
350                         || (!open_par && runparams.html_in_par && par == pbegin && nextpar != pend);
351
352                 if (open_par) {
353                         // We do not issue the paragraph id if we are doing
354                         // this for the TOC (or some similar purpose)
355                         openParTag(xs, lay, par->params(),
356                                    runparams.for_toc ? "" : par->magicLabel());
357                 }
358
359                 docstring const deferred = par->simpleLyXHTMLOnePar(buf, xs,
360                         runparams, text.outerFont(distance(begin, par)),
361                         open_par, close_par);
362
363                 if (close_par) {
364                         closeTag(xs, lay);
365                         xs << xml::CR();
366                 }
367
368                 if (!deferred.empty()) {
369                         xs << XMLStream::ESCAPE_NONE << deferred << xml::CR();
370                 }
371         }
372         return pend;
373 }
374
375
376 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
377                                 XMLStream & xs,
378                                 OutputParams const & runparams,
379                                 Text const & text,
380                                 ParagraphList::const_iterator const & pbegin,
381                                 ParagraphList::const_iterator const & pend)
382 {
383         // FIXME XHTML
384         // Use TextClass::htmlTOCLayout() to figure out how we should look.
385         xs << xml::StartTag("h2", "class='bibliography'")
386            << pbegin->layout().labelstring(false)
387            << xml::EndTag("h2")
388            << xml::CR()
389            << xml::StartTag("div", "class='bibliography'")
390            << xml::CR();
391         makeParagraphs(buf, xs, runparams, text, pbegin, pend);
392         xs << xml::EndTag("div");
393         return pend;
394 }
395
396
397 bool isNormalEnv(Layout const & lay)
398 {
399         return lay.latextype == LATEX_ENVIRONMENT
400             || lay.latextype == LATEX_BIB_ENVIRONMENT;
401 }
402
403
404 ParagraphList::const_iterator makeEnvironment(Buffer const & buf,
405                                               XMLStream & xs,
406                                               OutputParams const & runparams,
407                                               Text const & text,
408                                               ParagraphList::const_iterator const & pbegin,
409                                               ParagraphList::const_iterator const & pend)
410 {
411         ParagraphList::const_iterator const begin = text.paragraphs().begin();
412         ParagraphList::const_iterator par = pbegin;
413         Layout const & bstyle = par->layout();
414         depth_type const origdepth = pbegin->params().depth();
415
416         // open tag for this environment
417         if ((bstyle.labeltype == LABEL_ENUMERATE || bstyle.labeltype == LABEL_ITEMIZE)
418                 && bstyle.htmlclass().empty()) {
419                 // In this case, we have to calculate the CSS class ourselves, each time
420                 // through
421                 // FIXME We assume in these cases that the standard counters are being used.
422                 // (We also do not deal with 'resume' counters, though I'm not sure that can
423                 // be done at all in HTML.)
424
425                 // Code adapated from Buffer::Impl::setLabel
426                 bool const isenum = bstyle.labeltype == LABEL_ENUMERATE;
427                 docstring enumcounter = bstyle.counter.empty() ?
428                                         ( isenum ? from_ascii("enum") : from_ascii("lyxitem") ) :
429                                         bstyle.counter;
430                 switch (par->itemdepth) {
431                 case 2:
432                         enumcounter += 'i';
433                         // fall through
434                 case 1:
435                         enumcounter += 'i';
436                         // fall through
437                 case 0:
438                         enumcounter += 'i';
439                         break;
440                 case 3:
441                         enumcounter += "iv";
442                         break;
443                 default:
444                         // not a valid enumdepth...
445                         break;
446                 }
447                 openParTag(xs, bstyle,
448                                    string( isenum ? "lyxenum" : "lyxitem" ) + " "
449                                         + to_utf8(enumcounter), pbegin->magicLabel());
450         }
451         else
452                 openParTag(xs, bstyle, pbegin->magicLabel());
453         xs << xml::CR();
454
455         // we will on occasion need to remember a layout from before.
456         Layout const * lastlay = nullptr;
457
458         while (par != pend) {
459                 Layout const & style = par->layout();
460                 // the counter only gets stepped if we're in some kind of list,
461                 // or if it's the first time through.
462                 // note that enum, etc, are handled automatically.
463                 // FIXME There may be a bug here about user defined enumeration
464                 // types. If so, then we'll need to take the counter and add "i",
465                 // "ii", etc, as with enum.
466                 Counters & cnts = buf.masterBuffer()->params().documentClass().counters();
467                 docstring const & cntr = style.counter;
468                 if (!style.counter.empty()
469                     && (par == pbegin || !isNormalEnv(style))
470                                 && cnts.hasCounter(cntr)
471                         )
472                         cnts.step(cntr, OutputUpdate);
473                 ParagraphList::const_iterator send;
474
475                 switch (style.latextype) {
476                 case LATEX_ENVIRONMENT:
477                 case LATEX_LIST_ENVIRONMENT:
478                 case LATEX_ITEM_ENVIRONMENT: {
479                         // There are two possibilities in this case.
480                         // One is that we are still in the environment in which we
481                         // started---which we will be if the depth is the same.
482                         if (par->params().depth() == origdepth) {
483                                 LATTEST(bstyle == style);
484                                 if (lastlay != nullptr) {
485                                         closeItemTag(xs, *lastlay);
486                                         lastlay = nullptr;
487                                 }
488
489                                 // this will be positive, if we want to skip the
490                                 // initial word (if it's been taken for the label).
491                                 pos_type sep = 0;
492                                 bool const labelfirst = style.htmllabelfirst();
493                                 if (!labelfirst)
494                                         openItemTag(xs, style, par->params());
495
496                                 // label output
497                                 if (style.labeltype != LABEL_NO_LABEL &&
498                                     style.htmllabeltag() != "NONE") {
499                                         if (isNormalEnv(style)) {
500                                                 // in this case, we print the label only for the first
501                                                 // paragraph (as in a theorem).
502                                                 if (par == pbegin) {
503                                                         docstring const lbl =
504                                                                         pbegin->params().labelString();
505                                                         if (!lbl.empty()) {
506                                                                 openLabelTag(xs, style);
507                                                                 xs << lbl;
508                                                                 closeLabelTag(xs, style);
509                                                         }
510                                                         xs << xml::CR();
511                                                 }
512                                         } else { // some kind of list
513                                                 if (style.labeltype == LABEL_MANUAL) {
514                                                         openLabelTag(xs, style);
515                                                         sep = par->firstWordLyXHTML(xs, runparams);
516                                                         closeLabelTag(xs, style);
517                                                         xs << xml::CR();
518                                                 }
519                                                 else {
520                                                         docstring const & ls = par->params().labelString();
521                                                         if (!ls.empty()) {
522                                                                 openLabelTag(xs, style);
523                                                                 xs << ls;
524                                                                 closeLabelTag(xs, style);
525                                                                 xs << xml::CR();
526                                                         }
527                                                 }
528                                         }
529                                 } // end label output
530
531                                 if (labelfirst)
532                                         openItemTag(xs, style, par->params());
533
534                                 docstring deferred = par->simpleLyXHTMLOnePar(buf, xs, runparams,
535                                         text.outerFont(distance(begin, par)), true, true, sep);
536                                 xs << XMLStream::ESCAPE_NONE << deferred;
537                                 ++par;
538
539                                 // We may not want to close the tag yet, in particular:
540                                 // If we're not at the end...
541                                 if (par != pend
542                                         //  and are doing items...
543                                          && !isNormalEnv(style)
544                                          // and if the depth has changed...
545                                          && par->params().depth() != origdepth) {
546                                          // then we'll save this layout for later, and close it when
547                                          // we get another item.
548                                         lastlay = &style;
549                                 } else
550                                         closeItemTag(xs, style);
551                                 xs << xml::CR();
552                         }
553                         // The other possibility is that the depth has increased, in which
554                         // case we need to recurse.
555                         else {
556                                 send = findEndOfEnvironment(par, pend);
557                                 par = makeEnvironment(buf, xs, runparams, text, par, send);
558                         }
559                         break;
560                 }
561                 case LATEX_PARAGRAPH:
562                         send = findLastParagraph(par, pend);
563                         par = makeParagraphs(buf, xs, runparams, text, par, send);
564                         break;
565                 // Shouldn't happen
566                 case LATEX_BIB_ENVIRONMENT:
567                         send = par;
568                         ++send;
569                         par = makeParagraphs(buf, xs, runparams, text, par, send);
570                         break;
571                 // Shouldn't happen
572                 case LATEX_COMMAND:
573                         ++par;
574                         break;
575                 }
576         }
577
578         if (lastlay != nullptr)
579                 closeItemTag(xs, *lastlay);
580         closeTag(xs, bstyle);
581         xs << xml::CR();
582         return pend;
583 }
584
585
586 void makeCommand(Buffer const & buf,
587                  XMLStream & xs,
588                  OutputParams const & runparams,
589                  Text const & text,
590                  ParagraphList::const_iterator const & pbegin)
591 {
592         Layout const & style = pbegin->layout();
593         if (!style.counter.empty())
594                 buf.masterBuffer()->params().
595                     documentClass().counters().step(style.counter, OutputUpdate);
596
597         bool const make_parid = !runparams.for_toc && runparams.html_make_pars;
598
599         openParTag(xs, style, pbegin->params(),
600                    make_parid ? pbegin->magicLabel() : "");
601
602         // Label around sectioning number:
603         // FIXME Probably need to account for LABEL_MANUAL
604         // FIXME Probably also need now to account for labels ABOVE and CENTERED.
605         if (style.labeltype != LABEL_NO_LABEL) {
606                 openLabelTag(xs, style);
607                 xs << pbegin->params().labelString();
608                 closeLabelTag(xs, style);
609                 // Otherwise the label might run together with the text
610                 xs << from_ascii(" ");
611         }
612
613         ParagraphList::const_iterator const begin = text.paragraphs().begin();
614         pbegin->simpleLyXHTMLOnePar(buf, xs, runparams,
615                         text.outerFont(distance(begin, pbegin)));
616         closeTag(xs, style);
617         xs << xml::CR();
618 }
619
620 } // end anonymous namespace
621
622
623 void xhtmlParagraphs(Text const & text,
624                        Buffer const & buf,
625                        XMLStream & xs,
626                        OutputParams const & runparams)
627 {
628         ParagraphList const & paragraphs = text.paragraphs();
629         if (runparams.par_begin == runparams.par_end) {
630                 runparams.par_begin = 0;
631                 runparams.par_end = paragraphs.size();
632         }
633         pit_type bpit = runparams.par_begin;
634         pit_type const epit = runparams.par_end;
635         LASSERT(bpit < epit,
636                 { xs << XMLStream::ESCAPE_NONE << "<!-- XHTML output error! -->\n"; return; });
637
638         OutputParams ourparams = runparams;
639         ParagraphList::const_iterator const pend =
640                 (epit == (int) paragraphs.size()) ?
641                         paragraphs.end() : paragraphs.iterator_at(epit);
642         std::stack<int> headerLevels;
643
644         while (bpit < epit) {
645                 ParagraphList::const_iterator par = paragraphs.iterator_at(bpit);
646                 if (par->params().startOfAppendix()) {
647                         // We want to reset the counter corresponding to toplevel sectioning
648                         Layout const & lay =
649                                 buf.masterBuffer()->params().documentClass().getTOCLayout();
650                         docstring const cnt = lay.counter;
651                         if (!cnt.empty()) {
652                                 Counters & cnts =
653                                         buf.masterBuffer()->params().documentClass().counters();
654                                 cnts.reset(cnt);
655                         }
656                 }
657                 Layout const & style = par->layout();
658                 ParagraphList::const_iterator const lastpar = par;
659                 ParagraphList::const_iterator send;
660
661                 // Think about adding <section> and/or </section>s.
662                 // Document title is not in Sectioning, but rather in FrontMatter, so that it does not need to be taken
663                 // into account.
664                 if (style.category() == from_utf8("Sectioning")) {
665                         int level = style.toclevel;
666
667                         // Need to close a previous section if it has the same level or a higher one (close <section> if opening a
668                         // <h2> after a <h2>, <h3>, <h4>, <h5> or <h6>). More examples:
669                         //   - current: h2; back: h1; do not close any <section>
670                         //   - current: h1; back: h2; close two <section> (first the <h2>, then the <h1>, so a new <h1> can come)
671                         while (!headerLevels.empty() && level <= headerLevels.top()) {
672                                 // Output the tag only if it corresponds to a legit section.
673                                 int stackLevel = headerLevels.top();
674                                 if (stackLevel != Layout::NOT_IN_TOC) {
675                                         xs << xml::EndTag("section");
676                                         xs << xml::CR();
677                                 }
678                                 headerLevels.pop();
679                         }
680
681                         // Open the new section: first push it onto the stack, then output it in XHTML.
682                         headerLevels.push(level);
683                         // Some sectioning-like elements should not be output (such as FrontMatter).
684                         if (level != Layout::NOT_IN_TOC ) {
685                                 xs << xml::StartTag("section");
686                                 xs << xml::CR();
687                         }
688                 }
689
690                 switch (style.latextype) {
691                 case LATEX_COMMAND: {
692                         // The files with which we are working never have more than
693                         // one paragraph in a command structure.
694                         // FIXME
695                         // if (ourparams.html_in_par)
696                         //   fix it so we don't get sections inside standard, e.g.
697                         // note that we may then need to make runparams not const, so we
698                         // can communicate that back.
699                         // FIXME Maybe this fix should be in the routines themselves, in case
700                         // they are called from elsewhere.
701                         makeCommand(buf, xs, ourparams, text, par);
702                         ++par;
703                         break;
704                 }
705                 case LATEX_ENVIRONMENT:
706                 case LATEX_LIST_ENVIRONMENT:
707                 case LATEX_ITEM_ENVIRONMENT: {
708                         // FIXME Same fix here.
709                         send = findEndOfEnvironment(par, pend);
710                         par = makeEnvironment(buf, xs, ourparams, text, par, send);
711                         break;
712                 }
713                 case LATEX_BIB_ENVIRONMENT: {
714                         // FIXME Same fix here.
715                         send = findEndOfEnvironment(par, pend);
716                         par = makeBibliography(buf, xs, ourparams, text, par, send);
717                         break;
718                 }
719                 case LATEX_PARAGRAPH:
720                         send = findLastParagraph(par, pend);
721                         par = makeParagraphs(buf, xs, ourparams, text, par, send);
722                         break;
723                 }
724                 bpit += distance(lastpar, par);
725         }
726
727         // If need be, close <section>s, but only at the end of the document (otherwise, dealt with at the beginning
728         // of the loop).
729         while (!headerLevels.empty() && headerLevels.top() != Layout::NOT_IN_TOC) {
730                 headerLevels.pop();
731                 xs << xml::EndTag("section");
732                 xs << xml::CR();
733         }
734 }
735
736
737 string alignmentToCSS(LyXAlignment align)
738 {
739         switch (align) {
740         case LYX_ALIGN_BLOCK:
741                 // we are NOT going to use text-align: justify!!
742         case LYX_ALIGN_LEFT:
743                 return "left";
744         case LYX_ALIGN_RIGHT:
745                 return "right";
746         case LYX_ALIGN_CENTER:
747                 return "center";
748         default:
749                 break;
750         }
751         return "";
752 }
753
754 } // namespace lyx