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/cleanup_ref_fn.rb,
lib/metanorma/ieee/validate_style.rb,
lib/metanorma/ieee/validate_section.rb,
lib/metanorma/ieee/cleanup_boilerplate.rb

Constant Summary collapse

TERM_CLAUSE =
"//sections//terms".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
PREFACE_CLAUSE_NAMES =
%w(abstract foreword introduction acknowledgements participants
metanorma-extension misc-container).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[@normative='true']/bibitem[not(note[@type = 'Availability'])] | "\
"//references[@normative='false']/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 =
"//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)] | "\
"//clause[descendant::references][not(parent::clause)]".freeze
PARTICIPANT_BOILERPLATE_LOCATIONS =
{ "boilerplate-participants-wg": "working group",
"boilerplate-participants-bg": "balloting group",
"boilerplate-participants-sb": "standards board",
"boilerplate-participants-blank": nil }.freeze

Instance Method Summary collapse

Instance Method Details

#amend_validate(xmldoc) ⇒ Object

Style manual 20.2.2



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

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



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

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



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

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



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

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

#base_pubidObject



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

def base_pubid
  Pubid::Ieee::Identifier
end

#bib_pubs(bib) ⇒ Object



156
157
158
159
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 156

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

#bibdata_cleanup(xmldoc) ⇒ Object



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

def bibdata_cleanup(xmldoc)
  super
  draft_id(xmldoc)
  prefixed_title(xmldoc)
  provenance_title(xmldoc)
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_cleanup(xmldoc) ⇒ Object



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

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



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

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



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

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



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

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



151
152
153
154
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 151

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

#bibliography_validate(root) ⇒ Object

Style manual 19.1



108
109
110
111
112
113
114
115
116
117
118
# 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("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



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

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



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

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

#clause_attrs_preprocess(attrs, node) ⇒ Object



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

def clause_attrs_preprocess(attrs, node)
  case node.title.downcase
  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

#committee_contrib_org_prep(node, type, agency, _opts) ⇒ Object



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

def committee_contrib_org_prep(node, type, agency, _opts)
  super.merge(role: "authorizer")
end

#committee_contributors(node, xml, agency, opt) ⇒ Object



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

def committee_contributors(node, xml, agency, opt)
  (node) or return
  super
end

#compact_blank(hash) ⇒ Object



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

def compact_blank(hash)
  hash.compact.reject { |_, v| v.is_a?(String) && v.empty? }
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

#contributors_committees_nest1(committees) ⇒ Object



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

def contributors_committees_nest1(committees)
  committees.empty? and return committees
  committees = committees.map(&:reverse).reverse.flatten
  committees.each_with_index do |m, i|
    i.zero? and next
    m[:subdiv] = committees[i - 1]
  end
  committees[-1].nil? and return []
  [committees[-1]]
end

#datetypesObject



155
156
157
# File 'lib/metanorma/ieee/front.rb', line 155

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

#default_publisherObject



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

def default_publisher
  "IEEE"
end

#designator_docid(bib) ⇒ Object



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

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



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

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

#doc_converter(node) ⇒ Object



97
98
99
# File 'lib/metanorma/ieee/converter.rb', line 97

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

#doc_extract_attributes(node) ⇒ Object



75
76
77
78
79
# File 'lib/metanorma/ieee/converter.rb', line 75

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

#doctype_validate(xmldoc) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# 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("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

#draft_id(xmldoc) ⇒ Object

IEEE Draft Std 10000-2025/D1.2 => P10000/D1.2 TODO: this needs to go to pubid-ieee



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

