metanorma-standoc 1.7.2 → 1.8.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/rake.yml +17 -0
  3. data/.rubocop.yml +1 -1
  4. data/lib/asciidoctor/standoc/base.rb +34 -45
  5. data/lib/asciidoctor/standoc/basicdoc.rng +5 -3
  6. data/lib/asciidoctor/standoc/blocks.rb +20 -11
  7. data/lib/asciidoctor/standoc/blocks_notes.rb +2 -2
  8. data/lib/asciidoctor/standoc/cleanup.rb +29 -77
  9. data/lib/asciidoctor/standoc/cleanup_block.rb +39 -35
  10. data/lib/asciidoctor/standoc/cleanup_boilerplate.rb +56 -0
  11. data/lib/asciidoctor/standoc/cleanup_inline.rb +26 -29
  12. data/lib/asciidoctor/standoc/cleanup_maths.rb +123 -0
  13. data/lib/asciidoctor/standoc/cleanup_ref.rb +4 -98
  14. data/lib/asciidoctor/standoc/cleanup_ref_dl.rb +94 -0
  15. data/lib/asciidoctor/standoc/cleanup_section.rb +5 -0
  16. data/lib/asciidoctor/standoc/converter.rb +0 -77
  17. data/lib/asciidoctor/standoc/front.rb +2 -2
  18. data/lib/asciidoctor/standoc/inline.rb +3 -16
  19. data/lib/asciidoctor/standoc/isodoc.rng +97 -1
  20. data/lib/asciidoctor/standoc/lists.rb +2 -2
  21. data/lib/asciidoctor/standoc/macros_plantuml.rb +1 -1
  22. data/lib/asciidoctor/standoc/ref_sect.rb +2 -2
  23. data/lib/asciidoctor/standoc/reqt.rb +6 -1
  24. data/lib/asciidoctor/standoc/section.rb +13 -80
  25. data/lib/asciidoctor/standoc/table.rb +1 -1
  26. data/lib/asciidoctor/standoc/terms.rb +125 -0
  27. data/lib/asciidoctor/standoc/utils.rb +2 -120
  28. data/lib/asciidoctor/standoc/validate.rb +16 -1
  29. data/lib/metanorma/standoc/version.rb +1 -1
  30. data/metanorma-standoc.gemspec +3 -4
  31. data/spec/{asciidoctor-standoc → asciidoctor}/base_spec.rb +34 -5
  32. data/spec/{asciidoctor-standoc → asciidoctor}/blocks_spec.rb +0 -0
  33. data/spec/{asciidoctor-standoc → asciidoctor}/cleanup_sections_spec.rb +5 -5
  34. data/spec/{asciidoctor-standoc → asciidoctor}/cleanup_spec.rb +395 -6
  35. data/spec/{asciidoctor-standoc → asciidoctor}/datamodel/attributes_table_preprocessor_spec.rb +0 -0
  36. data/spec/{asciidoctor-standoc → asciidoctor}/datamodel/diagram_preprocessor_spec.rb +0 -0
  37. data/spec/{asciidoctor-standoc → asciidoctor}/inline_spec.rb +0 -0
  38. data/spec/{asciidoctor-standoc → asciidoctor}/isobib_cache_spec.rb +4 -4
  39. data/spec/{asciidoctor-standoc → asciidoctor}/lists_spec.rb +0 -0
  40. data/spec/{asciidoctor-standoc → asciidoctor}/macros_json2text_spec.rb +0 -0
  41. data/spec/{asciidoctor-standoc → asciidoctor}/macros_lutaml_spec.rb +1 -1
  42. data/spec/{asciidoctor-standoc → asciidoctor}/macros_plantuml_spec.rb +0 -0
  43. data/spec/{asciidoctor-standoc → asciidoctor}/macros_spec.rb +0 -0
  44. data/spec/{asciidoctor-standoc → asciidoctor}/macros_yaml2text_spec.rb +0 -0
  45. data/spec/{asciidoctor-standoc → asciidoctor}/refs_dl_spec.rb +17 -5
  46. data/spec/{asciidoctor-standoc → asciidoctor}/refs_spec.rb +175 -153
  47. data/spec/{asciidoctor-standoc → asciidoctor}/section_spec.rb +160 -3
  48. data/spec/{asciidoctor-standoc → asciidoctor}/table_spec.rb +0 -0
  49. data/spec/{asciidoctor-standoc → asciidoctor}/validate_spec.rb +20 -0
  50. data/spec/fixtures/action_schemaexpg1.svg +122 -0
  51. data/spec/spec_helper.rb +8 -0
  52. data/spec/vcr_cassettes/dated_iso_ref_joint_iso_iec.yml +49 -49
  53. data/spec/vcr_cassettes/isobib_get_123.yml +14 -14
  54. data/spec/vcr_cassettes/isobib_get_123_1.yml +27 -27
  55. data/spec/vcr_cassettes/isobib_get_123_1_fr.yml +36 -36
  56. data/spec/vcr_cassettes/isobib_get_123_2001.yml +13 -13
  57. data/spec/vcr_cassettes/isobib_get_124.yml +11 -11
  58. data/spec/vcr_cassettes/rfcbib_get_rfc8341.yml +94 -30
  59. data/spec/vcr_cassettes/separates_iev_citations_by_top_level_clause.yml +86 -66
  60. metadata +36 -48
  61. data/lib/asciidoctor/standoc/log.rb +0 -59
  62. data/spec/asciidoctor-standoc/converter_spec.rb +0 -21
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b74869d08fd6dece972c8a91e6d3bee248f3cd1f8f6dbf5ce1280358be4a6e27
4
- data.tar.gz: d3d41f3fc77fead3848808777fcfaf09c20baa24a3aebdde2ace278fcbb44cfd
3
+ metadata.gz: d069ef191d60dfccff15d8a39e2c961c343f1e19928e06645c78833be951f052
4
+ data.tar.gz: 61dff9a129459b4f2b0c4cb426dabd84a822285c8cce41cd88ce20cc4706d3a5
5
5
  SHA512:
