Sha256: 79c1082fd29ce93dd921b10bc8b036e09fc713c6424030d92251257c3607ebf3

Contents?: true

Size: 1.39 KB

Versions: 6

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 < Stepmod::Utils::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

6 entries across 6 versions & 1 rubygems

Version Path
stepmod-utils-0.4.14 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.4.13 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.4.12 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.4.11 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.4.10 lib/stepmod/utils/converters/term.rb
stepmod-utils-0.4.9 lib/stepmod/utils/converters/term.rb