def draft_id(xmldoc)
  published?(xmldoc.at("//bibdata/status/stage")&.text, xmldoc) and return
  id = xmldoc.at("//bibdata/docidentifier[@type = 'IEEE']") or return
  id.text.start_with?("IEEE Draft Std ") or return
  n = id.text.sub(/^IEEE Draft Std /, "P").sub(/(\d)-(\d\d\d\d)/, "\\1")
  id.next = %(<docidentifier type="IEEE-draft">#{n}</docidentifier>)
end

#emeritus_sign(xml) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/metanorma/ieee/cleanup_boilerplate.rb', line 59

def emeritus_sign(xml)
  p = xml.at(".//p[@type = 'emeritus_sign']") or return
  ul = xml.at("//clause[@anchor = 'boilerplate-participants-sb']//ul") or
    return
  has_asterisk = ul.xpath(".//p")&.any? do |li|
    li.text.strip.end_with?("*")
  end
  if has_asterisk then ul.next = p
  else p.remove
  end
end

#extract_participants(dlist) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

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

#html_extract_attributes(node) ⇒ Object



69
70
71
72
73
# File 'lib/metanorma/ieee/converter.rb', line 69

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

#ieee_draft_numbers(node) ⇒ Object



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

def ieee_draft_numbers(node)
  draft = node.attr("draft") or return nil
  d = draft.split(".")
  { version: d[0], revision: d[1] }.compact
end

#ieee_id(node, xml) ⇒ Object



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

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



130
131
132
133
# File 'lib/metanorma/ieee/front.rb', line 130

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



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

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



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

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



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/metanorma/ieee/front.rb', line 89

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),
          draft: ieee_draft_numbers(node),
          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



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

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



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

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



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

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

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

#ietf_available_note(bibitems, note) ⇒ Object



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

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

#image_name_prefix(xmldoc) ⇒ Object



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

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



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

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

#initial_boilerplate(xml, isodoc) ⇒ Object



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

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



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

def initial_note(xml)
  n = xml.at("//boilerplate//note[@anchor = '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



96
97
98
99
100
101
102
103
104
# File 'lib/metanorma/ieee/cleanup_ref_fn.rb', line 96

def insert_availability_note(bib, msg)
  bib or return
  Array(msg).each do |msg1|
    note = %(<note type="Availability"><p>#{msg1}</p></note>)
    if b = insert_availability_note_ins(bib)
      b.next = note
    end
  end
end

#insert_availability_note_ins(bib) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/metanorma/ieee/cleanup_ref_fn.rb', line 106

def insert_availability_note_ins(bib)
  if b = bib.at("./language | ./script | ./abstract | ./status")
    b.previous
  else bib.at("./contributor") || bib.at("./date") ||
    bib.at("./docnumber") || bib.at("./docidentifier") ||
    bib.at("./title")
  end
end

#intro_boilerplate(xml, isodoc) ⇒ Object



20
21
22
23
24
25
# File 'lib/metanorma/ieee/cleanup_boilerplate.rb', line 20

def intro_boilerplate(xml, isodoc)
  intro = xml.at("//introduction/title") or return
  adm = isodoc.populate_template(@i18n.introduction_disclaimer)
  intro.next = "<admonition>#{adm}</admonition>"
  add_id(intro.next)
end

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



60
61
62
63
64
65
66
67
68
69
# File 'lib/metanorma/ieee/cleanup_ref_fn.rb', line 60

def iso_iec_available_note(bibitems, note, iso, iec)
  ret = bibitems.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(bibitems, note, itu_t) ⇒ Object



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

def itu_available_note(bibitems, note, itu_t)
  ret = bibitems.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



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

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

#listcount_validate(doc) ⇒ Object

Style manual 13.3



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

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



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

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



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

def locality_erefs_validate(root)
  root.xpath("//eref[descendant::locality]").each do |t|
    if !/[:-](\d+{4})($|-\d\d)/.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



52
53
54
55
56
57
58
# 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("Style", e, "Cross-reference contains range, " \
                           "should be separate cross-references")
  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

#metadata_committee_prep(node) ⇒ Object



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

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_committee_types(_node) ⇒ Object



19
20
21
# File 'lib/metanorma/ieee/front.rb', line 19

def (_node)
  %w(society balloting-group working-group committee)
end

#metadata_ext(node, xml) ⇒ Object



173
174
175
176
177
# File 'lib/metanorma/ieee/front.rb', line 173

def (node, xml)
  super
  s = node.attr("trial-use") and xml.trial_use s
  program(node, xml)
end

#metadata_id(node, xml) ⇒ Object



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

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



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

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



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

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

#metadata_subdoctype(node, xml) ⇒ Object



159
160
161
# File 'lib/metanorma/ieee/front.rb', line 159

def (node, xml)
  xml.subdoctype (node.attr("docsubtype") || "document")
end

#nist_available_note(bibitems, note, fips) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/metanorma/ieee/cleanup_ref_fn.rb', line 87

def nist_available_note(bibitems, note, fips)
  ret = bibitems.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



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

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('./@anchor')&.text} is not dated.")
  end
end

#normref_cleanup(xmldoc) ⇒ Object



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

def normref_cleanup(xmldoc)
  super
  normref_no_ordinals(xmldoc)
  normref_reorder(xmldoc)
