Class: Metanorma::JIS::Converter
- Inherits:
-
ISO::Converter
- Object
- ISO::Converter
- Metanorma::JIS::Converter
- Defined in:
- lib/metanorma/jis/front.rb,
lib/metanorma/jis/cleanup.rb,
lib/metanorma/jis/validate.rb,
lib/metanorma/jis/converter.rb
Constant Summary collapse
- LANGS =
%w(ja en).freeze
- JIS_HASH =
{ "ja" => "日本工業規格", "en" => "Japanese Industrial Standards" }.freeze
- XML_ROOT_TAG =
"jis-standard".freeze
- XML_NAMESPACE =
"https://www.metanorma.org/ns/jis".freeze
Instance Method Summary collapse
- #base_pubid ⇒ Object
- #boilerplate_file(_x_orig) ⇒ Object
- #clean_example_keep_separate(xmldoc) ⇒ Object
- #copyright_parse(node) ⇒ Object
- #default_publisher ⇒ Object
- #doc_converter(node) ⇒ Object
- #docidentifier_cleanup(xmldoc) ⇒ Object
- #doctype(node) ⇒ Object
- #doctype_validate(_xmldoc) ⇒ Object
- #example_attrs(node) ⇒ Object
- #extract_org_attrs_address(node, opts, suffix) ⇒ Object
- #extract_org_attrs_complex(node, opts, source, suffix) ⇒ Object
- #get_typeabbr(node, amd: false) ⇒ Object
- #html_converter(node) ⇒ Object
- #init_i18n(node) ⇒ Object
- #iso_id_default(params) ⇒ Object
- #iso_id_out(xml, params, _with_prf) ⇒ Object
- #iso_id_params_add(node) ⇒ Object
- #iso_id_params_core(node) ⇒ Object
- #metadata_author(node, xml) ⇒ Object
- #metadata_id(node, xml) ⇒ Object
- #metadata_publisher(node, xml) ⇒ Object
- #multiling_docattr(node, attr, suffix, langs) ⇒ Object
- #multiling_noko_value(value, tag, xml) ⇒ Object
- #norm_ref_preface(ref) ⇒ Object
- #note_cleanup(xmldoc) ⇒ Object
- #note_example_to_table(xmldoc) ⇒ Object
- #org_abbrev ⇒ Object
- #org_attrs_complex_parse(node, opts, source) ⇒ Object
- #org_attrs_parse(node, opts) ⇒ Object
- #org_organization(node, xml, org) ⇒ Object
- #organization(xml, org, node = nil, default_org = nil) ⇒ Object
- #pdf_converter(node) ⇒ Object
- #presentation_xml_converter(node) ⇒ Object
- #ref_dated(ref) ⇒ Object
- #ref_empty?(ref) ⇒ Boolean
- #script_validate(xmldoc) ⇒ Object
- #section_attributes(node) ⇒ Object
- #table_footnote_renumber(xmldoc) ⇒ Object
- #title(node, xml) ⇒ Object
- #validate(doc) ⇒ Object
Instance Method Details
#base_pubid ⇒ Object
158 159 160 |
# File 'lib/metanorma/jis/front.rb', line 158 def base_pubid Pubid::Jis::Identifier end |
#boilerplate_file(_x_orig) ⇒ Object
28 29 30 |
# File 'lib/metanorma/jis/converter.rb', line 28 def boilerplate_file(_x_orig) File.join(@libdir, "jis_intro_jp.xml") end |
#clean_example_keep_separate(xmldoc) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/metanorma/jis/cleanup.rb', line 58 def clean_example_keep_separate(xmldoc) xmldoc.xpath("//example[@keep-separate] | " \ "//termexample[@keep-separate]").each do |n| n.delete("keep-separate") end end |
#copyright_parse(node) ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/metanorma/jis/front.rb', line 122 def copyright_parse(node) opt = { source: ["copyright-holder", "publisher", "pub"], role: "publisher", default: JIS_HASH } ret = org_attrs_parse(node, opt) ret.empty? and ret = [{ name: "-" }] ret end |
#default_publisher ⇒ Object
10 11 12 |
# File 'lib/metanorma/jis/front.rb', line 10 def default_publisher "JIS" end |
#doc_converter(node) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/metanorma/jis/converter.rb', line 55 def doc_converter(node) if node.nil? IsoDoc::JIS::WordConvert.new({}) else IsoDoc::JIS::WordConvert.new(doc_extract_attributes(node)) end end |
#docidentifier_cleanup(xmldoc) ⇒ Object
40 |
# File 'lib/metanorma/jis/cleanup.rb', line 40 def docidentifier_cleanup(xmldoc); end |
#doctype(node) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/metanorma/jis/converter.rb', line 21 def doctype(node) ret = node.attr("doctype")&.gsub(/\s+/, "-")&.downcase || "japanese-industrial-standard" ret = "japanese-industrial-standard" if ret == "article" ret end |
#doctype_validate(_xmldoc) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/metanorma/jis/validate.rb', line 4 def doctype_validate(_xmldoc) %w(japanese-industrial-standard technical-report technical-specification amendment).include? @doctype or @log.add("Document Attributes", nil, "#{@doctype} is not a recognised document type") end |
#example_attrs(node) ⇒ Object
42 43 44 45 |
# File 'lib/metanorma/jis/converter.rb', line 42 def example_attrs(node) attr_code(id_unnum_attrs(node).merge(keep_attrs(node)) .merge("keep-separate": node.attr("keep-separate"))) end |
#extract_org_attrs_address(node, opts, suffix) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/metanorma/jis/front.rb', line 86 def extract_org_attrs_address(node, opts, suffix) %w(address phone fax email uri).each_with_object({}) do |a, m| opts[:source]&.each do |s| p = multiling_docattr(node, "#{s}-#{a}", suffix, LANGS) and m[a.to_sym] = p end end end |
#extract_org_attrs_complex(node, opts, source, suffix) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/metanorma/jis/front.rb', line 77 def extract_org_attrs_complex(node, opts, source, suffix) { name: multiling_docattr(node, source, suffix, LANGS), role: opts[:role], desc: opts[:desc], abbr: multiling_docattr(node, "#{source}-abbr", suffix, LANGS), logo: multiling_docattr(node, "#{source}_logo", suffix, LANGS) } .compact .merge(extract_org_attrs_address(node, opts, suffix)) end |
#get_typeabbr(node, amd: false) ⇒ Object
149 150 151 152 153 154 155 156 |
# File 'lib/metanorma/jis/front.rb', line 149 def get_typeabbr(node, amd: false) amd || node.attr("amendment-number") and return :amd case doctype(node) when "technical-report" then :tr when "technical-specification" then :ts when "amendment" then :amd end end |
#html_converter(node) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/metanorma/jis/converter.rb', line 47 def html_converter(node) if node.nil? IsoDoc::JIS::HtmlConvert.new({}) else IsoDoc::JIS::HtmlConvert.new(html_extract_attributes(node)) end end |
#init_i18n(node) ⇒ Object
15 16 17 18 19 |
# File 'lib/metanorma/jis/converter.rb', line 15 def init_i18n(node) node.attr("language") or node.set_attr("language", "ja") node.attr("language") == "jp" and node.set_attr("language", "ja") super end |
#iso_id_default(params) ⇒ Object
185 186 187 188 189 |
# File 'lib/metanorma/jis/front.rb', line 185 def iso_id_default(params) base_pubid.create(**params) rescue StandardError => e clean_abort("Document identifier: #{e}", xml) end |
#iso_id_out(xml, params, _with_prf) ⇒ Object
180 181 182 183 |
# File 'lib/metanorma/jis/front.rb', line 180 def iso_id_out(xml, params, _with_prf) id = iso_id_default(params).to_s(with_publisher: false) xml.docidentifier id.strip, type: "JIS" end |
#iso_id_params_add(node) ⇒ Object
175 176 177 178 |
# File 'lib/metanorma/jis/front.rb', line 175 def iso_id_params_add(node) { number: node.attr("amendment-number"), year: iso_id_year(node) }.compact end |
#iso_id_params_core(node) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/metanorma/jis/front.rb', line 162 def iso_id_params_core(node) pub = (node.attr("publisher") || "JIS").split(/[;,]/) ret = { number: node.attr("docnumber") || "0", part: node.attr("partnumber"), series: node.attr("docseries"), language: node.attr("language") == "en" ? "E" : nil, type: get_typeabbr(node), publisher: pub[0], copublisher: pub[1..-1] }.compact ret[:copublisher].empty? and ret.delete(:copublisher) ret end |
#metadata_author(node, xml) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/metanorma/jis/front.rb', line 14 def (node, xml) org_contributor(node, xml, { source: ["publisher", "pub"], role: "author", default: JIS_HASH }) node.attr("doctype") == "expert-commentary" and (node, xml) end |
#metadata_id(node, xml) ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/metanorma/jis/front.rb', line 141 def (node, xml) if id = node.attr("docidentifier") xml.docidentifier id.sub(/^JIS /, ""), **attr_code(type: "JIS") else iso_id(node, xml) end xml.docnumber node.attr("docnumber") end |
#metadata_publisher(node, xml) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/metanorma/jis/front.rb', line 22 def (node, xml) [{ source: ["publisher", "pub"], role: "publisher", default: JIS_HASH }, { role: "authorizer", source: ["investigative-organization"], desc: "Investigative organization" }, { role: "authorizer", source: ["investigative-committee"], desc: "Investigative committee" }].each do |o| org_contributor(node, xml, o) end end |
#multiling_docattr(node, attr, suffix, langs) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/metanorma/jis/front.rb', line 67 def multiling_docattr(node, attr, suffix, langs) node.nil? and return nil ret = node.attr(attr + suffix) and return ret ret = langs.each_with_object({}).each do |l, m| x = node.attr("#{attr}-#{l}#{suffix}") and m[l] = x end.compact ret.empty? and return nil ret end |
#multiling_noko_value(value, tag, xml) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/metanorma/jis/front.rb', line 95 def multiling_noko_value(value, tag, xml) if value.is_a?(Hash) xml.send tag do |t| value.each do |k, v| t.variant v, language: k end end elsif value.is_a?(Array) value.each { |a| xml.send tag, a } else xml.send tag, value end end |
#norm_ref_preface(ref) ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/metanorma/jis/cleanup.rb', line 4 def norm_ref_preface(ref) if ref.at("./note[@type = 'boilerplate']") unwrap_boilerplate_clauses(ref, ".") else pref = if ref_empty?(ref) then @i18n.norm_empty_pref else @i18n.get[ref_dated(ref)] end ref.at("./title").next = "<p>#{pref}</p>" end end |
#note_cleanup(xmldoc) ⇒ Object
42 43 44 45 46 |
# File 'lib/metanorma/jis/cleanup.rb', line 42 def note_cleanup(xmldoc) note_example_to_table(xmldoc) super clean_example_keep_separate(xmldoc) end |
#note_example_to_table(xmldoc) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/metanorma/jis/cleanup.rb', line 48 def note_example_to_table(xmldoc) xmldoc.xpath("//table").each do |t| t.xpath("./following-sibling::*").each do |n| %w(note example).include?(n.name) or break n["keep-separate"] == "true" and break n.parent = t end end end |
#org_abbrev ⇒ Object
6 7 8 |
# File 'lib/metanorma/jis/front.rb', line 6 def org_abbrev super.merge("Japanese Industrial Standards" => "JIS") end |
#org_attrs_complex_parse(node, opts, source) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/metanorma/jis/front.rb', line 55 def org_attrs_complex_parse(node, opts, source) i = 1 suffix = "" ret = [] while multiling_docattr(node, source, suffix, LANGS) ret << extract_org_attrs_complex(node, opts, source, suffix) i += 1 suffix = "_#{i}" end ret end |
#org_attrs_parse(node, opts) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/metanorma/jis/front.rb', line 46 def org_attrs_parse(node, opts) source = opts[:source]&.detect { |s| node.attr(s) } source ||= opts[:source]&.detect do |s| LANGS.detect { |l| node.attr("#{s}-#{l}") } end org_attrs_simple_parse(node, opts, source) || org_attrs_complex_parse(node, opts, source) end |
#org_organization(node, xml, org) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/metanorma/jis/front.rb', line 39 def org_organization(node, xml, org) organization(xml, { name: org[:name], abbr: org[:abbr] }.compact, node, !multiling_docattr(node, "publisher", "", LANGS)) org_address(org, xml) org_logo(xml, org[:logo]) end |
#organization(xml, org, node = nil, default_org = nil) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/metanorma/jis/front.rb', line 108 def organization(xml, org, node = nil, default_org = nil) org.is_a?(Hash) && org[:name] or org = { name: org } abbrevs = org_abbrev name_str = org[:name].is_a?(Hash) ? org[:name]["en"] : org[:name] n = abbrevs.invert[org[:name]] and org = { name: n, abbr: org[:name] } multiling_noko_value(org[:name], "name", xml) default_org && a = multiling_docattr(node, "subdivision", "", LANGS) and multiling_noko_value(a, "subdivision", xml) abbr = org[:abbr] abbr ||= org_abbrev[name_str] default_org && b = node&.attr("subdivision-abbr") and abbr = b abbr and xml.abbreviation abbr end |
#pdf_converter(node) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/metanorma/jis/converter.rb', line 63 def pdf_converter(node) return if node.attr("no-pdf") if node.nil? IsoDoc::JIS::PdfConvert.new({}) else IsoDoc::JIS::PdfConvert.new(pdf_extract_attributes(node)) end end |
#presentation_xml_converter(node) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/metanorma/jis/converter.rb', line 73 def presentation_xml_converter(node) if node.nil? IsoDoc::JIS::PresentationXMLConvert.new({}) else IsoDoc::JIS::PresentationXMLConvert .new(doc_extract_attributes(node) .merge(output_formats: ::Metanorma::JIS::Processor.new.output_formats)) end end |
#ref_dated(ref) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/metanorma/jis/cleanup.rb', line 19 def ref_dated(ref) refs = ref.xpath("./bibitem").each_with_object({}) do |e, m| if e.at("./date") then m[:dated] = true else m[:undated] = true end end refs[:dated] && refs[:undated] and return "norm_with_refs_pref" refs[:dated] and return "norm_with_refs_pref_all_dated" "norm_with_refs_pref_none_dated" end |
#ref_empty?(ref) ⇒ Boolean
15 16 17 |
# File 'lib/metanorma/jis/cleanup.rb', line 15 def ref_empty?(ref) ref.xpath(".//bibitem").empty? end |
#script_validate(xmldoc) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/metanorma/jis/validate.rb', line 11 def script_validate(xmldoc) script = xmldoc&.at("//bibdata/script")&.text %w(Jpan Latn).include?(script) or @log.add("Document Attributes", nil, "#{script} is not a recognised script") end |
#section_attributes(node) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/metanorma/jis/converter.rb', line 32 def section_attributes(node) ret = super if node.attr("style") == "appendix" && node.level == 1 && node.option?("commentary") ret[:commentary] = true node.set_attr("obligation", "informative") end ret end |
#table_footnote_renumber(xmldoc) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/metanorma/jis/cleanup.rb', line 30 def table_footnote_renumber(xmldoc) xmldoc.xpath("//table | //figure").each do |t| seen = {} i = 0 t.xpath(".//fn").each do |fn| i, seen = table_footnote_renumber1(fn, i, seen) end end end |
#title(node, xml) ⇒ Object
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/metanorma/jis/front.rb', line 130 def title(node, xml) %w(en ja).each do |lang| at = { language: lang, format: "text/plain" } title_full(node, xml, lang, at) title_intro(node, xml, lang, at) title_main(node, xml, lang, at) title_part(node, xml, lang, at) title_amd(node, xml, lang, at) if @amd end end |
#validate(doc) ⇒ Object
18 19 20 21 22 |
# File 'lib/metanorma/jis/validate.rb', line 18 def validate(doc) content_validate(doc) schema_validate(formattedstr_strip(doc.dup), File.join(File.dirname(__FILE__), "jis.rng")) end |