Class: Metanorma::Iec::Converter

Inherits:
ISO::Converter
  • Object
show all
Defined in:
lib/metanorma/iec/front.rb,
lib/metanorma/iec/converter.rb

Constant Summary collapse

XML_ROOT_TAG =
"iec-standard".freeze
XML_NAMESPACE =
"https://www.metanorma.org/ns/iec".freeze

Instance Method Summary collapse

Instance Method Details

#base_pubidObject



54
55
56
# File 'lib/metanorma/iec/front.rb', line 54

def base_pubid
  Pubid::Iec::Identifier
end

#boilerplate_file(x_orig) ⇒ Object



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

def boilerplate_file(x_orig)
  lang = case x_orig&.at("//bibdata/language")&.text
         when "fr" then "fr"
         else
           "en"
         end
  File.join(@libdir, "boilerplate-#{lang}.adoc")
end

#default_publisherObject



6
7
8
# File 'lib/metanorma/iec/front.rb', line 6

def default_publisher
  "IEC"
end

#doc_converter(node) ⇒ Object



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

def doc_converter(node)
  if node.nil?
    IsoDoc::Iec::WordConvert.new({})
  else
    IsoDoc::Iec::WordConvert.new(doc_extract_attributes(node))
  end
end

#docidentifier_cleanup(xmldoc) ⇒ Object



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

def docidentifier_cleanup(xmldoc)
  prefix = get_id_prefix(xmldoc)
  id = xmldoc.at("//bibdata/docidentifier[@type = 'ISO']") or return
  id.content = id_prefix(prefix, id)
  id = xmldoc.at("//bibdata/ext/structuredidentifier/project-number") and
    id.content = id_prefix(prefix, id)
  %w(iso-with-lang iso-reference iso-undated).each do |t|
    id = xmldoc.at("//bibdata/docidentifier[@type = '#{t}']") and
      id.content = id_prefix(prefix, id)
  end
end

#doctype_validate(xmldoc) ⇒ Object



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

def doctype_validate(xmldoc)
  %w(international-standard technical-specification technical-report
     publicly-available-specification international-workshop-agreement
     guide interpretation-sheet).include? @doctype or
    @log.add("Document Attributes", nil,
             "#{@doctype} is not a recognised document type")
  if function = xmldoc.at("//bibdata/ext/function")&.text
    %w(emc quality-assurance safety environment).include? function or
      @log.add("Document Attributes", nil,
               "#{function} is not a recognised document function")
  end
end

#document_scheme(node) ⇒ Object



165
166
167
# File 'lib/metanorma/iec/converter.rb', line 165

def document_scheme(node)
  node.attr("document-scheme")
end

#get_id_prefix(xmldoc) ⇒ Object

TODO remove when I adopt pubid-iec



156
157
158
159
160
161
162
163
# File 'lib/metanorma/iec/converter.rb', line 156

def get_id_prefix(xmldoc)
  xmldoc.xpath("//bibdata/contributor[role/@type = 'publisher']" \
               "/organization").each_with_object([]) do |x, prefix|
    x1 = x.at("abbreviation")&.text || x.at("name")&.text
    # (x1 == "IEC" and prefix.unshift("IEC")) or prefix << x1
    prefix << x1
  end
end

#get_stage(node) ⇒ Object



125
126
127
128
129
# File 'lib/metanorma/iec/front.rb', line 125

def get_stage(node)
  stage = node.attr("status") || node.attr("docstage") || "60"
  m = /([0-9])CD$/.match(stage) and node.set_attr("iteration", m[1])
  stage
end

#get_substage(node) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/metanorma/iec/front.rb', line 131

def get_substage(node)
  ret = node.attr("docsubstage") and return ret
  st = get_stage(node)
  case st
  when "60" then "60"
  when "30", "40", "50" then "20"
  else "00"
  end
end

#get_typeabbr(node, amd: false) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/metanorma/iec/front.rb', line 33

def get_typeabbr(node, amd: false)
  node.attr("amendment-number") and return :amd
  node.attr("corrigendum-number") and return :cor
  case doctype(node)
  when "directive" then :dir
  when "white-paper" then :wp
  when "technology-report" then :tec
  when "social-technology-trend-report" then :sttr
  when "component-specification" then :cs
  when "systems-reference-document" then :srd
  when "operational-document" then :od
  when "conformity-assessment" then :ca
  when "test-report-form" then :trf
  when "technical-report" then :tr
  when "technical-specification" then :ts
  when "publicly-available-specification" then :pas
  when "guide" then :guide
  else :is
  end
end

#html_converter(node) ⇒ Object



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

def html_converter(node)
  if node.nil?
    IsoDoc::Iec::HtmlConvert.new({})
  else
    IsoDoc::Iec::HtmlConvert.new(html_extract_attributes(node))
  end
end

#id_prefix(_prefix, id) ⇒ Object



