Module: Metanorma::Standoc::Inline

Includes:
Utils
Included in:
Cleanup, Converter
Defined in:
lib/metanorma/cleanup/inline.rb,
lib/metanorma/converter/anchor.rb,
lib/metanorma/converter/inline.rb

Constant Summary collapse

XREF_ATTRS =
"hidden|style|droploc|capital|lowercase|label".freeze
STEM_ATTRS =
"number-format".freeze

Constants included from Utils

Utils::SECTION_CONTAINERS, Utils::SUBCLAUSE_XPATH

Instance Method Summary collapse

Methods included from Utils

#add_id, #add_id_text, #add_noko_elem, adoc2xml, #asciimath_key, #attr_code, #complete_and_compare_dates, #complete_iso_date, #complete_year_month, #complete_year_only, #convert, #csv_split, #dl_to_attrs, #dl_to_elems, #document_ns_attributes, #grkletters, #insert_before, #isodoc, #isolated_asciidoctor_convert, #kv_parse, #link_unwrap, #noko, #parse_complete_date, #parse_partial_date, #processor, #quoted_csv_split, #refid?, #section_containers, #separate_numbering_footnotes, #term_expr, #textcleanup, #to_xml, #wrap_in_para, #xml_encode

Instance Method Details

#altmedia_id_attr(node, altmedia) ⇒ Object



175
176
177
178
179
180
181
182
# File 'lib/metanorma/converter/inline.rb', line 175

def altmedia_id_attr(node, altmedia)
  ret = id_attr(node)
  if altmedia
    add_id(ret)
    ret.delete(:anchor)
  end
  ret
end

#anchor_cleanup(elem) ⇒ Object



133
134
135
# File 'lib/metanorma/cleanup/inline.rb', line 133

def anchor_cleanup(elem)
  contenthash_id_cleanup(elem)
end

#bookmark_cleanup(xmldoc) ⇒ Object



25
26
27
28
29
# File 'lib/metanorma/cleanup/inline.rb', line 25

def bookmark_cleanup(xmldoc)
  redundant_bookmark_cleanup(xmldoc)
  li_bookmark_cleanup(xmldoc)
  dt_bookmark_cleanup(xmldoc)
end

#bookmark_to_id(elem, bookmark) ⇒ Object



42
43
44
45
46
47
# File 'lib/metanorma/cleanup/inline.rb', line 42

def bookmark_to_id(elem, bookmark)
  parent = bookmark.parent
  elem["id"] = bookmark.remove["id"]
  elem["anchor"] = bookmark.remove["anchor"]
  strip_initial_space(parent)
end

#concatenate_attributes_to_xref_text(node) ⇒ Object



41
42
43
44
45
46
# File 'lib/metanorma/converter/anchor.rb', line 41

def concatenate_attributes_to_xref_text(node)
  node.attributes.each_with_object([]) do |(k, v), m|
    %w(path fragment refid).include?(k) and next
    m << "#{k}=#{v}%"
  end.map { |x| x.sub(/%+/, "%") }.join + (node.text || "")
end

#concept_cleanup(xmldoc) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/metanorma/cleanup/inline.rb', line 72

def concept_cleanup(xmldoc)
  xmldoc.xpath("//concept[not(termxref)]").each do |x|
    term = x.at("./refterm")
    term&.remove if term&.text&.empty?
    concept_cleanup1(x)
  end
end

#concept_cleanup1(elem) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/metanorma/cleanup/inline.rb', line 80

def concept_cleanup1(elem)
  elem.children.remove if elem&.children&.text&.strip&.empty?
  key_extract_locality(elem)
  if elem["key"].include?(":") then concept_termbase_cleanup(elem)
  elsif refid? elem["key"] then concept_eref_cleanup(elem)
  else concept_xref_cleanup(elem)
  end
  elem.delete("key")
end

#concept_eref_cleanup(elem) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/metanorma/cleanup/inline.rb', line 117

