lib/html2doc/math.rb in html2doc-1.4.3 vs lib/html2doc/math.rb in html2doc-1.4.4
- old
+ new
@@ -3,28 +3,30 @@
require "htmlentities"
require "nokogiri"
require "plane1converter"
class Html2Doc
- def asciimath_to_mathml1(expr)
- AsciiMath::MathMLBuilder.new(msword: true).append_expression(
- AsciiMath.parse(HTMLEntities.new.decode(expr)).ast,
- ).to_s
- .gsub(/<math>/, "<math xmlns='http://www.w3.org/1998/Math/MathML'>")
+ def asciimath_to_mathml1(expr, retain_asciimath)
+ ret = AsciiMath::MathMLBuilder.new(msword: true).append_expression(
+ AsciiMath.parse(@c.decode(expr)).ast,
+ ).to_s.gsub(/<math>/, "<math xmlns='http://www.w3.org/1998/Math/MathML'>")
+ retain_asciimath and
+ ret += "<asciimath>#{@c.encode(@c.decode(expr), :basic)}</asciimath>"
+ ret
rescue StandardError => e
puts "parsing: #{expr}"
puts e.message
raise e
end
- def asciimath_to_mathml(doc, delims)
+ def asciimath_to_mathml(doc, delims, retain_asciimath: false)
return doc if delims.nil? || delims.size < 2
m = doc.split(/(#{Regexp.escape(delims[0])}|#{Regexp.escape(delims[1])})/)
m.each_slice(4).map.with_index do |(*a), i|
progress_conv(i, 500, (m.size / 4).floor, 1000, "AsciiMath")
- a[2].nil? || a[2] = asciimath_to_mathml1(a[2])
+ a[2].nil? or a[2] = asciimath_to_mathml1(a[2], retain_asciimath)
a.size > 1 ? a[0] + a[2] : a[0]
end.join
end
def progress_conv(idx, step, total, threshold, msg)
@@ -120,10 +122,10 @@
def to_plane1(xml, font)
xml.traverse do |n|
next unless n.text?
- n.replace(Plane1Converter.conv(HTMLEntities.new.decode(n.text), font))
+ n.replace(Plane1Converter.conv(@c.decode(n.text), font))
end
xml
end
def mathml_to_ooml(docxml)