Class: Metanorma::IEEE::Converter

Inherits:
Standoc::Converter
  • Object
show all
Defined in:
lib/metanorma/ieee/front.rb,
lib/metanorma/ieee/cleanup.rb,
lib/metanorma/ieee/validate.rb,
lib/metanorma/ieee/converter.rb,
lib/metanorma/ieee/cleanup_ref.rb,
lib/metanorma/ieee/validate_style.rb,
lib/metanorma/ieee/validate_section.rb

Constant Summary collapse

TERM_CLAUSE =
"//sections//terms".freeze
PARTICIPANT_BOILERPLATE_LOCATIONS =
{ "boilerplate-participants-wg": "working group",
"boilerplate-participants-bg": "balloting group",
"boilerplate-participants-sb": "standards board",
"boilerplate-participants-blank": nil }.freeze
AMD_VALID_MOD =
[
  "'Modify' change description should start with _Change_ or _Replace_",
  "'Modify' change description for change involving figure or equation " \
  "should start with _Replace_",
  "'Modify' change description for change not involving figure or " \
  "equation should start with _Change_",
].freeze
XML_ROOT_TAG =
"ieee-standard".freeze
XML_NAMESPACE =
"https://www.metanorma.org/ns/ieee".freeze
PREFACE_CLAUSE_NAMES =
%w(abstract foreword introduction
acknowledgements participants).freeze
BIBLIO =
"//bibliography/references[@normative = 'false'][not(@hidden)] | " \
"//bibliography/clause[.//references[@normative = 'false']] | " \
"//annex//references[@normative = 'false'][not(@hidden)]".freeze
IEEE =
"Institute of Electrical and Electronics Engineers".freeze
BIBITEM_NO_AVAIL =
"//references/bibitem[not(note[@type = 'Availability'])]".freeze
AVAIL_PUBS =
{
  ieee: IEEE,
  cispr: "International special committee on radio interference",
  etsi: "European Telecommunications Standards Institute",
  oasis: "OASIS",
  "w3c": "World Wide Web Consortium",
  "3gpp": "3rd Generation Partnership Project",
}.freeze
ASSETS_TO_STYLE =
"//termsource | //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)] | "\
"//clause[descendant::references][not(parent::clause)]".freeze

Instance Method Summary collapse

Instance Method Details

#amend_validate(xmldoc) ⇒ Object

Style manual 20.2.2



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

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("Style", a,
                  "Editorial instruction is missing from change")
    end
  end
end

#amend_validate1(amend, description, figure_or_formula) ⇒ Object



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

def amend_validate1(amend, description, figure_or_formula)
  case amend["change"]
  when "add" then /^Insert /.match?(description) or
    @log.add("Style", amend,
             "'Add' change description should start with _Insert_")
  when "delete" then /^Insert /.match?(description) or
    @log.add("Style", amend,
             "'Delete' change description should start with _Delete_")
  when "modify"
    amend_validate_modify(amend, description, figure_or_formula)
  end
end

#amend_validate_modify(amend, description, figure_or_formula) ⇒ Object



235
236
237
238
239
240
241
242
243
# File 'lib/metanorma/ieee/validate.rb', line 235

def amend_validate_modify(amend, description, figure_or_formula)
  if !/^Change |^Replace/.match?(description)
    @log.add("Style", amend, AMD_VALID_MOD[0])
  elsif /^Change /.match?(description)
    !figure_or_formula or @log.add("Style", amend, AMD_VALID_MOD[1])
  else
    figure_or_formula or @log.add("Style", amend, AMD_VALID_MOD[2])
  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

#available_note(xmldoc, provenance_notes) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 172

def available_note(xmldoc, provenance_notes)
  iso_iec_available_note(xmldoc, provenance_notes["iso-iec"], true, true)
  iso_iec_available_note(xmldoc, provenance_notes["iso"], true, false)
  iso_iec_available_note(xmldoc, provenance_notes["iec"], false, true)
  itu_available_note(xmldoc, provenance_notes["itut"], true)
  itu_available_note(xmldoc, provenance_notes["itur"], false)
  nist_available_note(xmldoc, provenance_notes["fips"], true)
  nist_available_note(xmldoc, provenance_notes["nist"], false)
  ietf_available_note(xmldoc, provenance_notes["ietf"])
  AVAIL_PUBS.each do |k, v|
    sdo_available_note(xmldoc, provenance_notes[k.to_s], v)
  end
end

#base_pubidObject



107
108
109
# File 'lib/metanorma/ieee/front.rb', line 107

def base_pubid
  Pubid::Ieee::Identifier
end

#bib_pubs(bib) ⇒ Object



145
146
147
148
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 145

def bib_pubs(bib)
  bib.xpath("./contributor[role/@type = 'publisher']/organization/name")
    .map(&:text)
end

#bibdata_cleanup(xmldoc) ⇒ Object



220
221
222
223
# File 'lib/metanorma/ieee/cleanup.rb', line 220

def bibdata_cleanup(xmldoc)
  super
  provenance_title(xmldoc)
end

#bibdata_validate(doc) ⇒ Object



25
26
27
28
# File 'lib/metanorma/ieee/validate.rb', line 25

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

#bibitem_cleanup(xmldoc) ⇒ Object



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

def bibitem_cleanup(xmldoc)
  super
  f = File.join(File.dirname(__FILE__), "ieee-footnotes.yaml")
  @provenance_notes = YAML.safe_load(File.read(f))
  withdrawn_note(xmldoc, @provenance_notes)
