Class: Metanorma::Standoc::NumberInlineMacro

Inherits:
Asciidoctor::Extensions::InlineMacroProcessor
  • Object
show all
Includes:
Utils
Defined in:
lib/metanorma/standoc/macros_inline.rb

Constant Summary collapse

MATHML_NS =
"http://www.w3.org/1998/Math/MathML".freeze

Constants included from Utils

Utils::SUBCLAUSE_XPATH

Instance Method Summary collapse

Methods included from Utils

adoc2xml, #asciimath_key, #attr_code, #convert, #csv_split, #dl_to_attrs, #dl_to_elems, #document_ns_attributes, #grkletters, #isodoc, #kv_parse, #link_unwrap, #noko, #quoted_csv_split, #refid?, #term_expr, #to_xml, #wrap_in_para, #xml_encode

Instance Method Details

#format(attrs) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/metanorma/standoc/macros_inline.rb', line 163

def format(attrs)
  # a="," => "a=,"
  quoted_csv_split(attrs || "", ",").map do |x|
    m = /^(.+?)=(.+)?$/.match(x) or next
    "#{m[1]}='#{m[2]}'"
  end.join(",")
end

#number(text) ⇒ Object



171
172
173
174
175
176
# File 'lib/metanorma/standoc/macros_inline.rb', line 171

def number(text)
  n = BigDecimal(text)
  trailing_zeroes = 0
  m = /\.[1-9]*(0+)/.match(text) and trailing_zeroes += m[1].size
  n.to_s("E").sub("e", "0" * trailing_zeroes + "e")
end

#process(parent, target, attrs) ⇒ Object



178
179
180
181
182
183
184
185
186
# File 'lib/metanorma/standoc/macros_inline.rb', line 178

def process(parent, target, attrs)
  out = Asciidoctor::Inline.new(parent, :quoted, attrs["text"]).convert
  fmt = format(out)
  fmt.empty? and fmt = "default"
  fmt = %( number-format="#{fmt}")
  <<~OUTPUT
    <stem type="MathML"#{fmt}><math xmlns='#{MATHML_NS}'><mn>#{number(target)}</mn></math></stem>
  OUTPUT
end

#unquote(str) ⇒ Object



159
160
161
# File 'lib/metanorma/standoc/macros_inline.rb', line 159

def unquote(str)
  str.sub(/^(["'])(.+)\1$/, "\\2")
end