metanorma-standoc 1.8.3 → 1.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a5a0e8bdc48a2fa268df4c2d2342787a3c1312eb349c0a8a37306c2674c4440e
4
- data.tar.gz: e063188d68034654d5edf0d45b51b22b7018371dfeef1b252576b6d3d8fe57b9
3
+ metadata.gz: d069ef191d60dfccff15d8a39e2c961c343f1e19928e06645c78833be951f052
4
+ data.tar.gz: 61dff9a129459b4f2b0c4cb426dabd84a822285c8cce41cd88ce20cc4706d3a5
5
5
  SHA512:
6
- metadata.gz: fd3fe9734d21153f7955dbd3b3eeade50bee97403a6bc53c18ef97cb72345970c7951f9d96b51916f4ce7a89ff56d6c8e6d9cf5b6d67482ed598c2c6ae8090e8
7
- data.tar.gz: dbc628d7b0df786191086a908b0f849bb9f75dc23881030748313ed78350d9722675371483232bc52ebd38404ec1da79ee1e985484ae648212479a0b66ac346d
6
+ metadata.gz: 9a5991f1f7b0262f7b2e3069572d4221f0f944be86a869e9a6bb5bdabe26441305882898788840ef373dfb1671d7c2a8d9d59e1d5942e8d2df6ccc17eee1c114
7
+ data.tar.gz: 7e9c4dbf9f8d28970756ed8d516df46d5a4c2710754065818c2966de478ed32e1d189d0eec12de1ca5b0b23de5e077f9c4254e738442a7d4a1005e62d6a304d1
data/.rubocop.yml CHANGED
@@ -5,6 +5,6 @@
5
5
  inherit_from:
6
6
  - https://raw.githubusercontent.com/riboseinc/oss-guides/master/ci/rubocop.yml
7
7
  AllCops:
8
- TargetRubyVersion: 2.6
8
+ TargetRubyVersion: 2.4
9
9
  Rails:
10
10
  Enabled: true
@@ -139,7 +139,7 @@ module Asciidoctor
139
139
 
140
140
  def document(node)
141
141
  init(node)
142
- ret = makexml(node).to_xml(indent: 2)
142
+ ret = makexml(node).to_xml(encoding: "US-ASCII", indent: 2)
143
143
  outputs(node, ret) unless node.attr("nodoc") || !node.attr("docfile")
144
144
  clean_exit
145
145
  ret
@@ -45,7 +45,6 @@ module Asciidoctor
45
45
  xref_cleanup(xmldoc)
46
46
  concept_cleanup(xmldoc)
47
47
  origin_cleanup(xmldoc)
48
- svgmap_cleanup(xmldoc)
49
48
  termdef_cleanup(xmldoc)
50
49
  RelatonIev::iev_cleanup(xmldoc, @bibdb)
51
50
  element_name_cleanup(xmldoc)
@@ -59,6 +58,7 @@ module Asciidoctor
59
58
  bookmark_cleanup(xmldoc)
60
59
  requirement_cleanup(xmldoc)
61
60
  bibdata_cleanup(xmldoc)
61
+ svgmap_cleanup(xmldoc)
62
62
  boilerplate_cleanup(xmldoc)
63
63
  smartquotes_cleanup(xmldoc)
64
64
  variant_cleanup(xmldoc)
@@ -172,7 +172,7 @@ module Asciidoctor
172
172
 
173
173
  def img_cleanup(xmldoc)
174
174
  return xmldoc unless @datauriimage
175
- xmldoc.xpath("//image").each { |i| i["src"] = datauri(i["src"]) }
175
+ xmldoc.xpath("//image").each { |i| i["src"] = Metanorma::Utils::datauri(i["src"], @localdir) }
176
176
  end
177
177
 
178
178
  def variant_cleanup(xmldoc)
@@ -106,7 +106,7 @@ module Asciidoctor
106
106
  def bibdata_cleanup(xmldoc)
107
107
  bibdata_anchor_cleanup(xmldoc)
108
108
  bibdata_docidentifier_cleanup(xmldoc)