end

#bibitem_validate(root) ⇒ Object



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

def bibitem_validate(root)
  normative_dated_refs(root)
end

#biblio_preface(ref) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 14

def biblio_preface(ref)
  if ref.at("./note[@type = 'boilerplate']")
    unwrap_boilerplate_clauses(ref, ".")
  else
    ref.at("./title").next = "<p>#{@i18n.biblio_pref}</p>"
  end
end

#biblio_renumber(xmldoc) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 97

def biblio_renumber(xmldoc)
  i = 0
  xmldoc.xpath("//references[not(@normative = 'true')]" \
               "[not(@hidden = 'true')]").each do |r|
                 r.xpath("./bibitem[not(@hidden = 'true')]").each do |b|
                   i += 1
                   biblio_renumber1(b, i)
                 end
               end
end

#biblio_renumber1(bib, idx) ⇒ Object



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

def biblio_renumber1(bib, idx)
  docid = bib.at("./docidentifier[@type = 'metanorma' or " \
                 "@type = 'metanorma-ordinal']")
  if /^\[?\d+\]?$/.match?(docid&.text)
    docid.children = "[B#{idx}]"
  elsif docid = bib.at("./docidentifier") || bib.at("./title[last()]") ||
      bib.at("./formattedref")
    docid.next =
      "<docidentifier type='metanorma-ordinal'>[B#{idx}]</docidentifier>"
  end
end

#biblio_reorder(xmldoc) ⇒ Object



140
141
142
143
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 140

def biblio_reorder(xmldoc)
  super
  available_note(xmldoc, @provenance_notes)
end

#bibliography_validate(root) ⇒ Object

Style manual 19.1



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

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("Style", bib, "Bibliography must be either the first "\
                                  "or the last document annex")
end

#boilerplate_cleanup(xmldoc) ⇒ Object



9
10
11
12
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 9

def boilerplate_cleanup(xmldoc)
  f = xmldoc.at(BIBLIO) and biblio_preface(f)
  super
end

#boilerplate_file(xmldoc) ⇒ Object



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

def boilerplate_file(xmldoc)
  file = "boilerplate.adoc"
  doctype = xmldoc.at("//bibdata/ext/doctype")&.text
  doctype == "whitepaper" and file = "boilerplate_wp.adoc"
  File.join(@libdir, file)
end

#boilerplate_isodoc(xmldoc) ⇒ Object



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

def boilerplate_isodoc(xmldoc)
  x = xmldoc.dup
  x.root.add_namespace(nil, self.class::XML_NAMESPACE)
  xml = Nokogiri::XML(x.to_xml)
  i = isodoc(@lang, @script, @locale)
  i.bibdata_i18n(xml.at("//xmlns:bibdata"))
  i.info(xml, nil)
  i
end

#clause_parse(attrs, xml, node) ⇒ Object



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

def clause_parse(attrs, xml, node)
  case node.title
  when "Purpose" then attrs[:type] = "purpose"
  when "Overview" then attrs[:type] = "overview"
  when "Scope" then attrs[:type] = "scope"
  when "Word Usage" then attrs[:type] = "word-usage"
  when "Participants" then attrs[:type] = "participants"
  end
  super
end

#compact_blank(hash) ⇒ Object



60
61
62
# File 'lib/metanorma/ieee/front.rb', line 60

def compact_blank(hash)
  hash.compact.reject { |_, v| v.is_a?(String) && v.empty? }
end

#content_validate(doc) ⇒ Object



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

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

#datetypesObject



123
124
125
# File 'lib/metanorma/ieee/front.rb', line 123

def datetypes
  super + %w{feedback-ended}
end

#default_publisherObject



111
112
113
# File 'lib/metanorma/ieee/front.rb', line 111

def default_publisher
  "IEEE"
end

#designator_docid(bib) ⇒ Object



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

def designator_docid(bib)
  n = bib.at("./docidentifier[@primary]") ||
    bib.at("./docidentifier[not(#{skip_docid})]")
  n or return "ZZZZ"
  @isodoc.docid_prefix(n["type"], n.children.to_xml)
end

#designator_or_name(bib) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 40

def designator_or_name(bib)
  case bib["type"]
  when "standard", "techreport" then designator_docid(bib)
  else
    bib1 = bib.dup
    bib1.add_namespace(nil, self.class::XML_NAMESPACE)
    n = @i.creatornames(bib1)
    n.nil? && bib["type"].nil? and n = designator_docid(bib)
    n
  end
end

#doc_converter(node) ⇒ Object



99
100
101
# File 'lib/metanorma/ieee/converter.rb', line 99

def doc_converter(node)
  IsoDoc::IEEE::WordConvert.new(doc_extract_attributes(node))
end

#doc_extract_attributes(node) ⇒ Object



77
78
79
80
81
# File 'lib/metanorma/ieee/converter.rb', line 77

def doc_extract_attributes(node)
  super.merge(hierarchicalassets:
              node.attr("hierarchical-object-numbering"),
              ulstyle: "l11", olstyle: "l16")
end

#doctype_validate(xmldoc) ⇒ Object



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

