module Metanorma::Standoc::Inline

Public Instance Methods

highlight_parse(text, xml) click to toggle source
# File lib/metanorma/standoc/inline.rb, line 155
def highlight_parse(text, xml)
  xml << text
end
image_attributes(node) click to toggle source
# File lib/metanorma/standoc/inline.rb, line 192
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) click to toggle source
# File lib/metanorma/standoc/inline.rb, line 202
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) click to toggle source
# File lib/metanorma/standoc/inline.rb, line 16
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) click to toggle source
# File lib/metanorma/standoc/inline.rb, line 72
def inline_anchor_bibref(node)
  eref_contents = (node.text || node.target || node.id)
    &.sub(/^\[?([^\[\]]+?)\]?$/, "[\\1]")
  eref_attributes = { id: node.target || node.id }
  @refids << (node.target || node.id)
  noko do |xml|
    xml.ref **attr_code(eref_attributes) do |r|
      r << eref_contents
    end
  end.join
end
inline_anchor_ref(node) click to toggle source
# File lib/metanorma/standoc/inline.rb, line 25
def inline_anchor_ref(node)
  noko do |xml|
    xml.bookmark nil, **attr_code(id: node.id)
  end.join
end
inline_anchor_xref(node) click to toggle source
# File lib/metanorma/standoc/inline.rb, line 31
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) click to toggle source
# File lib/metanorma/standoc/inline.rb, line 42
def inline_anchor_xref_attrs(node)
  m = /^(?<drop>droploc%)?(?<case>capital%|lowercase%)?(?<drop2>droploc%)?
    (?<fn>fn:?\s*)?(?<text>.*)$/x.match node.text
  t = node.target.gsub(/^#/, "").gsub(%r{(\.xml|\.adoc)(#.*$)}, "\\2")
  m.nil? and return { target: t, type: "inline", text: node.text }
  droploc = m[:drop].nil? && m[:drop2].nil? ? nil : true
  f = m[:fn].nil? ? "inline" : "footnote"
  c = if %i[case fn drop drop2].any? do |x|
           !m[x].nil?
         end
        m[:text]
      else node.text
      end
  { target: t, type: f, case: m[:case]&.sub(/%$/, ""), droploc: droploc,
    text: c }
end
inline_break(node) click to toggle source
# File lib/metanorma/standoc/inline.rb, line 100
def inline_break(node)
  noko do |xml|
    xml << node.text
    xml.br
  end.join
end
inline_callout(node) click to toggle source
# File lib/metanorma/standoc/inline.rb, line 84
def inline_callout(node)
  noko do |xml|
    xml.callout node.text
  end.join
end
inline_footnote(node) click to toggle source
# File lib/metanorma/standoc/inline.rb, line 90
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) click to toggle source
# File lib/metanorma/standoc/inline.rb, line 213
def inline_image(node)
  noko do |xml|
    xml.image **image_attributes(node)
  end.join
end
inline_indexterm(node) click to toggle source
# File lib/metanorma/standoc/inline.rb, line 219
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) }
    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.join
end
inline_quoted(node) click to toggle source
# File lib/metanorma/standoc/inline.rb, line 159
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 }
      else
        xml << node.text
      end
    end
  end.join
end
latex_parse(text, xml) click to toggle source
# File lib/metanorma/standoc/inline.rb, line 145
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
  end
end
latex_parse1(text) click to toggle source
# File lib/metanorma/standoc/inline.rb, line 125
def latex_parse1(text)
  lxm_input = Unicode2LaTeX.unicode2latex(HTMLEntities.new.decode(text))
  results = Latexmath.parse(lxm_input).to_mathml
  results.nil? and
    @log.add("Math", nil,
             "latexmlmath failed to process equation:\n#{lxm_input}")
  results&.sub(%r{<math ([^>]+ )?display="block"}, "<math \\1")
end
page_break(node) click to toggle source
# File lib/metanorma/standoc/inline.rb, line 107
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) click to toggle source
# File lib/metanorma/standoc/inline.rb, line 12
def refid?(ref)
  @refids.include? ref
end
stem_parse(text, xml, style) click to toggle source
# File lib/metanorma/standoc/inline.rb, line 134
def stem_parse(text, xml, style)
  if /&lt;([^:>&]+:)?math(\s+[^>&]+)?&gt; |
    <([^:>&]+:)?math(\s+[^>&]+)?>/x.match? text
    math = xml_encode(text)
    xml.stem math, **{ type: "MathML" }
  elsif style == :latexmath then latex_parse(text, xml)
  else
    xml.stem text&.gsub(/&amp;#/, "&#"), **{ type: "AsciiMath" }
  end
end
thematic_break(_node) click to toggle source
# File lib/metanorma/standoc/inline.rb, line 114
def thematic_break(_node)
  noko { |xml| xml.hr }.join
end
xml_encode(text) click to toggle source
# File lib/metanorma/standoc/inline.rb, line 118
def xml_encode(text)
  HTMLEntities.new.encode(text, :basic, :hexadecimal)
    .gsub(/&amp;gt;/, ">").gsub(/&amp;lt;/, "<").gsub(/&amp;amp;/, "&")
    .gsub(/&gt;/, ">").gsub(/&lt;/, "<").gsub(/&amp;/, "&")
    .gsub(/&quot;/, '"').gsub(/&#xa;/, "\n").gsub(/&amp;#/, "&#")
end