Class: Metanorma::Ieee::Validate

Inherits:
Standoc::Validate
  • Object
show all
Defined in:
lib/metanorma/ieee/validate.rb,
lib/metanorma/ieee/validate_style.rb,
lib/metanorma/ieee/validate_section.rb

Constant Summary collapse

ASSETS_TO_STYLE =
"//term//source | //formula | //termnote | " \
"//p[not(ancestor::boilerplate)] | //li[not(p)] | //dt | " \
"//dd[not(p)] | //td[not(p)][not(ancestor::boilerplate)] | " \
"//th[not(p)][not(ancestor::boilerplate)] | //example".freeze
SI_UNIT =

leaving out as problematic: N J K C S T H h d B o E

"(m|cm|mm|km|μm|nm|g|kg|mgmol|cd|rad|sr|Hz|Hz|MHz|Pa|hPa|kJ|" \
"V|kV|W|MW|kW|F|μF|Ω|Wb|°C|lm|lx|Bq|Gy|Sv|kat|l|t|eV|u|Np|Bd|" \
"bit|kB|MB|Hart|nat|Sh|var)".freeze
SEQ =

spec of permissible section sequence we skip normative references, it goes to end of list

[
  { msg: "Initial section must be (content) Abstract",
    val: ["./self::abstract"] },
  { msg: "Prefatory material must be followed by (clause) Overview",
    val: ["./self::clause[@type = 'overview']"] },
  { msg: "Normative References must be followed by "\
         "Definitions",
    val: ["./self::terms | .//terms"] },
].freeze
SECTIONS_XPATH =
"//preface/abstract | //sections/terms | //annex | "\
"//sections/definitions | //sections/clause | "\
"//references[not(parent::clause)][not(ancestor::boilerplate)] | "\
"//clause[descendant::references][not(parent::clause)][not(ancestor::boilerplate)]".freeze

Instance Method Summary collapse

Instance Method Details

#amend_validate(xmldoc) ⇒ Object

Style manual 20.2.2



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/metanorma/ieee/validate.rb', line 190

def amend_validate(xmldoc)
  xmldoc.xpath("//amend").each do |a|
    desc = a.at("./description")
    if desc && !desc.text.strip.empty?
      amend_validate1(a, desc.text.strip,
                      a.at("./newcontent//figure | " \
                           "./newcontent//formula"))
    else @log.add("IEEE_16", a)
    end
  end
end

#amend_validate1(amend, description, figure_or_formula) ⇒ Object



202
203
204
205
206
207
208
209
210
211
# File 'lib/metanorma/ieee/validate.rb', line 202

def amend_validate1(amend, description, figure_or_formula)
  case amend["change"]
  when "add" then /^Insert /.match?(description) or
    @log.add("IEEE_17", amend)
  when "delete" then /^Insert /.match?(description) or
    @log.add("IEEE_18", amend)
  when "modify"
    amend_validate_modify(amend, description, figure_or_formula)
  end
end

#amend_validate_modify(amend, description, figure_or_formula) ⇒ Object



213
214
215
216
217
218
219
220
221
# File 'lib/metanorma/ieee/validate.rb', line 213

def amend_validate_modify(amend, description, figure_or_formula)
  if !/^Change |^Replace/.match?(description)
    @log.add("IEEE_19", amend)
  elsif /^Change /.match?(description)
    !figure_or_formula or @log.add("IEEE_20", amend)
  else
    figure_or_formula or @log.add("IEEE_21", amend)
  end
end

#asset_style(root) ⇒ Object



20
21
22
# File 'lib/metanorma/ieee/validate_style.rb', line 20

def asset_style(root)
  root.xpath(ASSETS_TO_STYLE).each { |e| style(e, extract_text(e)) }
end

#bibdata_validate(doc) ⇒ Object



23
24
25
26
# File 'lib/metanorma/ieee/validate.rb', line 23