109
- biblio_indirect_erefs(xmldoc, @internal_eref_namespaces&.uniq)
109
+ biblio_indirect_erefs(xmldoc, @internal_eref_namespaces&.uniq)
110
110
  end
111
111
 
112
112
  def bibdata_anchor_cleanup(xmldoc)
@@ -124,7 +124,7 @@ module Asciidoctor
124
124
  end
125
125
  end
126
126
 
127
- def gather_indirect_erefs(xmldoc, prefix)
127
+ def gather_indirect_erefs(xmldoc, prefix)
128
128
  xmldoc.xpath("//eref[@type = '#{prefix}']").each_with_object({}) do |e, m|
129
129
  e.delete("type")
130
130
  m[e["bibitemid"]] = true
@@ -2,6 +2,7 @@ require "nokogiri"
2
2
  require "pathname"
3
3
  require "open-uri"
4
4
  require "html2doc"
5
+ require "asciimath2unitsml"
5
6
  require_relative "./cleanup_block.rb"
6
7
  require_relative "./cleanup_footnotes.rb"
7
8
  require_relative "./cleanup_ref.rb"
@@ -73,13 +74,49 @@ module Asciidoctor
73
74
  end
74
75
  end
75
76
 
77
+ UNITSML_NS = "http://unitsml.nist.gov/2005".freeze
78
+
79
+ def add_misc_container(xmldoc)
80
+ unless ins = xmldoc.at("//misc-container")
81
+ a = xmldoc.at("//termdocsource") || xmldoc.at("//bibdata")
82
+ a.next = "<misc-container/>"
83
+ ins = xmldoc.at("//misc-container")
84
+ end
85
+ ins
86
+ end
87
+
88
+ def mathml_unitsML(xmldoc)
89
+ return unless xmldoc.at(".//m:*", "m" => UNITSML_NS)
90
+ misc = add_misc_container(xmldoc)
91
+ unitsml = misc.add_child("<UnitsML xmlns='#{UNITSML_NS}'/>").first
92
+ %w(Unit CountedItem Quantity Dimension Prefix).each do |t|
93
+ gather_unitsml(unitsml, xmldoc, t)
94
+ end
95
+ end
96
+
97
+ def gather_unitsml(unitsml, xmldoc, t)
98
+ tags = xmldoc.xpath(".//m:#{t}", "m" => UNITSML_NS).each_with_object({}) do |x, m|
99
+ m[x["id"]] = x.remove
100
+ end
101
+ return if tags.empty?
102
+ set = unitsml.add_child("<#{t}Set/>").first
103
+ tags.values.each { |v| set << v }
104
+ end
105
+
106
+ def asciimath2unitsml_options
107
+ { multiplier: :space }
108
+ end
109
+
76
110
  def mathml_cleanup(xmldoc)
111
+ unitsml = Asciimath2UnitsML::Conv.new(asciimath2unitsml_options)
77
112
  xmldoc.xpath("//stem[@type = 'MathML']").each do |x|
78
113
  xml_unescape_mathml(x)
79
114
  mathml_namespace(x)
80
115
  mathml_preserve_space(x)
81
116
  mathml_italicise(x)
117
+ unitsml.MathML2UnitsML(x)
82
118
  end
119
+ mathml_unitsML(xmldoc)
83
120
  end
84
121
  end
85
122
  end
@@ -50,6 +50,7 @@ module Asciidoctor
50
50
  def biblio_renumber(xmldoc)
51
51
  i = 0
52
52
  xmldoc.xpath("//bibliography//references | //clause//references | //annex//references").each do |r|
53
+ next if r["normative"] == "true"
53
54
  r.xpath("./bibitem").each do |b|
54
55
  i += 1
55
56
  next unless docid = b.at("./docidentifier[@type = 'metanorma']")
@@ -88,18 +89,8 @@ module Asciidoctor
88
89
  end
89
90
  end
90
91
 
