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

Constant Summary collapse

BIBLIO =
"//bibliography/references[@normative = 'false'][not(@hidden)] | "\
"//bibliography/clause[.//references[@normative = 'false']] | "\
"//annex//references[@normative = 'false'][not(@hidden)]".freeze
OTHERIDS =
"@type = 'DOI' or @type = 'metanorma' or @type = 'ISSN' or "\
"@type = 'ISBN'".freeze
XML_ROOT_TAG =
"ieee-standard".freeze
XML_NAMESPACE =
"https://www.metanorma.org/ns/ieee".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

#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

#bg_members(node, xml) ⇒ Object



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

def bg_members(node, xml)
  a = node.attr("balloting_group_members") and
    (a, "Balloting Group Member", xml)
end

#bibdata_validate(doc) ⇒ Object



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

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

#bibitem_validate(root) ⇒ Object



100
101
102
# File 'lib/metanorma/ieee/validate.rb', line 100

def bibitem_validate(root)
  normative_dated_refs(root)
end

#biblio_preface(ref) ⇒ Object



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

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

#boilerplate_cleanup(xmldoc) ⇒ Object



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

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

#clause_parse(attrs, xml, node) ⇒ Object



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

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"
  end
  super
end

#content_validate(doc) ⇒ Object



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

def content_validate(doc)
  super
  bibdata_validate(doc.root)
  title_validate(doc.root)
  locality_erefs_validate(doc.root)
  bibitem_validate(doc.root)
  listcount_validate(doc)
  table_style(doc)
  figure_validate(doc)
end

#doc_converter(node) ⇒ Object



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

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

#doc_extract_attributes(node) ⇒ Object



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

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

#doctype_validate(xmldoc) ⇒ Object



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

def doctype_validate(xmldoc)
  doctype = xmldoc&.at("//bibdata/ext/doctype")&.text
  %w(standard recommended-practice guide
     amendment technical-corrigendum).include? doctype or
    @log.add("Document Attributes", nil,
             "#{doctype} is not a recognised document type")
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_validate(xmldoc, xrefs) ⇒ Object



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

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") or next
      next if i["src"].start_with?("data:")

      num = xrefs.anchor(f["id"], :label)
      File.basename(i["src"], ".*") == "#{pref}_fig#{num}" or
        @log.add("Style", i,
                 "image name #{i['src']} is expected to be #{pref}_fig#{num}")
    end
end

#figure_validate(xmldoc) ⇒ Object

Style manual 17.1



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

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

#html_converter(node) ⇒ Object



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

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

#html_extract_attributes(node) ⇒ Object



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

def html_extract_attributes(node)
  super.merge(hierarchical_assets:
              node.attr("hierarchical-object-numbering"))
end

#image_name_prefix(xmldoc) ⇒ Object



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

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
# File 'lib/metanorma/ieee/converter.rb', line 17

def init(node)
  super
  @hierarchical_assets = node.attr("hierarchical-object-numbering")
end

#initial_boilerplate(xml, isodoc) ⇒ Object



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

def initial_boilerplate(xml, isodoc)
  intro_boilerplate(xml, isodoc)
  super
end

#intro_boilerplate(xml, isodoc) ⇒ Object



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

def intro_boilerplate(xml, isodoc)
  return unless intro = xml.at("//introduction/title")

  template = <<~ADM
    This introduction is not part of P{{ docnumeric }}{% if draft %}/D{{ draft }}{% endif %}, {{ doctitle }}
  ADM
  adm = isodoc.populate_template(template)
  intro.next = "<admonition>#{adm}</admonition>"
end

#listcount_validate(doc) ⇒ Object

Style manual 13.3



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