def bibdata_validate(doc)
  doctype_validate(doc)
  stage_validate(doc)
end

#bibitem_validate(root) ⇒ Object



68
69
70
# File 'lib/metanorma/ieee/validate.rb', line 68

def bibitem_validate(root)
  normative_dated_refs(root)
end

#bibliography_validate(root) ⇒ Object

Style manual 19.1



108
109
110
111
112
113
114
115
116
117
# File 'lib/metanorma/ieee/validate_section.rb', line 108

def bibliography_validate(root)
  bib = root.at("//references[@normative = 'false']") or return
  if annex = bib.at(".//ancestor::annex")
    prec = annex.xpath("./preceding-sibling::annex")
    foll = annex.xpath("./following-sibling::annex")
    valid = prec.empty? || foll.empty?
  else valid = false
  end
  valid or @log.add("IEEE_31", bib)
end

#content_validate(doc) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/metanorma/ieee/validate.rb', line 11

def content_validate(doc)
  super
  bibdata_validate(doc.root)
  title_validate(doc.root)
  locality_validate(doc.root)
  bibitem_validate(doc.root)
  list_validate(doc)
  table_style(doc)
  figure_validate(doc)
  amend_validate(doc)
end

#doctype_validate(xmldoc) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/metanorma/ieee/validate.rb', line 28

def doctype_validate(xmldoc)
  %w(standard recommended-practice guide whitepaper redline other)
    .include?(@doctype) or
    @log.add("IEEE_5", nil, params: [@doctype])
  docsubtype = xmldoc.at("//bibdata/ext/subdoctype")&.text or return
  %w(amendment corrigendum erratum document).include? docsubtype or
    @log.add("IEEE_6", nil, params: [docsubtype])
end

#extract_text(node) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/metanorma/ieee/validate_style.rb', line 10

def extract_text(node)
  return "" if node.nil?

  node1 = Nokogiri::XML.fragment(node.to_s)
  node1.xpath("//link | //locality | //localityStack").each(&:remove)
  ret = ""
  node1.traverse { |x| ret += x.text if x.text? }
  HTMLEntities.new.decode(ret)
end

#figure_name_style_validate(docxml) ⇒ Object

Style manual 17.2



154
155
156
157
158
159
# File 'lib/metanorma/ieee/validate.rb', line 154

def figure_name_style_validate(docxml)
  docxml.xpath("//figure/name").each do |td|
    style_regex(/^(?<num>\p{Lower}\s*)/,
                "figure heading should be capitalised", td, td.text)
  end
end

#figure_name_validate(xmldoc, xrefs) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/metanorma/ieee/validate.rb', line 141

def figure_name_validate(xmldoc, xrefs)
  pref = image_name_prefix(xmldoc)
  (xmldoc.xpath("//figure") - xmldoc.xpath("//table//figure"))
    .each do |f|
    (i = f.at("./image") and !i["src"]&.start_with?("data:")) or next
    num = xrefs.anchor(f["id"], :label)
    base = File.basename(i["src"], ".*")
    base == "#{pref}_fig#{num}" or
      @log.add("IEEE_13", i, params: [base, "#{pref}_fig#{num}"])
  end
end

#figure_validate(xmldoc) ⇒ Object

Style manual 17.1



113
114
115
116
117
118
119
# File 'lib/metanorma/ieee/validate.rb', line 113

def figure_validate(xmldoc)
  xrefs = xrefs(xmldoc)
  figure_name_validate(xmldoc, xrefs)
  figure_name_style_validate(xmldoc)
  table_figure_name_validate(xmldoc, xrefs)
  table_figure_quantity_validate(xmldoc)
end

#image_name_prefix(xmldoc) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/metanorma/ieee/validate.rb', line 132

