]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathHull.cpp
Assure correct spacing of colored items in mathed
[lyx.git] / src / mathed / InsetMathHull.cpp
index 2dcd43af78b4a54d6f26fdd1de1e153d5ad2f7ce..d0e11fe2ab1118b9b4401ac8f1eb91f460c1bce9 100644 (file)
@@ -38,7 +38,7 @@
 #include "InsetMathMacro.h"
 #include "InsetMathMacroTemplate.h"
 #include "MetricsInfo.h"
-#include "output_xhtml.h"
+#include "xml.h"
 #include "Paragraph.h"
 #include "ParIterator.h"
 #include "xml.h"
@@ -2404,14 +2404,8 @@ int InsetMathHull::plaintext(odocstringstream & os,
 }
 
 
-int InsetMathHull::docbook(odocstream & os, OutputParams const & runparams) const
+void InsetMathHull::docbook(XMLStream & xs, OutputParams const & runparams) const
 {
-       // With DocBook 5, MathML must be within its own namespace; defined in Buffer.cpp::writeDocBookSource as "m".
-       // Output everything in a separate stream so that this does not interfere with the standard flow of DocBook tags.
-       odocstringstream osmath;
-       MathStream ms(osmath, "m", true);
-       int res = 0;
-
        // Choose the tag around the MathML equation.
        docstring name;
        if (getType() == hullSimple)
@@ -2419,54 +2413,61 @@ int InsetMathHull::docbook(odocstream & os, OutputParams const & runparams) cons
        else
                name = from_ascii("informalequation");
 
-       docstring bname = name;
+       // DocBook also has <equation>, but it comes with a title.
+
+       docstring attr;
        for (row_type i = 0; i < nrows(); ++i) {
                if (!label(i).empty()) {
-                       bname += " xml:id=\"" + xml::cleanID(label(i)) + "\"";
+                       attr = "xml:id=\"" + xml::cleanID(label(i)) + "\"";
                        break;
                }
        }
 
-       ++ms.tab(); ms.cr(); ms.os() << '<' << bname << '>';
+       xs << xml::StartTag(name, attr);
+       xs << xml::CR();
+
+       // With DocBook 5, MathML must be within its own namespace; defined in Buffer.cpp::writeDocBookSource as "m".
+       // Output everything in a separate stream so that this does not interfere with the standard flow of DocBook tags.
+       odocstringstream osmath;
+       MathStream ms(osmath, "m", true);
 
+       // Output the MathML subtree.
        odocstringstream ls;
        otexstream ols(ls);
-       if (runparams.flavor == OutputParams::XML) {
-               ms << MTag("alt role='tex' ");
-               // Workaround for db2latex: db2latex always includes equations with
-               // \ensuremath{} or \begin{display}\end{display}
-               // so we strip LyX' math environment
-               WriteStream wi(ols, false, false, WriteStream::wsDefault, runparams.encoding);
-               InsetMathGrid::write(wi);
-               ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&amp;"), "<", "&lt;"));
-               ms << ETag("alt");
-               ms << MTag("math");
-               ms << ETag("alt");
+
+       // TeX transcription. Avoid MTag/ETag so that there are no extraneous spaces.
+       ms << "<" << from_ascii("alt") << " role='tex'" << ">";
+       // Workaround for db2latex: db2latex always includes equations with
+       // \ensuremath{} or \begin{display}\end{display}
+       // so we strip LyX' math environment
+       WriteStream wi(ols, false, false, WriteStream::wsDefault, runparams.encoding);
+       InsetMathGrid::write(wi);
+       ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&amp;"), "<", "&lt;"));
+       ms << "</" << from_ascii("alt") << ">";
+
+       // Actual transformation of the formula into MathML. This translation may fail (for example, due to custom macros).
+       // The new output stream is required to deal with the errors: first write completely the formula into this
+       // temporary stream; then, if it is possible without error, then copy it back to the "real" stream. Otherwise,
+       // some incomplete tags might be put into the real stream.
+       try {
+               // First, generate the MathML expression.
+               odocstringstream ostmp;
+               MathStream mstmp(ostmp, ms.xmlns(), ms.xmlMode());
+               InsetMathGrid::mathmlize(mstmp);
+
+               // Then, output it (but only if the generation can be done without errors!).
                ms << MTag("math");
-               InsetMathGrid::mathmlize(ms);
+               ms.cr();
+               osmath << ostmp.str(); // osmath is not a XMLStream, so no need for XMLStream::ESCAPE_NONE.
                ms << ETag("math");
-       } else {
-               ms << MTag("alt role='tex'");
-               latex(ols, runparams);
-               res = ols.texrow().rows();
-               ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&amp;"), "<", "&lt;"));
-               ms << ETag("alt");
+       } catch (MathExportException const &) {
+               osmath << "MathML export failed. Please report this as a bug.";
        }
 
-       ms << from_ascii("<graphic fileref=\"eqn/");
-       if (!label(0).empty())
-               ms << xml::cleanID(label(0));
-       else
-               ms << xml::uniqueID(from_ascii("anon"));
-
-       if (runparams.flavor == OutputParams::XML)
-               ms << from_ascii("\"/>");
-       else
-               ms << from_ascii("\">");
-
-       ms.cr(); --ms.tab(); ms.os() << "</" << name << '>';
-
-       return ms.line() + res;
+       // Output the complete formula to the DocBook stream.
+       xs << XMLStream::ESCAPE_NONE << osmath.str();
+       xs << xml::CR();
+       xs << xml::EndTag(name);
 }