Sha256: 4ed7b6e41700007309d3f308fe814b516acd4792db5ebd9d665ee7ffb3d758c0

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module Stepmod
  module Utils
    module Converters
      class Em < ReverseAdoc::Converters::Base
        def convert(node, state = {})
          italic_converted(node, state)
        end

        private

        def italic_converted(node, state)
          cloned_node = node.clone
          equations = extract_equations(cloned_node)
          content = treat_children(cloned_node, state.merge(already_italic: true))
          equation_content = equations.map do |equation|
            treat(equation, state.merge(equation: true, already_italic: true))
          end

          content = if state[:equation] && !content.strip.empty?
            "ii(#{content.strip})"
          elsif content.strip.empty? || state[:already_italic]
            content
          else
            "#{content[/^\s*/]}_#{content.strip}_#{content[/\s*$/]}"
          end

          [content, equation_content].compact.join("")
        end

        def extract_equations(node)
          equations = []

          node.children.each do |n|
            next if n.name != "eqn"

            equations << n
            n.unlink
          end

          equations
        end
      end

      ReverseAdoc::Converters.register :em, Em.new
      ReverseAdoc::Converters.register :i,  Em.new
      ReverseAdoc::Converters.register :cite, Em.new
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stepmod-utils-0.3.36 lib/stepmod/utils/converters/em_express_description.rb