def image_name_prefix(xmldoc)
  num = xmldoc.at("//bibdata/docnumber") or return
  yr = xmldoc.at("//bibdata/date[@type = 'published']") ||
    xmldoc.at("//bibdata/date[@type = 'issued']") ||
    xmldoc.at("//bibdata/copyright/from")
  yr = yr&.text || Date.now.year
  "#{num.text}-#{yr.sub(/-*$/, '')}"
end

#list_validate(doc) ⇒ Object



80
81
82
83
# File 'lib/metanorma/ieee/validate.rb', line 80

def list_validate(doc)
  listcount_validate(doc)
  listdepth_validate(doc)
end

#listcount_validate(doc) ⇒ Object

Style manual 13.3



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/metanorma/ieee/validate.rb', line 100

def listcount_validate(doc)
  doc.xpath("//sections//clause | //annex").each do |c|
    next if c.xpath(".//ol").empty?

    ols = c.xpath(".//ol") -
      c.xpath(".//ul//ol | .//ol//ol | .//clause//ol")
    ols.size > 1 and
      style_warning(c, "More than 1 ordered list in a numbered clause",
                    nil)
  end
end

#listdepth_validate(doc) ⇒ Object

Template provision of styles



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/metanorma/ieee/validate.rb', line 86

def listdepth_validate(doc)
  doc.xpath("//ul[.//ul//ul]").each do |u|
    next unless u.ancestors("ul").empty?

    @log.add("IEEE_11", u)
  end
  doc.xpath("//ol[.//ol//ol//ol//ol//ol]").each do |u|
    next unless u.ancestors("ol").empty?

    @log.add("IEEE_12", u)
  end
end

#locality_erefs_validate(root) ⇒ Object

Style manual 12.3.2



60
61
62
63
64
65
66
# File 'lib/metanorma/ieee/validate.rb', line 60

def locality_erefs_validate(root)
  root.xpath("//eref[descendant::locality]").each do |t|
    if !/[:-](\d+{4})($|-\d\d)/.match?(t["citeas"])
      @log.add("IEEE_9", t, params: [t["citeas"]])
    end
  end
end

#locality_range_validate(root) ⇒ Object

Style manual 17.2 &c



52
53
54
55
56
57
# File 'lib/metanorma/ieee/validate.rb', line 52

def locality_range_validate(root)
  root.xpath("//eref | xref").each do |e|
    e.at(".//localityStack[@connective = 'from'] | .//referenceTo") and
      @log.add("IEEE_8", e)
  end
end

#locality_validate(root) ⇒ Object



46
47
48
49
# File 'lib/metanorma/ieee/validate.rb', line 46

def locality_validate(root)
  locality_range_validate(root)
  locality_erefs_validate(root)
end

#normative_dated_refs(root) ⇒ Object

Style manual 12.3.1



73
74
75
76
77
78
# File 'lib/metanorma/ieee/validate.rb', line 73

def normative_dated_refs(root)
  root.xpath("//references[@normative = 'true']/bibitem").each do |b|
    b.at(".//date") or
      @log.add("IEEE_10", b, params: [b.at("./@anchor")&.text])
  end
end

#onlychild_clause_validate(root) ⇒ Object

Style manual 13.1



100
101
102
103
104
105
# File 'lib/metanorma/ieee/validate_section.rb', line 100

def onlychild_clause_validate(root)
  root.xpath(Standoc::Utils::SUBCLAUSE_XPATH).each do |c|
    c.xpath("../clause").size == 1 or next
    @log.add("IEEE_30", c)
  end
end

#preposition?(word) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
150
151
152
153
154
155
# File 'lib/metanorma/ieee/validate_style.rb', line 147

def preposition?(word)
  %w(aboard about above across after against along amid among anti around
     as at before behind below beneath beside besides between beyond but
     by concerning considering despite down during except excepting
     excluding following for from in inside into like minus near of off
     on onto opposite outside over past per plus regarding round save
     since than through to toward towards under underneath unlike until
     up upon versus via with within without a an the).include?(word)
