From d1daa092f6f82ff040109df583f7f55a862e6e4c Mon Sep 17 00:00:00 2001 From: Thibaut Cuvelier Date: Wed, 8 Dec 2021 02:02:39 +0100 Subject: [PATCH] MathML: share some code for XArrow. --- src/mathed/InsetMathXArrow.cpp | 56 +++++++++++++--------------------- 1 file changed, 22 insertions(+), 34 deletions(-) diff --git a/src/mathed/InsetMathXArrow.cpp b/src/mathed/InsetMathXArrow.cpp index cd97b7c7d4..c150a4454c 100644 --- a/src/mathed/InsetMathXArrow.cpp +++ b/src/mathed/InsetMathXArrow.cpp @@ -122,30 +122,26 @@ static std::map latex_to_xml_entities = { }; +docstring map_latex_to(docstring latex, bool xml = false) +{ + auto dict = (xml) ? latex_to_xml_entities : latex_to_html_entities; + + auto mapping = dict.find(to_ascii(latex)); + if (mapping != dict.end()) { + return from_ascii(mapping->second); + } else { + std::string format = (xml) ? "XML" : "HTML"; + lyxerr << "mathmlize " << format << " conversion for '" << latex << "' not implemented" << endl; + LASSERT(false, return from_ascii(dict["xrightarrow"])); + } +} + + void InsetMathXArrow::mathmlize(MathMLStream & ms) const { - std::string arrow; - - if (!ms.xmlMode()) { // Use HTML entities. - auto mapping = latex_to_html_entities.find(to_ascii(name_)); - if (mapping != latex_to_html_entities.end()) { - arrow = mapping->second; - } else { - lyxerr << "mathmlize conversion for '" << name_ << "' not implemented" << endl; - LASSERT(false, arrow = latex_to_html_entities["xrightarrow"]); - } - } else { // Use XML entities. - auto mapping = latex_to_xml_entities.find(to_ascii(name_)); - if (mapping != latex_to_xml_entities.end()) { - arrow = mapping->second; - } else { - lyxerr << "mathmlize XML conversion for '" << name_ << "' not implemented" << endl; - LASSERT(false, arrow = latex_to_xml_entities["xrightarrow"]); - } - } - + docstring arrow = map_latex_to(name_, ms.xmlMode()); ms << "<" << from_ascii(ms.namespacedTag("munderover")) << " accent='false' accentunder='false'>" - << MTagInline("mo") << from_ascii(arrow) << ETagInline("mo") + << MTagInline("mo") << arrow << ETagInline("mo") << cell(1) << cell(0) << ""; } @@ -153,20 +149,11 @@ void InsetMathXArrow::mathmlize(MathMLStream & ms) const void InsetMathXArrow::htmlize(HtmlStream & os) const { - string arrow; - - auto mapping = latex_to_html_entities.find(to_ascii(name_)); - if (mapping != latex_to_html_entities.end()) { - arrow = mapping->second; - } else { - lyxerr << "htmlize conversion for '" << name_ << "' not implemented" << endl; - LASSERT(false, arrow = latex_to_html_entities["xrightarrow"]); - } - + docstring arrow = map_latex_to(name_); os << MTag("span", "class='xarrow'") - << MTag("span", "class='xatop'") << cell(0) << ETag("span") - << MTag("span", "class='xabottom'") << from_ascii(arrow) << ETag("span") - << ETag("span"); + << MTag("span", "class='xatop'") << cell(0) << ETag("span") + << MTag("span", "class='xabottom'") << arrow << ETag("span") + << ETag("span"); } @@ -176,6 +163,7 @@ void InsetMathXArrow::validate(LaTeXFeatures & features) const features.require("amsmath"); else features.require("mathtools"); + if (features.runparams().math_flavor == OutputParams::MathAsHTML) // CSS adapted from eLyXer features.addCSSSnippet(