91
- def docid_prefix(prefix, docid)
92
- docid = "#{prefix} #{docid}" unless omit_docid_prefix(prefix)
93
- docid
94
- end
95
-
96
- def omit_docid_prefix(prefix)
97
- return true if prefix.nil? || prefix.empty?
98
- %(ISO IEC IEV ITU metanorma).include? prefix
99
- end
100
-
101
- def format_ref(ref, type, isopub)
102
- return docid_prefix(type, ref) if isopub
92
+ def format_ref(ref, type)
93
+ return @isodoc.docid_prefix(type, ref) if type != "metanorma"
103
94
  return "[#{ref}]" if /^\d+$/.match(ref) && !/^\[.*\]$/.match(ref)
104
95
  ref
105
96
  end
@@ -115,7 +106,7 @@ module Asciidoctor
115
106
  isopub = ref.at(ISO_PUBLISHER_XPATH)
116
107
  docid = ref.at("./docidentifier[@type = 'metanorma']") ||
117
108
  ref.at("./docidentifier[not(@type = 'DOI')]") or next
118
- reference = format_ref(docid.text, docid["type"], isopub)
109
+ reference = format_ref(docid.text, docid["type"])
119
110
  @anchors[ref["id"]] = { xref: reference }
120
111
  end
121
112
  end
@@ -62,10 +62,15 @@ module Asciidoctor
62
62
  end
63
63
  end
64
64
 
65
+ def make_indexsect(x, s)
66
+ x.xpath("//sections/indexsect").reverse_each { |r| s.next = r.remove }
67
+ end
68
+
65
69
  def sections_order_cleanup(x)
66
70
  s = x.at("//sections")
67
71
  make_preface(x, s)
68
72
  make_annexes(x)
73
+ make_indexsect(x, s)
69
74
  make_bibliography(x, s)
70
75
  x.xpath("//sections/annex").reverse_each { |r| s.next = r.remove }
71
76
  end
@@ -175,25 +175,12 @@ module Asciidoctor
175
175
  end.join
176
176
  end
177
177
 
178
- def datauri(uri)
179
- return uri if /^data:/.match(uri)
180
- types = MIME::Types.type_for(@localdir + uri)
181
- type = types ? types.first.to_s : 'text/plain; charset="utf-8"'
182
- # FIXME: nested uri path error(
183
- # sources/plantuml/plantuml20200524-90467-1iqek5i.png ->
184
- # sources/sources/plantuml/plantuml20200524-90467-1iqek5i.png)
185
- path = File.file?(uri) ? uri : @localdir + uri
186
- bin = File.open(path, 'rb', &:read)
187
- data = Base64.strict_encode64(bin)
188
- "data:#{type};base64,#{data}"
189
- end
190
-
191
178
  def image_attributes(node)
192
179
  uri = node.image_uri (node.attr("target") || node.target)
193
- types = /^data:/.match(uri) ? datauri2mime(uri) : MIME::Types.type_for(uri)
180
+ types = /^data:/.match(uri) ? Metanorma::Utils::datauri2mime(uri) : MIME::Types.type_for(uri)
194
181
  type = types.first.to_s
195
182
  uri = uri.sub(%r{^data:image/\*;}, "data:#{type};")