end

#schema_fileObject



7
8
9
# File 'lib/metanorma/ieee/validate.rb', line 7

def schema_file
  "ieee.rng"
end

#section_validate(doc) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/metanorma/ieee/validate_section.rb', line 4

def section_validate(doc)
  unless %w(amendment technical-corrigendum).include? @doctype
    sections_presence_validate(doc.root)
    sections_sequence_validate(doc.root)
  end
  subclause_validate(doc.root)
  onlychild_clause_validate(doc.root)
  super
end

#sections_presence_validate(root) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/metanorma/ieee/validate_section.rb', line 14

def sections_presence_validate(root)
  root.at("//sections/clause[@type = 'overview']") or
    @log.add("IEEE_22", nil)
  root.at("//sections/clause[@type = 'overview']/clause[@type = 'scope']") or
    @log.add("IEEE_23", nil)
  root.at("//sections/clause[@type = 'overview']/clause[@type = 'word-usage']") or
    @log.add("IEEE_24", nil)
  # ID = IEEE_24
  root.at("//references[@normative = 'true']") or
    @log.add("IEEE_25", nil)
  # ID = IEEE_25
  root.at("//terms") or
    @log.add("IEEE_26", nil)
end

#sections_sequence_validate(root) ⇒ Object



58
59
60
61
62
63
# File 'lib/metanorma/ieee/validate_section.rb', line 58

def sections_sequence_validate(root)
  names, n = sections_sequence_validate_start(root)
  names, n = sections_sequence_validate_body(names, n)
  sections_sequence_validate_end(names, n)
  bibliography_validate(root)
end

#sections_sequence_validate_body(names, elem) ⇒ Object



76
77
78
# File 'lib/metanorma/ieee/validate_section.rb', line 76

def sections_sequence_validate_body(names, elem)
  [names, elem]
end

#sections_sequence_validate_end(names, elem) ⇒ Object



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

def sections_sequence_validate_end(names, elem)
  while elem&.name == "annex"
    elem = names.shift
    if elem.nil?
      @log.add("IEEE_28", nil)
    end
  end
  elem&.at("./self::references[@normative = 'true']") ||
    @log.add("IEEE_28", nil)
end

#sections_sequence_validate_start(root) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/metanorma/ieee/validate_section.rb', line 65

def sections_sequence_validate_start(root)
  names = root.xpath(SECTIONS_XPATH)
  names = seqcheck(names, SEQ[0][:msg], SEQ[0][:val])
  n = names[0]
  names = seqcheck(names, SEQ[1][:msg], SEQ[1][:val])
  names = seqcheck(names, SEQ[2][:msg], SEQ[2][:val])
  n = names.shift
  n = names.shift if n&.at("./self::definitions")
  [names, n]
end

#seqcheck(names, msg, accepted) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/metanorma/ieee/validate_section.rb', line 29

def seqcheck(names, msg, accepted)
  n = names.shift
  return [] if n.nil?

  test = accepted.map { |a| n.at(a) }
  if test.all?(&:nil?)
    @log.add("IEEE_27", nil, params: [msg])
  end
  names
end

#stage_validate(xmldoc) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/metanorma/ieee/validate.rb', line 37

def stage_validate(xmldoc)
  stage = xmldoc&.at("//bibdata/status/stage")&.text
  # a taste's stage repertoire (:docstage-valid:) supersedes the
  # flavour's own
  stages = @docstage_valid || %w(draft approved superseded withdrawn)
  stages.include? stage or
    @log.add("IEEE_7", nil, params: [stage])
end

#strict_capitalize_phrase(str) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/metanorma/ieee/validate_style.rb', line 126

def strict_capitalize_phrase(str)
  ret = str.split(/[ -]/).map do |w|
    letters = w.chars
    letters.first.upcase! unless /^[ -]/.match?(w)
    letters.join
  end.join(" ")
  ret = "Trial-Use" if ret == "Trial Use"
  ret