31
32
33
# File 'lib/metanorma/iec/converter.rb', line 31

def id_prefix(_prefix, id)
  id.text
end

#iev_variant_titles(xmldoc) ⇒ Object



143
144
145
146
147
148
149
150
151
152
# File 'lib/metanorma/iec/converter.rb', line 143

def iev_variant_titles(xmldoc)
  id = xmldoc&.at("//bibdata/docidentifier[@type = 'ISO']")&.text
  m = /60050-(\d+)/.match(id) or return
  xmldoc.xpath("//sections/clause/terms/title").each_with_index do |t, i|
    num = "%02d" % [i + 1]
    t.next = "<variant-title type='toc'>" \
             "#{@i18n.section_iev} #{m[1]}-#{num} &#x2013; " \
             "#{t.children.to_xml}</variant-title>"
  end
end

#image_name_validate(xmldoc) ⇒ Object



132
# File 'lib/metanorma/iec/converter.rb', line 132

def image_name_validate(xmldoc); end

#init(node) ⇒ Object



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

def init(node)
  super
  if @is_iev = node.attr("docnumber") == "60050"
    @vocab = true
    node.set_attr("docsubtype", "vocabulary")
  end
end

#iso_id_out(xml, params, _with_prf) ⇒ Object



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

def iso_id_out(xml, params, _with_prf)
  params[:stage] == "60.60" and params.delete(:stage)
  super
end

#iso_id_out_common(xml, params, _with_prf) ⇒ Object



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

def iso_id_out_common(xml, params, _with_prf)
  xml.docidentifier iso_id_default(params).to_s,
                    **attr_code(type: "ISO", primary: "true")
  xml.docidentifier iso_id_reference(params).to_s,
                    **attr_code(type: "iso-reference")
  @id_revdate and
    xml.docidentifier iso_id_revdate(params.merge(year: @id_revdate))
      .to_s(with_edition_month_date: true),
                      **attr_code(type: "iso-revdate")
  xml.docidentifier iso_id_reference(params).urn,
                    **attr_code(type: "URN")
end

#iso_id_out_non_amd(xml, params, _with_prf) ⇒ Object



103
104
105
106
107
108
# File 'lib/metanorma/iec/front.rb', line 103

def iso_id_out_non_amd(xml, params, _with_prf)
  xml.docidentifier iso_id_undated(params).to_s,
                    **attr_code(type: "iso-undated")
  xml.docidentifier iso_id_with_lang(params).to_s,
                    **attr_code(type: "iso-with-lang")
end

#iso_id_params_add(node) ⇒ Object



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

def iso_id_params_add(node)
  stage = iso_id_stage(node)
  @id_revdate = node.attr("revdate")
  ret = { number: node.attr("amendment-number") ||
    node.attr("corrigendum-number"),
          year: iso_id_year(node) }
  if stage && !cen?(node.attr("publisher"))
    ret[:stage] = stage
  end
  compact_blank(ret)
end

#iso_id_params_core(node) ⇒ Object



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

def iso_id_params_core(node)
  pub = iso_id_pub(node)
  ret = { number: node.attr("docnumber"),
          part: node.attr("partnumber"),
          language: node.attr("language")&.split(/,\s*/) || "en",
          type: get_typeabbr(node),
          edition: node.attr("edition"), publisher: pub[0],
          unpublished: /^[0-5]/.match?(get_stage(node)),
          copublisher: pub[1..-1] }
  ret[:copublisher].empty? and ret.delete(:copublisher)
  compact_blank(ret)
end

#iso_id_params_resolve(params, params2, node, orig_id) ⇒ Object



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

def iso_id_params_resolve(params, params2, node, orig_id)
  ret = super
  params[:number].nil? && !@amd and ret[:number] = "0"
  ret
end

#iso_id_revdate(params) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/metanorma/iec/front.rb', line 110

def iso_id_revdate(params)
  params1 = params.dup.tap { |hs| hs.delete(:unpublished) }
  m = params1[:year].match(/^(\d{4})(-\d{2})?(-\d{2})?/)
  params1[:year] = m[1]
  params1[:month] = m[2].sub(/^-/, "")
  # skipping day for now
  pubid_select(params1).create(**params1)
end

#iso_id_stage(node) ⇒ Object



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

def iso_id_stage(node)
  ret = "#{get_stage(node)}.#{get_substage(node)}"
  if /[A-Z]/.match?(ret) # abbreviation
    ret = get_stage(node)
  end
  ret
end

#metadata_approval_committee(node, xml) ⇒ Object



175
# File 'lib/metanorma/iec/front.rb', line 175

def (node, xml); end

#metadata_ext(node, xml) ⇒ Object



165
166
167
168
169
170
171
172
173
# File 'lib/metanorma/iec/front.rb', line 165