end

#normref_no_ordinals(xmldoc) ⇒ Object



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

def normref_no_ordinals(xmldoc)
  xmldoc.xpath("//references[@normative = 'true']/bibitem/" \
              "docidentifier[@type = 'metanorma']").each do |d|
    /^\[?\d+\]?$/.match?(d.text) and d.remove
  end
end

#normref_reorder(xmldoc) ⇒ Object



75
76
77
78
79
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 75

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

#note_cleanup(xmldoc) ⇒ Object



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

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

#obligations_cleanup_norm(xml) ⇒ Object



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

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
# 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("Style", c, "subclause is only child")
  end
end

#org_abbrevObject



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

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

#org_attrs_add_committees(node, ret, opts, opts_orig) ⇒ Object



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

def org_attrs_add_committees(node, ret, opts, opts_orig)
  opts_orig[:groups]&.each_with_index do |g, i|
    i.zero? and next
    opts = committee_contrib_org_prep(node, g, nil, opts_orig)
    ret << org_attrs_parse_core(node, opts).map do |x|
      x.merge(subdivtype: opts[:subdivtype])
    end
  end
  contributors_committees_nest1(ret)
end

#other_footnote_renumber1(fnote, idx, seen) ⇒ Object



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

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



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

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



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

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



21
22
23
24
# File 'lib/metanorma/ieee/cleanup.rb', line 21

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

#overview_once_cleanup(xml) ⇒ Object



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

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



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

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
  emeritus_sign(xml)
  xml.at("//sections//clause[@type = 'participants']")&.remove
end

#participants_dl_to_ul(clause) ⇒ Object



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

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



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

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



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

def populate_participants(xml, target, subtitle)
  t = xml.at("//clause[@anchor = '#{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



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

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/metanorma/ieee/cleanup_boilerplate.rb', line 99

def populate_participants2(list)
  add_id(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 #{add_id_text}><p>#{ret[k]}</p></dd>"
    end.join
  else list.children = "    <dl><dt>name</dt><dd \#{add_id_text}><p>\#{curr.children.to_xml}\n    </p></dd><dt>role</dt><dd \#{add_id_text}><p>member</p></dd></dl>\n  XML\n  end\nend\n"

#prefixed_title(xmldoc) ⇒ Object



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

def prefixed_title(xmldoc)
  t, stage, trial = prefixed_title_prep(xmldoc)
  %w(main title-abbrev).reverse_each do |type|
    xmldoc.at("//bibdata/title[@type = '#{type}']") and next
    p = prefixed_title1(stage, trial, type)
    t.previous = "      <title type='\#{type}' language='en'>\#{p}\#{to_xml(t.children)}</title>\n    XML\n  end\nend\n"

#prefixed_title1(stage, trial, type) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/metanorma/ieee/cleanup.rb', line 131

def prefixed_title1(stage, trial, type)
  m = []
  m << (stage == "draft" ? "Draft" : "IEEE")
  trial and m << "Trial-Use"
  doctype = @doctype.split(/[- ]/).map(&:capitalize).join(" ")
  type == "title-abbrev" && a = @i18n.get["doctype_abbrev"][@doctype] and
    doctype = a
  m << doctype
  m << "for"
  "#{m.join(' ')} "
end

#prefixed_title_prep(xmldoc) ⇒ Object



143
144
145
146
147
148
# File 'lib/metanorma/ieee/cleanup.rb', line 143

def prefixed_title_prep(xmldoc)
  t = xmldoc.at("//bibdata/title[@type = 'title-main']")
  stage = xmldoc.at("//status/stage")&.text
  trial = xmldoc.at("//bibdata/ext/trial-use[text() = 'true']")
  [t, stage, trial]
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



81
82
83
84
85
# File 'lib/metanorma/ieee/converter.rb', line 81

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



179
180
181
# File 'lib/metanorma/ieee/front.rb', line 179

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

#provenance_title(xmldoc) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/metanorma/ieee/cleanup.rb', line 150

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[@type = 'title-main']")
  t = provenance_title1(u, m)
  ins.next = "<title type='provenance' language='en'>#{t}</title>"
end

#provenance_title1(updates, merges) ⇒ Object



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

def provenance_title1(updates, merges)
  ret = ""
  u = @isodoc.i18n.boolean_conj(tm_id_extract(updates), "and")
    .gsub(%r{</?(conn|comma|enum-comma)>}, "")
  m = @isodoc.i18n.boolean_conj(tm_id_extract(merges), "and")
    .gsub(%r{</?(conn|comma|enum-comma)>}, "")
  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



135
136
137
# File 'lib/metanorma/ieee/front.rb', line 135

def pubid_select(_params)
  base_pubid
end

#published?(stage, _xmldoc) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/metanorma/ieee/cleanup.rb', line 179

def published?(stage, _xmldoc)
  %w(approved superseded withdrawn).include?(stage&.downcase)
end

#quotesource_cleanup(xmldoc) ⇒ Object

end of citeas generation



82
83
84
85
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 82

def quotesource_cleanup(xmldoc)
  super
  trademark_ieee_erefs(xmldoc)
end

#relaton_relationsObject



169
170
171
# File 'lib/metanorma/ieee/front.rb', line 169

def relaton_relations
  super + %w(merges updates)
end

#schema_fileObject



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

def schema_file
  "ieee.rng"
end

#sdo_available_note(bibitems, note, publisher) ⇒ Object



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

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

#section_names_refs_cleanup(xml) ⇒ Object



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

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



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

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



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

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



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

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
30
# 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)
  sort_keys = bib.each_with_object({}) do |b, m|
    m[b["id"]] = sort_biblio_key(b)
  end
  bib.sort { |a, b| sort_keys[a["id"]] <=> sort_keys[b["id"]] }
end

#sort_biblio_key(bib) ⇒ Object

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



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

def sort_biblio_key(bib)
  name, docid = 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} #{docid}").strip.downcase
end

#sorted_bibitem_no_avail(xmldoc) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/metanorma/ieee/cleanup_ref_fn.rb', line 8

def sorted_bibitem_no_avail(xmldoc)
  # Get normative references first, maintaining their order
  normative_bibitems = xmldoc.xpath("//references[@normative='true']/bibitem[not(note[@type = 'Availability'])]")
  
  # Get non-normative references second, maintaining their order  
  non_normative_bibitems = xmldoc.xpath("//references[@normative='false']/bibitem[not(note[@type = 'Availability'])]")
  
  # Return concatenated array with normative first
  normative_bibitems.to_a + non_normative_bibitems.to_a
end

#stage_validate(xmldoc) ⇒ Object



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

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



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/metanorma/ieee/front.rb', line 183

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) 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



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

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



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

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



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

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



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

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



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