end

#style(node, text) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/metanorma/ieee/validate_style.rb', line 36

def style(node, text)
  return if @novalid

  style_number(node, text)
  style_percent(node, text)
  style_units(node, text)
end

#style_number(node, text) ⇒ Object

Style manual 14.2



45
46
47
48
49
50
# File 'lib/metanorma/ieee/validate_style.rb', line 45

def style_number(node, text)
  style_regex(/\b(?<num>[0-9]+,[0-9]+)/i,
              "possible decimal comma", node, text)
  style_regex(/(?:^|\s)(?<num>[\u2212-]?\.[0-9]+)/i,
              "decimal without initial zero", node, text)
end

#style_percent(node, text) ⇒ Object

Style manual 14.2



53
54
55
56
# File 'lib/metanorma/ieee/validate_style.rb', line 53

def style_percent(node, text)
  style_regex(/\b(?<num>[0-9.]+%)/,
              "no space before percent sign", node, text)
end

#style_regex(regex, warning, node, text) ⇒ Object



24
25
26
# File 'lib/metanorma/ieee/validate_style.rb', line 24

def style_regex(regex, warning, node, text)
  (m = regex.match(text)) && style_warning(node, warning, m[:num])
end

#style_units(node, text) ⇒ Object

Style manual 14.2



64
65
66
67
68
69
# File 'lib/metanorma/ieee/validate_style.rb', line 64

