Class: Metanorma::JIS::Converter

Inherits:
ISO::Converter
  • Object
show all
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
XML_ROOT_TAG =
"jis-standard".freeze
XML_NAMESPACE =
"https://www.metanorma.org/ns/jis".freeze

Instance Method Summary collapse

Instance Method Details

#base_pubidObject



157
158
159
# File 'lib/metanorma/jis/front.rb', line 157

def base_pubid
  Pubid::Jis::Identifier
end

#bibdata_cleanup(xmldoc) ⇒ Object



65
66
67
68
# File 'lib/metanorma/jis/cleanup.rb', line 65

def bibdata_cleanup(xmldoc)
  super
  bibdata_supply_chairperson_role(xmldoc)
end

#bibdata_supply_chairperson_role(xmldoc) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/metanorma/jis/cleanup.rb', line 70

def bibdata_supply_chairperson_role(xmldoc)
  xpath =
    "//bibdata/contributor" \
    "[role/@type = 'authorizer'][role/description = " \
    "'investigative committee']/person/affiliation"
  xmldoc.xpath(xpath).each do |a|
    a.at("./name") or next
    a.children.first.previous = "<name>#{@i18n.chairperson}</name>"
  end
end

#biblio_reorder(xmldoc) ⇒ Object



86
87
88
89
90
# File 'lib/metanorma/jis/cleanup.rb', line 86

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

#boilerplate_file(_x_orig) ⇒ Object



41
42
43
# File 'lib/metanorma/jis/converter.rb', line 41

def boilerplate_file(_x_orig)
  File.join(@libdir, "boilerplate-#{@lang}.adoc")
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


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

def copyright_parse(node)
  opt = { source: ["copyright-holder", "publisher", "pub"],
          role: "publisher", default: pub_hash }
  ret = org_attrs_parse(node, opt)
  ret.empty? and ret = [{ name: "-" }]
  ret
end

#default_publisherObject



10
11
12
# File 'lib/metanorma/jis/front.rb', line 10

def default_publisher
  "JIS"
end

#doc_converter(node) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/metanorma/jis/converter.rb', line 57

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

#document_scheme(node) ⇒ Object



45
46
47
# File 'lib/metanorma/jis/converter.rb', line 45

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

#example_attrs(node) ⇒ Object



36
37
38
39
# File 'lib/metanorma/jis/converter.rb', line 36

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



85
86
87
88
89
90
91
92
# File 'lib/metanorma/jis/front.rb', line 85

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
# File 'lib/metanorma/jis/front.rb', line 77

def extract_org_attrs_complex(node, opts, source, suffix)
  compact_blank({ 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) })
    .merge(extract_org_attrs_address(node, opts, suffix))
end

#get_typeabbr(node, amd: false) ⇒ Object



148
149
150
151
152
153
154
155
# File 'lib/metanorma/jis/front.rb', line 148

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



49
50
51
52
53
54
55
# File 'lib/metanorma/jis/converter.rb', line 49

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

#init_misc(node) ⇒ Object



21
22
23
24
# File 'lib/metanorma/jis/converter.rb', line 21

def init_misc(node)
  super
  @default_doctype = "japanese-industrial-standard"
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", primary: "true"
end

#iso_id_params_add(node) ⇒ Object



174
175
176
177
178
# File 'lib/metanorma/jis/front.rb', line 174

def iso_id_params_add(node)
  ret = { number: node.attr("amendment-number"),
          year: iso_id_year(node) }
  compact_blank(ret)
end

#iso_id_params_core(node) ⇒ Object



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

def iso_id_params_core(node)
  pub = iso_id_pub(node)
  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)
  compact_blank(ret)
end

#metadata_author(node, xml) ⇒ Object



14
15
16
17
18
19
# File 'lib/metanorma/jis/front.rb', line 14

def (node, xml)
  org_contributor(node, xml,
                  { source: ["publisher", "pub"], role: "author",
                    default: pub_hash })
  personal_author(node, xml)
end

#metadata_id(node, xml) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/metanorma/jis/front.rb', line 140

def (node, xml)
  if id = node.attr("docidentifier")
    xml.docidentifier id.sub(/^JIS /, ""),
                      **attr_code(type: "JIS", primary: "true")
  else iso_id(node, xml)
  end
end

#metadata_publisher(node, xml) ⇒ Object



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

def (node, xml)
  [{ source: ["publisher", "pub"], role: "publisher", default: pub_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
  ret.empty? and return nil
  compact_blank(ret)
end

#multiling_noko_value(value, tag, xml) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/metanorma/jis/front.rb', line 94

def multiling_noko_value(value, tag, xml)
  if value.is_a?(Hash)
    value.each do |k, v|
      xml.send tag, language: k do |x|
        x << v
      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

#ol_cleanup(doc) ⇒ Object



81
82
83
84
# File 'lib/metanorma/jis/cleanup.rb', line 81

def ol_cleanup(doc)
  ::Metanorma::Standoc::Converter.instance_method(:ol_cleanup).bind(self)
    .call(doc)
end

#org_abbrevObject



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)
  (xml, org[:logo])
end

#organization(xml, org, node = nil, default_org = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/metanorma/jis/front.rb', line 107

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



65
66
67
68
69
70
71
72
73
# File 'lib/metanorma/jis/converter.rb', line 65

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



75
76
77
78
79
80
81
82
83
84
# File 'lib/metanorma/jis/converter.rb', line 75

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

#pub_class(bib) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/metanorma/jis/cleanup.rb', line 105

def pub_class(bib)
  iso, iec, jis = pub_class_prep(bib)
  jis && iec && iso and return 2
  jis && iec and return 2
  jis && iso and return 3
  jis and return 1
  iso && iec and return 4
  iso and return 4
  iec and return 5
  6
end

#pub_class_prep(bib) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/metanorma/jis/cleanup.rb', line 92

def pub_class_prep(bib)
  iso = bib.at("#{PUBLISHER}[abbreviation = 'ISO']") ||
    bib.at("#{PUBLISHER}[name = 'International Organization " \
                             "for Standardization']")
  iec = bib.at("#{PUBLISHER}[abbreviation = 'IEC']") ||
    bib.at("#{PUBLISHER}[name = 'International " \
                             "Electrotechnical Commission']")
  jis = bib.at("#{PUBLISHER}[abbreviation = 'JIS']") ||
    bib.at("#{PUBLISHER}[name = '#{pub_hash['ja']}']") ||
    bib.at("#{PUBLISHER}[name = '#{pub_hash['en']}']")
  [iso, iec, jis]
end

#pub_hashObject



35
36
37
# File 'lib/metanorma/jis/front.rb', line 35

def pub_hash
  { "ja" => "日本工業規格", "en" => "Japanese Industrial Standards" }
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

Returns:

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



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

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



129
130
131
132
133
134
135
136
137
138
# File 'lib/metanorma/jis/front.rb', line 129

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