6
- metadata.gz: da9c7ea38fa9545e6657d178296a7c022a4ab460ba8ea31a9e34b188e7be5451c2ff6e4e202767f4ca57a1548803744f7bdec6bac1b1a1684429fe2b66be1118
7
- data.tar.gz: 2e67fb7b6f31ca587c37855d65ede4ef7ea3c9153eedce5a00f7003312b20cb87821060ecf4abdc3637a6aaa5899847659c153491e18a1e2fce81d7dd320bd8b
6
+ metadata.gz: 9a5991f1f7b0262f7b2e3069572d4221f0f944be86a869e9a6bb5bdabe26441305882898788840ef373dfb1671d7c2a8d9d59e1d5942e8d2df6ccc17eee1c114
7
+ data.tar.gz: 7e9c4dbf9f8d28970756ed8d516df46d5a4c2710754065818c2966de478ed32e1d189d0eec12de1ca5b0b23de5e077f9c4254e738442a7d4a1005e62d6a304d1
@@ -36,6 +36,9 @@ jobs:
36
36
  with:
37
37
  ruby-version: ${{ matrix.ruby }}
38
38
 
39
+ - if: matrix.os == 'macos-latest'
40
+ run: brew install autoconf automake libtool
41
+
39
42
  - uses: actions/cache@v2
40
43
  with:
41
44
  path: vendor/bundle
@@ -64,3 +67,17 @@ jobs:
64
67
  run: cinst -y plantuml
65
68
 
66
69
  - run: bundle exec rake
70
+
71
+ tests-passed:
72
+ needs: rake
73
+ runs-on: ubuntu-latest
74
+ continue-on-error: true
75
+ steps:
76
+ - name: Trigger tests passed event
77
+ uses: Sibz/github-status-action@v1
78
+ with:
79
+ authToken: ${{ secrets.METANORMA_CI_PAT_TOKEN || secrets.GITHUB_TOKEN }}
80
+ context: 'tests-passed-successfully'
81
+ description: 'Tests passed successfully'
82
+ state: 'success'
83
+ sha: ${{ github.event.pull_request.head.sha || github.sha }}
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
@@ -7,6 +7,7 @@ require "open-uri"
7
7
  require "isodoc"
8
8
  require "relaton"
9
9
  require "fileutils"
10
+ require "metanorma-utils"
10
11
 
11
12
  module Asciidoctor
12
13
  module Standoc
@@ -91,6 +92,7 @@ module Asciidoctor
91
92
  @draft = false
92
93
  @refids = Set.new
93
94
  @anchors = {}
95
+ @internal_eref_namespaces = []
94
96
  @draft = node.attributes.has_key?("draft")
95
97
  @novalid = node.attr("novalid")
96
98
  @smartquotes = node.attr("smartquotes") != "false"
@@ -98,28 +100,32 @@ module Asciidoctor
98
100
  @fontheader = default_fonts(node)
99
101
  @files_to_delete = []
100
102
  @filename = node.attr("docfile") ? File.basename(node.attr("docfile")).gsub(/\.adoc$/, "") : ""
101
- @localdir = Utils::localdir(node)
103
+ @localdir = Metanorma::Utils::localdir(node)
102
104
  @output_dir = outputdir node
103
105
  @no_isobib_cache = node.attr("no-isobib-cache")
104
106
  @no_isobib = node.attr("no-isobib")
107
+ @sourcecode_markup_start = node.attr("sourcecode-markup-start") || "{{{"
108
+ @sourcecode_markup_end = node.attr("sourcecode-markup-end") || "}}}"
105
109
  @bibdb = nil
106
110
  @seen_headers = []
107
111
  @datauriimage = node.attr("data-uri-image")
108
112
  @boilerplateauthority = node.attr("boilerplate-authority")
109
- @log = Asciidoctor::Standoc::Log.new
113
+ @sourcecode_markup_start = node.attr("sourcecode-markup-start") || "{{{"
114
+ @sourcecode_markup_end = node.attr("sourcecode-markup-end") || "}}}"
115
+ @log = Metanorma::Utils::Log.new
110
116
  init_bib_caches(node)
111
117
  init_iev_caches(node)
112
118
  @lang = (node.attr("language") || "en")
113
- @script = (node.attr("script") || "Latn")
119
+ @script = (node.attr("script") || default_script(node.attr("language")))
114
120
  @isodoc = isodoc(@lang, @script, node.attr("i18nyaml"))
115
121
  @i18n = @isodoc.i18n
116
122
  end
117
123
 
118
124
  def default_fonts(node)
119
125
  b = node.attr("body-font") ||
120
- (node.attr("script") == "Hans" ? '"SimSun",serif' : '"Cambria",serif')
126
+ (node.attr("script") == "Hans" ? '"Source Han Sans",serif' : '"Cambria",serif')
121
127
  h = node.attr("header-font") ||
122
- (node.attr("script") == "Hans" ? '"SimHei",sans-serif' : '"Cambria",serif')
128
+ (node.attr("script") == "Hans" ? '"Source Han Sans",sans-serif' : '"Cambria",serif')
123
129
  m = node.attr("monospace-font") || '"Courier New",monospace'
124
130
  "$bodyfont: #{b};\n$headerfont: #{h};\n$monospacefont: #{m};\n"
125
131
  end
@@ -133,7 +139,7 @@ module Asciidoctor
133
139
 
134
140
  def document(node)
135
141
  init(node)
136
- ret = makexml(node).to_xml(indent: 2)
142
+ ret = makexml(node).to_xml(encoding: "US-ASCII", indent: 2)
137
143
  outputs(node, ret) unless node.attr("nodoc") || !node.attr("docfile")
138
144
  clean_exit
139
145
  ret
@@ -185,52 +191,35 @@ module Asciidoctor
185
191
  end
186
192
  end
187
193
 
188
- def term_source_attrs(seen_xref)
189
- { bibitemid: seen_xref.children[0]["target"],
190
- format: seen_xref.children[0]["format"], type: "inline" }
191
- end
192
-
193
- def add_term_source(xml_t, seen_xref, m)
194
- if seen_xref.children[0].name == "concept"
195
- xml_t.origin { |o| o << seen_xref.children[0].to_xml }
194
+ def default_script(lang)
195
+ case lang
196
+ when "ar", "fa"
197
+ "Arab"
198
+ when "ur"
199
+ "Aran"
200
+ when "ru", "bg"
201
+ "Cyrl"
202
+ when "hi"
203
+ "Deva"
204
+ when "el"
205
+ "Grek"
206
+ when "zh"
207
+ "Hans"
208
+ when "ko"
209
+ "Kore"
210
+ when "he"
211
+ "Hebr"
212
+ when "ja"
213
+ "Jpan"
196
214
  else
197
- xml_t.origin seen_xref.children[0].content, **attr_code(term_source_attrs(seen_xref))
215
+ "Latn"
198
216
  end
199
- m[:text] && xml_t.modification do |mod|
200
- mod.p { |p| p << m[:text].sub(/^\s+/, "") }
201
- end
202
- end
203
-
204
- TERM_REFERENCE_RE_STR = <<~REGEXP.freeze
205
- ^(?<xref><(xref|concept)[^>]+>([^<]*</(xref|concept)>)?)
206
- (,\s(?<text>.*))?
207
- $
208
- REGEXP
209
- TERM_REFERENCE_RE =
210
- Regexp.new(TERM_REFERENCE_RE_STR.gsub(/\s/, "").gsub(/_/, "\\s"),
211
- Regexp::IGNORECASE | Regexp::MULTILINE)
212
-
213
- def extract_termsource_refs(text, node)
214
- matched = TERM_REFERENCE_RE.match text
215
- matched.nil? and @log.add("AsciiDoc Input", node, "term reference not in expected format: #{text}")
216
- matched
217
- end
218
-
219
- def termsource(node)
220
- matched = extract_termsource_refs(node.content, node) || return
221
- noko do |xml|
222
- attrs = { status: matched[:text] ? "modified" : "identical" }
223
- xml.termsource **attrs do |xml_t|
224
- seen_xref = Nokogiri::XML.fragment(matched[:xref])
225
- add_term_source(xml_t, seen_xref, matched)
226
- end
227
- end.join("\n")
228
217
  end
229
218
 
230
219
  private
231
220
 
232
221
  def outputdir(node)
233
- if node.attr("output_dir").nil_or_empty? then Utils::localdir(node)
222
+ if node.attr("output_dir").nil_or_empty? then Metanorma::Utils::localdir(node)
234
223
  else File.join(node.attr("output_dir"), "")
235
224
  end
236
225
  end
@@ -729,9 +729,11 @@
729
729
  </define>
730
730
  <define name="index">
731
731
  <element name="index">
732
- <attribute name="to">
733
- <data type="IDREF"/>
734
- </attribute>
732
+ <optional>
733
+ <attribute name="to">
734
+ <data type="IDREF"/>
735
+ </attribute>
736
+ </optional>
735
737
  <element name="primary">
736
738
  <oneOrMore>
737
739
  <ref name="PureTextElement"/>
@@ -6,11 +6,11 @@ module Asciidoctor
6
6
  module Standoc
7
7
  module Blocks
8
8
  def id_attr(node = nil)
9
- { id: Utils::anchor_or_uuid(node) }
9
+ { id: Metanorma::Utils::anchor_or_uuid(node) }
10
10
  end
11
11
 
12
12
  def id_unnum_attrs(node)
13
- attr_code( id: Utils::anchor_or_uuid(node),
13
+ attr_code( id: Metanorma::Utils::anchor_or_uuid(node),
14
14
  unnumbered: node.option?("unnumbered") ? "true" : nil,
15
15
  number: node.attr("number"),
16
16
  subsequence: node.attr("subsequence") )
@@ -29,7 +29,7 @@ module Asciidoctor
29
29
  # We append each contained block to its parent
30
30
  def open(node)
31
31
  role = node.role || node.attr("style")
32
- Utils::reqt_subpart(role) and return requirement_subpart(node)
32
+ reqt_subpart(role) and return requirement_subpart(node)
33
33
  result = []
34
34
  node.blocks.each do |b|
35
35
  result << send(b.context, b)
@@ -45,7 +45,7 @@ module Asciidoctor
45
45
  noko do |xml|
46
46
  xml.figure **literal_attrs(node) do |f|
47
47
  figure_title(node, f)
48
- f.pre node.lines.join("\n"), **attr_code(id: Utils::anchor_or_uuid,
48
+ f.pre node.lines.join("\n"), **attr_code(id: Metanorma::Utils::anchor_or_uuid,
49
49
  alt: node.attr("alt"))
50
50
  end
51
51
  end
@@ -74,11 +74,20 @@ module Asciidoctor
74
74
  %w(recommendation requirement permission).include?(role) and
75
75
  return requirement(node, role)
76
76
  return pseudocode_example(node) if role == "pseudocode"
77
+ return svgmap_example(node) if role == "svgmap"
77
78
  example_proper(node)
78
79
  end
79
80
 
81
+ def svgmap_example(node)
82
+ noko do |xml|
83
+ xml.svgmap **attr_code(example_attrs(node).merge(src: node.attr("src"), alt: node.attr("alt"))) do |ex|
84
+ ex << node.content
85
+ end
86
+ end.join("\n")
87
+ end
88
+
89
+ # prevent A's and other subs inappropriate for pseudocode
80
90
  def pseudocode_example(node)
81
- # prevent A's and other subs inappropriate for pseudocode
82
91
  node.blocks.each { |b| b.remove_sub(:replacements) }
83
92
  noko do |xml|
84
93
  xml.figure **example_attrs(node).merge(class: "pseudocode") do |ex|
@@ -121,7 +130,7 @@ module Asciidoctor
121
130
 
122
131
  def para_attrs(node)
123
132
  attr_code(keep_attrs(node).merge(align: node.attr("align"),
124
- id: Utils::anchor_or_uuid(node)))
133
+ id: Metanorma::Utils::anchor_or_uuid(node)))
125
134
  end
