]> git.lyx.org Git - lyx.git/blobdiff - src/output_xhtml.cpp
Don't wrap empty labels.
[lyx.git] / src / output_xhtml.cpp
index 1c61421c7fc3356cb8038ec282810307ebf8d056..90204b2e82046327a83fea41a05c558812dc58bf 100644 (file)
 #include "Layout.h"
 #include "OutputParams.h"
 #include "Paragraph.h"
-#include "paragraph_funcs.h"
 #include "ParagraphList.h"
 #include "ParagraphParameters.h"
 #include "sgml.h"
+#include "Text.h"
 #include "TextClass.h"
 
 #include "support/lassert.h"
@@ -116,40 +116,58 @@ namespace {
 
 bool openTag(odocstream & os, Layout const & lay)
 {
-       return html::openTag(os, lay.htmltag(), lay.htmlattr());
+       string const tag = lay.htmltag().empty() 
+                       ? "div" : lay.htmltag();
+       string const attr = lay.htmlattr().empty()
+                       ? "class=\"" + to_utf8(lay.name()) + "\"" : lay.htmlattr();
+       return html::openTag(os, tag, attr);
 }
 
 
 bool closeTag(odocstream & os, Layout const & lay)
 {
-       return html::closeTag(os, lay.htmltag());
+       string const tag = lay.htmltag().empty() 
+                       ? "div" : lay.htmltag();
+       return html::closeTag(os, tag);
 }
 
 
 bool openLabelTag(odocstream & os, Layout const & lay)
 {
-       return html::openTag(os, lay.htmllabel(), lay.htmllabelattr());
+       string const tag = lay.htmllabel().empty() 
+                       ? "span" : lay.htmllabel();
+       string const attr = lay.htmllabelattr().empty()
+                       ? "class=\"" + to_utf8(lay.name()) + "label\"" : lay.htmllabelattr();
+       return html::openTag(os, tag, attr);
 }
 
 
 bool closeLabelTag(odocstream & os, Layout const & lay)
 {
-       return html::closeTag(os, lay.htmllabel());
+       string const tag = lay.htmllabel().empty() 
+                       ? "span" : lay.htmllabel();
+       return html::closeTag(os, tag);
 }
 
 
 bool openItemTag(odocstream & os, Layout const & lay)
 {
-       return html::openTag(os, lay.htmlitem(), lay.htmlitemattr());
+       string const tag = lay.htmlitem().empty() 
+                       ? "div" : lay.htmlitem();
+       string const attr = lay.htmlitemattr().empty()
+                       ? "class=\"" + to_utf8(lay.name()) + "item\"" : lay.htmllabelattr();
+       return html::openTag(os, tag, attr);
 }
 
 
 bool closeItemTag(odocstream & os, Layout const & lay)
 {
-       return html::closeTag(os, lay.htmlitem());
+       string const tag = lay.htmlitem().empty() 
+                       ? "div" : lay.htmlitem();
+       return html::closeTag(os, tag);
 }
 
-ParagraphList::const_iterator searchParagraph(
+ParagraphList::const_iterator searchParagraphHtml(
        ParagraphList::const_iterator p,
   ParagraphList::const_iterator const & pend)
 {
@@ -160,10 +178,11 @@ ParagraphList::const_iterator searchParagraph(
 }
 
 
-ParagraphList::const_iterator searchEnvironment(
-               ParagraphList::const_iterator p,
+ParagraphList::const_iterator searchEnvironmentHtml(
+               ParagraphList::const_iterator const pstart,
                ParagraphList::const_iterator const & pend)
 {
+       ParagraphList::const_iterator p = pstart;
        Layout const & bstyle = p->layout();
        size_t const depth = p->params().depth();
        for (++p; p != pend; ++p) {
@@ -183,7 +202,6 @@ ParagraphList::const_iterator searchEnvironment(
                if (style.latextype == LATEX_PARAGRAPH
                    || style.latexname() != bstyle.latexname())
                        return p;
-
        }
        return pend;
 }
@@ -192,10 +210,11 @@ ParagraphList::const_iterator searchEnvironment(
 ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
                                            odocstream & os,
                                            OutputParams const & runparams,
-                                           ParagraphList const & paragraphs,
+                                           Text const & text,
                                            ParagraphList::const_iterator const & pbegin,
                                            ParagraphList::const_iterator const & pend)
 {
+       ParagraphList::const_iterator const begin = text.paragraphs().begin();
        ParagraphList::const_iterator par = pbegin;
        for (; par != pend; ++par) {
                Layout const & lay = par->layout();
@@ -207,7 +226,7 @@ ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
                        os << '\n';
                bool const opened = openTag(os, lay);
                docstring const deferred = par->simpleLyXHTMLOnePar(buf, os, runparams,
-                               outerFont(distance(paragraphs.begin(), par), paragraphs));
+                               text.outerFont(distance(begin, par)));
                if (opened) {
                        closeTag(os, lay);
                        os << '\n';
@@ -222,7 +241,7 @@ ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
 ParagraphList::const_iterator makeBibliography(Buffer const & buf,
                                odocstream & os,
                                OutputParams const & runparams,
-                               ParagraphList const & paragraphs,
+                               Text const & text,
                                ParagraphList::const_iterator const & pbegin,
                                ParagraphList::const_iterator const & pend) 
 {
@@ -230,19 +249,27 @@ ParagraphList::const_iterator makeBibliography(Buffer const & buf,
           << pbegin->layout().labelstring(false) 
           << "</h2>\n"
           << "<div class='bibliography'>\n";
-                       makeParagraphs(buf, os, runparams, paragraphs, pbegin, pend);
+                       makeParagraphs(buf, os, runparams, text, pbegin, pend);
        os << "</div>";
        return pend;
 }
 
 
-ParagraphList::const_iterator makeEnvironment(Buffer const & buf,
+namespace {
+       bool isNormalEnv(Layout const & lay)
+       {
+               return lay.latextype == LATEX_ENVIRONMENT;
+       }
+}
+
+ParagraphList::const_iterator makeEnvironmentHtml(Buffer const & buf,
                                              odocstream & os,
                                              OutputParams const & runparams,
-                                             ParagraphList const & paragraphs,
+                                             Text const & text,
                                              ParagraphList::const_iterator const & pbegin,
                                              ParagraphList::const_iterator const & pend) 
 {
+       ParagraphList::const_iterator const begin = text.paragraphs().begin();
        ParagraphList::const_iterator par = pbegin;
        Layout const & bstyle = par->layout();
        depth_type const origdepth = pbegin->params().depth();
@@ -256,7 +283,9 @@ ParagraphList::const_iterator makeEnvironment(Buffer const & buf,
 
        while (par != pend) {
                Layout const & style = par->layout();
-               if (!style.counter.empty())
+               // the counter only gets stepped if we're in some kind of list,
+               // or if it's the first time through.
+               if (!style.counter.empty() && (par == pbegin || !isNormalEnv(style)))
                        buf.params().documentClass().counters().step(style.counter);
                ParagraphList::const_iterator send;
                // this will be positive, if we want to skip the initial word
@@ -271,38 +300,53 @@ ParagraphList::const_iterator makeEnvironment(Buffer const & buf,
                        // One is that we are still in the environment in which we 
                        // started---which we will be if the depth is the same.
                        if (par->params().depth() == origdepth) {
-                               Layout const & cstyle = par->layout();
+                               LASSERT(bstyle == style, /* */);
                                if (lastlay != 0) {
                                        closeItemTag(os, *lastlay);
                                        lastlay = 0;
                                }
-                               bool const labelfirst = cstyle.htmllabelfirst();
                                bool item_tag_opened = false;
-                               if (!labelfirst)
-                                       item_tag_opened = openItemTag(os, cstyle);
-                               if (cstyle.labeltype == LABEL_MANUAL) {
-                                       bool const label_tag_opened = openLabelTag(os, cstyle);
-                                       sep = par->firstWordLyXHTML(os, runparams);
-                                       if (label_tag_opened)
-                                               closeLabelTag(os, cstyle);
-                                       os << '\n';
-                               }
-                               // FIXME Why did I put that first condition??
-                               else if (style.latextype == LATEX_ENVIRONMENT 
-                                          && style.labeltype != LABEL_NO_LABEL) {
-                                       bool const label_tag_opened = openLabelTag(os, cstyle);
-                                       os << pbegin->expandLabel(style, buf.params(), false);
-                                       if (label_tag_opened)
-                                               closeLabelTag(os, cstyle);
-                                       os << '\n';
+                               bool const labelfirst = style.htmllabelfirst();
+                               if (isNormalEnv(style)) {
+                                       // in this case, we print the label only for the first 
+                                       // paragraph (as in a theorem).
+                                       item_tag_opened = openItemTag(os, style);
+                                       if (par == pbegin) {
+                                               docstring const lbl = 
+                                                               pbegin->expandLabel(style, buf.params(), false);
+                                               if (!lbl.empty()) {
+                                                       bool const label_tag_opened = openLabelTag(os, style);
+                                                       os << lbl;
+                                                       if (label_tag_opened)
+                                                               closeLabelTag(os, style);
+                                               }
+                                               os << '\n';
+                                       }
+                               }       else { // some kind of list
+                                       if (!labelfirst)
+                                               item_tag_opened = openItemTag(os, style);
+                                       if (style.labeltype == LABEL_MANUAL) {
+                                               bool const label_tag_opened = openLabelTag(os, style);
+                                               sep = par->firstWordLyXHTML(os, runparams);
+                                               if (label_tag_opened)
+                                                       closeLabelTag(os, style);
+                                               os << '\n';
+                                       }
+                                       else if (style.labeltype != LABEL_NO_LABEL) {
+                                               bool const label_tag_opened = openLabelTag(os, style);
+                                               os << par->expandLabel(style, buf.params(), false);
+                                               if (label_tag_opened)
+                                                       closeLabelTag(os, style);
+                                               os << '\n';
+                                       }
+                                       if (labelfirst)
+                                               item_tag_opened = openItemTag(os, style);
+                                       else
+                                               os << "<span class='item'>";
                                }
-                               if (labelfirst)
-                                       item_tag_opened = openItemTag(os, cstyle);
-                               else
-                                       os << "<span class='item'>";
                                par->simpleLyXHTMLOnePar(buf, os, runparams, 
-                                       outerFont(distance(paragraphs.begin(), par), paragraphs), sep);
-                               if (!labelfirst)
+                                       text.outerFont(distance(begin, par)), sep);
+                               if (!isNormalEnv(style) && labelfirst)
                                        os << "</span>";
                                ++par;
                                if (item_tag_opened) {
@@ -315,30 +359,29 @@ ParagraphList::const_iterator makeEnvironment(Buffer const & buf,
                                     && par->params().depth() != origdepth) {
                                     // then we'll save this layout for later, and close it when
                                     // we get another item.
-                                               lastlay = &cstyle;
-                                       } else {
-                                               closeItemTag(os, cstyle);
-                                       }
+                                               lastlay = &style;
+                                       } else
+                                               closeItemTag(os, style);
                                        os << '\n';
                                }
                        }
                        // The other possibility is that the depth has increased, in which
                        // case we need to recurse.
                        else {
-                               send = searchEnvironment(par, pend);
-                               par = makeEnvironment(buf, os, runparams, paragraphs, par, send);
+                               send = searchEnvironmentHtml(par, pend);
+                               par = makeEnvironmentHtml(buf, os, runparams, text, par, send);
                        }
                        break;
                }
                case LATEX_PARAGRAPH:
-                       send = searchParagraph(par, pend);
-                       par = makeParagraphs(buf, os, runparams, paragraphs, par, send);
+                       send = searchParagraphHtml(par, pend);
+                       par = makeParagraphs(buf, os, runparams, text, par, send);
                        break;
                // Shouldn't happen
                case LATEX_BIB_ENVIRONMENT:
                        send = par;
                        ++send;
-                       par = makeParagraphs(buf, os, runparams, paragraphs, par, send);
+                       par = makeParagraphs(buf, os, runparams, text, par, send);
                        break;
                // Shouldn't happen
                case LATEX_COMMAND:
@@ -359,7 +402,7 @@ ParagraphList::const_iterator makeEnvironment(Buffer const & buf,
 void makeCommand(Buffer const & buf,
                                          odocstream & os,
                                          OutputParams const & runparams,
-                                         ParagraphList const & paragraphs,
+                                         Text const & text,
                                          ParagraphList::const_iterator const & pbegin)
 {
        Layout const & style = pbegin->layout();
@@ -379,8 +422,9 @@ void makeCommand(Buffer const & buf,
                os << ' ';
        }
 
+       ParagraphList::const_iterator const begin = text.paragraphs().begin();
        pbegin->simpleLyXHTMLOnePar(buf, os, runparams,
-                       outerFont(distance(paragraphs.begin(), pbegin), paragraphs));
+                       text.outerFont(distance(begin, pbegin)));
        if (main_tag_opened)
                closeTag(os, style);
        os << '\n';
@@ -389,11 +433,12 @@ void makeCommand(Buffer const & buf,
 } // end anonymous namespace
 
 
-void xhtmlParagraphs(ParagraphList const & paragraphs,
+void xhtmlParagraphs(Text const & text,
                       Buffer const & buf,
                       odocstream & os,
                       OutputParams const & runparams)
 {
+       ParagraphList const & paragraphs = text.paragraphs();
        ParagraphList::const_iterator par = paragraphs.begin();
        ParagraphList::const_iterator pend = paragraphs.end();
 
@@ -406,25 +451,25 @@ void xhtmlParagraphs(ParagraphList const & paragraphs,
                case LATEX_COMMAND: {
                        // The files with which we are working never have more than
                        // one paragraph in a command structure.
-                       makeCommand(buf, os, runparams, paragraphs, par);
+                       makeCommand(buf, os, runparams, text, par);
                        ++par;
                        break;
                }
                case LATEX_ENVIRONMENT:
                case LATEX_LIST_ENVIRONMENT:
                case LATEX_ITEM_ENVIRONMENT: {
-                       send = searchEnvironment(par, pend);
-                       par = makeEnvironment(buf, os, runparams, paragraphs, par, send);
+                       send = searchEnvironmentHtml(par, pend);
+                       par = makeEnvironmentHtml(buf, os, runparams, text, par, send);
                        break;
                }
                case LATEX_BIB_ENVIRONMENT: {
-                       send = searchEnvironment(par, pend);
-                       par = makeBibliography(buf, os, runparams, paragraphs, par, send);
+                       send = searchEnvironmentHtml(par, pend);
+                       par = makeBibliography(buf, os, runparams, text, par, send);
                        break;
                }
                case LATEX_PARAGRAPH:
-                       send = searchParagraph(par, pend);
-                       par = makeParagraphs(buf, os, runparams, paragraphs, par, send);
+                       send = searchParagraphHtml(par, pend);
+                       par = makeParagraphs(buf, os, runparams, text, par, send);
                        break;
                }
                // FIXME??