def listcount_validate(doc)
  doc.xpath("//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

#locality_erefs_validate(root) ⇒ Object

Style manual 12.3.2



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

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

#metadata_author(node, xml) ⇒ Object



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

def (node, xml)
  super
  wg_members(node, xml)
  bg_members(node, xml)
  std_board_members(node, xml)
end

#metadata_committee(node, xml) ⇒ Object



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

def (node, xml)
  return unless node.attr("committee") || node.attr("society")

  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


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

def (node, xml)
  publishers = node.attr("copyright-holder") || node.attr("publisher") ||
    "IEEE"
  csv_split(publishers).each do |p|
    xml.copyright do |c|
      c.from (node.attr("copyright-year") || Date.today.year)
      c.owner do |owner|
        owner.organization do |o|
          organization(
            o, p, true, node,
            !(node.attr("copyright-holder") || node.attr("publisher"))
          )
        end
      end
    end
  end
end

#metadata_editor(name, role, xml) ⇒ Object



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

def (name, role, xml)
  xml.contributor do |c|
    c.role role, **{ type: "editor" }
    c.person do |p|
      p.name do |n|
        n.completename name
      end
    end
  end
end

#metadata_id(node, xml) ⇒ Object



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

def (node, xml)
  id = node.attr("docnumber") || ""
  xml.docidentifier id, type: "IEEE"
  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"
  xml.docnumber node.attr("docnumber")
end

#metadata_multi_editors(names, role, xml) ⇒ Object



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

def (names, role, xml)
  csv_split(names).each do |n|
    (n, role, xml)
  end
end

#metadata_other_id(node, xml) ⇒ Object



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

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"
end

#metadata_publisher(node, xml) ⇒ Object



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

def (node, xml)
  publishers = node.attr("publisher") || "IEEE"
  csv_split(publishers).each do |p|
    xml.contributor do |c|
      c.role **{ type: "publisher" }
      c.organization do |a|
        organization(a, p, true, node, !node.attr("publisher"))
      end
    end
  end
end

#normative_dated_refs(root) ⇒ Object

Style manual 12.3.1



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

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



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

def normref_cleanup(xmldoc)
  super
  normref_reorder(xmldoc)
end

#normref_reorder(xmldoc) ⇒ Object



94
95
96
97
98
# File 'lib/metanorma/ieee/cleanup.rb', line 94

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

#note_cleanup(xmldoc) ⇒ Object



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

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><p>#{@i18n.note_inform_fn}</p></fn>"
end

#obligations_cleanup_norm(xml) ⇒ Object



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

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



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

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



115
116
117
# File 'lib/metanorma/ieee/front.rb', line 115

def org_abbrev
  { "Institute of Electrical and Electronic Engineers" => "IEEE" }
end

#outputs(node, ret) ⇒ Object



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

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_cleanup(xml) ⇒ Object



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

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

#pdf_converter(node) ⇒ Object



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

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

#preposition?(word) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

def presentation_xml_converter(node)
  IsoDoc::IEEE::PresentationXMLConvert.new(html_extract_attributes(node))
end

#quotesource_cleanup(xmldoc) ⇒ Object

end of citeas generation



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

def quotesource_cleanup(xmldoc)
  super
  trademark_ieee_erefs(xmldoc)
end

#section_validate(doc) ⇒ Object



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

def section_validate(doc)
  doctype = doc&.at("//bibdata/ext/doctype")&.text
  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



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

def sections_cleanup(xml)
  super
  overview_cleanup(xml)
end

#sections_presence_validate(root) ⇒ Object



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

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



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

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)
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
90
91
92
93
94
95
96
# 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")
  elem = names&.shift
  elem&.at("./self::references[@normative = 'false']") ||
    @log.add("Style", elem,
             "Final section must be (references) Bibliography")
  names.empty? ||
    @log.add("Style", elem,
             "There are sections after the final Bibliography")
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



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

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

#seqcheck(names, msg, accepted) ⇒ Object



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

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



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

def sort_biblio(bib)
  bib.sort do |a, b|
    sort_biblio_key(a) <=> sort_biblio_key(b)
  end
end

#sort_biblio_key(bib) ⇒ Object



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

def sort_biblio_key(bib)
  id = bib.at("./docidentifier[not(#{OTHERIDS})]")
  title = bib.at("./title[@type = 'main']") ||
    bib.at("./title") || bib.at("./formattedref")
  "#{id&.text || 'ZZZZZ'} :: #{title&.text}"
end

#sort_terms(terms) ⇒ Object



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

def sort_terms(terms)
  terms.sort do |a, b|
    sort_terms_key(a) <=> sort_terms_key(b)
  end
end

#sort_terms_key(term) ⇒ Object



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

def sort_terms_key(term)
  d = term.at("./preferred/expression/name | "\
              "./preferred/letter-designation/name | "\
              "./preferred/graphical-symbol/figure/name | "\
              "./preferred/graphical-symbol/figure/@id")
  d.text.downcase
end

#std_board_members(node, xml) ⇒ Object



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

