Module: Metanorma::Standoc::Inline

Included in:
Converter
Defined in:
lib/metanorma/standoc/inline.rb

Instance Method Summary collapse

Instance Method Details

#concatenate_attributes_to_xref_text(node) ⇒ Object



49
50
51
52
53
54
# File 'lib/metanorma/standoc/inline.rb', line 49

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

#highlight_parse(text, xml) ⇒ Object



176
177
178
# File 'lib/metanorma/standoc/inline.rb', line 176

def highlight_parse(text, xml)
  xml << text
end

#image_attributes(node) ⇒ Object



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

def image_attributes(node)
  uri = node.image_uri (node.attr("target") || node.target)
  types = if /^data:/.match?(uri) then Metanorma::Utils::datauri2mime(uri)
          else MIME::Types.type_for(uri)
          end
  type = types.first.to_s
  uri = uri.sub(%r{^data:image/\*;}, "data:#{type};")
  image_attributes1(node, uri, type)
end

#image_attributes1(node, uri, type) ⇒ Object



227
228
229
230
231
232
233
234
235
236
# File 'lib/metanorma/standoc/inline.rb', line 227

def image_attributes1(node, uri, type)
  attr_code(src: uri,
            id: Metanorma::Utils::anchor_or_uuid,
            mimetype: type,
            height: node.attr("height") || "auto",
            width: node.attr("width") || "auto",
            filename: node.attr("filename"),
            title: node.attr("titleattr"),
            alt: node.alt == node.attr("default-alt") ? nil : node.alt)
end

#inline_anchor(node) ⇒ Object



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

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



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/metanorma/standoc/inline.rb', line 95

def inline_anchor_bibref(node)
  eref_contents =
    @c.decode(node.text || node.target || node.id)
    &.sub(/^\[?([^\[\]]+?)\]?$/, "[\\1]")
  @refids << (node.target || node.id)
  noko do |xml|
    xml.ref **attr_code(id: node.target || node.id) do |r|
      r << eref_contents
    end
  end.join
end


82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/metanorma/standoc/inline.rb', line 82

def inline_anchor_link(node)
  contents = node.text
  contents = "" if node.target.gsub(%r{^mailto:}, "") == node.text
  attributes = { target: node.target, alt: node.attr("title"),
                 "update-type": node.attr("updatetype") ||
                   node.attr("update-type") }
  noko do |xml|
    xml.link **attr_code(attributes) do |l|
      l << contents
    end
  end.join
end

#inline_anchor_ref(node) ⇒ Object



24
25
26
27
28
# File 'lib/metanorma/standoc/inline.rb', line 24

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

#inline_anchor_xref(node) ⇒ Object



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

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.join
end

#inline_anchor_xref_attrs(node) ⇒ Object



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

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

#inline_anchor_xref_attrs1(match, target, text) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/metanorma/standoc/inline.rb', line 56

def inline_anchor_xref_attrs1(match, target, text)
  { target: target,
    type: match[:fn].nil? ? "inline" : "footnote",
    case: match[:case]&.sub(/%$/, ""),
    style: match[:style]&.sub(/^style=/, "")&.sub(/%$/, "") || @xrefstyle,
    droploc: match[:drop].nil? && match[:drop2].nil? ? nil : true,
    text: inline_anchor_xref_text(match, text),
    hidden: match[:hidden] }
end

#inline_anchor_xref_match(text) ⇒ Object



66
67
68
69
70
71
# File 'lib/metanorma/standoc/inline.rb', line 66

def inline_anchor_xref_match(text)
  /^(?:hidden%(?<hidden>[^,]+),?)?
    (?<style>style=[^%]+%)?
    (?<drop>droploc%)?(?<case>capital%|lowercase%)?(?<drop2>droploc%)?
    (?<fn>fn:?\s*)?(?<text>.*)$/x.match text
end

#inline_anchor_xref_text(match, text) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/metanorma/standoc/inline.rb', line 73

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

#inline_break(node) ⇒ Object



123
124
125
126
127
128
# File 'lib/metanorma/standoc/inline.rb', line 123

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

#inline_callout(node) ⇒ Object



107
108
109
110
111
# File 'lib/metanorma/standoc/inline.rb', line 107

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

#inline_footnote(node) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/metanorma/standoc/inline.rb', line 113

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

#inline_image(node) ⇒ Object



238
239
240
241
242
# File 'lib/metanorma/standoc/inline.rb', line 238

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

#inline_indexterm(node) ⇒ Object



244
245
246
247
248
249
250
# File 'lib/metanorma/standoc/inline.rb', line 244

def inline_indexterm(node)
  noko do |xml|
    node.type == :visible and xml << node.text
    terms = (node.attr("terms") || [node.text]).map { |x| xml_encode(x) }
    inline_indexterm1(xml, terms)
  end.join
end

#inline_indexterm1(xml, terms) ⇒ Object



252
253
254
255
256
257
258
# File 'lib/metanorma/standoc/inline.rb', line 252

def inline_indexterm1(xml, terms)
  xml.index do |i|
    i.primary { |x| x << terms[0] }
    a = terms[1] and i.secondary { |x| x << a }
    a = terms[2] and i.tertiary { |x| x << a }
  end
end

#inline_quoted(node) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/metanorma/standoc/inline.rb', line 180

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)
    when :latexmath then stem_parse(node.text, xml, :latexmath)
    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" then xml.domain { |a| a << node.text }

      when "strike" then xml.strike { |s| s << node.text }
      when "underline" then xml.underline { |s| s << node.text }
      when "smallcap" then xml.smallcap { |s| s << node.text }
      when "keyword" then xml.keyword { |s| s << node.text }
      when /^css /
        xml.span style: node.role.sub(/^css /, "") do |s|
          s << node.text
        end
      else
        xml << node.text
      end
    end
  end.join
end

#latex_parse(text, xml) ⇒ Object



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

def latex_parse(text, xml)
  latex = latex_parse1(text) or return xml.stem type: "MathML"
  xml.stem 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) ⇒ Object



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

def latex_parse1(text)
  lxm_input = Unicode2LaTeX.unicode2latex(@c.decode(text))
  results = Plurimath::Math.parse(lxm_input, "latex").to_mathml
  if results.nil?
    @log.add("Math", nil,
             "latexmlmath failed to process equation:\n#{lxm_input}")
    return
  end
  results.sub(%r{<math ([^>]+ )?display="block"}, "<math \\1")
end

#page_break(node) ⇒ Object



130
131
132
133
134
135
# File 'lib/metanorma/standoc/inline.rb', line 130

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) }.join
end

#refid?(ref) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/metanorma/standoc/inline.rb', line 11

def refid?(ref)
  @refids.include? ref
end

#stem_parse(text, xml, style) ⇒ Object



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

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

#thematic_break(_node) ⇒ Object



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

def thematic_break(_node)
  noko { |xml| xml.hr }.join
end