commit bbec301c715a539370659530b3cb67664d0d106c
Author: Thibaut Cuvelier <[email protected]>
Date: Mon Mar 24 00:05:35 2025 +0100
InsetMathHull: rework escaping of raw LaTeX code for formulae when image
export doesn't work.
---
autotests/export/xhtml/math_output_latex.lyx | 34 ++++++++++++++++++++--
autotests/export/xhtml/math_output_latex.xhtml | 11 +++++--
src/mathed/InsetMathHull.cpp | 40 +++++++++++++++++++++++++-
3 files changed, 79 insertions(+), 6 deletions(-)
diff --git a/autotests/export/xhtml/math_output_latex.lyx
b/autotests/export/xhtml/math_output_latex.lyx
index 7c49de59f6..b1c85b3c97 100644
--- a/autotests/export/xhtml/math_output_latex.lyx
+++ b/autotests/export/xhtml/math_output_latex.lyx
@@ -1,5 +1,5 @@
-#LyX 2.4 created this file. For more info see https://www.lyx.org/
-\lyxformat 620
+#LyX 2.5 created this file. For more info see https://www.lyx.org/
+\lyxformat 636
\begin_document
\begin_header
\save_transient_properties true
@@ -55,10 +55,18 @@
\paperorientation portrait
\suppress_date false
\justification true
-\use_refstyle 1
+\crossref_package prettyref
\use_formatted_ref 0
\use_minted 0
\use_lineno 0
+\backgroundcolor none
+\fontcolor none
+\notefontcolor lightgray
+\boxbgcolor red
+\table_border_color default
+\table_odd_row_color default
+\table_even_row_color default
+\table_alt_row_colors_start 1
\index Index
\shortcut idx
\color #008000
@@ -84,6 +92,7 @@
\html_be_strict false
\docbook_table_output 0
\docbook_mathml_prefix 1
+\docbook_mathml_version 0
\end_header
\begin_body
@@ -106,6 +115,25 @@ x^{2}<\log x\label{eq:1}
\end_inset
+\end_layout
+
+\begin_layout Standard
+Be careful with that escaping axe,
+ though,
+ this should still be valid XHTML:
+
+\begin_inset Formula
+\[
+\left(\begin{array}{cccc}
+A & B & C & D\\
+\hdotsfor[2]{4}\\
+q & w & e & r
+\end{array}\right)
+\]
+
+\end_inset
+
+
\end_layout
\end_body
diff --git a/autotests/export/xhtml/math_output_latex.xhtml
b/autotests/export/xhtml/math_output_latex.xhtml
index 713def3459..f070e9bf9a 100644
--- a/autotests/export/xhtml/math_output_latex.xhtml
+++ b/autotests/export/xhtml/math_output_latex.xhtml
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
-<meta name="generator" content="LyX 2.4.0~devel" />
+<meta name="generator" content="LyX 2.5.0~devel" />
<title>Math formula output as raw LaTeX</title>
<style>
/* Layout-provided Styles */
@@ -22,7 +22,14 @@ div.standard {
<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-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 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 class='standard' id='magicparlabel-4'>Be careful with that escaping axe,
though, this should still be valid XHTML: <div
class='math'>\left(\begin{array}{cccc}
+A & B & C & D\\
+\hdotsfor[2]{4}\\
+q & w & e & r
+\end{array}\right)</div>
</div>
</body>
</html>
diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp
index d973b2a512..d9e5c5c67b 100644
--- a/src/mathed/InsetMathHull.cpp
+++ b/src/mathed/InsetMathHull.cpp
@@ -2772,13 +2772,51 @@ docstring InsetMathHull::xhtml(XMLStream & xs,
OutputParams const & op) const
// The returned value already has the correct escaping for HTML.
docstring const latex = mathAsLatex();
+ // Escaping this string is hairy. `latex` contains some XHTML
and
+ // we don't want the output to look like this:
+ // <table class='mathtable'>
+ // We have to perform some escaping, otherwise a matrix will be
+ // output with raw ampersands:
+ // \left(\begin{array}{cccc}
+ // A & B & C & D\\
+ // \hdotsfor[2]{4}\\
+ // q & w & e & r
+ // \end{array}\right)
+ // We cannot perform standard XML escaping, otherwise the XHTML
+ // will be off. We must escape at the very least &, as it's
common
+ // (not escaping it causes many failures in tests for
doc/Math.lyx).
+ // Not escaping < causes problems if it comes from LaTeX.
Escaping >
+ // is not really required (but it would be best).
+ //
+ // Current compromise: only escape &. It is safe to do so.
Unescape
+ // some angle brackets too.
+ //
+ // To get a better result, we would need to have a better
handling
+ // of escaping, as `latex` already has some parts that are
escaped.
+ // The root cause is that `mathAsLatex` outputs both LaTeX and
XHTML
+ // code intertwined in the right way.
+ //
+ // When debugging this code, pay attention to the differences
+ // between exporting and previewing (in preview mode, LyX won't
+ // attempt generating an image, thus never exercising this code).
+ docstring const latex_escaped = subst(
+ subst(
+ subst(
+ latex,
+ from_ascii("&"), from_ascii("&")
+ ),
+ from_ascii("&lt;"), from_ascii("<")
+ ),
+ from_ascii("&gt;"), from_ascii(">")
+ );
+
// class='math' allows for use of jsMath
// http://www.math.union.edu/~dpvc/jsMath/
// FIXME XHTML
// probably should allow for some kind of customization here
string const tag = (getType() == hullSimple) ? "span" : "div";
xs << xml::StartTag(tag, "class='math'")
- << XMLStream::ESCAPE_NONE << latex // Don't escape anything:
latex might contain XML.
+ << XMLStream::ESCAPE_NONE << latex_escaped
<< xml::EndTag(tag)
<< xml::CR();
}
--
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs