]> git.lyx.org Git - lyx.git/blobdiff - src/output_xhtml.cpp
Check path of Qt tools if qtchooser is detected
[lyx.git] / src / output_xhtml.cpp
index 35f0ea5c6d69017531f0890ab2cdd5646df38ace..585ee2cb34e5c5b7d387275b6fee9b56bae73709 100644 (file)
@@ -85,53 +85,10 @@ docstring htmlize(docstring const & str, XHTMLStream::EscapeSettings e)
 }
 
 
-string escapeChar(char c, XHTMLStream::EscapeSettings e)
+docstring escapeChar(char c, XHTMLStream::EscapeSettings e)
 {
-       string str;
-       switch (e) {
-       case XHTMLStream::ESCAPE_NONE:
-               str += c;
-               break;
-       case XHTMLStream::ESCAPE_ALL:
-               if (c == '<') {
-                       str += "&lt;";
-                       break;
-               } else if (c == '>') {
-                       str += "&gt;";
-                       break;
-               }
-       // fall through
-       case XHTMLStream::ESCAPE_AND:
-               if (c == '&')
-                       str += "&amp;";
-               else
-                       str     +=c ;
-               break;
-       }
-       return str;
-}
-
-
-// escape what needs escaping
-string htmlize(string const & str, XHTMLStream::EscapeSettings e)
-{
-       ostringstream d;
-       string::const_iterator it = str.begin();
-       string::const_iterator en = str.end();
-       for (; it != en; ++it)
-               d << escapeChar(*it, e);
-       return d.str();
-}
-
-
-string cleanAttr(string const & str)
-{
-       string newname;
-       string::const_iterator it = str.begin();
-       string::const_iterator en = str.end();
-       for (; it != en; ++it)
-               newname += isAlnumASCII(*it) ? *it : '_';
-       return newname;
+       LATTEST(static_cast<unsigned char>(c) < 0x80);
+       return escapeChar(static_cast<char_type>(c), e);
 }
 
 
@@ -150,11 +107,11 @@ docstring cleanAttr(docstring const & str)
 
 docstring StartTag::writeTag() const
 {
-       string output = "<" + tag_;
+       docstring output = '<' + from_utf8(tag_);
        if (!attr_.empty())
-               output += " " + html::htmlize(attr_, XHTMLStream::ESCAPE_NONE);
+               output += ' ' + html::htmlize(from_utf8(attr_), XHTMLStream::ESCAPE_NONE);
        output += ">";
-       return from_utf8(output);
+       return output;
 }
 
 
@@ -178,26 +135,23 @@ docstring EndTag::writeEndTag() const
 }
 
 
-docstring ParTag::writeTag() const
+ParTag::ParTag(std::string const & tag, std::string attr,
+       std::string const & parid)
+  : StartTag(tag)
 {
-       docstring output = StartTag::writeTag();
-
-       if (parid_.empty())
-               return output;
-
-       string const pattr = "id='" + parid_ + "'";
-       output += html::CompTag("a", pattr).writeTag();
-       return output;
+       if (!parid.empty())
+               attr += " id='" + parid + "'";
+       attr_ = attr;
 }
 
 
 docstring CompTag::writeTag() const
 {
-       string output = "<" + tag_;
+       docstring output = '<' + from_utf8(tag_);
        if (!attr_.empty())
-               output += " " + html::htmlize(attr_, XHTMLStream::ESCAPE_NONE);
+               output += ' ' + html::htmlize(from_utf8(attr_), XHTMLStream::ESCAPE_NONE);
        output += " />";
-       return from_utf8(output);
+       return output;
 }
 
 