def style_units(node, text)
  style_regex(/(\b|^)(?<num>[0-9][0-9.]*#{SI_UNIT})\b/o,
              "no space between number and SI unit", node, text)
  style_regex(/(\b|^)(?<num>[0-9.]+\s*\u00b1\s*[0-9.]+\s*#{SI_UNIT})\b/o,
              "unit is needed on both value and tolerance", node, text)
end

#style_warning(node, msg, text = nil) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/metanorma/ieee/validate_style.rb', line 28

def style_warning(node, msg, text = nil)
  return if @novalid

  w = msg
  w += ": #{text}" if text
  @log.add("STANDOC_48", node, params: [w])
end

#subclause_validate(root) ⇒ Object

Style manual 13.1



92
93
94
95
96
97
# File 'lib/metanorma/ieee/validate_section.rb', line 92

def subclause_validate(root)
  root.xpath("//clause/clause/clause/clause/clause/clause")
    .each do |c|
    style_warning(c, "Exceeds the maximum clause depth of 5", nil)
  end
end

#table_extract_columns(table) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/metanorma/ieee/validate_style.rb', line 99

def table_extract_columns(table)
  ret = table.xpath(".//tr").each_with_object([]) do |tr, m|
    tr.xpath("./td | ./th").each_with_index do |d, i|
      m[i] ||= []
      m[i] << d.text
    end
  end
  ret.map { |x| x.is_a?(Array) ? x : [] }
end

#table_figure_name_validate(xmldoc, xrefs) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/metanorma/ieee/validate.rb', line 161

def table_figure_name_validate(xmldoc, xrefs)
  xmldoc.xpath("//table[.//figure]").each do |t|
    xmldoc.xpath(".//figure").each do |f|
      (i = f.at("./image") and !i["src"]&.start_with?("data:")) or next
      num = tablefigurenumber(t, f, xrefs)
      base = File.basename(i["src"])
      base == num or
        @log.add("IEEE_13", i, params: [base, num])
    end
  end
end

#table_figure_quantity_validate(xmldoc) ⇒ Object



182
183
184
185
186
187
# File 'lib/metanorma/ieee/validate.rb', line 182

def table_figure_quantity_validate(xmldoc)
  xmldoc.xpath("//td[.//image] | //th[.//image]").each do |d|
    d.xpath(".//image").size > 1 and
      @log.add("IEEE_15", d)
  end
end

#table_style(docxml) ⇒ Object

Style manual 16.2, 16.3.2



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/metanorma/ieee/validate_style.rb', line 72

def table_style(docxml)
  docxml.xpath("//td").each do |td|
    style_regex(/^(?<num>[\u2212-]?[0-9]{5,}[.0-9]*|-?[0-9]+\.[0-9]{5,})$/,
                "number in table not broken up in threes", td, td.text)
  end
  docxml.xpath("//table").each { |t| table_style_columns(t) }
  docxml.xpath("//table/name | //th").each do |td|
    style_regex(/^(?<num>\p{Lower}\S*)/, "table heading should be capitalised",
                td, td.text)
  end
end

#table_style_columns(table) ⇒ Object

deliberately doing naive, ignoring rowspan



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/metanorma/ieee/validate_style.rb', line 85

def table_style_columns(table)
  table_extract_columns(table).each do |col|
    col.any? do |x|
      /^[0-9. ]+$/.match?(x) &&
        (/\d{3} \d/.match?(x) || /\d \d{3}/.match?(x))
    end or next

    col.each do |x|
      /^[0-9. ]+$/.match?(x) && /\d{4}/.match?(x) and
        @log.add("IEEE_2", table, params: [x])
    end
  end
end

#tablefigurenumber(table, figure, xrefs) ⇒ Object



173
174
175
176
177
178
179
180
# File 'lib/metanorma/ieee/validate.rb', line 173

def tablefigurenumber(table, figure, xrefs)
  tab = xrefs.anchor(table["id"], :label)
  td = figure.at("./ancestor::td | ./ancestor::th")
  cols = td.xpath("./preceding-sibling::td | ./preceding-sibling::td")
  rows = td.parent.xpath("./preceding::tr") &
    td.at("./ancestor::table").xpath(".//tr")
  "Tab#{tab}Row#{rows.size + 1}Col#{cols.size + 1}"
end

#title_validate(xml) ⇒ Object



109
110
111
112
# File 'lib/metanorma/ieee/validate_style.rb', line 109

def title_validate(xml)
  title_validate_type(xml)
  title_validate_capitalisation(xml)
end

#title_validate_capitalisation(xml) ⇒ Object

Style Manual 11.3



137
138
139
140
141
142
143
144
145
# File 'lib/metanorma/ieee/validate_style.rb', line 137

def title_validate_capitalisation(xml)
  title = xml.at("//bibdata/title") or return
  found = false
  title.text.split(/[ -]/).each do |w|
    /^[[:upper:]]/.match?(w) or preposition?(w) or
      found = true
  end
  found and @log.add("IEEE_4", title)
end

#title_validate_type(xml) ⇒ Object

Style Manual 11.3



115
116
117
118
119
120
121
122
123
124
# File 'lib/metanorma/ieee/validate_style.rb', line 115

def title_validate_type(xml)
  title = xml.at("//bibdata/title") or return
  draft = xml.at("//bibdata//version")
  trial = xml.at("//bibdata/ext/trial-use[text() = 'true']")
  target = draft ? "Draft " : ""
  target += trial ? "Trial-Use " : ""
  target += @doctype ? "#{strict_capitalize_phrase(@doctype)} " : ""
  /^#{target}/.match?(title.text) or
    @log.add("IEEE_3", title, params: [target])
end

#xrefs(xmldoc) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/metanorma/ieee/validate.rb', line 121

def xrefs(xmldoc)
  klass = IsoDoc::Ieee::HtmlConvert.new(language: @lang, script: @script)
  xrefs = IsoDoc::Ieee::Xref
    .new(@lang, @script, klass, IsoDoc::Ieee::I18n.new(@lang, @script),
         { hierarchicalassets: @hierarchical_assets })
  # don't process refs without relaton-render init
  xrefs.parse_inclusions(clauses: true, assets: true)
    .parse(Nokogiri::XML(xmldoc.to_xml))
  xrefs
end