Module: Metanorma::Mirror::Handlers::Term

Defined in:
lib/metanorma/mirror/handlers/term.rb

Class Method Summary collapse

Class Method Details

.call(element, context:) ⇒ Object



7
8
9
10
11
12
13
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
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/metanorma/mirror/handlers/term.rb', line 7

def self.call(element, context:)
  attrs = {}
  attrs[:id] = SafeAttr.read(element, :id)
  attrs[:anchor] = SafeAttr.read(element, :anchor)

  fmt_name = SafeAttr.read(element, :fmt_name)
  if fmt_name
    number = Inline.extract_formatted_text(fmt_name)
    attrs[:number] = number unless number.empty?
  end

  content = []

  # Term name from fmt_preferred paragraphs
  extract_fmt_paragraphs(element, :fmt_preferred, content, context)

  # Definition from fmt_definition → semx → p
  fmt_def = SafeAttr.read(element, :fmt_definition)
  if fmt_def
    extract_semx_paragraphs(fmt_def, content, context)
  end

  # Source from fmt_termsource
  fmt_ts_list = SafeAttr.read(element, :fmt_termsource)
  if fmt_ts_list && !fmt_ts_list.empty?
    source_text = Inline.extract_formatted_text(fmt_ts_list.first)
    unless source_text.empty?
      content << Handlers.build_node("paragraph",
                                     attrs: { class: "source" },
                                     content: [context.text_node(source_text)])
    end
  end

  # Term notes
  SafeAttr.read(element, :termnote)&.each do |tn|
    note_attrs = { id: SafeAttr.read(tn, :id) }
    fn = SafeAttr.read(tn, :fmt_name)
    if fn
      num = Inline.extract_formatted_text(fn)
      note_attrs[:number] = num unless num.empty?
    end
    note_content = context.extract_named_collections(tn, %i[p ul ol dl])
    content << Handlers.build_node("note", attrs: note_attrs.compact,
                                           content: note_content)
  end

  # Term examples
  SafeAttr.read(element, :termexample)&.each do |te|
    ex_attrs = { id: SafeAttr.read(te, :id) }
    ex_content = context.extract_named_collections(te, %i[p ul ol dl])
    content << Handlers.build_node("example", attrs: ex_attrs.compact,
                                              content: ex_content)
  end

  # Nested terms
  SafeAttr.read(element, :term)&.each do |t|
    result = context.registry.handle(t, context: context)
    result.append_to(content)
  end

  Handlers.build_node("term", attrs: attrs.compact, content: content)
end

.extract_fmt_paragraphs(element, attr_name, content, context) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/metanorma/mirror/handlers/term.rb', line 70

def self.extract_fmt_paragraphs(element, attr_name, content, context)
  fmt_list = SafeAttr.read(element, attr_name)
  return unless fmt_list

  fmt_list.each do |fmt_el|
    ps = SafeAttr.read(fmt_el, :p)
    next unless ps

    ps.each do |p|
      result = context.registry.handle(p, context: context)
      result.append_to(content)
    end
  end
end

.extract_semx_paragraphs(fmt_def, content, context) ⇒ Object

fmt_definition wraps content in semx elements → extract p from semx



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/metanorma/mirror/handlers/term.rb', line 86

def self.extract_semx_paragraphs(fmt_def, content, context)
  semx_list = SafeAttr.read(fmt_def, :semx)
  return unless semx_list

  Array(semx_list).each do |s|
    SafeAttr.read(s, :p)&.each do |p|
      result = context.registry.handle(p, context: context)
      result.append_to(content)
    end
  end
end