def doctype_validate(xmldoc)
  %w(standard recommended-practice guide whitepaper redline other)
    .include?(@doctype) or
    @log.add("Document Attributes", nil,
             "#{@doctype} is not a recognised document type")
  docsubtype = xmldoc.at("//bibdata/ext/subdoctype")&.text or return
  %w(amendment corrigendum erratum document).include? docsubtype or
    @log.add("Document Attributes", nil,
             "#{docsubtype} is not a recognised document subtype")
end

#extract_participants(dlist) ⇒ Object



204
205
206
207
208
209
210
211
212
213
# File 'lib/metanorma/ieee/cleanup.rb', line 204

def extract_participants(dlist)
  key = ""
  map = dlist.xpath("./dt | ./dd").each_with_object({}) do |dtd, m|
    (dtd.name == "dt" and key = dtd.text.sub(/:+$/, "")) or
      m[key.strip.downcase] = text_from_paras(dtd)
  end
  map["company"] &&= "<span class='organization'>#{map['company']}</span>"
  map["role"] ||= "member"
  map
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



163
164
165
166
167
168
# File 'lib/metanorma/ieee/validate.rb', line 163

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



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/metanorma/ieee/validate.rb', line 149

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("Style", i,
                 "Image name #{base} is expected to be #{pref}_fig#{num}")
    end
end

#figure_validate(xmldoc) ⇒ Object

Style manual 17.1



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

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

#footnote_boilerplate_renumber(xml) ⇒ Object



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

def footnote_boilerplate_renumber(xml)
  xml.xpath("//boilerplate//fn").each_with_index do |f, i|
    f["reference"] = "_boilerplate_#{i + 1}"
  end
end

#html_converter(node) ⇒ Object



89
90
91
# File 'lib/metanorma/ieee/converter.rb', line 89

def html_converter(node)
  IsoDoc::IEEE::HtmlConvert.new(html_extract_attributes(node))
end

#html_extract_attributes(node) ⇒ Object



71
72
73
74
75
# File 'lib/metanorma/ieee/converter.rb', line 71

def html_extract_attributes(node)
  super.merge(hierarchicalassets:
              node.attr("hierarchical-object-numbering"),
              ieeedtd: node.attr("ieee-dtd"))
end

#ieee_id(node, xml) ⇒ Object



48
49
50
51
52
# File 'lib/metanorma/ieee/front.rb', line 48

def ieee_id(node, xml)
  params = ieee_id_params(node)
  params[:number] or return
  ieee_id_out(xml, params)
end

#ieee_id_out(xml, params) ⇒ Object



98
99
100
101
# File 'lib/metanorma/ieee/front.rb', line 98

def ieee_id_out(xml, params)
  id = pubid_select(params).create(**params)
  xml.docidentifier id.to_s, type: "IEEE", primary: "true"
end

#ieee_id_params(node) ⇒ Object



54
55
56
57
58
# File 'lib/metanorma/ieee/front.rb', line 54

def ieee_id_params(node)
  core = ieee_id_params_core(node)
  amd = ieee_id_params_amd(node, core) || {}
  core.merge(amd)
end

#ieee_id_params_amd(node, core) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/metanorma/ieee/front.rb', line 76

def ieee_id_params_amd(node, core)
  if a = node.attr("corrigendum-number")
    { corrigendum: { version: a,
                     year: ieee_id_year(node, initial: false) } }
  elsif node.attr("amendment-number")
    { amendment: pubid_select(core).create(**core) }
  end
end

#ieee_id_params_core(node) ⇒ Object



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

def ieee_id_params_core(node)
  pub = ieee_id_pub(node)
  ret = { number: node.attr("docnumber"),
          part: node.attr("partnumber"),
          year: ieee_id_year(node, initial: true),
          redline: @doctype == "redline",
          publisher: pub[0],
          copublisher: pub[1..-1] }
  ret[:copublisher].empty? and ret.delete(:copublisher)
  compact_blank(ret)
end

#ieee_id_pub(node) ⇒ Object



85
86
87
88
# File 'lib/metanorma/ieee/front.rb', line 85

def ieee_id_pub(node)
  (node.attr("publisher") || default_publisher).split(/[;,]/)
    .map(&:strip).map { |x| org_abbrev[x] || x }
end

#ieee_id_year(node, initial: false) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/metanorma/ieee/front.rb', line 90

def ieee_id_year(node, initial: false)
  unless initial
    y = node.attr("copyright-year") || node.attr("updated-date")
  end
  y ||= node.attr("published-date") || node.attr("copyright-year")
  y&.sub(/-.*$/, "") || Date.today.year
end

#ieee_xml_converter(node) ⇒ Object



103
104
105
106
107
# File 'lib/metanorma/ieee/converter.rb', line 103

def ieee_xml_converter(node)
  return nil if node.attr("no-pdf")

  IsoDoc::Iso::IEEEXMLConvert.new(html_extract_attributes(node))
end

#ietf_available_note(xmldoc, note) ⇒ Object



204
205
206
207
208
209
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 204

def ietf_available_note(xmldoc, note)
  ret = xmldoc.xpath(BIBITEM_NO_AVAIL).detect do |b|
    b.at("./docidentifier[@type = 'IETF']")
  end
  insert_availability_note(ret, note)
end

#image_name_prefix(xmldoc) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/metanorma/ieee/validate.rb', line 140

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

#init(node) ⇒ Object



17
18
19
20
21
# File 'lib/metanorma/ieee/converter.rb', line 17

def init(node)
  super
  @document_scheme ||= "ieee-sa-2021"
  @hierarchical_assets = node.attr("hierarchical-object-numbering")