def (node, xml)
  super
  a = node.attr("accessibility-color-inside") and
    xml.accessibility_color_inside a
  a = node.attr("price-code") and xml.price_code a
  a = node.attr("cen-processing") and xml.cen_processing a
  a = node.attr("secretary") and xml.secretary a
  a = node.attr("interest-to-committees") and xml.interest_to_committees a
end

#metadata_stagename(id) ⇒ Object



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

def (id)
  if @amd
    id.amendments&.first&.stage&.name ||
      id.corrigendums&.first&.stage&.name
  else
    begin
      id.typed_stage_name
    rescue StandardError
      id.stage&.name
    end
  end
end

#metadata_status(node, xml) ⇒ Object



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

def (node, xml)
  x = iso_id_default(iso_id_params(node)).stage
  xml.status do |s|
    s.stage x.harmonized_code.stage, **attr_code(abbreviation: x.abbr)
    s.substage x.harmonized_code.substage
  end
rescue *STAGE_ERROR
  report_illegal_stage(get_stage(node), get_substage(node))
end

#metadata_subdoctype(node, xml) ⇒ Object



160
161
162
163
# File 'lib/metanorma/iec/front.rb', line 160

def (node, xml)
  super
  a = node.attr("function") and xml.function a
end

#note(note) ⇒ Object



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

def note(note)
  if note.title == "Note from TC/SC Officers"
    noko do |xml|
      xml.tc_sc_officers_note do |c|
        wrap_in_para(note, c)
      end
    end.join("\n")
  else
    super
  end
end

#note_cleanup(xmldoc) ⇒ Object



126
127
128
129
130
# File 'lib/metanorma/iec/converter.rb', line 126

def note_cleanup(xmldoc)
  super
  n = xmldoc.at("//tc-sc-officers-note") and
    xmldoc.at("//bibdata/ext").add_child(n.remove)
end

#ol_cleanup(doc) ⇒ Object

preserve ol/@type within boilerplate, not elsewhere in doc



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

def ol_cleanup(doc)
  if doc.at("//metanorma-extension/semantic-metadata/" \
         "headless[text() = 'true']")
    doc.xpath("//ol[@explicit-type]").each do |x|
      x["type"] = x["explicit-type"]
      x.delete("explicit-type")
    end
  end
  ::Metanorma::Standoc::Converter.instance_method(:ol_cleanup).bind(self)
    .call(doc)
end

#pdf_converter(node) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/metanorma/iec/converter.rb', line 70

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

  if node.nil?
    IsoDoc::Iec::PdfConvert.new({})
  else
    IsoDoc::Iec::PdfConvert.new(pdf_extract_attributes(node))
  end
end

#presentation_xml_converter(node) ⇒ Object



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

def presentation_xml_converter(node)
  if node.nil?
    IsoDoc::Iec::PresentationXMLConvert.new({})
  else
    IsoDoc::Iec::PresentationXMLConvert
      .new(doc_extract_attributes(node)
      .merge(output_formats: ::Metanorma::Iec::Processor
      .new.output_formats))
  end
end

#report_illegal_stage(stage, substage) ⇒ Object



153
154
155
156
157
158
# File 'lib/metanorma/iec/front.rb', line 153

def report_illegal_stage(stage, substage)
  out = stage || ""
  /[A-Z]/.match?(out) or out += ".#{substage}"
  err = "Illegal document stage: #{out}"
  @log.add("Document Attributes", nil, err)
end

#sections_names_pref_cleanup(xml) ⇒ Object



95
96
97
98
99
# File 'lib/metanorma/iec/converter.rb', line 95

def sections_names_pref_cleanup(xml)
  super
  @is_iev and replace_title(xml, "//introduction",
                            @i18n&.introduction_iev)
end

#status_abbrev1(node) ⇒ Object



119
120
121
122
123
# File 'lib/metanorma/iec/front.rb', line 119

def status_abbrev1(node)
  id = iso_id_default({ stage: "60.60" }.merge(iso_id_params(node)))
  id.stage or return ""
  id.stage.abbr
end

#term_defs_boilerplate(div, source, term, preface, isodoc) ⇒ Object



91
92
93
# File 'lib/metanorma/iec/converter.rb', line 91

def term_defs_boilerplate(div, source, term, preface, isodoc)
  super unless @is_iev
end

#toc_cleanup(xmldoc) ⇒ Object



134
135
136
137
# File 'lib/metanorma/iec/converter.rb', line 134

def toc_cleanup(xmldoc)
  toc_iev_cleanup(xmldoc) if @is_iev
  super
end

#toc_iev_cleanup(xmldoc) ⇒ Object



139
140
141
# File 'lib/metanorma/iec/converter.rb', line 139

def toc_iev_cleanup(xmldoc)
  iev_variant_titles(xmldoc)
end

#validate(doc) ⇒ Object



48
49
50
51
52
# File 'lib/metanorma/iec/converter.rb', line 48

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