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
# 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}`
    accepted: term[4..-1],
    alt: alts,
  }
end

.definition_xml_converted_definition(designation, definition) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/stepmod/utils/concept.rb', line 102

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

  result = "    === \#{designation[:accepted].strip}\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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/stepmod/utils/concept.rb', line 80

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



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

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?

  if definition_xml.name == "ext_description"
    converted_definition = "      \#{converted_definition}\n\n      NOTE: This term is incompletely defined in this document.\n      Reference <<\#{reference_anchor}>> for the complete definition.\n    TEXT\n  end\n\n  # https://github.com/metanorma/stepmod-utils/issues/86\n\n  # TODO: This portion DOES NOT HANDLE the <synonym> element. WTF??\n  if definition_xml.name == \"definition\"\n    designation = definition_designation(definition_xml)\n    definition = definition_xml_definition(definition_xml,\n                                           reference_anchor)\n    converted_definition = definition_xml_converted_definition(\n      designation, definition\n    ).strip\n  end\n\n  new(\n    designations: [designation],\n    definition: definition,\n    converted_definition: converted_definition,\n    id: \"\#{reference_anchor}.\#{reference_clause}\",\n    reference_anchor: reference_anchor,\n    reference_clause: reference_clause,\n    file_path: file_path,\n    language_code: language_code,\n  )\nend\n"

Instance Method Details

#to_mn_adocObject



125
126
127
128
129
130
131
132
133
134
# File 'lib/stepmod/utils/concept.rb', line 125

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