end

#initial_boilerplate(xml, isodoc) ⇒ Object



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

def initial_boilerplate(xml, isodoc)
  intro_boilerplate(xml, isodoc)
  super if @document_scheme == "ieee-sa-2021"
  xml.at("//boilerplate") or return
  initial_note(xml)
  word_usage(xml)
  participants(xml)
  footnote_boilerplate_renumber(xml)
end

#initial_note(xml) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/metanorma/ieee/cleanup.rb', line 32

def initial_note(xml)
  n = xml.at("//boilerplate//note[@id = 'boilerplate_front']")
  s = xml.at("//sections")
  (n && s) or return
  s.children.empty? and s << " "
  s.children.first.previous = n.remove
end

#insert_availability_note(bib, msg) ⇒ Object



229
230
231
232
233
234
235
236
237
238
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 229

def insert_availability_note(bib, msg)
  bib or return
  note = %(<note type="Availability"><p>#{msg}</p></note>)
  if b = bib.at("./language | ./script | ./abstract | ./status")
    b.previous = note
  else b = bib.at("./contributor") || bib.at("./date") ||
    bib.at("./docnumber") || bib.at("./docidentifier") ||
    bib.at("./title") and b.next = note
  end
end

#intro_boilerplate(xml, isodoc) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/metanorma/ieee/cleanup.rb', line 23

def intro_boilerplate(xml, isodoc)
  intro = xml.at("//introduction/title") or return
  template = <<~ADM
    This introduction is not part of P{{ docnumeric }}{% if draft %}/D{{ draft }}{% endif %}, {{ full_doctitle }}
  ADM
  adm = isodoc.populate_template(template)
  intro.next = "<admonition>#{adm}</admonition>"
end

#iso_iec_available_note(xmldoc, note, iso, iec) ⇒ Object



193
194
195
196
197
198
199
200
201
202
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 193

def iso_iec_available_note(xmldoc, note, iso, iec)
  ret = xmldoc.xpath(BIBITEM_NO_AVAIL).detect do |b|
    pubs = bib_pubs(b)
    has_iec = pubs.include?("International Electrotechnical Commission")
    has_iso = pubs.include?("International Organization for Standardization")
    ((has_iec && iec) || (!has_iec && !iec)) &&
      ((has_iso && iso) || (!has_iso && !iso))
  end
  insert_availability_note(ret, note)
end

#itu_available_note(xmldoc, note, itu_t) ⇒ Object



211
212
213
214
215
216
217
218
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 211

def itu_available_note(xmldoc, note, itu_t)
  ret = xmldoc.xpath(BIBITEM_NO_AVAIL).detect do |b|
    has_itu_t = /^ITU-T/.match?(b.at("./docidentifier[@type = 'ITU']")&.text)
    bib_pubs(b).include?("International Telecommunication Union") &&
      (!has_itu_t && !itu_t) || (has_itu_t && itu_t)
  end
  insert_availability_note(ret, note)
end

#list_validate(doc) ⇒ Object



86
87
88
89
# File 'lib/metanorma/ieee/validate.rb', line 86

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

#listcount_validate(doc) ⇒ Object

Style manual 13.3



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

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/metanorma/ieee/validate.rb', line 92

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

    @log.add("Style", u,
             "Use ordered lists for lists more than two levels deep.")
  end
  doc.xpath("//ol[.//ol//ol//ol//ol//ol]").each do |u|
    next unless u.ancestors("ol").empty?

    @log.add("Style", u,
             "Ordered lists should not be more than five levels deep.")
  end
end

#locality_erefs_validate(root) ⇒ Object

Style manual 12.3.2



63
64
65
66
67
68
69
70
71
# File 'lib/metanorma/ieee/validate.rb', line 63

def locality_erefs_validate(root)
  root.xpath("//eref[descendant::locality]").each do |t|
    if !/[:-](\d+{4})$/.match?(t["citeas"])
      @log.add("Style", t,
               "Undated reference #{t['citeas']} should not contain " \
               "specific elements")
    end
  end
end

#locality_range_validate(root) ⇒ Object

Style manual 17.2 &c



54
55
56
57
58
59
60
# File 'lib/metanorma/ieee/validate.rb', line 54

def locality_range_validate(root)
  root.xpath("//eref | xref").each do |e|
    e.at(".//localityStack[@connective = 'from'] | .//referenceTo") and
      @log.add("Style", e, "Cross-reference contains range, " \
                           "should be separate cross-references")
  end
end

#locality_validate(root) ⇒ Object



48
49
50
51
# File 'lib/metanorma/ieee/validate.rb', line 48

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

#metadata_committee(node, xml) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/metanorma/ieee/front.rb', line 7

def (node, xml)
  (node) or return
  xml.editorialgroup do |a|
    committee_component("society", node, a)
    committee_component("balloting-group", node, a)
    committee_component("working-group", node, a)
    committee_component("committee", node, a)
  end
end

#metadata_committee_prep(node) ⇒ Object



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

def (node)
  node.attr("doctype") == "whitepaper" &&
    node.attr("docsubtype") == "industry-connection-report" and
    node.set_attr("working-group",
                  "IEEE SA Industry Connections activity")
  node.attr("committee") || node.attr("society") ||
    node.attr("working-group") or return
  node.attr("balloting-group") && !node.attr("balloting-group-type") and
    node.set_attr("balloting-group-type", "individual")
  true
end

#metadata_ext(node, xml) ⇒ Object



142
143
144
145
146
# File 'lib/metanorma/ieee/front.rb', line 142

def (node, xml)
  super
  structured_id(node, xml)
  program(node, xml)
end

#metadata_id(node, xml) ⇒ Object



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

def (node, xml)
  if id = node.attr("docidentifier")
    xml.docidentifier id, **attr_code(type: "IEEE", primary: "true")
  else ieee_id(node, xml)
  end
  id = node.attr("stdid-pdf") and
    xml.docidentifier id, type: "IEEE", scope: "PDF"
  id = node.attr("stdid-print") and
    xml.docidentifier id, type: "IEEE", scope: "print"
end

#metadata_other_id(node, xml) ⇒ Object



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

def (node, xml)
  a = node.attr("isbn-pdf") and
    xml.docidentifier a, type: "ISBN", scope: "PDF"
  a = node.attr("isbn-print") and
    xml.docidentifier a, type: "ISBN", scope: "print"
  xml.docnumber node.attr("docnumber")
end

#metadata_status(node, xml) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/metanorma/ieee/front.rb', line 115

def (node, xml)
  status = node.attr("status") || node.attr("docstage") ||
    (node.attr("draft") ? "draft" : "approved")
  xml.status do |s|
    s.stage status
  end
end

#metadata_subdoctype(node, xml) ⇒ Object



127
128
129
130
# File 'lib/metanorma/ieee/front.rb', line 127

def (node, xml)
  xml.subdoctype (node.attr("docsubtype") || "document")
  s = node.attr("trial-use") and xml.trial_use s
end

#nist_available_note(xmldoc, note, fips) ⇒ Object



220
221
222
223
224
225
226
227
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 220

def nist_available_note(xmldoc, note, fips)
  ret = xmldoc.xpath(BIBITEM_NO_AVAIL).detect do |b|
    id = b.at("./docidentifier[@type = 'NIST']")
    has_fips = /\bFIPS\b/.match?(id&.text)
    id && ((has_fips && !fips) || (!has_fips && fips))
  end
  insert_availability_note(ret, note)
end

#normative_dated_refs(root) ⇒ Object

Style manual 12.3.1



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

def normative_dated_refs(root)
  root.xpath("//references[@normative = 'true']/bibitem").each do |b|
    b.at(".//date") or
      @log.add("Style", b,
               "Normative reference #{b&.at('./@id')&.text} is not dated.")
  end
end

#normref_cleanup(xmldoc) ⇒ Object



59
60
61
62
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 59

def normref_cleanup(xmldoc)
  super
  normref_reorder(xmldoc)
end

#normref_reorder(xmldoc) ⇒ Object



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

def normref_reorder(xmldoc)
  xmldoc.xpath("//references[@normative = 'true']").each do |r|
    biblio_reorder1(r)
  end
end

#note_cleanup(xmldoc) ⇒ Object



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

def note_cleanup(xmldoc)
  super
  n = xmldoc.at("//preface//note[not(@type = 'boilerplate')]" \
                "[not(./ancestor::abstract)] | " \
                "//sections//note[not(@type = 'boilerplate')] | " \
                "//annex//note[not(@type = 'boilerplate')]") or
    return
  ins = n.at("./p[last()]")
  ins << "<fn reference='_boilerplate_cleanup1'>" \
         "<p>#{@i18n.note_inform_fn}</p></fn>"
end

#obligations_cleanup_norm(xml) ⇒ Object



49
50
51
52
53
54
# File 'lib/metanorma/ieee/cleanup.rb', line 49

def obligations_cleanup_norm(xml)
  super
  xml.xpath("//sections/clause").each do |r|
    r["obligation"] = "normative"
  end
end

#onlychild_clause_validate(root) ⇒ Object

Style manual 13.1



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

def onlychild_clause_validate(root)
  root.xpath(Standoc::Utils::SUBCLAUSE_XPATH).each do |c|
    next unless c.xpath("../clause").size == 1

    title = c.at("./title")
    location = c["id"] || "#{c.text[0..60]}..."
    location += ":#{title.text}" if c["id"] && !title.nil?
    @log.add("Style", nil, "#{location}: subclause is only child")
  end
end

#org_abbrevObject



132
133
134
135
136
# File 'lib/metanorma/ieee/front.rb', line 132

def org_abbrev
  { "Institute of Electrical and Electronic Engineers" => "IEEE",
    "International Organization for Standardization" => "ISO",
    "International Electrotechnical Commission" => "IEC" }
end

#other_footnote_renumber1(fnote, idx, seen) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/metanorma/ieee/cleanup.rb', line 111

def other_footnote_renumber1(fnote, idx, seen)
  fnote["table"] and return [idx, seen]
  content = footnote_content(fnote)
  idx += 1
  if seen[content]
    fnote.children = "<p>See Footnote #{seen[content]}.</p>"
  else seen[content] = idx
  end
  fnote["reference"] = idx.to_s
  [idx, seen]
end

#outputs(node, ret) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/metanorma/ieee/converter.rb', line 52

def outputs(node, ret)
  File.open("#{@filename}.xml", "w:UTF-8") { |f| f.write(ret) }
  presentation_xml_converter(node).convert("#{@filename}.xml")
  html_converter(node).convert("#{@filename}.presentation.xml",
                               nil, false, "#{@filename}.html")
  doc_converter(node).convert("#{@filename}.presentation.xml",
                              nil, false, "#{@filename}.doc")
  node.attr("no-pdf") or
    pdf_converter(node)&.convert("#{@filename}.presentation.xml",
                                 nil, false, "#{@filename}.pdf")
end

#overview_children_cleanup(xml) ⇒ Object



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

def overview_children_cleanup(xml)
  %w(scope purpose word-usage).each do |x|
    (xml.xpath("//clause[@type = '#{x}']") -
      xml.xpath("//sections/clause[@type = 'overview']" \
                "//clause[@type = '#{x}']"))
      .each { |c| c.delete("type") }
  end
end

#overview_cleanup(xml) ⇒ Object



61
62
63
64
# File 'lib/metanorma/ieee/cleanup.rb', line 61

def overview_cleanup(xml)
  overview_once_cleanup(xml)
  overview_children_cleanup(xml)
end

#overview_once_cleanup(xml) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/metanorma/ieee/cleanup.rb', line 75

def overview_once_cleanup(xml)
  found = false
  xml.xpath("//sections//clause[@type = 'overview']").each do |c|
    found and c.delete("type")
    found = true if c.parent.name == "sections"
  end
  xml.xpath("//annex//clause[@type = 'overview'] | " \
            "//preface//clause[@type = 'overview']").each do |c|
    c.delete("type")
  end
end

#participants(xml) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'lib/metanorma/ieee/cleanup.rb', line 152

def participants(xml)
  @document_scheme == "ieee-sa-2021" or return
  PARTICIPANT_BOILERPLATE_LOCATIONS.each do |k, v|
    populate_participants(xml, k.to_s, v)
  end
  p = xml.at(".//p[@type = 'emeritus_sign']")
  ul = xml.at("//clause[@id = 'boilerplate-participants-sb']//ul")
  p && ul and ul.next = p
  xml.at("//sections//clause[@type = 'participants']")&.remove
end

#participants_dl_to_ul(clause) ⇒ Object



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

def participants_dl_to_ul(clause)
  clause.xpath(".//dl").each do |dl|
    dl.ancestors("dl, ul, ol").empty? or next
    dl.name = "ul"
    dl.xpath("./dt").each(&:remove)
    dl.xpath("./dd").each { |li| li.name = "li" }
  end
end

#pdf_converter(node) ⇒ Object



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

def pdf_converter(node)
  return nil if node.attr("no-pdf")

  IsoDoc::IEEE::PdfConvert.new(pdf_extract_attributes(node))
end

#populate_participants(xml, target, subtitle) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/metanorma/ieee/cleanup.rb', line 163

def populate_participants(xml, target, subtitle)
  t = xml.at("//clause[@id = '#{target}']/membership") or return
  s = xml.xpath("//clause[@type = 'participants']/clause").detect do |x|
    n = x.at("./title") and n.text.strip.downcase == subtitle
  end
  t.replace(populate_participants1(s || t))
end

#populate_participants1(clause) ⇒ Object



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

def populate_participants1(clause)
  participants_dl_to_ul(clause)
  clause.xpath(".//ul | .//ol").each do |ul|
    ul.name = "ul"
    ul.xpath("./li").each { |li| populate_participants2(li) }
    ul.xpath(".//p[normalize-space() = '']").each(&:remove)
  end
  clause.at("./title")&.remove
  clause.children.to_xml
end

#populate_participants2(list) ⇒ Object



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

def populate_participants2(list)
  curr = list
  p = curr.at("./p[text() != '']") and curr = p
  if dl = curr.at("./dl")
    ret = extract_participants(dl)
    dl.children = ret.keys.map do |k|
      "<dt>#{k}</dt><dd><p>#{ret[k]}</p></dd>"
    end.join
  else list.children = "<dl><dt>name</dt><dd><p>#{curr.children.to_xml}" \
                    "</p></dd><dt>role</dt><dd><p>member</p></dd></dl>"
  end
end

#preposition?(word) ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
154
155
156
157
158
159
# File 'lib/metanorma/ieee/validate_style.rb', line 151

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

#presentation_xml_converter(node) ⇒ Object



83
84
85
86
87
# File 'lib/metanorma/ieee/converter.rb', line 83

def presentation_xml_converter(node)
  IsoDoc::IEEE::PresentationXMLConvert
    .new(html_extract_attributes(node)
    .merge(output_formats: ::Metanorma::IEEE::Processor.new.output_formats))
end

#program(node, xml) ⇒ Object



148
149
150
# File 'lib/metanorma/ieee/front.rb', line 148

def program(node, xml)
  p = node.attr("program") and xml.program p
end

#provenance_title(xmldoc) ⇒ Object



225
226
227
228
229
230
231
232
233
# File 'lib/metanorma/ieee/cleanup.rb', line 225

def provenance_title(xmldoc)
  u = xmldoc.xpath("//bibdata/relation[@type = 'updates']")
  m = xmldoc.xpath("//bibdata/relation[@type = 'merges']")
  u.empty? and m.empty? and return
  ins = xmldoc.at("//bibdata/title")
  t = provenance_title1(u, m)
  ins.next = "<title type='provenance' language='en' " \
             "format='application/xml'>#{t}</title>"
end

#provenance_title1(updates, merges) ⇒ Object



235
236
237
238
239
240
241
242
243
# File 'lib/metanorma/ieee/cleanup.rb', line 235

def provenance_title1(updates, merges)
  ret = ""
  u = @isodoc.i18n.boolean_conj(tm_id_extract(updates), "and")
  m = @isodoc.i18n.boolean_conj(tm_id_extract(merges), "and")
  u.empty? or ret += "Revision of #{u}"
  !u.empty? && !m.empty? and ret += "<br/>"
  m.empty? or ret += "Incorporates #{m}"
  ret
end

#pubid_select(_params) ⇒ Object



103
104
105
# File 'lib/metanorma/ieee/front.rb', line 103

def pubid_select(_params)
  base_pubid
end

#quotesource_cleanup(xmldoc) ⇒ Object

end of citeas generation



71
72
73
74
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 71

def quotesource_cleanup(xmldoc)
  super
  trademark_ieee_erefs(xmldoc)
end

#relaton_relationsObject



138
139
140
# File 'lib/metanorma/ieee/front.rb', line 138

def relaton_relations
  super + %w(merges updates)
end

#sdo_available_note(xmldoc, note, publisher) ⇒ Object



186
187
188
189
190
191
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 186

def sdo_available_note(xmldoc, note, publisher)
  ret = xmldoc.xpath(BIBITEM_NO_AVAIL).detect do |b|
    bib_pubs(b).include?(publisher)
  end
  insert_availability_note(ret, note)
end

#section_names_refs_cleanup(xml) ⇒ Object



128
129
130
131
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 128

def section_names_refs_cleanup(xml)
  @doctype == "whitepaper" or return super
  replace_title(xml, "//bibliography/references", @i18n&.references, true)
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_cleanup(xml) ⇒ Object



56
57
58
59
# File 'lib/metanorma/ieee/cleanup.rb', line 56

def sections_cleanup(xml)
  super
  overview_cleanup(xml)
end

#sections_presence_validate(root) ⇒ Object



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

def sections_presence_validate(root)
  root.at("//sections/clause[@type = 'overview']") or
    @log.add("Style", nil, "Overview clause missing")
  root.at("//sections/clause[@type = 'overview']/clause[@type = 'scope']") or
    @log.add("Style", nil, "Scope subclause missing")
  root.at("//sections/clause[@type = 'overview']/clause[@type = 'word-usage']") or
    @log.add("Style", nil, "Word Usage subclause missing")
  root.at("//references[@normative = 'true']") or
    @log.add("Style", nil, "Normative references missing")
  root.at("//terms") or
    @log.add("Style", nil, "Definitions missing")
end

#sections_sequence_validate(root) ⇒ Object



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

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



74
75
76
# File 'lib/metanorma/ieee/validate_section.rb', line 74

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

#sections_sequence_validate_end(names, elem) ⇒ Object



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

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

#sections_sequence_validate_start(root) ⇒ Object



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

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

#sectiontype_streamline(ret) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/metanorma/ieee/converter.rb', line 26

def sectiontype_streamline(ret)
  case ret
  when "definitions", "definitions, acronyms and abbreviations"
    "terms and definitions"
  when "acronyms and abbreviations" then "symbols and abbreviated terms"
  else super
  end
end

#select_docid(ref, type = nil) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 120

def select_docid(ref, type = nil)
  ret = super
  if %w(standard techreport).include?(ref["type"]) then ret
  else
    ref.at("./docidentifier[@type = 'metanorma-ordinal']") || ret
  end
end

#seqcheck(names, msg, accepted) ⇒ Object



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

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("Style", nil, msg)
  end
  names
end

#sort_biblio(bib) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 22

def sort_biblio(bib)
  @i = IsoDoc::IEEE::PresentationXMLConvert
    .new({ lang: @lang, script: @script, locale: @locale })
  @i.i18n_init(@lang, @script, @locale)
  bib.sort do |a, b|
    sort_biblio_key(a) <=> sort_biblio_key(b)
  end
end

#sort_biblio_key(bib) ⇒ Object

Alphabetic by rendering: author surname or designation, followed by title



32
33
34
35
36
37
38
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 32

def sort_biblio_key(bib)
  name = designator_or_name(bib)
  title = bib.at("./title[@type = 'main']")&.text ||
    bib.at("./title")&.text || bib.at("./formattedref")&.text
  title.gsub!(/[[:punct:]]/, "")
  @c.decode("#{name} #{title}").strip.downcase
end

#stage_validate(xmldoc) ⇒ Object



41
42
43
44
45
46
# File 'lib/metanorma/ieee/validate.rb', line 41

def stage_validate(xmldoc)
  stage = xmldoc&.at("//bibdata/status/stage")&.text
  %w(draft approved superseded withdrawn).include? stage or
    @log.add("Document Attributes", nil,
             "#{stage} is not a recognised stage")
end

#strict_capitalize_phrase(str) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/metanorma/ieee/validate_style.rb', line 129

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

#structured_id(node, xml) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/metanorma/ieee/front.rb', line 152

def structured_id(node, xml)
  node.attr("docnumber") or return
  xml.structuredidentifier do |i|
    i.docnumber node.attr("docnumber")
    i.agency "IEEE"
    i.class_ doctype(node)
    a = node.attr("edition") and i.edition a
    a = node.attr("draft") and i.version a
    a = node.attr("amendment-number") and i.amendment a
    a = node.attr("corrigendum-number") and i.corrigendum a
    a = node.attr("copyright-year") and i.year a
  end
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("Style", node, 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



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

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



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

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("Style", i,
                 "Image name #{base} is expected to be #{num}")
    end
  end
end

#table_figure_quantity_validate(xmldoc) ⇒ Object



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

def table_figure_quantity_validate(xmldoc)
  xmldoc.xpath("//td[.//image] | //th[.//image]").each do |d|
    d.xpath(".//image").size > 1 and
      @log.add("Style", d,
               "More than one image in the table cell")
  end
end

#table_footnote_renumber1(fnote, idx, seen) ⇒ Object



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

def table_footnote_renumber1(fnote, idx, seen)
  content = footnote_content(fnote)
  idx += 1
  if seen[content]
    fnote.children = "<p>See Footnote #{seen[content]}.</p>"
  else seen[content] = idx
  end
  fnote["reference"] = (idx - 1 + "a".ord).chr
  fnote["table"] = true
  [idx, seen]
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
98
99
# 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("Style", table,
                 "#{x} is a 4-digit number in a table column with " \
                 "numbers broken up in threes")
    end
  end
end

#tablefigurenumber(table, figure, xrefs) ⇒ Object



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

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

#term_defs_boilerplate_cont(src, term, isodoc) ⇒ Object



130
# File 'lib/metanorma/ieee/cleanup.rb', line 130

def term_defs_boilerplate_cont(src, term, isodoc); end

#termdef_boilerplate_insert(xmldoc, isodoc, once = false) ⇒ Object



125
126
127
128
# File 'lib/metanorma/ieee/cleanup.rb', line 125

def termdef_boilerplate_insert(xmldoc, isodoc, once = false)
  once = true
  super
end

#termlookup_cleanup(xmldoc) ⇒ Object



132
133
134
# File 'lib/metanorma/ieee/cleanup.rb', line 132

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

#termsource_attrs(node, matched) ⇒ Object



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

def termsource_attrs(node, matched)
  ret = super
  node.option? "adapted" and ret[:status] = "adapted"
  ret
end

#text_from_paras(node) ⇒ Object



215
216
217
218
# File 'lib/metanorma/ieee/cleanup.rb', line 215

def text_from_paras(node)
  r = node.at("./p") and node = r
  node.children.to_xml.strip
end

#title_validate(xml) ⇒ Object



111
112
113
114
# File 'lib/metanorma/ieee/validate_style.rb', line 111

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

#title_validate_capitalisation(xml) ⇒ Object

Style Manual 11.3



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

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("Style", title,
                     "Title contains uncapitalised word other than preposition")
end

#title_validate_type(xml) ⇒ Object

Style Manual 11.3



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

def title_validate_type(xml)
  title = xml.at("//bibdata/title") or return
  draft = xml.at("//bibdata//draft")
  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("Style", title,
             "Expected title to start as: #{target}")
end

#tm_id_extract(relations) ⇒ Object



245
246
247
248
249
250
251
# File 'lib/metanorma/ieee/cleanup.rb', line 245

def tm_id_extract(relations)
  relations.map do |u|
    u.at("./bibitem/docidentifier[@scope = 'trademark']") ||
      u.at("./bibitem/docidentifier[@primary = 'true']") ||
      u.at("./bibitem/docidentifier")
  end.map(&:text)
end

#trademark_ieee_erefs(xmldoc) ⇒ Object

Style manual 12.3.5



79
80
81
82
83
84
85
86
87
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 79

def trademark_ieee_erefs(xmldoc)
  ieee = xmldoc.xpath("//references/bibitem")
    .each_with_object({}) do |b, m|
    bib_pubs(b).include?(IEEE) and
      m[b["id"]] = b.at("./docidentifier[@scope = 'trademark']")&.text
  end
  trademark_ieee_erefs1(xmldoc, "//preface//eref", ieee)
  trademark_ieee_erefs1(xmldoc, "//sections//eref | //annex//eref", ieee)
end

#trademark_ieee_erefs1(xmldoc, path, ieee) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 89

def trademark_ieee_erefs1(xmldoc, path, ieee)
  xmldoc.xpath(path).each_with_object({}) do |e, m|
    ieee[e["bibitemid"]] or next
    m[e["bibitemid"]] or e["citeas"] = ieee[e["bibitemid"]]
    m[e["bibitemid"]] = true
  end
end

#validate(doc) ⇒ Object



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

def validate(doc)
  content_validate(doc)
  schema_validate(formattedstr_strip(doc.dup),
                  File.join(File.dirname(__FILE__), "ieee.rng"))
end

#withdrawn_note(xmldoc, provenance_notes) ⇒ Object



153
154
155
156
157
158
159
160
161
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 153

def withdrawn_note(xmldoc, provenance_notes)
  xmldoc.xpath(BIBITEM_NO_AVAIL).each do |b|
    bib_pubs(b).include?(IEEE) or next
    b.at("./status/stage")&.text == "withdrawn" or next
    docid = b.at("./docidentifier[@type = 'IEEE'][not(@scope)]")
    note = provenance_notes["ieee-withdrawn"].sub("%", docid.text)
    insert_availability_note(b, note)
  end
end

#word_usage(xml) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/metanorma/ieee/cleanup.rb', line 40

def word_usage(xml)
  @document_scheme == "ieee-sa-2021" or return
  n = xml.at("//boilerplate//clause[@id = 'boilerplate_word_usage']")
    &.remove
  s = xml.at("//clause[@type = 'overview']")
  (n && s) or return
  s << n
end

#xrefs(xmldoc) ⇒ Object



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

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