196
- attr_code(src: uri, #@datauriimage ? datauri(uri) : uri,
183
+ attr_code(src: uri,
197
184
  id: Metanorma::Utils::anchor_or_uuid,
198
185
  mimetype: type,
199
186
  height: node.attr("height") || "auto",
@@ -919,6 +919,9 @@
919
919
  </choice>
920
920
  </attribute>
921
921
  <ref name="bibdata"/>
922
+ <optional>
923
+ <ref name="misccontainer"/>
924
+ </optional>
922
925
  <optional>
923
926
  <ref name="boilerplate"/>
924
927
  </optional>
@@ -929,11 +932,21 @@
929
932
  <zeroOrMore>
930
933
  <ref name="annex"/>
931
934
  </zeroOrMore>
935
+ <optional>
936
+ <ref name="bibliography"/>
937
+ </optional>
932
938
  <zeroOrMore>
933
- <ref name="references"/>
939
+ <ref name="indexsect"/>
934
940
  </zeroOrMore>
935
941
  </element>
936
942
  </define>
943
+ <define name="misccontainer">
944
+ <element name="misc-container">
945
+ <oneOrMore>
946
+ <ref name="AnyElement"/>
947
+ </oneOrMore>
948
+ </element>
949
+ </define>
937
950
  <define name="preface">
938
951
  <element name="preface">
939
952
  <oneOrMore>
@@ -957,6 +970,11 @@
957
970
  <ref name="Content-Section"/>
958
971
  </element>
959
972
  </define>
973
+ <define name="indexsect">
974
+ <element name="indexsect">
975
+ <ref name="Content-Section"/>
976
+ </element>
977
+ </define>
960
978
  <define name="boilerplate">
961
979
  <element name="boilerplate">
962
980
  <optional>
@@ -88,6 +88,8 @@ module Asciidoctor
88
88
  bibliography_parse(a, xml, node)
89
89
  elsif node.attr("style") == "abstract"
90
90
  abstract_parse(a, xml, node)
91
+ elsif node.attr("style") == "index"
92
+ indexsect_parse(a, xml, node)
91
93
  elsif node.attr("style") == "appendix" && node.level == 1
92
94
  annex_parse(a, xml, node)
93
95
  else
@@ -114,6 +116,13 @@ module Asciidoctor
114
116
  end.join("\n")
115
117
  end
116
118
 
119
+ def indexsect_parse(attrs, xml, node)
120
+ xml.indexsect **attr_code(attrs) do |xml_section|
121
+ xml_section.title { |name| name << node.title }
122
+ xml_section << node.content
123
+ end
124
+ end
125
+
117
126
  def abstract_parse(attrs, xml, node)
118
127
  xml.abstract **attr_code(attrs) do |xml_section|
119
128
  xml_section << node.content
@@ -5,7 +5,6 @@ require "json"
5
5
  require "pathname"
6
6
  require "open-uri"
7
7
  require "uuidtools"
8
- require "mimemagic"
9
8
 
10
9
  module Asciidoctor
11
10
  module Standoc
@@ -57,29 +56,6 @@ module Asciidoctor
57
56
  end
58
57
  end
59
58
 
60
- def datauri2mime(uri)
61
- %r{^data:image/(?<imgtype>[^;]+);base64,(?<imgdata>.+)$} =~ uri
62
- type = nil
63
- imgtype = "png" unless /^[a-z0-9]+$/.match imgtype
64
- Tempfile.open(["imageuri", ".#{imgtype}"]) do |file|
65
- type = datauri2mime1(file, imgdata)
66
- end
67
- [type]
68
- end
69
-
70
- def datauri2mime1(file, imgdata)
71
- type = nil
72
- begin
73
- file.binmode
74
- file.write(Base64.strict_decode64(imgdata))
75
- file.rewind
76
- type = MimeMagic.by_magic(file)
77
- ensure
78
- file.close!
79
- end
80
- type
81
- end
82
-
83
59
  SUBCLAUSE_XPATH = "//clause[not(parent::sections)]"\
84
60
  "[not(ancestor::boilerplate)]".freeze
85
61
 
@@ -35,10 +35,25 @@ module Asciidoctor
35
35
 
36
36
  def content_validate(doc)
37
37
  section_validate(doc)
38
+ norm_ref_validate(doc)
38
39
  repeat_id_validate(doc.root)
39
40
  iev_validate(doc.root)
40
41
  end
41
42
 
43
+ def norm_ref_validate(doc)
44
+ found = false
45
+ doc.xpath("//references[@normative = 'true']/bibitem").each do |b|
46
+ next unless docid = b.at("./docidentifier[@type = 'metanorma']")
47
+ next unless /^\[\d+\]$/.match(docid.text)
48
+ @log.add("Bibliography", b, "Numeric reference in normative references")
49
+ found = true
50
+ end
51
+ if found
52
+ clean_exit
53
+ abort("Numeric reference in normative references")
54
+ end
55
+ end
56
+
42
57
  def repeat_id_validate1(ids, x)
43
58
  if ids[x["id"]]
44
59
  @log.add("Anchors", x, "Anchor #{x['id']} has already been used "\
@@ -89,7 +104,7 @@ module Asciidoctor
89
104
  def formattedstr_strip(doc)
90
105
  doc.xpath("//*[@format] | //stem | //bibdata//description | "\
91
106
  "//formattedref | //bibdata//note | //bibdata/abstract | "\
92
- "//bibitem/abstract | //bibitem/note").each do |n|
107
+ "//bibitem/abstract | //bibitem/note | //misc-container").each do |n|
93
108
  n.elements.each do |e|
94
109
  e.traverse do |e1|
95
110
  e1.element? and e1.each { |k, _v| e1.delete(k) }
@@ -19,6 +19,6 @@ module Metanorma
19
19
  end
20
20
 
21
21
  module Standoc
22
- VERSION= "1.8.3".freeze
22
+ VERSION= "1.8.4".freeze
23
23
  end
24
24
  end
@@ -38,9 +38,9 @@ Gem::Specification.new do |spec|
38
38
  spec.add_dependency "concurrent-ruby"
39
39
  spec.add_dependency "unicode2latex", "~> 0.0.1"
40
40
  spec.add_dependency "metanorma-utils", "~> 1.0.2"
41
- spec.add_dependency "mimemagic"
42
41
  spec.add_dependency "mathml2asciimath"
43
42
  spec.add_dependency "latexmath"
43
+ spec.add_dependency "asciimath2unitsml", "~> 0.1.1"
44
44
 
45
45
  spec.add_development_dependency "byebug"
46
46
  spec.add_development_dependency "sassc", "2.4.0"
@@ -2118,6 +2118,120 @@ expect(xmlpp(Asciidoctor.convert(input, backend: :standoc, header_footer: true))
2118
2118
  OUTPUT
2119
2119
 
2120
2120
  end
2121
+
2122
+ it "converts UnitsML to MathML" do
2123
+ expect(xmlpp(strip_guid(Asciidoctor.convert(<<~INPUT, backend: :standoc, header_footer: true)))).to be_equivalent_to xmlpp(<<~"OUTPUT")
2124
+ = Document title
2125
+ Author
2126
+ :stem:
2127
+
2128
+ [stem]
2129
+ ++++
2130
+ <math xmlns='http://www.w3.org/1998/Math/MathML'>
2131
+ <mrow>
2132
+ <mn>7</mn>
2133
+ <mtext>unitsml(m*kg^-2)</mtext>
2134
+ <mo>+</mo>
2135
+ <mn>8</mn>
2136
+ <mtext>unitsml(m*kg^-2)</mtext>
2137
+ </mrow>
2138
+ </math>
2139
+ ++++
2140
+ INPUT
2141
+ #{BLANK_HDR}
2142
+ <misc-container>
2143
+ <UnitsML xmlns='http://unitsml.nist.gov/2005'>
2144
+ <UnitSet>
2145
+ <Unit xml:id='U_m.kg-2' dimensionURL='#D_LM-2'>
2146
+ <UnitSystem name='SI' type='SI_derived' xml:lang='en-US'/>
2147
+ <UnitName xml:lang='en'>m*kg^-2</UnitName>
2148
+ <UnitSymbol type='HTML'>
2149
+ m&#160;kg
2150
+ <sup>&#8722;2</sup>
2151
+ </UnitSymbol>
2152
+ <UnitSymbol type='MathML'>
2153
+ <math xmlns='http://www.w3.org/1998/Math/MathML'>
2154
+ <mrow>
2155
+ <mi mathvariant='normal'>m</mi>
2156
+ <mo rspace='thickmathspace'>&#8290;</mo>
2157
+ <msup>
2158
+ <mrow>
2159
+ <mi mathvariant='normal'>kg</mi>
2160
+ </mrow>
2161
+ <mrow>
2162
+ <mo>&#8722;</mo>
2163
+ <mn>2</mn>
2164
+ </mrow>
2165
+ </msup>
2166
+ </mrow>
2167
+ </math>
2168
+ </UnitSymbol>
2169
+ <RootUnits>
2170
+ <EnumeratedRootUnit unit='meter'/>
2171
+ <EnumeratedRootUnit unit='gram' prefix='k' powerNumerator='-2'/>
2172
+ </RootUnits>
2173
+ </Unit>
2174
+ </UnitSet>
2175
+ <DimensionSet>
2176
+ <Dimension xml:id='D_LM-2'>
2177
+ <Length symbol='L' powerNumerator='1'/>
2178
+ <Mass symbol='M' powerNumerator='-2'/>
2179
+ </Dimension>
2180
+ </DimensionSet>
2181
+ <PrefixSet>
2182
+ <Prefix prefixBase='10' prefixPower='3' xml:id='NISTp10_3'>
2183
+ <PrefixName xml:lang='en'>kilo</PrefixName>
2184
+ <PrefixSymbol type='ASCII'>k</PrefixSymbol>
2185
+ </Prefix>
2186
+ </PrefixSet>
2187
+ </UnitsML>
2188
+ </misc-container>
2189
+ <sections>
2190
+ <formula id='_'>
2191
+ <stem type='MathML'>
2192
+ <math xmlns='http://www.w3.org/1998/Math/MathML'>
2193
+ <mrow>
2194
+ <mn>7</mn>
2195
+ <mo rspace='thickmathspace'>&#8290;</mo>
2196
+ <mrow xref='U_m.kg-2'>
2197
+ <mi mathvariant='normal'>m</mi>
2198
+ <mo rspace='thickmathspace'>&#8290;</mo>
2199
+ <msup>
2200
+ <mrow>
2201
+ <mi mathvariant='normal'>kg</mi>
2202
+ </mrow>
2203
+ <mrow>
2204
+ <mo>&#8722;</mo>
2205
+ <mn>2</mn>
2206
+ </mrow>
2207
+ </msup>
2208
+ </mrow>
2209
+ <mo>+</mo>
2210
+ <mn>8</mn>
2211
+ <mo rspace='thickmathspace'>&#8290;</mo>
2212
+ <mrow xref='U_m.kg-2'>
2213
+ <mi mathvariant='normal'>m</mi>
2214
+ <mo rspace='thickmathspace'>&#8290;</mo>
2215
+ <msup>
2216
+ <mrow>
2217
+ <mi mathvariant='normal'>kg</mi>
2218
+ </mrow>
2219
+ <mrow>
2220
+ <mo>&#8722;</mo>
2221
+ <mn>2</mn>
2222
+ </mrow>
2223
+ </msup>
2224
+ </mrow>
2225
+ </mrow>
2226
+ </math>
2227
+ </stem>
2228
+ </formula>
2229
+ </sections>
2230
+ </standard-document>
2231
+ OUTPUT
2232
+
2233
+ end
2234
+
2121
2235
  it "customises italicisation of MathML" do
2122
2236
  input = <<~INPUT
2123
2237
  = Document title
@@ -2350,14 +2464,17 @@ end
2350
2464
  it "process express_ref macro with no existing bibliography" do
2351
2465
  expect(xmlpp(strip_guid(Asciidoctor.convert(<<~"INPUT", backend: :standoc, header_footer: true, agree_to_terms: true)))).to be_equivalent_to xmlpp(<<~"OUTPUT")
2352
2466
  #{ASCIIDOC_BLANK_HDR}
2467
+ [[B]]
2468
+ [type="express-schema"]
2353
2469
  == Clause
2354
2470
 
2355
2471
  <<express-schema:A:A.B.C,C>>
2356
2472
  <<express-schema:A>>
2473
+ <<express-schema:B>>
2357
2474
  INPUT
2358
2475
  #{BLANK_HDR}
2359
2476
  <sections>
2360
- <clause id='_' inline-header='false' obligation='normative'>
2477
+ <clause id='B' inline-header='false' obligation='normative' type="express-schema">
2361
2478
  <title>Clause</title>
2362
2479
  <p id='_'>
2363
2480
  <eref bibitemid='express-schema_A' citeas="">
@@ -2367,6 +2484,7 @@ end
2367
2484
  C
2368
2485
  </eref>
2369
2486
  <eref bibitemid='express-schema_A' citeas=""/>
2487
+ <xref target='B'/>
2370
2488
  </p>
2371
2489
  </clause>
2372
2490
  </sections>