@@ -205,7 +159,7 @@ docstring CompTag::writeTag() const
 namespace {
 
 string fontToTag(html::FontTypes type)
- {
+{
        switch(type) {
        case FT_EMPH:
                return "em";
@@ -245,68 +199,66 @@ string fontToTag(html::FontTypes type)
        return "";
 }
 
-StartTag fontToStartTag(html::FontTypes type)
- {
-       string tag = fontToTag(type);
+string fontToAttribute(html::FontTypes type)
+{
        switch(type) {
        case FT_EMPH:
-               return html::StartTag(tag);
        case FT_BOLD:
-               return html::StartTag(tag);
+               return "";
        case FT_NOUN:
-               return html::StartTag(tag, "class='lyxnoun'");
+               return "class='lyxnoun'";
        case FT_UBAR:
-               return html::StartTag(tag);
+               return "";
        case FT_DBAR:
-               return html::StartTag(tag, "class='dline'");
+               return "class='dline'";
        case FT_SOUT:
-               return html::StartTag(tag, "class='strikeout'");
+               return "class='strikeout'";
        case FT_WAVE:
-               return html::StartTag(tag, "class='wline'");
+               return "class='wline'";
        case FT_ITALIC:
-               return html::StartTag(tag);
+               return "";
        case FT_UPRIGHT:
-               return html::StartTag(tag, "style='font-style:normal;'");
+               return "style='font-style:normal;'";
        case FT_SLANTED:
-               return html::StartTag(tag, "style='font-style:oblique;'");
+               return "style='font-style:oblique;'";
        case FT_SMALLCAPS:
-               return html::StartTag(tag, "style='font-variant:small-caps;'");
+               return "style='font-variant:small-caps;'";
        case FT_ROMAN:
-               return html::StartTag(tag, "style='font-family:serif;'");
+               return "style='font-family:serif;'";
        case FT_SANS:
-               return html::StartTag(tag, "style='font-family:sans-serif;'");
+               return "style='font-family:sans-serif;'";
        case FT_TYPE:
-               return html::StartTag(tag, "style='font-family:monospace;'");
+               return "style='font-family:monospace;'";
        case FT_SIZE_TINY:
        case FT_SIZE_SCRIPT:
        case FT_SIZE_FOOTNOTE:
-               return html::StartTag(tag, "style='font-size:x-small;'");
+               return "style='font-size:x-small;'";
        case FT_SIZE_SMALL:
-               return html::StartTag(tag, "style='font-size:small;'");
+               return "style='font-size:small;'";
        case FT_SIZE_NORMAL:
-               return html::StartTag(tag, "style='font-size:normal;'");
+               return "style='font-size:normal;'";
        case FT_SIZE_LARGE:
-               return html::StartTag(tag, "style='font-size:large;'");
+               return "style='font-size:large;'";
        case FT_SIZE_LARGER:
        case FT_SIZE_LARGEST:
-               return html::StartTag(tag, "style='font-size:x-large;'");
+               return "style='font-size:x-large;'";
        case FT_SIZE_HUGE:
        case FT_SIZE_HUGER:
-               return html::StartTag(tag, "style='font-size:xx-large;'");
+               return "style='font-size:xx-large;'";
        case FT_SIZE_INCREASE:
-               return html::StartTag(tag, "style='font-size:larger;'");
+               return "style='font-size:larger;'";
        case FT_SIZE_DECREASE:
-               return html::StartTag(tag, "style='font-size:smaller;'");
+               return "style='font-size:smaller;'";
        }
        // kill warning
-       return StartTag("");
+       return "";
 }
 
 } // end anonymous namespace
 
 
 FontTag::FontTag(FontTypes type)
-  : StartTag(fontToStartTag(type)), font_type_(type)
+  : StartTag(fontToTag(type), fontToAttribute(type)), font_type_(type)
 {}
 
 
@@ -339,21 +291,27 @@ XHTMLStream::XHTMLStream(odocstream & os)
 
 
 #ifdef XHTML_DEBUG
-void XHTMLStream::dumpTagStack(string const & msg) const
+void XHTMLStream::dumpTagStack(string const & msg)
 {
-       writeError(msg + ": Tag Stack");
-       TagStack::const_reverse_iterator it = tag_stack_.rbegin();
-       TagStack::const_reverse_iterator en = tag_stack_.rend();
+       *this << html::CR();
+       writeError(msg);
+       *this << html::CR();
+       writeError("Tag Stack");
+       TagDeque::const_reverse_iterator it = tag_stack_.rbegin();
+       TagDeque::const_reverse_iterator en = tag_stack_.rend();
        for (; it != en; ++it) {
-               writeError(it->tag_);
+               writeError(it->get()->tag_);
        }
+       writeError("End Tag Stack");
+       *this << html::CR();
        writeError("Pending Tags");
        it = pending_tags_.rbegin();
        en = pending_tags_.rend();
        for (; it != en; ++it) {
-               writeError(it->tag_);
+               writeError(it->get()->tag_);
        }
-       writeError("End Tag Stack");
+       writeError("End Pending Tags");
+       *this << html::CR();
 }
 #endif
 
@@ -410,7 +368,7 @@ bool XHTMLStream::closeFontTags()
 }
 
 
-void XHTMLStream::startParagraph(bool keep_empty)
+void XHTMLStream::startDivision(bool keep_empty)
 {
        pending_tags_.push_back(makeTagPtr(html::StartTag(parsep_tag)));
        if (keep_empty)
@@ -418,7 +376,7 @@ void XHTMLStream::startParagraph(bool keep_empty)
 }
 
 
-void XHTMLStream::endParagraph()
+void XHTMLStream::endDivision()
 {
        if (isTagPending(parsep_tag)) {
                // this case is normal. it just means we didn't have content,
@@ -436,7 +394,7 @@ void XHTMLStream::endParagraph()
        }
 
        if (!isTagOpen(parsep_tag)) {
-               writeError("No paragraph separation tag found in endParagraph().");
+               writeError("No division separation tag found in endDivision().");
                return;
        }
 
@@ -497,7 +455,7 @@ XHTMLStream & XHTMLStream::operator<<(char_type c)
 XHTMLStream & XHTMLStream::operator<<(char c)
 {
        clearTagDeque();
-       string const d = html::escapeChar(c, escape_);
+       os_ << html::escapeChar(c, escape_);
        escape_ = ESCAPE_ALL;
        return *this;
 }
@@ -877,25 +835,26 @@ ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
                if (!lay.counter.empty())
                        buf.masterBuffer()->params().
                            documentClass().counters().step(lay.counter, OutputUpdate);
+
                // FIXME We should see if there's a label to be output and
                // do something with it.
                if (par != pbegin)
                        xs << html::CR();
 
-               // If we are already in a paragraph, and this is the first one, then we
-               // do not want to open the paragraph tag.
-               // we also do not want to open it if the current layout does not permit
-               // multiple paragraphs.
-               bool const opened = runparams.html_make_pars &&
-                       (par != pbegin || !runparams.html_in_par);
-               bool const make_parid = !runparams.for_toc && runparams.html_make_pars;
-
-               if (opened)
-                       openParTag(xs, lay, par->params(),
-                                  make_parid ? par->magicLabel() : "");
-
-               docstring const deferred =
-                       par->simpleLyXHTMLOnePar(buf, xs, runparams, text.outerFont(distance(begin, par)));
+               // We want to open the paragraph tag if:
+               //   (i) the current layout permits multiple paragraphs
+               //  (ii) we are either not already inside a paragraph (HTMLIsBlock) OR
+               //       we are, but this is not the first paragraph
+               // But we do not want to open the paragraph tag if this paragraph contains
+               // only one item, and that item is "inline", i.e., not HTMLIsBlock (such 
+               // as a branch). That is the "special case" we handle first.
+               Inset const * specinset = par->size() == 1 ? par->getInset(0) : 0;
+               bool const special_case =  
+                       specinset && !specinset->getLayout().htmlisblock();
+
+               bool const open_par = runparams.html_make_pars
+                       && (!runparams.html_in_par || par != pbegin)
+                       && !special_case;
 
                // We want to issue the closing tag if either:
                //   (i)  We opened it, and either html_in_par is false,
@@ -904,16 +863,28 @@ ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
                //        but we are in the first par, and there is a next par.
                ParagraphList::const_iterator nextpar = par;
                ++nextpar;
-               bool const needclose =
-                       (opened && (!runparams.html_in_par || nextpar != pend))
-                       || (!opened && runparams.html_in_par && par == pbegin && nextpar != pend);
-               if (needclose) {
-                       closeTag(xs, lay);
-                       xs << html::CR();
+               bool const close_par =
+                       (open_par && (!runparams.html_in_par || nextpar != pend))
+                       || (!open_par && runparams.html_in_par && par == pbegin && nextpar != pend);
+
+               if (open_par) {
+                       // We do not issue the paragraph id if we are doing 
+                       // this for the TOC (or some similar purpose)
+                       openParTag(xs, lay, par->params(),
+                                  runparams.for_toc ? "" : par->magicLabel());
                }
+
+               docstring const deferred = par->simpleLyXHTMLOnePar(buf, xs, 
+                       runparams, text.outerFont(distance(begin, par)),
+                       open_par, close_par);
+
                if (!deferred.empty()) {
                        xs << XHTMLStream::ESCAPE_NONE << deferred << html::CR();
                }
+               if (close_par) {
+                       closeTag(xs, lay);
+                       xs << html::CR();
+               }
        }
        return pend;
 }
@@ -982,9 +953,6 @@ ParagraphList::const_iterator makeEnvironment(Buffer const & buf,
                )
                        cnts.step(cntr, OutputUpdate);
                ParagraphList::const_iterator send;
-               // this will be positive, if we want to skip the initial word
-               // (if it's been taken for the label).
-               pos_type sep = 0;
 
                switch (style.latextype) {
                case LATEX_ENVIRONMENT:
@@ -1000,6 +968,9 @@ ParagraphList::const_iterator makeEnvironment(Buffer const & buf,
                                        lastlay = 0;
                                }
 
+                               // this will be positive, if we want to skip the
+                               // initial word (if it's been taken for the label).
+                               pos_type sep = 0;
                                bool const labelfirst = style.htmllabelfirst();
                                if (!labelfirst)
                                        openItemTag(xs, style, par->params());
@@ -1020,7 +991,7 @@ ParagraphList::const_iterator makeEnvironment(Buffer const & buf,
                                                        }
                                                        xs << html::CR();
                                                }
-                                       }       else { // some kind of list
+                                       } else { // some kind of list
                                                if (style.labeltype == LABEL_MANUAL) {
                                                        openLabelTag(xs, style);
                                                        sep = par->firstWordLyXHTML(xs, runparams);
@@ -1040,7 +1011,7 @@ ParagraphList::const_iterator makeEnvironment(Buffer const & buf,
                                        openItemTag(xs, style, par->params());
 
                                par->simpleLyXHTMLOnePar(buf, xs, runparams,
-                                       text.outerFont(distance(begin, par)), sep);
+                                       text.outerFont(distance(begin, par)), true, true, sep);
                                ++par;
 
                                // We may not want to close the tag yet, in particular:
@@ -1102,14 +1073,24 @@ void makeCommand(Buffer const & buf,
                    documentClass().counters().step(style.counter, OutputUpdate);
 
        bool const make_parid = !runparams.for_toc && runparams.html_make_pars;
+       
+       if (style.labeltype == LABEL_ABOVE)
+               xs << html::StartTag("div")
+                  << pbegin->params().labelString()
+                  << html::EndTag("div");
+       else if (style.labeltype == LABEL_CENTERED)
+               xs << html::StartTag("div", "style = \"text-align: center;\"")
+                  << pbegin->params().labelString()
+                  << html::EndTag("div");
 
        openParTag(xs, style, pbegin->params(),
                   make_parid ? pbegin->magicLabel() : "");
 
        // Label around sectioning number:
        // FIXME Probably need to account for LABEL_MANUAL
-       // FIXME Probably also need now to account for labels ABOVE and CENTERED.
-       if (style.labeltype != LABEL_NO_LABEL) {
+       if (style.labeltype != LABEL_NO_LABEL &&
+           style.labeltype != LABEL_ABOVE &&
+           style.labeltype != LABEL_CENTERED ) {
                openLabelTag(xs, style);
                xs << pbegin->params().labelString();
                closeLabelTag(xs, style);