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
XML_ROOT_TAG =
"ieee-standard".freeze
XML_NAMESPACE =
"https://www.metanorma.org/ns/ieee".freeze
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
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



114
115
116
# File 'lib/metanorma/ieee/validate.rb', line 114

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_ref.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

#biblio_renumber(xmldoc) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 93

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



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 104

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

#bibliography_validate(root) ⇒ Object

Style manual 19.1



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

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

#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_validate(doc.root)
  bibitem_validate(doc.root)
  list_validate(doc)
  table_style(doc)
  figure_validate(doc)
end

#designator_or_name(bib) ⇒ Object



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

def designator_or_name(bib)
  case bib["type"]
  when "standard", "techreport"
    n = bib.at("./docidentifier[@primary]") ||
      bib.at("./docidentifier[not(#{OTHERIDS})]")
    n&.text || "ZZZZ"
  else
    bib1 = bib.dup
    bib1.add_namespace(nil, self.class::XML_NAMESPACE)
    i = IsoDoc::IEEE::PresentationXMLConvert.new({ lang: @lang,
                                                   script: @script })
    i.i18n_init(@lang, @script)
    i.creatornames(bib1)
  end
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_style_validate(docxml) ⇒ Object

Style manual 17.2



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

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



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

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



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

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

#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



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

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



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

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

#intro_boilerplate(xml, isodoc) ⇒ Object



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

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 %}, {{ full_doctitle }}
  ADM
  adm = isodoc.populate_template(template)
  intro.next = "<admonition>#{adm}</admonition>"
end

#list_validate(doc) ⇒ Object



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

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

#listcount_validate(doc) ⇒ Object

Style manual 13.3



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

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



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/metanorma/ieee/validate.rb', line 133

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



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

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



95
96
97
98
99
100
101
# File 'lib/metanorma/ieee/validate.rb', line 95

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



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

def locality_validate(root)
  locality_range_validate(root)
  locality_erefs_validate(root)
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



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

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



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

def normref_cleanup(xmldoc)
  super
  normref_reorder(xmldoc)
end

#normref_reorder(xmldoc) ⇒ Object



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

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

#note_cleanup(xmldoc) ⇒ Object



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

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



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

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



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

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

#other_footnote_renumber1(fnote, idx, seen) ⇒ Object



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

def other_footnote_renumber1(fnote, idx, seen)
  return [idx, seen] if fnote["table"]

  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



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



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

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



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

def quotesource_cleanup(xmldoc)
  super
  trademark_ieee_erefs(xmldoc)
end

#relaton_relationsObject



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

def relaton_relations
  super + %w(merges updates)
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



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

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
62
# 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)
  bibliography_validate(root)
end

#sections_sequence_validate_body(names, elem) ⇒ Object



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

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

#sections_sequence_validate_end(names, elem) ⇒ Object



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

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



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

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



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

def sort_biblio(bib)
  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



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

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
  "#{name}. #{title}"
end

#sort_terms(terms) ⇒ Object



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

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

#sort_terms_key(term) ⇒ Object



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

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



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

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



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

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



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

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



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

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



224
225
226
227
228
229
230
231
# File 'lib/metanorma/ieee/validate.rb', line 224

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



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

def term_defs_boilerplate_cont(src, term, isodoc); end

#term_reorder(xmldoc) ⇒ Object



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

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

#term_reorder1(terms) ⇒ Object



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

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_boilerplate_insert(xmldoc, isodoc, once = false) ⇒ Object



116
117
118
119
# File 'lib/metanorma/ieee/cleanup.rb', line 116

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

#termdef_cleanup(xmldoc) ⇒ Object



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

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



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

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



85
86
87
88
89
90
91
# File 'lib/metanorma/ieee/cleanup_ref.rb', line 85

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



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

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