Class: Stepmod::Utils::Concept

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

Constant Summary collapse

CUSTOM_ATTRIBUTES =
%w(
  reference_clause
  reference_anchor
  converted_definition
  file_path
  schema
  part
  document
).freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Concept

Returns a new instance of Concept.



147
148
149
150
151
152
153
# File 'lib/stepmod/utils/concept.rb', line 147

def initialize(hash = {})
  super(hash)

  CUSTOM_ATTRIBUTES.each do |attr|
    public_send("#{attr}=", hash[attr] || hash[attr.to_sym])
  end
end

Class Method Details

.definition_designation(definition_xml) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/stepmod/utils/concept.rb', line 66

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.map do |des|
    Glossarist::Designation::Base.from_yaml(des.to_yaml)
  end
end

.definition_xml_converted_definition(designation, definition) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/stepmod/utils/concept.rb', line 117

def definition_xml_converted_definition(designation, definition)
  accepted_designation = designation.select(&:preferred?)
  alt_designations = designation.reject(&:preferred?)

  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 = "    === \#{accepted_designation.map { |d| d.designation.strip }.join(',')}\n  TEXT\n\n  if alt_notation\n    result += <<~TEXT\n\n      \#{alt_notation}\n    TEXT\n  end\n\n  <<~TEXT\n    \#{result}\n    \#{definition}\n  TEXT\nend\n"

.definition_xml_definition(definition_xml, reference_anchor) ⇒ Object



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

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



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
58
59
60
61
62
63
64
# File 'lib/stepmod/utils/concept.rb', line 21

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(
    data: Glossarist::ConceptData.new(
      {
        terms: designation,
        definition: [
          Glossarist::DetailedDefinition.new({ content: definition }),
        ],
        language_code: language_code,
      },
    ),
    converted_definition: converted_definition,
    id: "#{reference_anchor}.#{reference_clause}",
    reference_anchor: reference_anchor,
    reference_clause: reference_clause,
    file_path: file_path,
  )
end

Instance Method Details

#domainObject



164
165
166
# File 'lib/stepmod/utils/concept.rb', line 164

def domain
  data.domain
end

#to_hObject



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

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

#to_mn_adocObject



168
169
170
171
172
173
174
175
176
177
# File 'lib/stepmod/utils/concept.rb', line 168

def to_mn_adoc
  "    // STEPmod path:\#{file_path.to_s.empty? ? '' : \" \#{file_path}\"}\n    \#{converted_definition}\n\n    [.source]\n    <<\#{reference_anchor}\#{reference_clause ? \",clause=\#{reference_clause}\" : ''}>>\n\n  TEXT\nend\n"