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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/stepmod/utils/concept.rb', line 59

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}`
  designations = [
    { "designation" => term[4..-1], "type" => "expression",
      "normative_status" => "preferred" },
  ]

  alts.each do |alt|
    designations << { "designation" => alt, "type" => "expression" }
  end

  designations
end

.definition_xml_converted_definition(designation, definition) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/stepmod/utils/concept.rb', line 108

def definition_xml_converted_definition(designation, definition)
  accepted_designation = designation.select do |des|
    des["normative_status"] == "preferred"
  end

  alt_designations = designation.reject do |des|
    des["normative_status"] == "preferred"
  end

  if alt_designations.length.positive?
    alt_designations_text = alt_designations.map do |d|
      d["designation"].strip
    end.join(",")

    alt_notation = "alt:[#{alt_designations_text}]"
  end

  result = <<~TEXT
    === #{accepted_designation.map { |d| d['designation'].strip }.join(',')}
  TEXT

  if alt_notation
    result += <<~TEXT

      #{alt_notation}
    TEXT
  end

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

.definition_xml_definition(definition_xml, reference_anchor) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/stepmod/utils/concept.rb', line 86

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, notes, figures and tables
        no_notes_examples: true,
        reference_anchor: reference_anchor,
      },
    )
end

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



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
53
54
55
56
57
# File 'lib/stepmod/utils/concept.rb', line 20

def parse(definition_xml, reference_anchor:, reference_clause:,
file_path:, language_code: "eng")
  converted_definition = Stepmod::Utils::StepmodDefinitionConverter.convert(
    definition_xml,
    {
      # We don't want examples, notes, figures and tables
      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_hObject



143
144
145
146
147
148
149
150
# File 'lib/stepmod/utils/concept.rb', line 143

def to_h
  super.merge({
    "domain" => domain,
    "part" => part,
    "schema" => schema,
    "document" => document,
  }.compact)
end

#to_mn_adocObject



152
153
154
155
156
157
158
159
160
161
# File 'lib/stepmod/utils/concept.rb', line 152

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

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

  TEXT
end