126
135
 
127
136
  def paragraph(node)
@@ -135,7 +144,7 @@ module Asciidoctor
135
144
 
136
145
  def quote_attrs(node)
137
146
  attr_code(keep_attrs(node).merge(align: node.attr("align"),
138
- id: Utils::anchor_or_uuid(node)))
147
+ id: Metanorma::Utils::anchor_or_uuid(node)))
139
148
  end
140
149
 
141
150
  def quote_attribution(node, out)
@@ -160,10 +169,10 @@ module Asciidoctor
160
169
 
161
170
  def listing_attrs(node)
162
171
  attr_code(keep_attrs(node).merge(lang: node.attr("language"),
163
- id: Utils::anchor_or_uuid(node),
164
- unnumbered: node.option?("unnumbered") ? "true" : nil,
165
- number: node.attr("number"),
166
- filename: node.attr("filename")))
172
+ id: Metanorma::Utils::anchor_or_uuid(node),
173
+ unnumbered: node.option?("unnumbered") ? "true" : nil,
174
+ number: node.attr("number"),
175
+ filename: node.attr("filename")))
167
176
  end
168
177
 
169
178
  # NOTE: html escaping is performed by Nokogiri
@@ -35,7 +35,7 @@ module Asciidoctor
35
35
  date = node.attr("date") || Date.today.iso8601.gsub(/\+.*$/, "")
36
36
  date += "T00:00:00Z" unless /T/.match date
37
37
  attr_code(
38
- id: Utils::anchor_or_uuid(node),
38
+ id: ::Metanorma::Utils::anchor_or_uuid(node),
39
39
  reviewer: node.attr("reviewer") || node.attr("source") || "(Unknown)",
40
40
  date: date )
41
41
  end
@@ -69,7 +69,7 @@ module Asciidoctor
69
69
  a = node.attr("type") and ["danger", "safety precautions"].each do |t|
70
70
  name = t if a.casecmp(t).zero?
71
71
  end
72
- attr_code(keep_attrs(node).merge(id: Utils::anchor_or_uuid(node), type: name,
72
+ attr_code(keep_attrs(node).merge(id: Metanorma::Utils::anchor_or_uuid(node), type: name,
73
73
  beforeclauses: node.attr("beforeclauses") == "true" ? "true" : nil))
74
74
  end
75
75
 
@@ -5,11 +5,13 @@ require "html2doc"
5
5
  require_relative "./cleanup_block.rb"
6
6
  require_relative "./cleanup_footnotes.rb"
7
7
  require_relative "./cleanup_ref.rb"
8
+ require_relative "./cleanup_ref_dl.rb"
8
9
  require_relative "./cleanup_boilerplate.rb"
9
10
  require_relative "./cleanup_section.rb"
10
11
  require_relative "./cleanup_terms.rb"
11
12
  require_relative "./cleanup_inline.rb"
12
13
  require_relative "./cleanup_amend.rb"
14
+ require_relative "./cleanup_maths.rb"
13
15
  require "relaton_iev"
14
16
 
15
17
  module Asciidoctor
@@ -23,26 +25,15 @@ module Asciidoctor
23
25
  </passthrough>}mx) { |m| HTMLEntities.new.decode($1) }
