Module: Metanorma::Standoc::Validate::Term

Included in:
Metanorma::Standoc::Validate
Defined in:
lib/metanorma/validate/term.rb

Constant Summary collapse

SOURCELOCALITY =
"./origin//locality[@type = 'clause']/" \
"referenceFrom".freeze

Instance Method Summary collapse

Instance Method Details

#concept_validate(doc, tag, refterm) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/metanorma/validate/term.rb', line 62

def concept_validate(doc, tag, refterm)
  concept_validate_ids(doc)
  doc.xpath("//#{tag}/xref").each do |x|
    @concept_ids[x["target"]] and next
    @log.add("STANDOC_23", x,
             params: [concept_validate_msg(doc, tag, refterm, x)])
  end
end

#concept_validate_ids(doc) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/metanorma/validate/term.rb', line 71

def concept_validate_ids(doc)
  @concept_ids ||= doc.xpath("//term | //definitions//dt")
    .each_with_object({}) { |x, m| m[x["anchor"]] = true }
  @concept_terms_tags ||= doc.xpath("//terms")
    .each_with_object({}) { |t, m| m[t["anchor"]] = true }
  nil
end

#concept_validate_msg(_doc, tag, refterm, xref) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/metanorma/validate/term.rb', line 79

def concept_validate_msg(_doc, tag, refterm, xref)
  t = @doc_ids.dig(xref["target"], :anchor) || xref["target"]
  ret = <<~LOG
    #{tag.capitalize} #{xref.at("../#{refterm}")&.text} is pointing to #{t}, which is not a term or symbol
  LOG
  if @concept_terms_tags[xref["target"]]
    ret = ret.strip
    ret += ". Did you mean to point to a subterm?"
  end
  ret
end

#find_illegal_designations(xmldoc) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/metanorma/validate/term.rb', line 112

def find_illegal_designations(xmldoc)
  xmldoc.xpath("//preferred | //admitted | //deprecates")
    .each_with_object({}) do |d, m|
      d.ancestors.detect { |x| x.name == "terms" } and next
      c = d.ancestors.detect do |x|
        section_containers.include?(x.name)
      end
      c["id"] or add_id(c["id"])
      m[c["id"]] ||= { clause: c, designations: [] }
      m[c["id"]][:designations] << d
  end
end

#iev_validate(xmldoc) ⇒ Object

Functionality currently disabled, Electropedia blocking Github Actions



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/metanorma/validate/term.rb', line 32

def iev_validate(xmldoc)
  @iev = init_iev
  unless @iev
    warn "IEV validation unavailable!"
    return
  end
  xmldoc.xpath("//term").each do |t|
    t.xpath("./source | ./preferred/source | ./admitted/source | " \
      "./deprecates/source | ./related/source").each do |src|
      (/^IEC[  ]60050-/.match(src.at("./origin/@citeas")&.text) &&
    loc = src.xpath(SOURCELOCALITY)&.text) or next
      iev_validate1(t, loc, xmldoc)
    end
  end
end

#iev_validate1(term, loc, xmldoc) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/metanorma/validate/term.rb', line 48

def iev_validate1(term, loc, xmldoc)
  iev = @iev.fetch(loc,
                   xmldoc.at("//language")&.text || "en")
  unless iev
    warn "IEV retrieval of #{loc} failed!"
    return
  end
  pref = term.xpath("./preferred//name").inject([]) do |m, x|
    m << x.text&.downcase
  end
  pref.include?(iev.downcase) or
    @log.add("STANDOC_22", term, params: [pref[0], loc, iev])
end

#init_ievObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/metanorma/validate/term.rb', line 10

def init_iev
  # Treat empty string as falsy (set by Asciidoctor for some attributes)
  return nil if @no_isobib && !@no_isobib.empty?

  @iev and return @iev
  begin
    # Same check: only initialize if not explicitly disabled
    unless @no_isobib && !@no_isobib.empty?
      @iev = ::Iev::Db.new(@iev_globalname,
                           @iev_localname)
    end
  rescue StandardError => e
    warn "IEV initialization failed: #{e.class}: #{e.message}"
    warn "  Global cache: #{@iev_globalname.inspect}"
    warn "  Local cache: #{@iev_localname.inspect}"
    warn "  Backtrace: #{e.backtrace[0..2].join("\n  ")}" if e.backtrace
    @iev = nil
  end
  @iev
end

#preferred_validate(doc) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/metanorma/validate/term.rb', line 91

def preferred_validate(doc)
  ret = doc.xpath("//term").each_with_object({}) do |t, m|
    prefix = t.at("./domain")&.text
    t.xpath("./preferred//name").each do |n|
      ret = n.text
      prefix and ret = "<#{prefix}> #{ret}"
      m[ret] ||= []
      m[ret] << t
    end
  end
  preferred_validate_report(ret)
end

#preferred_validate_report(terms) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/metanorma/validate/term.rb', line 104

def preferred_validate_report(terms)
  terms.each do |k, v|
    v.size > 1 or next
    loc = v.map { |x| x["anchor"] }.join(", ")
    @log.add("STANDOC_24", v.first, params: [k, loc])
  end
end

#termsect_validate(xmldoc) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/metanorma/validate/term.rb', line 125

def termsect_validate(xmldoc)
  errors = find_illegal_designations(xmldoc)
  errors.each_value do |v|
    desgns = v[:designations].map do |x|
      @c.encode(x.text.strip, :basic, :hexadecimal)
    end.join(", ")
    @log.add("STANDOC_25", v[:clause], params: [desgns])
  end
end