The previous commit introduced wrong behaviours for <>. The new code carefully escapes what needs to be escaped from LaTeX, using the now-standard XML tools (XMLStream).
This commit is contained in:
Thibaut Cuvelier 2024-03-21 21:32:45 +01:00
parent 16660d12b4
commit fd37845075
5 changed files with 45 additions and 27 deletions

View File

@ -100,7 +100,7 @@ The problem occurs when adding a label.
\begin_layout Standard \begin_layout Standard
\begin_inset Formula \begin_inset Formula
\begin{equation} \begin{equation}
x^{2}\label{eq:1} x^{2}<\log x\label{eq:1}
\end{equation} \end{equation}
\end_inset \end_inset

View File

@ -22,7 +22,7 @@ div.standard {
<h1 class='title' id='magicparlabel-1'>Math formula output as raw LaTeX</h1> <h1 class='title' id='magicparlabel-1'>Math formula output as raw LaTeX</h1>
<div class='standard' id='magicparlabel-2'>The problem occurs when adding a label. https://www.lyx.org/trac/ticket/13048</div> <div class='standard' id='magicparlabel-2'>The problem occurs when adding a label. https://www.lyx.org/trac/ticket/13048</div>
<div class='standard' id='magicparlabel-3'><a id="eq_1" /><div class='math'><table class='mathtable'><tr><td class='math'>x^{2}</td><td>(1)</td></tr></table></div> <div class='standard' id='magicparlabel-3'><a id="eq_1" /><div class='math'><table class='mathtable'><tr><td class='math'>x^{2}<\log x</td><td>(1)</td></tr></table></div>
</div> </div>
</body> </body>
</html> </html>

View File

@ -380,6 +380,8 @@ void InsetLabel::docbook(XMLStream & xs, OutputParams const & runparams) const
docstring InsetLabel::xhtml(XMLStream & xs, OutputParams const &) const docstring InsetLabel::xhtml(XMLStream & xs, OutputParams const &) const
{ {
// Print the label as an HTML anchor, so that an external link can point to this equation.
// (URL: FILE.html#EQ-ID.)
// FIXME XHTML // FIXME XHTML
// Unfortunately, the name attribute has been deprecated, so we have to use // Unfortunately, the name attribute has been deprecated, so we have to use
// id here to get the document to validate as XHTML 1.1. This will cause a // id here to get the document to validate as XHTML 1.1. This will cause a

View File

@ -2574,35 +2574,56 @@ void InsetMathHull::mathmlize(MathMLStream & ms) const
} }
void InsetMathHull::mathAsLatex(TeXMathStream & os) const docstring InsetMathHull::mathAsLatex() const
{ {
MathEnsurer ensurer(os, false);
bool const havenumbers = haveNumbers(); bool const havenumbers = haveNumbers();
bool const havetable = havenumbers || nrows() > 1 || ncols() > 1; bool const havetable = havenumbers || nrows() > 1 || ncols() > 1;
if (!havetable) { if (!havetable) {
odocstringstream ls;
otexrowstream ots(ls);
TeXMathStream os(ots, false, true, TeXMathStream::wsPreview);
ModeSpecifier specifier(os, MATH_MODE);
MathEnsurer ensurer(os, false);
os << cell(index(0, 0)); os << cell(index(0, 0));
return; return ls.str();
} }
os << "<table class='mathtable'>"; odocstringstream ods;
XMLStream xs(ods);
xs << xml::StartTag("table", "class='mathtable'");
for (row_type row = 0; row < nrows(); ++row) { for (row_type row = 0; row < nrows(); ++row) {
os << "<tr>"; xs << xml::StartTag("tr");
for (col_type col = 0; col < ncols(); ++col) { for (col_type col = 0; col < ncols(); ++col) {
os << "<td class='math'>"; xs << xml::StartTag("td", "class='math'");
os << cell(index(row, col));
os << "</td>"; odocstringstream ls;
otexrowstream ots(ls);
TeXMathStream os(ots, false, true, TeXMathStream::wsPreview);
ModeSpecifier specifier(os, MATH_MODE);
MathEnsurer ensurer(os, false);
os << cell(index(0, 0));
// ls.str() contains a raw LaTeX string, which might require some encoding before being valid XML.
xs << ls.str();
xs << xml::EndTag("td");
} }
if (havenumbers) { if (havenumbers) {
os << "<td>"; xs << xml::StartTag("td");
docstring const & num = numbers_[row]; docstring const & num = numbers_[row];
if (!num.empty()) if (!num.empty()) {
os << '(' << num << ')'; xs << '(' << num << ')';
os << "</td>"; }
xs << xml::EndTag("td");
} }
os << "</tr>"; xs << xml::EndTag("tr");
} }
os << "</table>"; xs << xml::EndTag("table");
return ods.str();
} }
@ -2703,7 +2724,7 @@ docstring InsetMathHull::xhtml(XMLStream & xs, OutputParams const & op) const
string const tag = (getType() == hullSimple) ? "span" : "div"; string const tag = (getType() == hullSimple) ? "span" : "div";
xs << xml::CR() xs << xml::CR()
<< xml::StartTag(tag, "style = \"text-align: center;\"") << xml::StartTag(tag, "style = \"text-align: center;\"")
<< xml::CompTag("img", "src=\"" + filename + "\" alt=\"Mathematical Equation\"") << xml::CompTag("img", "src=\"" + filename + R"(" alt="Mathematical Equation")")
<< xml::EndTag(tag) << xml::EndTag(tag)
<< xml::CR(); << xml::CR();
success = true; success = true;
@ -2716,12 +2737,8 @@ docstring InsetMathHull::xhtml(XMLStream & xs, OutputParams const & op) const
if (!success /* || mathtype != BufferParams::LaTeX */) { if (!success /* || mathtype != BufferParams::LaTeX */) {
// Unfortunately, we cannot use latexString() because we do not want // Unfortunately, we cannot use latexString() because we do not want
// $...$ or whatever. // $...$ or whatever.
odocstringstream ls; // The returned value already has the correct escaping for HTML.
otexrowstream ots(ls); docstring const latex = mathAsLatex();
TeXMathStream wi(ots, false, true, TeXMathStream::wsPreview);
ModeSpecifier specifier(wi, MATH_MODE);
mathAsLatex(wi);
docstring const latex = ls.str();
// class='math' allows for use of jsMath // class='math' allows for use of jsMath
// http://www.math.union.edu/~dpvc/jsMath/ // http://www.math.union.edu/~dpvc/jsMath/
@ -2729,8 +2746,7 @@ docstring InsetMathHull::xhtml(XMLStream & xs, OutputParams const & op) const
// probably should allow for some kind of customization here // probably should allow for some kind of customization here
string const tag = (getType() == hullSimple) ? "span" : "div"; string const tag = (getType() == hullSimple) ? "span" : "div";
xs << xml::StartTag(tag, "class='math'") xs << xml::StartTag(tag, "class='math'")
<< XMLStream::ESCAPE_AND << latex // Don't escape <> tags: latex might contain them << XMLStream::ESCAPE_NONE << latex // Don't escape anything: latex might contain XML.
// (typically, when there is a label).
<< xml::EndTag(tag) << xml::EndTag(tag)
<< xml::CR(); << xml::CR();
} }

View File

@ -151,8 +151,8 @@ public:
void mathmlize(MathMLStream &) const override; void mathmlize(MathMLStream &) const override;
/// ///
void htmlize(HtmlStream &) const override; void htmlize(HtmlStream &) const override;
/// /// Returns the hull as a LaTeX string for embedding in HTML or XML.
void mathAsLatex(TeXMathStream &) const; docstring mathAsLatex() const;
/// ///
void toString(odocstream &) const override; void toString(odocstream &) const override;
/// ///