Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/metanorma/isodoc
Browse files Browse the repository at this point in the history
  • Loading branch information
opoudjis committed Jul 8, 2024
2 parents a357fcd + 1016e30 commit b5ea159
Show file tree
Hide file tree
Showing 3 changed files with 302 additions and 12 deletions.
2 changes: 2 additions & 0 deletions isodoc.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Gem::Specification.new do |spec|
end
spec.required_ruby_version = Gem::Requirement.new(">= 3.1.0")

spec.add_dependency "base64"
spec.add_dependency "bigdecimal"
spec.add_dependency "html2doc", "~> 1.8.1"
# spec.add_dependency "isodoc-i18n", "~> 1.1.0" # already in relaton-render and mn-requirements
# spec.add_dependency "relaton-cli"
Expand Down
79 changes: 72 additions & 7 deletions lib/isodoc/presentation_function/math.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,86 @@ def mathml(docxml)
# TwitterCldr::DataReaders::NumberDataReader.new(locale).symbols
def localize_maths(node, locale)
node.xpath(".//m:mn", MATHML).each do |x|
x.children = @numfmt
.localized_number(x.text, locale: locale,
precision: num_precision(x.text))
x.children =
if fmt = x["data-metanorma-numberformat"]
x.delete("data-metanorma-numberformat")
explicit_number_formatter(x, locale, fmt)
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}"
end
end

def normalise_number(num)
n = BigDecimal(num).to_s("F")
/\.\d/.match?(num) or n.sub!(/\.\d+$/, "")
n
end

def implicit_number_formatter(num, locale)
fmt = { digit_count: 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

def numberformat_extract(options)
options.gsub!(/([a-z_]+)='/, %('\\1=))
CSV.parse_line(options, quote_char: "'").each_with_object({}) do |x, acc|
m = /^(.+?)=(.+)?$/.match(x) or next
acc[m[1].to_sym] = m[2].sub(/^(["'])(.+)\1$/, "\\2")
end
end

def numberformat_type(ret)
%i(precision 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|
ret[i] &&= ret[i].to_sym
end
ret
end

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)
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)
.localized_number(n, precision:,
format: symbols.merge(digit_count:))
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]
end

def num_precision(num)
precision = 0
/\./.match?(num) and precision =
twitter_cldr_localiser_symbols[:precision] ||
num.sub(/^.*\./, "").size
precision = nil
/\.(?!\d+e)/.match?(num) and
precision = twitter_cldr_localiser_symbols[:precision] ||
num.sub(/^.*\./, "").size
precision
end

def num_totaldigits(num)
totaldigits = nil
/\.(?=\d+e)/.match?(num) and
totaldigits = twitter_cldr_localiser_symbols[:digit_count] ||
num.sub(/^.*\./, "").sub(/e.*$/, "").size
totaldigits
end

def twitter_cldr_localiser_symbols
{}
end
Expand Down
233 changes: 228 additions & 5 deletions spec/isodoc/presentation_xml_maths_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@
end

context "overrides localisation of numbers in MathML" do
it "overrides localisation of numbers in MathML, with no grouping of digits" do
it "with no grouping of digits" do
input = <<~INPUT
<iso-standard xmlns="http://riboseinc.com/isoxml">
<bibdata>
Expand Down Expand Up @@ -295,10 +295,8 @@
.sub(%r{<localized-strings>.*</localized-strings>}m, "")))
.to be_equivalent_to xmlpp(output2)
end
end

context "overrides localisation of numbers in MathML" do
it "overrides localisation of numbers in MathML with grouping of digits" do
it "with grouping of digits" do
input = <<~INPUT
<iso-standard xmlns="http://riboseinc.com/isoxml">
<bibdata>
Expand All @@ -320,7 +318,7 @@
</bibdata>
<preface>
<clause type="toc" id="_" displayorder="1"> <title depth="1">Table of contents</title> </clause>
<p displayorder='2'> ...
<p displayorder='2'> ...
6=42=12=14=96=77=26=45=15
64=21=21=49=67=72;64$51$5
3=00=00
Expand All @@ -337,6 +335,231 @@
.to be_equivalent_to xmlpp(output1)
end
end
context "overrides localisation of numbers in MathML" do
# before do
# allow_any_instance_of(IsoDoc::PresentationXMLConvert)
# .to(receive(:twitter_cldr_localiser_symbols)
# .and_return({
# fraction_group_digits: 2,
# fraction_group: "'",
# precision: 2,
# }))
# end
let(:input) do
<<~INPUT
<iso-standard xmlns="http://riboseinc.com/isoxml">
<bibdata>
<title language="en">test</title>
</bibdata>
<preface>
<p>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="notation='basic'">0.31e2</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="notation='basic'">0.3274287432878432992e6</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="decimal='.'">0.3274287432878432992e6</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="digit_count='3'">0.3274287432878432992e6</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="digit_count='9'">0.3274287432878432992e6</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="precision='3'">0.3274287432878432992e6</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="decimal='.',notation='basic',digit_count='3'">0.3274287432878432992e6</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="decimal=',',notation='basic',digit_count='9'">0.3274287432878432992e6</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="decimal=',',notation='basic',precision='3'">0.3274287432878432992e6</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="decimal=',',notation='basic'">0.1e1</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="decimal=',',notation='basic'">0.11e1</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="decimal=',',notation='basic'">0.1100e1</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="decimal=',',notation='basic'">0.1e22</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="decimal=',',notation='basic'">0.10e20</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="decimal=',',notation='basic'">0.10e-18</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="decimal='.',notation='e',digit_count='3'">0.3274287432878432992e6</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="decimal=',',notation='e',digit_count='9'">0.3274287432878432992e6</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="decimal=',',notation='e',precision='3'">0.3274287432878432992e6</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="decimal=',',notation='e'">0.1e1</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="decimal=',',notation='e'">0.11e1</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="decimal=',',notation='e'">0.1100e1</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="decimal=',',notation='e'">0.1e22</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="decimal=',',notation='e'">0.10e20</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="decimal=',',notation='e'">0.10e-18</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML"><mn>...</mn></math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="precision='7',digitcount='10',group='x',group_digits='3',decimal=',',fraction_group='y',fraction_group_digits='4'">642121496772.6451564515</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="precision='7',digitcount='10',group='x',group_digits='3',decimal=',',fraction_group='y',fraction_group_digits='4',notation='scientific',exponent_sign='true',e='EE'">642121496772.6451564515</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML">
<mn data-metanorma-numberformat="locale='de',digitcount='10',group='x',group_digits='3',decimal=','">642121496772.6451564515</mn>
</math></stem>
<stem type="MathML"><math xmlns="http://www.w3.org/1998/Math/MathML"><mn data-metanorma-numberformat="locale='fr'">30000</mn></math></stem>
</preface>
</iso-standard>
INPUT
end

