Class: Metanorma::Iec::Converter

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

Constant Summary collapse

IEC_LOG_MESSAGES =
{
  # rubocop:disable Naming/VariableNumber
  "IEC_1": { category: "Document Attributes",
             error: "Illegal document stage: %s",
             severity: 2 },
  "IEC_2": { category: "Document Attributes",
             error: "%s is not a recognised document type",
             severity: 2 },
  "IEC_3": { category: "Document Attributes",
             error: "%s is not a recognised document function",
             severity: 2 },
}.freeze

Instance Method Summary collapse

Instance Method Details

#base_pubidObject



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

def base_pubid
  Pubid::Iec::Identifier
end

#default_publisherObject



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

def default_publisher
  "IEC"
end

#doc_converter(node) ⇒ Object



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

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

#document_scheme(node) ⇒ Object



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

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

#get_stage(node) ⇒ Object



128
129
130
131
132
# File 'lib/metanorma/iec/front.rb', line 128

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



134
135
136
137
138
139
140
141
142
# File 'lib/metanorma/iec/front.rb', line 134

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



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

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



19
20
21
22
23
24
25
# File 'lib/metanorma/iec/converter.rb', line 19

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

#init(node) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/metanorma/iec/converter.rb', line 11

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



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

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



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

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

#iso_id_out_non_amd(xml, params, _with_prf) ⇒ Object



106
107
108
109
110
111
# File 'lib/metanorma/iec/front.rb', line 106

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

#iso_id_params_add(node) ⇒ Object



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

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



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

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



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

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



113
114
115
116
117
118
119
120
# File 'lib/metanorma/iec/front.rb', line 113

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



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

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

#log_messagesObject

rubocop:enable Naming/VariableNumber



18
19
20
# File 'lib/metanorma/iec/log.rb', line 18

def log_messages
  super.merge(IEC_LOG_MESSAGES)
end

#metadata_ext(node, xml) ⇒ Object



162
163
164
165
166
167
168
169
170
171
# File 'lib/metanorma/iec/front.rb', line 162

def (node, xml)
  super
  add_noko_elem(xml, "function", node.attr("function"))
  add_noko_elem(xml, "accessibility_color_inside",
                node.attr("accessibility-color-inside"))
  add_noko_elem(xml, "cen_processing", node.attr("cen-processing"))
  add_noko_elem(xml, "secretary", node.attr("secretary"))
  add_noko_elem(xml, "interest_to_committees",
                node.attr("interest-to-committees"))
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
32
# File 'lib/metanorma/iec/front.rb', line 23

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

#note(note) ⇒ Object



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

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

#pdf_converter(node) ⇒ Object



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

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



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

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



156
157
158
159
160
# File 'lib/metanorma/iec/front.rb', line 156

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

#status_abbrev1(node) ⇒ Object



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

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