24
26
  end
25
27
 
26
- def asciimath2mathml(text)
27
- text = text.gsub(%r{<stem type="AsciiMath">(.+?)</stem>}m) do |m|
28
- "<amathstem>#{HTMLEntities.new.decode($1)}</amathstem>"
29
- end
30
- text = Html2Doc.
31
- asciimath_to_mathml(text, ["<amathstem>", "</amathstem>"])
32
- x = Nokogiri::XML(text)
33
- x.xpath("//*[local-name() = 'math'][not(parent::stem)]").each do |y|
34
- y.wrap("<stem type='MathML'></stem>")
35
- end
36
- x.to_xml
37
- end
38
-
39
28
  def cleanup(xmldoc)
40
29
  element_name_cleanup(xmldoc)
41
30
  sections_cleanup(xmldoc)
42
31
  obligations_cleanup(xmldoc)
43
32
  table_cleanup(xmldoc)
44
33
  formula_cleanup(xmldoc)
34
+ sourcecode_cleanup(xmldoc)
45
35
  figure_cleanup(xmldoc)
36
+ element_name_cleanup(xmldoc)
46
37
  ref_cleanup(xmldoc)
47
38
  note_cleanup(xmldoc)
48
39
  clausebefore_cleanup(xmldoc)
@@ -67,6 +58,7 @@ module Asciidoctor
67
58
  bookmark_cleanup(xmldoc)
68
59
  requirement_cleanup(xmldoc)
69
60
  bibdata_cleanup(xmldoc)
61
+ svgmap_cleanup(xmldoc)
70
62
  boilerplate_cleanup(xmldoc)
71
63
  smartquotes_cleanup(xmldoc)
72
64
  variant_cleanup(xmldoc)
@@ -78,14 +70,13 @@ module Asciidoctor
78
70
  end
79
71
 
80
72
  def smartquotes_cleanup(xmldoc)
81
- xmldoc.xpath("//date").each { |d| Utils::endash_date(d) }
73
+ xmldoc.xpath("//date").each { |d| Metanorma::Utils::endash_date(d) }
82
74
  xmldoc.traverse do |n|
83
75
  next unless n.text?
84
76
  if @smartquotes
