Class: Metanorma::Iec::Converter
- Inherits:
-
Iso::Converter
- Object
- Iso::Converter
- Metanorma::Iec::Converter
- 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
- #base_pubid ⇒ Object
- #boilerplate_file(x_orig) ⇒ Object
- #default_publisher ⇒ Object
- #doc_converter(node) ⇒ Object
- #docidentifier_cleanup(xmldoc) ⇒ Object
- #doctype_validate(xmldoc) ⇒ Object
- #document_scheme(node) ⇒ Object
-
#get_id_prefix(xmldoc) ⇒ Object
TODO remove when I adopt pubid-iec.
- #get_stage(node) ⇒ Object
- #get_substage(node) ⇒ Object
- #get_typeabbr(node, amd: false) ⇒ Object
- #html_converter(node) ⇒ Object
- #id_prefix(_prefix, id) ⇒ Object
- #iev_variant_titles(xmldoc) ⇒ Object
- #image_name_validate(xmldoc) ⇒ Object
- #init(node) ⇒ Object
- #iso_id_out(xml, params, _with_prf) ⇒ Object
- #iso_id_out_common(xml, params, _with_prf) ⇒ Object
- #iso_id_out_non_amd(xml, params, _with_prf) ⇒ Object
- #iso_id_params_add(node) ⇒ Object
- #iso_id_params_core(node) ⇒ Object
- #iso_id_params_resolve(params, params2, node, orig_id) ⇒ Object
- #iso_id_revdate(params) ⇒ Object
- #iso_id_stage(node) ⇒ Object
-
#log_messages ⇒ Object
rubocop:enable Naming/VariableNumber.
- #metadata_ext(node, xml) ⇒ Object
- #metadata_stagename(id) ⇒ Object
- #metadata_status(node, xml) ⇒ Object
- #note(note) ⇒ Object
- #note_cleanup(xmldoc) ⇒ Object
-
#ol_cleanup(doc) ⇒ Object
preserve ol/@type within boilerplate, not elsewhere in doc.
- #pdf_converter(node) ⇒ Object
- #presentation_xml_converter(node) ⇒ Object
- #report_illegal_stage(stage, substage) ⇒ Object
- #schema_file ⇒ Object
- #sections_names_pref_cleanup(xml) ⇒ Object
- #status_abbrev1(node) ⇒ Object
- #term_defs_boilerplate(div, source, term, preface, isodoc) ⇒ Object
- #terms_terms_cleanup(xmldoc) ⇒ Object
- #toc_cleanup(xmldoc) ⇒ Object
- #toc_iev_cleanup(xmldoc) ⇒ Object
Instance Method Details
#base_pubid ⇒ Object
57 58 59 |
# File 'lib/metanorma/iec/front.rb', line 57 def base_pubid Pubid::Iec::Identifier end |
#boilerplate_file(x_orig) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/metanorma/iec/converter.rb', line 19 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_publisher ⇒ Object
6 7 8 |
# File 'lib/metanorma/iec/front.rb', line 6 def default_publisher "IEC" end |
#doc_converter(node) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/metanorma/iec/converter.rb', line 55 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
167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/metanorma/iec/converter.rb', line 167 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
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/metanorma/iec/converter.rb', line 32 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("IEC_2", nil, params: [@doctype]) if function = xmldoc.at("//bibdata/ext/function")&.text %w(emc quality-assurance safety environment).include? function or @log.add("IEC_3", nil, params: [function]) end end |
#document_scheme(node) ⇒ Object
163 164 165 |
# File 'lib/metanorma/iec/converter.rb', line 163 def document_scheme(node) node.attr("document-scheme") end |
#get_id_prefix(xmldoc) ⇒ Object
TODO remove when I adopt pubid-iec
154 155 156 157 158 159 160 161 |
# File 'lib/metanorma/iec/converter.rb', line 154 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
143 144 145 146 147 |
# File 'lib/metanorma/iec/front.rb', line 143 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
149 150 151 152 153 154 155 156 157 |
# File 'lib/metanorma/iec/front.rb', line 149 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
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/metanorma/iec/front.rb', line 36 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
47 48 49 50 51 52 53 |
# File 'lib/metanorma/iec/converter.rb', line 47 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
28 29 30 |
# File 'lib/metanorma/iec/converter.rb', line 28 def id_prefix(_prefix, id) id.text end |
#iev_variant_titles(xmldoc) ⇒ Object
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/metanorma/iec/converter.rb', line 142 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 #{add_id_text} type='toc'>" \ "#{@i18n.section_iev} #{m[1]}-#{num} – " \ "#{t.children.to_xml}</variant-title>" end end |
#image_name_validate(xmldoc) ⇒ Object
131 |
# File 'lib/metanorma/iec/converter.rb', line 131 def image_name_validate(xmldoc); 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
88 89 90 91 |
# File 'lib/metanorma/iec/front.rb', line 88 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
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/metanorma/iec/front.rb', line 93 def iso_id_out_common(xml, params, _with_prf) add_noko_elem(xml, "docidentifier", iso_id_default(params).to_s, type: "ISO", primary: "true") # xml.docidentifier iso_id_default(params).to_s, # **attr_code(type: "ISO", primary: "true") add_noko_elem(xml, "docidentifier", iso_id_reference(params).to_s, type: "iso-reference") # xml.docidentifier iso_id_reference(params).to_s, # **attr_code(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") # xml.docidentifier iso_id_revdate(params.merge(year: @id_revdate)) # .to_s(with_edition_month_date: true), # **attr_code(type: "iso-revdate") add_noko_elem(xml, "docidentifier", iso_id_reference(params).urn, type: "URN") # xml.docidentifier iso_id_reference(params).urn, # **attr_code(type: "URN") end |
#iso_id_out_non_amd(xml, params, _with_prf) ⇒ Object
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/metanorma/iec/front.rb', line 117 def iso_id_out_non_amd(xml, params, _with_prf) add_noko_elem(xml, "docidentifier", iso_id_undated(params).to_s, type: "iso-undated") # xml.docidentifier iso_id_undated(params).to_s, # **attr_code(type: "iso-undated") add_noko_elem(xml, "docidentifier", iso_id_with_lang(params).to_s, type: "iso-with-lang") # xml.docidentifier iso_id_with_lang(params).to_s, # **attr_code(type: "iso-with-lang") end |
#iso_id_params_add(node) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/metanorma/iec/front.rb', line 159 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
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/metanorma/iec/front.rb', line 61 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
82 83 84 85 86 |
# File 'lib/metanorma/iec/front.rb', line 82 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
128 129 130 131 132 133 134 135 |
# File 'lib/metanorma/iec/front.rb', line 128 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
74 75 76 77 78 79 80 |
# File 'lib/metanorma/iec/front.rb', line 74 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_messages ⇒ Object
rubocop:enable Naming/VariableNumber
18 19 20 |
# File 'lib/metanorma/iec/log.rb', line 18 def super.merge(IEC_LOG_MESSAGES) end |
#metadata_ext(node, xml) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/metanorma/iec/front.rb', line 177 def (node, xml) super add_noko_elem(xml, "function", node.attr("function")) # a = node.attr("function") and xml.function a add_noko_elem(xml, "accessibility_color_inside", node.attr("accessibility-color-inside")) # a = node.attr("accessibility-color-inside") and # xml.accessibility_color_inside a add_noko_elem(xml, "cen_processing", node.attr("cen-processing")) # a = node.attr("cen-processing") and xml.cen_processing a add_noko_elem(xml, "secretary", node.attr("secretary")) # a = node.attr("secretary") and xml.secretary a add_noko_elem(xml, "interest_to_committees", node.attr("interest-to-committees")) # 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 32 33 34 |
# 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: x.abbr) # s.stage x.harmonized_code.stage, **attr_code(abbreviation: x.abbr) add_noko_elem(s, "substage", x.harmonized_code.substage) # s.substage x.harmonized_code.substage end rescue *STAGE_ERROR report_illegal_stage(get_stage(node), get_substage(node)) end |
#note(note) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/metanorma/iec/converter.rb', line 113 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 |
#note_cleanup(xmldoc) ⇒ Object
125 126 127 128 129 |
# File 'lib/metanorma/iec/converter.rb', line 125 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
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/metanorma/iec/converter.rb', line 100 def ol_cleanup(doc) doc.xpath("//ol[@type]").each { |x| x.delete("type") } 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
63 64 65 66 67 68 69 70 71 |
# File 'lib/metanorma/iec/converter.rb', line 63 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
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/metanorma/iec/converter.rb', line 73 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
171 172 173 174 175 |
# File 'lib/metanorma/iec/front.rb', line 171 def report_illegal_stage(stage, substage) out = stage || "" /[A-Z]/.match?(out) or out += ".#{substage}" @log.add("IEC_1", nil, params: [out]) end |
#schema_file ⇒ Object
43 44 45 |
# File 'lib/metanorma/iec/converter.rb', line 43 def schema_file "iec.rng" end |
#sections_names_pref_cleanup(xml) ⇒ Object
93 94 95 96 97 |
# File 'lib/metanorma/iec/converter.rb', line 93 def sections_names_pref_cleanup(xml) super @is_iev and replace_title(xml, "//introduction", @i18n&.introduction_iev) end |
#status_abbrev1(node) ⇒ Object
137 138 139 140 141 |
# File 'lib/metanorma/iec/front.rb', line 137 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
84 85 86 |
# File 'lib/metanorma/iec/converter.rb', line 84 def term_defs_boilerplate(div, source, term, preface, isodoc) super unless @is_iev end |
#terms_terms_cleanup(xmldoc) ⇒ Object
88 89 90 91 |
# File 'lib/metanorma/iec/converter.rb', line 88 def terms_terms_cleanup(xmldoc) @is_iev and return super end |
#toc_cleanup(xmldoc) ⇒ Object
133 134 135 136 |
# File 'lib/metanorma/iec/converter.rb', line 133 def toc_cleanup(xmldoc) toc_iev_cleanup(xmldoc) if @is_iev super end |
#toc_iev_cleanup(xmldoc) ⇒ Object
138 139 140 |
# File 'lib/metanorma/iec/converter.rb', line 138 def toc_iev_cleanup(xmldoc) iev_variant_titles(xmldoc) end |