class Stepmod::Utils::Converters::Eqn

Constants

TAGS_NOT_IN_CONTEXT

Public Instance Methods

convert(node, state = {}) click to toggle source
# File lib/stepmod/utils/converters/eqn.rb, line 11
def convert(node, state = {})
  cloned_node = node.clone
  if definition_node?(cloned_node)
    return definition_converted(cloned_node, state)
  end

  stem_converted(cloned_node, state)
end

Private Instance Methods

definition_converted(cloned_node, state) click to toggle source
# File lib/stepmod/utils/converters/eqn.rb, line 35
def definition_converted(cloned_node, state)
  first_strong_node = cloned_node
    .children
    .find do |n|
    return false if !n.text? && n.name != "b"

    n.name == "b"
  end
  first_strong_node.next.content = first_strong_node.next.content.gsub(
    /\s?:/, ""
  )
  term = first_strong_node.text.strip
  first_strong_node.remove
  "\n\n#{term}:: #{remove_trash_symbols(treat_children(cloned_node,
                                                       state))}\n"
end
definition_node?(node) click to toggle source
# File lib/stepmod/utils/converters/eqn.rb, line 22
def definition_node?(node)
  first_strong_node = node
    .children
    .find do |n|
    return false if !n.text? && n.name != "b"

    n.name == "b"
  end
  first_strong_node&.next &&
    first_strong_node.next.text? &&
    first_strong_node.next.content =~ /\s+:/
end
remove_tags_not_in_context(node) click to toggle source

Remove all tags that make no sense in equations, eg: strong, italic Search for such tags, move their children into the root context and remove them

# File lib/stepmod/utils/converters/eqn.rb, line 81
def remove_tags_not_in_context(node)
  TAGS_NOT_IN_CONTEXT.each do |tag_name|
    node
      .children
      .each do |n|
        remove_tags_not_in_context(n) if n.children.length.positive?
        next if n.name != tag_name

        n.add_previous_sibling(n.children)
        n.unlink
      end
  end
end
remove_trash_symbols(content) click to toggle source
# File lib/stepmod/utils/converters/eqn.rb, line 68
def remove_trash_symbols(content)
  content
    .gsub(/ /, "")
    .strip
    .gsub(/\(\d\)$/, "")
    .gsub(/\b(\w*?_+\w+)\b/, '"\1"')
    .gsub(/([^\s])\s+_{/, '\1_{')
    .strip
end
stem_converted(cloned_node, state) click to toggle source
# File lib/stepmod/utils/converters/eqn.rb, line 52
        def stem_converted(cloned_node, state)
          remove_tags_not_in_context(cloned_node)
          internal_content = treat_children(cloned_node, state)
          content = Stepmod::Utils::HtmlToAsciimath.new.call(internal_content)
          res = <<~TEMPLATE

            [stem]
            ++++
            #{remove_trash_symbols(content.strip)}
            ++++

          TEMPLATE
          res = "[[#{cloned_node['id']}]]\n#{res}" if cloned_node["id"]&.length&.positive?
          res
        end