Skip to content

Commit

Permalink
Merge pull request #582 from metanorma/fix/significant
Browse files Browse the repository at this point in the history
Fix/significant
  • Loading branch information
opoudjis authored Jul 16, 2024
2 parents 6a6c765 + bafe844 commit 676ad00
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 109 deletions.
1 change: 1 addition & 0 deletions Gemfile.devel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gem "plurimath", git: "https://github.com/plurimath/plurimath", branch: "features/significant_digits"
6 changes: 2 additions & 4 deletions lib/isodoc/css.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,14 @@ def convert_scss(filename, stylesheet, stripwordcss)

# stripwordcss if HTML stylesheet, !stripwordcss if DOC stylesheet
def generate_css(filename, stripwordcss)
return nil if filename.nil?

filename.nil? and return nil
filename = precompiled_style_or_original(filename)
stylesheet = File.read(filename, encoding: "UTF-8")
stylesheet = populate_template(stylesheet, :word)
stylesheet.gsub!(/(\s|\{)mso-[^:]+:[^;]+;/m, "\\1") if stripwordcss
stylesheet.gsub!(/--/, "-DOUBLE_HYPHEN_ESCAPE-") unless stripwordcss
if File.extname(filename) == ".scss"
File.extname(filename) == ".scss" and
stylesheet = convert_scss(filename, stylesheet, stripwordcss)
end
Tempfile.open([File.basename(filename, ".*"), "css"],
mode: File::BINARY | File::SHARE_DELETE,
encoding: "utf-8") do |f|
Expand Down
50 changes: 32 additions & 18 deletions lib/isodoc/presentation_function/math.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def localize_maths(node, locale)
else implicit_number_formatter(x, locale)
end
rescue ArgumentError
rescue Error => e
warn "Failure to localised MathML/mn\n#{node.parent.to_xml}\n#{e}"
rescue StandardError, RuntimeError => e
warn "Failure to localise MathML/mn\n#{node.parent.to_xml}\n#{e}"
end
end

Expand All @@ -40,10 +40,8 @@ def normalise_number(num)
end

def implicit_number_formatter(num, locale)
fmt = { digit_count: num_totaldigits(num.text) }.compact
fmt = { significant: num_totaldigits(num.text) }.compact
n = normalise_number(num.text)
# Plurimath confused by exponent notation
#warn "IMPLICIT: precision: #{num_precision(num.text)} ; symbols: #{fmt}, n: #{n}; output: #{@numfmt.localized_number(n, locale:, format: fmt, precision: num_precision(num.text))}"
@numfmt.localized_number(n, locale:, format: fmt,
precision: num_precision(num.text))
end
Expand All @@ -57,7 +55,8 @@ def numberformat_extract(options)
end

def numberformat_type(ret)
%i(precision digit_count group_digits fraction_group_digits).each do |i|
%i(precision significant digit_count group_digits fraction_group_digits)
.each do |i|
ret[i] &&= ret[i].to_i
end
%i(notation exponent_sign locale).each do |i|
Expand All @@ -69,20 +68,20 @@ def numberformat_type(ret)
def explicit_number_formatter(num, locale, options)
ret = numberformat_type(numberformat_extract(options))
l = ret[:locale] || locale
precision, symbols, digit_count = explicit_number_formatter_cfg(num, ret)
precision, symbols, significant = explicit_number_formatter_cfg(num, ret)
n = normalise_number(num.text)
# Plurimath confused by exponent notation
#warn "EXPLICIT: precision: #{precision} ; symbols: #{symbols}, n: #{n}; output: #{Plurimath::NumberFormatter.new(l, localizer_symbols: symbols).localized_number(n, precision:, format: symbols.merge(digit_count:))}"
Plurimath::NumberFormatter.new(l, localizer_symbols: symbols)
Plurimath::NumberFormatter.new(l)
.localized_number(n, precision:,
format: symbols.merge(digit_count:))
format: symbols.merge(significant:))
end

def explicit_number_formatter_cfg(num, fmt)
symbols = twitter_cldr_localiser_symbols.dup.merge(fmt)
precision = symbols[:precision]&.to_i || num_precision(num.text)
symbols[:precision] or digit_count = num_totaldigits(num.text)
[precision, symbols, digit_count]
precision = symbols[:precision] || num_precision(num.text)
signif = symbols[:significant]
(symbols.keys & %i(precision digit_count)).empty? and
signif ||= num_totaldigits(num.text)
[precision, symbols, signif]
end

def num_precision(num)
Expand All @@ -96,8 +95,8 @@ def num_precision(num)
def num_totaldigits(num)
totaldigits = nil
/\.(?=\d+e)/.match?(num) and
totaldigits = twitter_cldr_localiser_symbols[:digit_count] ||
num.sub(/^.*\./, "").sub(/e.*$/, "").size
totaldigits = twitter_cldr_localiser_symbols[:significant] ||
num.sub(/^0\./, ".").sub(/^.*\./, "").sub(/e.*$/, "").size
totaldigits
end

Expand All @@ -109,14 +108,16 @@ def asciimath_dup(node)
@suppressasciimathdup || node.parent.at(ns("./asciimath")) and return
math = node.to_xml.gsub(/ xmlns=["'][^"']+["']/, "")
.gsub(%r{<[^:/>]+:}, "<").gsub(%r{</[^:/>]+:}, "</")
.gsub(%r{ data-metanorma-numberformat="[^"]+"}, "")
ret = Plurimath::Math.parse(math, "mathml").to_asciimath
node.next = "<asciimath>#{@c.encode(ret, :basic)}</asciimath>"
rescue StandardError => e
warn "Failure to convert MathML to AsciiMath\n#{node.parent.to_xml}\n#{e}"
end

def maths_just_numeral(node)
mn = node.at(".//m:mn", MATHML).children
mn = node.at(".//m:mn", MATHML).children.text
.sub(/\^([0-9+-]+)$/, "<sup>\\1</sup>")
if node.parent.name == "stem"
node.parent.replace(mn)
else
Expand All @@ -142,11 +143,24 @@ def mathml_linebreak(node)
OUTPUT
end

# convert any Ascii superscripts to correct(ish) MathML
# Not bothering to match times, base of 1.0 x 10^-20, just ^-20
def mn_to_msup(node)
node.xpath(".//m:mn", MATHML).each do |n|
m = %r{^(.+)\^([0-9+-]+)$}.match(n.text) or next
n.replace("<msup><mn>#{m[1]}</mn><mn>#{m[2]}</mn></msup>")
end
end

def mathml_number(node, locale)
justnumeral = numeric_mathml?(node)
justnumeral or asciimath_dup(node)
localize_maths(node, locale)
justnumeral and maths_just_numeral(node)
if justnumeral
maths_just_numeral(node)
else
mn_to_msup(node)
end
end

def numeric_mathml?(node)
Expand Down
Loading

0 comments on commit 676ad00

Please sign in to comment.