Sha256: a82e99e094e1cd96558a82ee939dd81a255b82348b2ca8cddba220b82619c083

Contents?: true

Size: 1.39 KB

Versions: 29

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

require "stepmod/utils/converters/synonym"
require "stepmod/utils/term"
require "glossarist"

module Stepmod
  module Utils
    module Converters
      class Term < ReverseAdoc::Converters::Base

        # We strip all the children in the case for "stem:[d]-manifold"
        # vs "stem:[d] -manifold"
        def treat_children(node, state)
          res = node.children.map { |child| treat(child, state) }
          res.map(&:strip).reject(&:empty?).join("")
        end

        def convert(node, state = {})
          first_child = node.children.find do |child|
            child.name == "text" && !child.text.to_s.strip.empty?
          end

          unless first_child &&
              node.text.split(";").length == 2 &&
              defined?(Stepmod::Utils::Converters::Synonym)

            return Stepmod::Utils::Term.from_h(
              "definition" => treat_children(node, state).strip,
            ).to_mn_adoc
          end

          term_def, alt = node.text.split(";")
          alt_xml = Nokogiri::XML::Text.new(alt, Nokogiri::XML::Document.new)
          converted_alt = Stepmod::Utils::Converters::Synonym.new.convert(alt_xml)

          Stepmod::Utils::Term.from_h(
            "definition" => term_def,
            "synonyms" => [converted_alt],
          ).to_mn_adoc
        end
      end

      ReverseAdoc::Converters.register :term, Term.new
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
stepmod-utils-0.4.8 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.4.7 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.4.6 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.4.5 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.4.2 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.4.1 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.4.0 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.3.37 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.3.36 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.3.35 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.3.34 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.3.33 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.3.32 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.3.31 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.3.30 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.3.29 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.3.28 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.3.27 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.3.26 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.3.25 lib/stepmod/utils/converters/term.rb