Skip to content

Commit

Permalink
Merge pull request #87 from metanorma/fix/omml-linebreak
Browse files Browse the repository at this point in the history
Fix/omml linebreak
  • Loading branch information
opoudjis authored Dec 20, 2023
2 parents 5e0e15e + 993cd07 commit e8aed98
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
35 changes: 17 additions & 18 deletions lib/html2doc/math.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ def unwrap_accents(doc)

# random fixes to MathML input that OOXML needs to render properly
def ooxml_cleanup(math, docnamespaces)
#encode_math(
unwrap_accents(
mathml_preserve_space(
mathml_insert_rows(math, docnamespaces), docnamespaces
),
)
#)
# encode_math(
unwrap_accents(
mathml_preserve_space(
mathml_insert_rows(math, docnamespaces), docnamespaces
),
)
# )
math.add_namespace(nil, MATHML_NS)
math
end
Expand Down Expand Up @@ -165,9 +165,9 @@ def ooml_clean(xml)
def mathml_to_ooml1(xml, docnamespaces)
doc = Nokogiri::XML::Document::new
doc.root = ooxml_cleanup(xml, docnamespaces)
d = xml.parent["block"] != "false" # display_style
ooxml = Nokogiri::XML(Plurimath::Math.parse(doc.to_xml(indent: 0),
:mathml).to_omml)
# d = xml.parent["block"] != "false" # display_style
ooxml = Nokogiri::XML(Plurimath::Math
.parse(doc.to_xml(indent: 0), :mathml).to_omml(split_on_linebreak: true))
ooxml = unitalic(accent_tr(ooxml))
ooxml = ooml_clean(uncenter(xml, ooxml))
xml.swap(ooxml)
Expand Down Expand Up @@ -201,9 +201,10 @@ def math_only_para?(node)
x.text.strip.empty?
end

def math_block?(_ooxml, mathml)
def math_block?(ooxml, mathml)
# ooxml.name == "oMathPara" || mathml["displaystyle"] == "true"
mathml["displaystyle"] == "true"
mathml["displaystyle"] == "true" &&
ooxml.xpath("./m:oMath", "m" => OOXML_NS).size <= 1
end

STYLE_BEARING_NODE =
Expand All @@ -227,11 +228,9 @@ def uncenter(math, ooxml)

def uncenter_unneeded(math, ooxml, alignnode)
(math_block?(ooxml, math) || !alignnode) and return ooxml
if !math_only_para?(alignnode)
ooxml.name == "oMathPara" and
ooxml = ooxml.elements.detect { |x| x.name == "oMath" }
return ooxml
end
nil
math_only_para?(alignnode) and return nil
ooxml.name == "oMathPara" and
ooxml = ooxml.elements.select { |x| %w(oMath r).include?(x.name) }
ooxml.size > 1 ? nil : Nokogiri::XML::NodeSet.new(math.document, ooxml)
end
end
2 changes: 1 addition & 1 deletion lib/html2doc/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Html2Doc
VERSION = "1.7.1".freeze
VERSION = "1.7.2".freeze
end
32 changes: 32 additions & 0 deletions spec/html2doc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,38 @@ def image_clean(xml)
OUTPUT
end

it "processes linebreaks in MathML mtext" do
Html2Doc.new(filename: "test", asciimathdelims: ["{{", "}}"])
.process(html_input("<div><math xmlns='http://www.w3.org/1998/Math/MathML' displaystyle='true'>
<mstyle displaystyle='true'>
<mi>x</mi><mo>=</mo><mo linebreak='newline'/><mi>y</mi>
<mo>=</mo><mo linebreak='newline'/><mi>z</mi>
</mstyle>
</math></div>"))
expect(guid_clean(File.read("test.doc", encoding: "utf-8")))
.to match_fuzzy(<<~OUTPUT)
#{WORD_HDR} #{DEFAULT_STYLESHEET} #{WORD_HDR_END}
#{word_body('<div><m:oMathPara>
<m:oMathParaPr><m:jc m:val="left"/></m:oMathParaPr><m:oMath>
<m:r><m:t>x</m:t></m:r><m:r><m:t>=</m:t></m:r>
</m:oMath>
<m:r>
<br/>
</m:r>
<m:oMath>
<m:r><m:t>y</m:t></m:r><m:r><m:t>=</m:t></m:r>
</m:oMath>
<m:r>
<br/>
</m:r>
<m:oMath>
<m:r><m:t>z</m:t></m:r>
</m:oMath>
</m:oMathPara></div>', '<div style="mso-element:footnote-list"/>')}
#{WORD_FTR1}
OUTPUT
end

it "unwraps and converts accent in MathML" do
Html2Doc.new(filename: "test", asciimathdelims: ["{{", "}}"])
.process(html_input("<div><math xmlns='http://www.w3.org/1998/Math/MathML' displaystyle='true'>
Expand Down

0 comments on commit e8aed98

Please sign in to comment.