85
77
  /[-'"(<>]|\.\.|\dx/.match(n) or next
86
- n.ancestors("pre, tt, sourcecode, bibdata, on, "\
87
- "stem, figure[@class = 'pseudocode']").empty? or next
88
- n.replace(Utils::smartformat(n.text))
78
+ n.ancestors("pre, tt, sourcecode, bibdata, on, stem, figure[@class = 'pseudocode']").empty? or next
79
+ n.replace(Metanorma::Utils::smartformat(n.text))
89
80
  else
90
81
  n.replace(n.text.gsub(/(?<=\p{Alnum})\u2019(?=\p{Alpha})/, "'"))#.
91
82
  #gsub(/</, "&lt;").gsub(/>/, "&gt;"))
@@ -148,60 +139,26 @@ module Asciidoctor
148
139
  align_callouts_to_annotations(xmldoc)
149
140
  end
150
141
 
151
- def xml_unescape_mathml(x)
152
- return if x.children.any? { |y| y.element? }
153
- math = x.text.gsub(/&lt;/, "<").gsub(/&gt;/, ">").gsub(/&quot;/, '"').
154
- gsub(/&apos;/, "'").gsub(/&amp;/, "&").
155
- gsub(/<[^: \r\n\t\/]+:/, "<").gsub(/<\/[^ \r\n\t:]+:/, "</")
156
- x.children = math
157
- end
158
-
159
- MATHML_NS = "http://www.w3.org/1998/Math/MathML".freeze
160
-
161
- def mathml_preserve_space(m)
162
- m.xpath(".//m:mtext", "m" => MATHML_NS).each do |x|
163
- x.children = x.children.to_xml.gsub(/^\s/, "&#xA0;").
164
- gsub(/\s$/, "&#xA0;")
165
- end
166
- end
167
-
168
- def mathml_namespace(stem)
169
- stem.xpath("./math", ).each { |x| x.default_namespace = MATHML_NS }
170
- end
171
-
172
- def mathml_mi_italics
173
- { uppergreek: true, upperroman: true,
174
- lowergreek: true, lowerroman: true }
175
- end
176
-
177
- # presuppose multichar mi upright, singlechar mi MathML default italic
178
- def mathml_italicise(x)
179
- x.xpath(".//m:mi[not(ancestor::*[@mathvariant])]", "m" => MATHML_NS).each do |i|
180
- char = HTMLEntities.new.decode(i.text)
181
- i["mathvariant"] = "normal" if mi_italicise?(char)
182
- end
183
- end
184
-
185
- def mi_italicise?(c)
186
- return false if c.length > 1
187
- if /\p{Greek}/.match(c)
188
- /\p{Lower}/.match(c) && !mathml_mi_italics[:lowergreek] ||
189
- /\p{Upper}/.match(c) && !mathml_mi_italics[:uppergreek]
190
- elsif /\p{Latin}/.match(c)
191
- /\p{Lower}/.match(c) && !mathml_mi_italics[:lowerroman] ||
192
- /\p{Upper}/.match(c) && !mathml_mi_italics[:upperroman]
193
- else
194
- false
142
+ def sourcecode_cleanup(xmldoc)
143
+ xmldoc.xpath("//sourcecode").each do |x|
144
+ x.traverse do |n|
145
+ next unless n.text?
146
+ next unless /#{Regexp.escape(@sourcecode_markup_start)}/.match(n.text)
147
+ n.replace(sourcecode_markup(n))
148
+ end
195
149
  end
196
150
  end
197
151
 
198
- def mathml_cleanup(xmldoc)
199
- xmldoc.xpath("//stem[@type = 'MathML']").each do |x|
200
- xml_unescape_mathml(x)
201
- mathml_namespace(x)
202
- mathml_preserve_space(x)
203
- mathml_italicise(x)
152
+ def sourcecode_markup(n)
153
+ acc = []
154
+ n.text.split(/(#{Regexp.escape(@sourcecode_markup_start)}|#{Regexp.escape(@sourcecode_markup_end)})/).
155
+ each_slice(4).map do |a|
156
+ acc << Nokogiri::XML::Text.new(a[0], n.document).
157
+ to_xml(encoding: "US-ASCII", save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION)
158
+ next unless a.size == 4
159
+ acc << Asciidoctor.convert(a[2], backend: (self&.backend&.to_sym || :standoc), doctype: :inline)
204
160
  end
161
+ acc.join
205
162
  end
206
163
 
207
164
  # allows us to deal with doc relation localities,
@@ -215,23 +172,18 @@ module Asciidoctor
215
172
 
216
173
  def img_cleanup(xmldoc)
217
174
  return xmldoc unless @datauriimage
218
- xmldoc.xpath("//image").each do |i|
219
- i["src"] = datauri(i["src"])
220
- end
175
+ xmldoc.xpath("//image").each { |i| i["src"] = Metanorma::Utils::datauri(i["src"], @localdir) }
221
176
  end
222
177
 
223
178
  def variant_cleanup(xmldoc)
224
179
  xmldoc.xpath("//*[variant]").each do |c|
225
- c&.next&.text? && c&.next&.next&.name == "variant" &&
226
- c.next.text.gsub(/\s/, "").empty? and c.next.remove
180
+ c&.next&.text? && c&.next&.next&.name == "variant" && c.next.text.gsub(/\s/, "").empty? and
181
+ c.next.remove
227
182
  end
228
183
  xmldoc.xpath("//*[variant]").each do |c|
229
- next unless c.children.any? do |n|
230
- n.name != "variant" && (!n.text? || !n.text.gsub(/\s/, "").empty?)
231
- end
184
+ next unless c.children.any? { |n| n.name != "variant" && (!n.text? || !n.text.gsub(/\s/, "").empty?) }
232
185
  c.xpath("./variant").each do |n|
233
- if n.at_xpath('preceding-sibling::node()[not(self::text()'\
234
- '[not(normalize-space())])][1][self::variantwrap]')
186
+ if n.at_xpath('preceding-sibling::node()[not(self::text()[not(normalize-space())])][1][self::variantwrap]')
235
187
  n.previous_element << n
236
188
  else
237
189
  n.replace('<variantwrap/>').first << n