Module: Metanorma::Standoc::Terms

Included in:
Cleanup
Defined in:
lib/metanorma/cleanup/terms.rb

Constant Summary collapse

TERMDEF_BLOCKS =
"./p | ./ol | ./dl[not(@metadata = 'true')] | ./ul | ./figure | " \
"./formula | ./table".freeze

Instance Method Summary collapse

Instance Method Details

#alternate_termdefinitions(xmldoc) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/metanorma/cleanup/terms.rb', line 60

def alternate_termdefinitions(xmldoc)
  xmldoc.xpath("//term").each do |t|
    t.xpath("./definition").each do |d|
      d1 = d.next_element or next
      if (v = d.at("./verbal-definition")) &&
          !d.at("./non-verbal-representation") &&
          !d1.at("./verbal-definition") &&
          nv = d1.at("./non-verbal-representation")
        v.next = nv.remove
        d1.remove
      end
    end
  end
end

#generate_termdefinitions(xmldoc) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/metanorma/cleanup/terms.rb', line 33

def generate_termdefinitions(xmldoc)
  xmldoc.xpath("//term[not(definition)]").each do |d|
    first_child = d.at(TERMDEF_BLOCKS) || next
    t = Nokogiri::XML::Element.new("definition", xmldoc)
    add_id(t)
    first_child.replace(t)
    t << first_child.remove
    d.xpath(TERMDEF_BLOCKS).each do |n|
      t << n.remove
    end
  end
end

#split_termdefinitions(xmldoc) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/metanorma/cleanup/terms.rb', line 46

def split_termdefinitions(xmldoc)
  xmldoc.xpath("//definition").each do |d|
    if d.at("./p | ./ol | ./dl | ./ul")
      d.children = <<~XML.strip
        <verbal-definition #{add_id_text}>#{d.children}</verbal-definition>
      XML
    else
      d.children = <<~XML.strip
        <non-verbal-representation #{add_id_text}>#{d.children}</non-verbal-representation>
      XML
    end
  end
end

#term_children_cleanup(xmldoc) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/metanorma/cleanup/terms.rb', line 87

def term_children_cleanup(xmldoc)
  xmldoc.xpath("//term").each do |t|
    %w(termnote termexample source term).each do |w|
      t.xpath("./#{w}").each { |n| t << n.remove }
    end
  end
end

#term_id_attr_cleanup(xmldoc) ⇒ Object



136
137
138
139
140
# File 'lib/metanorma/cleanup/terms.rb', line 136

def term_id_attr_cleanup(xmldoc)
  xmldoc.xpath("//usage-info[@id]").each do |u|
    u.delete("id")
  end
end

#termdef_cleanup(xmldoc) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/metanorma/cleanup/terms.rb', line 117

def termdef_cleanup(xmldoc)
  term_designation_unnest_cleanup(xmldoc)
  termdef_unnest_cleanup(xmldoc)
  termlookup_cleanup(xmldoc)
  term_nonverbal_designations(xmldoc)
  (xmldoc)
  term_termsource_to_designation(xmldoc)
  term_designation_reorder(xmldoc)
  term_designation_redundant(xmldoc)
  termdef_from_termbase(xmldoc)
  termdomain_cleanup(xmldoc)
  termdef_stem_cleanup(xmldoc)
  termdefinition_cleanup(xmldoc)
  termdomain1_cleanup(xmldoc)
  termnote_example_cleanup(xmldoc)
  term_id_attr_cleanup(xmldoc)
  term_children_cleanup(xmldoc)
end

#termdef_from_termbase(xmldoc) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/metanorma/cleanup/terms.rb', line 95

def termdef_from_termbase(xmldoc)
  xmldoc.xpath("//term").each do |x|
    if (c = x.at("./origin/termref")) && !x.at("./definition")
      x.at("./origin").previous = fetch_termbase(c["base"], c.text)
    end
  end
end

#termdefinition_cleanup(xmldoc) ⇒ Object



23
24
25
26
27
# File 'lib/metanorma/cleanup/terms.rb', line 23

def termdefinition_cleanup(xmldoc)
  generate_termdefinitions(xmldoc)
  split_termdefinitions(xmldoc)
  alternate_termdefinitions(xmldoc)
end

#termdocsource_cleanup(xmldoc) ⇒ Object



75
76
77
78
# File 'lib/metanorma/cleanup/terms.rb', line 75

def termdocsource_cleanup(xmldoc)
  f = xmldoc.at("//preface | //sections")
  xmldoc.xpath("//termdocsource").each { |s| f.previous = s.remove }
end

#termdomain1_cleanup(xmldoc) ⇒ Object



16
17
18
19
20
21
# File 'lib/metanorma/cleanup/terms.rb', line 16

def termdomain1_cleanup(xmldoc)
  xmldoc.xpath("//term").each do |t|
    d = t.xpath("./domain | ./subject").last or next
    defn = d.at("../definition") and defn.previous = d.remove
  end
end

#termdomain_cleanup(xmldoc) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/metanorma/cleanup/terms.rb', line 7

def termdomain_cleanup(xmldoc)
  xmldoc.xpath("//p/domain").each do |a|
    parent = a.parent
    prev = parent.previous
    prev.next = a.remove
    parent.text.strip.empty? and parent.remove
  end
end

#termlookup_cleanup(xmldoc) ⇒ Object



113
114
115
# File 'lib/metanorma/cleanup/terms.rb', line 113

def termlookup_cleanup(xmldoc)
  Metanorma::Standoc::TermLookupCleanup.new(xmldoc, @log).call
end

#termnote_example_cleanup(xmldoc) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/metanorma/cleanup/terms.rb', line 103

def termnote_example_cleanup(xmldoc)
  %w(note example).each do |w|
    xmldoc.xpath("//term#{w}[not(ancestor::term)]").each do |x|
      if x["keepasterm"] then x.delete("keepasterm")
      else x.name = w
      end
    end
  end
end

#terms_terms_cleanup(xmldoc) ⇒ Object



80
81
82
83
84
85
# File 'lib/metanorma/cleanup/terms.rb', line 80

def terms_terms_cleanup(xmldoc)
  xmldoc.xpath("//terms[terms][not(term)]").each do |t|
    t.name = "clause"
    t["type"] = "terms"
  end
end