Class: Stepmod::Utils::Concept

Inherits:
Glossarist::LocalizedConcept
  • Object
show all
Defined in:
lib/stepmod/utils/concept.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.definition_designation(definition_xml) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/stepmod/utils/concept.rb', line 54

def definition_designation(definition_xml)
  # We take the <p> that is an alternative term (length<=20).
  # Add in the <synonym> elements.
  alts = definition_xml.xpath(".//synonym").map(&:text) +
    definition_xml.xpath(".//def/p").map(&:text).reject do |text|
      text.length > 20
    end

  term = Stepmod::Utils::Converters::Term
    .new
    .convert(
      definition_xml.xpath(".//term").first
    )

  {
    # [4..-1] because we want to skip the initial `=== {title}`
    accepted: term[4..-1],
    alt: alts,
  }
end

.definition_xml_converted_definition(designation, definition) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/stepmod/utils/concept.rb', line 97

def definition_xml_converted_definition(designation, definition)
  if designation[:alt].length.positive?
    alt_notation = "alt:[#{designation[:alt].map(&:strip).join(',')}]"
  end

  result = <<~TEXT
    === #{designation[:accepted].strip}
  TEXT

  if alt_notation
    result += <<~TEXT

      #{alt_notation}
    TEXT
  end

  <<~TEXT
    #{result}
    #{definition}
  TEXT
end

.definition_xml_definition(definition_xml, reference_anchor) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/stepmod/utils/concept.rb', line 75

def definition_xml_definition(definition_xml, reference_anchor)
  # We reject the <p> that was considered an alternative term (length<=20)
  text_nodes = definition_xml
    .xpath(".//def")
    .first
    .children
    .reject { |n| n.name == "p" && n.text.length <= 20 }

  wrapper = "<def>#{text_nodes.map(&:to_s).join}</def>"

  Stepmod::Utils::Converters::Def
    .new
    .convert(
      Nokogiri::XML(wrapper).root,
      {
        # We don't want examples and notes
        no_notes_examples: true,
        reference_anchor: reference_anchor,
      },
    )
end

.parse(definition_xml, reference_anchor:, reference_clause:, file_path:, language_code: "en") ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/stepmod/utils/concept.rb', line 16

def parse(definition_xml, reference_anchor:, reference_clause:, file_path:, language_code: "en")
  converted_definition = Stepmod::Utils::StepmodDefinitionConverter.convert(
    definition_xml,
    {
      # We don't want examples and notes
      no_notes_examples: true,
      reference_anchor: reference_anchor,
    },
  )

  return nil if converted_definition.nil? || converted_definition.strip.empty?

  # https://github.com/metanorma/stepmod-utils/issues/86

  # TODO: This portion DOES NOT HANDLE the <synonym> element. WTF??
  if definition_xml.name == "definition"
    designation = definition_designation(definition_xml)
    definition = definition_xml_definition(definition_xml,
                                           reference_anchor)
    converted_definition = definition_xml_converted_definition(
      designation, definition
    ).strip
  end

  # TODO: `designations:` should include the `alt:[...]` terms here,
  # they are now only included in definition_xml_converted_definition.
  new(
    designations: [designation],
    definition: definition,
    converted_definition: converted_definition,
    id: "#{reference_anchor}.#{reference_clause}",
    reference_anchor: reference_anchor,
    reference_clause: reference_clause,
    file_path: file_path,
    language_code: language_code,
  )
end

Instance Method Details

#to_mn_adoc ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/stepmod/utils/concept.rb', line 120

def to_mn_adoc
  <<~TEXT
    // STEPmod path:#{file_path.empty? ? '' : " #{file_path}"}
    #{converted_definition}

    [.source]
    <<#{reference_anchor}#{reference_clause ? ",clause=#{reference_clause}" : ''}>>

  TEXT
end