Sha256: 07d79a4910e9fc5c47184bc84949111ef4d5310985570a921792bc17b5ee5878

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module Stepmod
  module Utils
    module Converters
      class Em < Stepmod::Utils::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] && state[:convert_bold_and_italics]
            "ii(#{content.strip})"
          elsif content.strip.empty? || state[:already_italic] || state[:equation]
            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

4 entries across 4 versions & 1 rubygems

Version Path
stepmod-utils-0.4.14 lib/stepmod/utils/converters/em_express_description.rb
stepmod-utils-0.4.13 lib/stepmod/utils/converters/em_express_description.rb
stepmod-utils-0.4.12 lib/stepmod/utils/converters/em_express_description.rb
stepmod-utils-0.4.11 lib/stepmod/utils/converters/em_express_description.rb