def term_defs_boilerplate_cont(src, term, isodoc); end

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



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

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

#termlookup_cleanup(xmldoc) ⇒ Object



93
94
95
# File 'lib/metanorma/ieee/cleanup.rb', line 93

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

#termsource_attrs(node, matched) ⇒ Object



44
45
46
47
48
# File 'lib/metanorma/ieee/converter.rb', line 44

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

#text_from_paras(node) ⇒ Object



108
109
110
111
# File 'lib/metanorma/ieee/cleanup.rb', line 108

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

#title_english(node, xml) ⇒ Object



197
198
199
200
201
202
203
# File 'lib/metanorma/ieee/front.rb', line 197

def title_english(node, xml)
  title = node.attr("title") || node.attr("title-en") ||
    node.attr("doctitle")
  title_english1(title, "title-main", xml)
  title_english1(node.attr("title-full"), "main", xml)
  title_english1(node.attr("title-abbrev"), "title-abbrev", xml)
end

#title_english1(title, type, xml) ⇒ Object



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

def title_english1(title, type, xml)
  title.nil? and return
  at = { language: "en", format: "text/plain" }
  title = Metanorma::Utils::asciidoc_sub(title)
  xml.title **attr_code(at.merge(type: type)) do |t|
    t << title
  end
end

#title_otherlangs(node, xml) ⇒ Object



214
# File 'lib/metanorma/ieee/front.rb', line 214

def title_otherlangs(node, xml); 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



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

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



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

def trademark_ieee_erefs(xmldoc)
  ieee = xmldoc.xpath("//references/bibitem")
    .each_with_object({}) do |b, m|
    bib_pubs(b).include?(IEEE) and
      m[b["anchor"]] = 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



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

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

#withdrawn_note(xmldoc, provenance_notes) ⇒ Object



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

def withdrawn_note(xmldoc, provenance_notes)
  sorted_bibitem_no_avail(xmldoc).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



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

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

#xrefs(xmldoc) ⇒ Object



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

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