def std_board_members(node, xml)
  a = node.attr("std_board_chair") and
    (a, "Standards Board Chair", xml)
  a = node.attr("std_board_vicechair") and
    (a, "Standards Board Vice-Chair", xml)
  a = node.attr("std_board_pastchair") and
    (a, "Standards Board Past Chair", xml)
  a = node.attr("std_board_secretary") and
    (a, "Standards Board Secretary", xml)
  a = node.attr("balloting_group_members") and
    (a, "Standards Board Member", xml)
end

#strict_capitalize_phrase(str) ⇒ Object



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

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

#style(node, text) ⇒ Object



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

def style(node, text)
  return if @novalid

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

#style_number(node, text) ⇒ Object

Style manual 14.2



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

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

#style_percent(node, text) ⇒ Object

Style manual 14.2



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

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

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



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

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

#style_units(node, text) ⇒ Object

Style manual 14.2



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

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

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



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

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

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

#subclause_validate(root) ⇒ Object

Style manual 13.1



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

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



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

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



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

def table_figure_name_validate(xmldoc, xrefs)
  xmldoc.xpath("//table[.//figure]").each do |t|
    xmldoc.xpath(".//figure").each do |f|
      i = f.at("./image") or next
      next if i["src"].start_with?("data:")

      num = tablefigurenumber(t, f, xrefs)
      File.basename(i["src"]) == num or
        @log.add("Style", i,
                 "image name #{i['src']} is expected to be #{num}")
    end
  end
end

#table_figure_quantity_validate(xmldoc) ⇒ Object



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

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_style(docxml) ⇒ Object

Style manual 16.3.2



72
73
74
75
76
77
78
# 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) }
end

#table_style_columns(table) ⇒ Object

deliberately doing naive, ignoring rowspan



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/metanorma/ieee/validate_style.rb', line 81

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

    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



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

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_reorder(xmldoc) ⇒ Object



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

def term_reorder(xmldoc)
  xmldoc.xpath("//terms").each do |t|
    term_reorder1(t)
  end
end

#term_reorder1(terms) ⇒ Object



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

def term_reorder1(terms)
  ins = terms.at("./term")&.previous_element or return
  coll = terms.xpath("./term")
  ret = sort_terms(coll)
  coll.each(&:remove)
  ret.reverse.each { |t| ins.next = t }
end

#termdef_cleanup(xmldoc) ⇒ Object



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

def termdef_cleanup(xmldoc)
  term_reorder(xmldoc)
  super
end

#title_validate(xml) ⇒ Object



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

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

#title_validate_capitalisation(xml) ⇒ Object

Style Manual 11.3



68
69
70
71
72
73
74
75
76
77
# File 'lib/metanorma/ieee/validate.rb', line 68

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



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/metanorma/ieee/validate.rb', line 44

def title_validate_type(xml)
  title = xml.at("//bibdata/title") or return
  draft = xml.at("//bibdata//draft")
  type = xml.at("//bibdata/ext/doctype")
  subtype = xml.at("//bibdata/ext/subdoctype")
  target = draft ? "Draft " : ""
  target += subtype ? "#{strict_capitalize_phrase(subtype.text)} " : ""
  target += type ? "#{strict_capitalize_phrase(type.text)} " : ""
  /^#{target}/.match?(title.text) or
    @log.add("Style", title,
             "Expected title to start as: #{target}")
end

#trademark_ieee_erefs(xmldoc) ⇒ Object

Style manual 12.3.5



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

def trademark_ieee_erefs(xmldoc)
  ieee = xmldoc.xpath("//references/bibitem")
    .each_with_object({}) do |b, m|
    n = b.at("./contributor[role/@type = 'publisher']/organization/name")
    n&.text == "Institute of Electrical and Electronics Engineers" and
      m[b["id"]] = true
  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



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

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"] += "\u2122"
    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

#wg_members(node, xml) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/metanorma/ieee/front.rb', line 56

def wg_members(node, xml)
  a = node.attr("wg_chair") and
    (a, "Working Group Chair", xml)
  a = node.attr("wg_vicechair") and
    (a, "Working Group Vice-Chair", xml)
  a = node.attr("wg_secretary") and
    (a, "Working Group Secretary", xml)
  a = node.attr("wg_members") and
    (a, "Working Group Member", xml)
end

#xrefs(xmldoc) ⇒ Object



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

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),
         { hierarchical_assets: @hierarchical_assets })
  xrefs.parse(Nokogiri::XML(xmldoc.to_xml))
  xrefs
end