it "with data-metanorma-numberformat attributes and default precision" do
allow_any_instance_of(IsoDoc::PresentationXMLConvert)
.to(receive(:twitter_cldr_localiser_symbols)
.and_return({
fraction_group_digits: 2,
fraction_group: "'",
precision: 2,
}))

output1 = <<~OUTPUT
<iso-standard xmlns='http://riboseinc.com/isoxml' type='presentation'>
<bibdata>
<title language='en'>test</title>
</bibdata>
<preface>
<clause type="toc" id="_" displayorder="1"> <title depth="1">Table of contents</title> </clause>
<p displayorder="2">
31;00
32=74=28;74
32=74=28.74
32=74=28.74
32=74=28.74
32=74=28.74'3
32=74=28.74
32=74=28,74
32=74=28,74'3
1,00
1,10
1,10
10=00=00=00=00=00=00=00=00=00=00,00
10=00=00=00=00=00=00=00=00=00,00
0,00
3.27e5
3,27e5
3,27'4e5
1,00e0
1,10e0
1,10e0
1,00e+21
1,00e+19
1,00e-19
...
642x121x496x772,6451y564
6,4212y150 × 10^+11
642x121x496x772,64
30'000,00
</p>
</preface>
</iso-standard>
OUTPUT
TwitterCldr.reset_locale_fallbacks

expect(xmlpp(strip_guid(IsoDoc::PresentationXMLConvert
.new({ localizenumber: "#=#0;##$#" }
.merge(presxml_options))
.convert("test", input, true))
.sub(%r{<localized-strings>.*</localized-strings>}m, "")))
.to be_equivalent_to xmlpp(output1)
end

it "with data-metanorma-numberformat attributes and no default precision" do
allow_any_instance_of(IsoDoc::PresentationXMLConvert)
.to(receive(:twitter_cldr_localiser_symbols)
.and_return({
fraction_group_digits: 2,
fraction_group: "'",
}))

output1 = <<~OUTPUT
<iso-standard xmlns='http://riboseinc.com/isoxml' type='presentation'>
<bibdata>
<title language='en'>test</title>
</bibdata>
<preface>
<clause type="toc" id="_" displayorder="1"> <title depth="1">Table of contents</title> </clause>
<p displayorder="2">
31
327x428,74'32'87'84'32'99'2
327x428.74'32'87'84'32'99'2
327x428.74'32'87'84'32'99'2
327x428.74'32'87'84'32'99'2
327x428.74'3
327x428.74'32'87'84'32'99'2
327x428,74'32'87'84'32'99'2
327x428,74'3
1
1,1
1,10'0
1x000x000x000x000x000x000x000
10x000x000x000x000x000x000
0,0
3.27'42'87'43'28'78'43'30'28e5
3,27'42'87'43'28'78'43'30'28e5
3,27'4e5
1e0
1,1e0
1,10'0e0
1e+21
1,00'00'00'00'00'00'00'00'00'00e+19
9,0e-20
...
642x121x496x772,6451y564
6,4212y150 × 10^+11
642x121x496x772,64'51'56'45'15
30'000
</p>
</preface>
</iso-standard>
OUTPUT
TwitterCldr.reset_locale_fallbacks

expect(xmlpp(strip_guid(IsoDoc::PresentationXMLConvert
.new({ localizenumber: "#=#0;##$#" }
.merge(presxml_options))
.convert("test", input, true))
.sub(%r{<localized-strings>.*</localized-strings>}m, "")))
.to be_equivalent_to xmlpp(output1)
end
end

it "propagates boldface into MathML" do
input = <<~INPUT
Expand Down

0 comments on commit b5ea159

Please sign in to comment.