def concept_eref_cleanup(elem)
  t = elem.at("./xrefrender")&.remove&.children&.to_xml
  l = elem.at("./locality")&.remove&.children&.to_xml
  elem.add_child "<eref bibitemid='#{elem['key']}'>#{l}</eref>"
  extract_localities(elem.elements[-1])
  elem.elements[-1].add_child(t) if t
end

#concept_termbase_cleanup(elem) ⇒ Object



105
106
107
108
109
110
# File 'lib/metanorma/cleanup/inline.rb', line 105

def concept_termbase_cleanup(elem)
  t = elem&.at("./xrefrender")&.remove&.children
  termbase, key = elem["key"].split(":", 2)
  elem.add_child(%(<termref base="#{termbase}" target="#{key}">) +
                 "#{t&.to_xml}</termref>")
end

#concept_xref_cleanup(elem) ⇒ Object



112
113
114
115
# File 'lib/metanorma/cleanup/inline.rb', line 112

def concept_xref_cleanup(elem)
  t = elem&.at("./xrefrender")&.remove&.children
  elem.add_child(%(<xref target="#{elem['key']}">#{t&.to_xml}</xref>))
end

#contenthash_id_cleanup(doc) ⇒ Object



137
138
139
# File 'lib/metanorma/cleanup/inline.rb', line 137

def contenthash_id_cleanup(doc)
  @contenthash_ids = contenthash_id_make(doc)
end

#contenthash_id_make(doc) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/metanorma/cleanup/inline.rb', line 141

def contenthash_id_make(doc)
  doc.xpath("//*[@id]").each_with_object({}) do |x, m|
    # should always be true
    Metanorma::Utils::guid_anchor?(x["id"]) or next
    m[x["id"]] = Metanorma::Utils::contenthash(x)
    x["anchor"] and m[x["anchor"]] = m[x["id"]]
    x["id"] = m[x["id"]]
  end
end

#dt_bookmark_cleanup(xmldoc) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/metanorma/cleanup/inline.rb', line 59

def dt_bookmark_cleanup(xmldoc)
  xmldoc.xpath("//dt[descendant::bookmark]").each do |x|
    if x.at("./*[1][local-name() = 'p']/" \
            "*[1][local-name() = 'bookmark']") &&
        empty_text_before_first_element(x.elements[0])
      bookmark_to_id(x, x.elements[0].elements[0])
    elsif x.at("./*[1][local-name() = 'bookmark']") &&
        empty_text_before_first_element(x)
      bookmark_to_id(x, x.elements[0])
    end
  end
end

#duplicate_langvariants(container, variants) ⇒ Object



208
209
210
211
212
213
214
215
# File 'lib/metanorma/cleanup/inline.rb', line 208

def duplicate_langvariants(container, variants)
  lang_variant_to_node(variants.first, container)
  variants[1..].reverse.each do |node|
    new = container.dup
    lang_variant_to_node(node, new)
    container.next = new
  end
end

#empty_text_before_first_element(elem) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/metanorma/cleanup/inline.rb', line 8

def empty_text_before_first_element(elem)
  elem.children.each do |c|
    return false if c.text? && /\S/.match(c.text)
    return true if c.element?
  end
  true
end

#hash2styles(role) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/metanorma/converter/inline.rb', line 123

def hash2styles(role)
  CSV.parse_line(role, liberal_parsing: true)
    .each_with_object({}) do |r, m|
    kv = r.split(":", 2).map(&:strip)
    case kv[0]
    when "custom-charset"
      m[kv[0]] = kv[1]
    end
  end
end

#highlight_parse(text, xml) ⇒ Object



82
83
84
# File 'lib/metanorma/converter/inline.rb', line 82

def highlight_parse(text, xml)
  add_noko_elem(xml, "span", text, class: "fmt-hi")
end

#image_attributes(node, src: true, altmedia: nil) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/metanorma/converter/inline.rb', line 141

def image_attributes(node, src: true, altmedia: nil)
  if src
    sourceuri = image_src_uri(node)
    uri = sourceuri
    type = image_mimetype(uri)
    uri = uri.sub(%r{^data:image/\*;}, "data:#{type};")
  end
  image_attributes1(node, uri, sourceuri, type, altmedia)
end

#image_attributes1(node, uri, sourceuri, type, altmedia) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/metanorma/converter/inline.rb', line 163

def image_attributes1(node, uri, sourceuri, type, altmedia)
  /^data:/.match?(sourceuri) and sourceuri = nil
  attr_code(altmedia_id_attr(node, altmedia)
    .merge(src: uri, mimetype: type,
           height: node.attr("height") || "auto",
           width: node.attr("width") || "auto",
           filename: node.attr("filename") || sourceuri,
           title: node.attr("titleattr"),
           media: node.attr("media"), altmedia: altmedia,
           alt: node.alt == node.attr("default-alt") ? nil : node.alt))
end

#image_mimetype(uri) ⇒ Object



134
135
136
137
138
139
# File 'lib/metanorma/converter/inline.rb', line 134

def image_mimetype(uri)
  types = if /^data:/.match?(uri) then Vectory::Utils::datauri2mime(uri)
          else MIME::Types.type_for(uri)
          end
  types.first.to_s
end

#image_src_uri(node) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/metanorma/converter/inline.rb', line 151

def image_src_uri(node)
  nodetarget = node.attr("target") || node.target
  if Gem.win_platform? && /^[a-zA-Z]:/.match?(nodetarget)
    nodetarget.prepend("/")
  end
  uri = node.image_uri(nodetarget)
  if Gem.win_platform? && /^\/[a-zA-Z]:/.match?(uri)
    uri = uri[1..]
  end
  uri
end

#inline_anchor(node) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/metanorma/converter/anchor.rb', line 6

def inline_anchor(node)
  case node.type
  when :ref then inline_anchor_ref node
  when :xref then inline_anchor_xref node
  when :link then inline_anchor_link node
  when :bibref then inline_anchor_bibref node
  end
end

#inline_anchor_bibref(node) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/metanorma/converter/anchor.rb', line 100

def inline_anchor_bibref(node)
  eref_contents = inline_anchor_bibref_contents(node)
  @refids << (node.target || node.id)
  noko do |xml|
    xml.ref **attr_code(id: node.target || node.id) do |r|
      r << eref_contents
    end
  end
end

#inline_anchor_bibref_contents(node) ⇒ Object



110
111
112
113
# File 'lib/metanorma/converter/anchor.rb', line 110

def inline_anchor_bibref_contents(node)
  @c.decode(node.text || node.target || node.id)
    &.sub(/^\[?([^\[\]]+?)\]?$/, "[\\1]")
end


81
82
83
84
85
86
87
88
# File 'lib/metanorma/converter/anchor.rb', line 81

def inline_anchor_link(node)
  contents, attributes = inline_anchor_link_attrs(node)
  noko do |xml|
    xml.link **attr_code(attributes) do |l|
      l << contents
    end
  end
end


90
91
92
93
94
95
96
97
98
# File 'lib/metanorma/converter/anchor.rb', line 90

def inline_anchor_link_attrs(node)
  contents = node.text
  contents = "" if node.target.gsub(%r{^mailto:}, "") == node.text
  attributes = { target: node.target, alt: node.attr("title"),
                 style: node.attr("style")&.sub(/%$/, ""),
                 "update-type": node.attr("updatetype") ||
                   node.attr("update-type") }
  [contents, attributes]
end

#inline_anchor_ref(node) ⇒ Object



15
16
17
18
19
# File 'lib/metanorma/converter/anchor.rb', line 15

def inline_anchor_ref(node)
  noko do |xml|
    xml.bookmark nil, **attr_code(id_attr(node))
  end
end

#inline_anchor_xref(node) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/metanorma/converter/anchor.rb', line 21

def inline_anchor_xref(node)
  noko do |xml|
    attrs = inline_anchor_xref_attrs(node)
    c = attrs[:text]
    attrs.delete(:text) unless c.nil?
    xml.xref **attr_code(attrs) do |x|
      x << c
    end
  end
end

#inline_anchor_xref_attrs(node) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/metanorma/converter/anchor.rb', line 32

def inline_anchor_xref_attrs(node)
  text = concatenate_attributes_to_xref_text(node)
  t = node.target.gsub(/^#/, "").gsub(%r{(\.xml|\.adoc)(#.*$)}, "\\2")
  attrs, text = inline_anchor_xref_match(text)
  attrs.empty? and
    return { target: t, type: "inline", text:, defaultstyle: @xrefstyle }
  inline_anchor_xref_attrs1(attrs, t, text)
end

#inline_anchor_xref_attrs1(attrs, target, text) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/metanorma/converter/anchor.rb', line 48

def inline_anchor_xref_attrs1(attrs, target, text)
  { target:, hidden: attrs["hidden"],
    type: attrs.key?("fn") ? "footnote" : "inline",
    case: %w(capital lowercase).detect { |x| attrs.key?(x) },
    label: attrs["label"],
    style: attrs["style"],
    defaultstyle: @xrefstyle,
    droploc: attrs.key?("droploc") || nil,
    text: }.compact
end

#inline_anchor_xref_match(text) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/metanorma/converter/anchor.rb', line 61

def inline_anchor_xref_match(text)
  attrs = {}
  while m = /^(#{XREF_ATTRS})(=[^%]+)?%(.*)$/o.match(text)
    text = m[3]
    attrs[m[1]] = m[2]&.sub(/^=/, "")
  end
  if m = /^(fn:?\s*)(\S.*)?$/.match(text)
    text = m[2]
    attrs["fn"] = ""
  end
  [attrs, text]
end

#inline_anchor_xref_text(match, text) ⇒ Object



74
75
76
77
78
79
# File 'lib/metanorma/converter/anchor.rb', line 74

def inline_anchor_xref_text(match, text)
  if %i[case fn drop drop2 hidden style].any? { |x| !match[x].nil? }
    match[:text]
  else text
  end
end

#inline_break(node) ⇒ Object



9
10
11
12
13
14
# File 'lib/metanorma/converter/inline.rb', line 9

def inline_break(node)
  noko do |xml|
    xml << node.text
    xml.br
  end
end

#inline_callout(node) ⇒ Object



115
116
117
118
119
# File 'lib/metanorma/converter/anchor.rb', line 115

def inline_callout(node)
  noko do |xml|
    xml.callout node.text
  end
end

#inline_footnote(node) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/metanorma/converter/anchor.rb', line 121

def inline_footnote(node)
  @fn_number ||= 0
  noko do |xml|
    @fn_number += 1
    xml.fn **attr_code(id_attr(nil).merge(reference: @fn_number)) do |fn|
      fn.p { |p| p << node.text }
    end
  end
end

#inline_image(node) ⇒ Object



184
185
186
187
188
# File 'lib/metanorma/converter/inline.rb', line 184

def inline_image(node)
  noko do |xml|
    xml.image **image_attributes(node)
  end
end

#inline_indexterm(node) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/metanorma/converter/inline.rb', line 190

def inline_indexterm(node)
  noko do |xml|
    node.type == :visible and xml << node.text
    terms, see, also = inline_indexterm_extract(node)
    (see || also) or inline_indexterm1(xml, terms)
    also and
      inline_indexterm_see(xml, terms, also, true)
    see and
      inline_indexterm_see(xml, terms, [see], false)
  end
end

#inline_indexterm1(xml, terms) ⇒ Object



209
210
211
212
213
214
215
# File 'lib/metanorma/converter/inline.rb', line 209

def inline_indexterm1(xml, terms)
  xml.index do |i|
    add_noko_elem(i, "primary", terms[0])
    add_noko_elem(i, "secondary", terms[1])
    add_noko_elem(i, "tertiary", terms[2])
  end
end

#inline_indexterm_extract(node) ⇒ Object



202
203
204
205
206
207
# File 'lib/metanorma/converter/inline.rb', line 202

def inline_indexterm_extract(node)
  terms = (node.attr("terms") || [node.text]).map { |x| xml_encode(x) }
  see = node.attr("see")
  also = node.attr("see-also")
  [terms, see, also]
end

#inline_indexterm_see(xml, terms, ref, also) ⇒ Object



217
218
219
220
221
222
223
224
225
226
# File 'lib/metanorma/converter/inline.rb', line 217

def inline_indexterm_see(xml, terms, ref, also)
  ref.each do |r|
    xml.index_xref also: also do |i|
      add_noko_elem(i, "primary", terms[0])
      add_noko_elem(i, "secondary", terms[1])
      add_noko_elem(i, "tertiary", terms[2])
      add_noko_elem(i, "target", r)
    end
  end
end

#inline_quoted(node) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/metanorma/converter/inline.rb', line 86

def inline_quoted(node)
  noko do |xml|
    case node.type
    when :emphasis then xml.em { |s| s << node.text }
    when :strong then xml.strong { |s| s << node.text }
    when :monospaced then xml.tt { |s| s << node.text }
    when :double then xml << "\"#{node.text}\""
    when :single then xml << "'#{node.text}'"
    when :superscript then xml.sup { |s| s << node.text }
    when :subscript then xml.sub { |s| s << node.text }
    when :asciimath then stem_parse(node.text, xml, :asciimath, node)
    when :latexmath then stem_parse(node.text, xml, :latexmath, node)
    when :mark then highlight_parse(node.text, xml)
    else
      case node.role
        # the following three are legacy, they are now handled by macros
      when "alt"
        term_designation(xml, node, "admitted", node.text)
      when "deprecated"
        term_designation(xml, node, "deprecates", node.text)
      when "domain", "strike", "underline", "smallcap", "keyword"
        xml.send(node.role) { |s| s << node.text }
      when /^css /
        xml.span style: node.role.sub(/^css /, "") do |s|
          s << node.text
        end
      when /:/
        xml.span **attr_code(hash2styles(node.role)) do |s|
          s << node.text
        end
      else
        xml << node.text
      end
    end
  end
end

#key_extract_locality(elem) ⇒ Object



99
100
101
102
103
# File 'lib/metanorma/cleanup/inline.rb', line 99

def key_extract_locality(elem)
  elem["key"].include?(",") or return
  elem.add_child("<locality>#{elem['key'].sub(/^[^,]+,/, '')}</locality>")
  elem["key"] = elem["key"].sub(/(^[^,]+),.*$/, "\\1")
end

#lang_variant_to_node(variant, node) ⇒ Object



217
218
219
220
221
222
# File 'lib/metanorma/cleanup/inline.rb', line 217

def lang_variant_to_node(variant, node)
  node.children = variant.children
  node["lang"] = variant["lang"]
  node.delete("script")
  variant["script"] and node["script"] = variant["script"]
end

#latex_parse(text, xml, attr) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/metanorma/converter/inline.rb', line 70

def latex_parse(text, xml, attr)
  latex = latex_parse1(text, attr[:block]) or
    return xml.stem **attr, type: "MathML"
  xml.stem **attr, type: "MathML" do |s|
    math = Nokogiri::XML.fragment(latex.sub(/<\?[^>]+>/, ""))
      .elements[0]
    math.delete("alttext")
    s.parent.children = math
    s << "<latexmath>#{text}</latexmath>"
  end
end

#latex_parse1(text, block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/metanorma/converter/inline.rb', line 32

def latex_parse1(text, block)
  lxm_input = @c.decode(text)
  results = Plurimath::Math.parse(lxm_input, "latex")
    .to_mathml(display_style: block)
  if results.nil?
    @log.add("STANDOC_51", nil, params: [lxm_input])
    return
  end
  results.sub(%r{<math ([^>]+ )?display="block"}, "<math \\1")
end

#li_bookmark_cleanup(xmldoc) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/metanorma/cleanup/inline.rb', line 49

def li_bookmark_cleanup(xmldoc)
  xmldoc.xpath("//li[descendant::bookmark]").each do |x|
    if x.at("./*[1][local-name() = 'p']/" \
            "*[1][local-name() = 'bookmark']") &&
        empty_text_before_first_element(x.elements[0])
      bookmark_to_id(x, x.elements[0].elements[0])
    end
  end
end


168
169
170
# File 'lib/metanorma/cleanup/inline.rb', line 168

def link_cleanup(xmldoc)
  uri_cleanup(xmldoc)
end

#only_langvariant_children?(node) ⇒ Boolean

Returns:

  • (Boolean)


202
203
204
205
206
# File 'lib/metanorma/cleanup/inline.rb', line 202

def only_langvariant_children?(node)
  node.children.none? do |n|
    n.name != "lang-variant" && (!n.text? || !n.text.strip.empty?)
  end
end

#page_break(node) ⇒ Object



20
21
22
23
24
25
# File 'lib/metanorma/converter/inline.rb', line 20

def page_break(node)
  attrs = {}
  node.option?("landscape") and attrs[:orientation] = "landscape"
  node.option?("portrait") and attrs[:orientation] = "portrait"
  noko { |xml| xml.pagebreak **attr_code(attrs) }
end

#pass(node) ⇒ Object



16
17
18
# File 'lib/metanorma/converter/inline.rb', line 16

def pass(node)
  "<passthrough-inline formats='metanorma'>#{node.content}</passthrough-inline>"
end

#passthrough_cleanup(doc) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/metanorma/cleanup/inline.rb', line 151

def passthrough_cleanup(doc)
  doc.xpath("//passthrough-inline").each do |p|
    p.name = "passthrough"
    p.children = select_odd_chars(p.children.to_xml)
  end
  doc.xpath("//identifier").each do |p|
    p.children = select_odd_chars(p.children.to_xml)
  end
end

#passthrough_metanorma_cleanup(doc) ⇒ Object

overwrite xmldoc, so must assign result to xmldoc



162
163
164
165
166
# File 'lib/metanorma/cleanup/inline.rb', line 162

def passthrough_metanorma_cleanup(doc)
  ret = to_xml(doc)
    .gsub(%r{<passthrough formats="metanorma">([^<]*)</passthrough>}) { @c.decode($1) }
  doc.root = Nokogiri::XML(ret, &:huge).root
end

#redundant_bookmark_cleanup(xmldoc) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/metanorma/cleanup/inline.rb', line 31

def redundant_bookmark_cleanup(xmldoc)
  xmldoc.xpath("//bookmark").each do |b|
    p = b
    while !p.xml? && p = p.parent
      p["anchor"] == b["anchor"] or next
      b.remove
      break
    end
  end
end


90
91
92
93
94
95
96
97
# File 'lib/metanorma/cleanup/inline.rb', line 90

def related_cleanup(xmldoc)
  xmldoc.xpath("//related[not(termxref)]").each do |x|
    term = x.at("./refterm")
    term.replace("<preferred>#{term_expr(term.children.to_xml)}" \
                 "</preferred>")
    concept_cleanup1(x)
  end
end

#source_id_cleanup(xmldoc) ⇒ Object



184
185
186
187
188
189
# File 'lib/metanorma/cleanup/inline.rb', line 184

def source_id_cleanup(xmldoc)
  xmldoc.xpath("//span[normalize-space(.)=''][@source]").each do |s|
    s.parent["source"] = s["source"]
    s.remove
  end
end

#stem_attrs(node, text) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/metanorma/converter/inline.rb', line 59

def stem_attrs(node, text)
  attrs = STEM_ATTRS.split("|").each_with_object({}) do |k, m|
    n = node.attr(k) and m[k.to_sym] = n
  end
  while m = /^(#{STEM_ATTRS})(=[^%]+)?%(.*)$/o.match(text)
    text = m[3]
    attrs[m[1].to_sym] = m[2]&.sub(/^=/, "")
  end
  [{ block: node.block? }.merge(attrs), text]
end

#stem_parse(text, xml, style, node) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/metanorma/converter/inline.rb', line 43

def stem_parse(text, xml, style, node)
  attrs, text = stem_attrs(node, text)
  if /&lt;([^:>&]+:)?math(\s+[^>&]+)?&gt; |
    <([^:>&]+:)?math(\s+[^>&]+)?>/x.match? text
    xml.stem **attrs, type: "MathML" do |s|
      s << xml_encode(text)
    end
  elsif style == :latexmath then latex_parse(text, xml, attrs)
  else
    xml.stem text&.gsub("&amp;#", "&#") || "",
             **attrs, type: "AsciiMath"
  end
end

#strip_initial_space(elem) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/metanorma/cleanup/inline.rb', line 16

def strip_initial_space(elem)
  a = elem.children[0]
  a.text? or return
  if /\S/.match?(a.text)
    a.content = a.text.lstrip
  else a.remove
  end
end

#thematic_break(_node) ⇒ Object



27
28
29
30
# File 'lib/metanorma/converter/inline.rb', line 27

def thematic_break(_node)
  # noko(&:hr).join # Do not do this, noko blows up
  noko { |xml| xml.hr } # rubocop:disable Style/SymbolProc
end

#to_xreftarget(str) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/metanorma/cleanup/inline.rb', line 125

def to_xreftarget(str)
  /^[^#]+#.+$/.match?(str) or return Metanorma::Utils::to_ncname(str)
  /^(?<pref>[^#]+)#(?<suff>.+)$/ =~ str
  pref = pref.gsub(%r([#{Metanorma::Utils::NAMECHAR}])o, "_")
  suff = suff.gsub(%r([#{Metanorma::Utils::NAMECHAR}])o, "_")
  "#{pref}##{suff}"
end

#uri_cleanup(xmldoc) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/metanorma/cleanup/inline.rb', line 172

def uri_cleanup(xmldoc)
  xmldoc.xpath("//link[@target]").each do |l|
    l["target"] = Addressable::URI.parse(l["target"]).to_s
  rescue Addressable::URI::InvalidURIError
    @log.add("STANDOC_8", l, params: [l["target"]])
  end
end

#uri_component_encode(comp) ⇒ Object



180
181
182
# File 'lib/metanorma/cleanup/inline.rb', line 180

def uri_component_encode(comp)
  CGI.escape(comp).gsub("+", "%20")
end

#variant_cleanup(xmldoc) ⇒ Object



191
192
193
194
195
196
197
198
199
200
# File 'lib/metanorma/cleanup/inline.rb', line 191

def variant_cleanup(xmldoc)
  variant_space_cleanup(xmldoc)
  xmldoc.xpath("//*[lang-variant]").each do |c|
    if only_langvariant_children?(c)
      duplicate_langvariants(c, c.xpath("./lang-variant"))
    else
      c.xpath(".//lang-variant").each { |x| x.name = "span" }
    end
  end
end

#variant_space_cleanup(xmldoc) ⇒ Object



224
225
226
227
228
229
230
231
# File 'lib/metanorma/cleanup/inline.rb', line 224

def variant_space_cleanup(xmldoc)
  xmldoc.xpath("//*[lang-variant]").each do |c|
    c.next.nil? || c.next.next.nil? and next
    c.next.text? && c.next.next.name == "lang-variant" &&
      c.next.text.gsub(/\s/, "").empty? and
      c.next.remove
  end
end