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

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

Instance Method Summary collapse

Instance Method Details

#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

#doc_converter(node) ⇒ Object



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

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
10
# File 'lib/metanorma/jis/validate.rb', line 4

def doctype_validate(xmldoc)
  doctype = xmldoc&.at("//bibdata/ext/doctype")&.text
  %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

#get_typeabbr(node, amd: false) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/metanorma/jis/front.rb', line 73

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



42
43
44
45
46
47
48
# File 'lib/metanorma/jis/converter.rb', line 42

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(node, xml) ⇒ Object



82
83
84
85
86
87
# File 'lib/metanorma/jis/front.rb', line 82

def iso_id(node, xml)
  (!@amd && node.attr("docnumber")) || (@amd && node.attr("updates")) or
    return
  params = iso_id_params(node)
  iso_id_out(xml, params, true)
end

#iso_id_default(params) ⇒ Object



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

def iso_id_default(params)
  Pubid::Jis::Identifier.create(**params)
rescue StandardError => e
  clean_abort("Document identifier: #{e}", xml)
end

#iso_id_out(xml, params, _with_prf) ⇒ Object



117
118
119
120
# File 'lib/metanorma/jis/front.rb', line 117

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(node) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/metanorma/jis/front.rb', line 89

def iso_id_params(node)
  params = iso_id_params_core(node)
  params2 = iso_id_params_add(node)
  if node.attr("updates")
    orig_id = Pubid::Jis::Identifier::Base.parse(node.attr("updates"))
    orig_id.edition ||= 1
  end
  iso_id_params_resolve(params, params2, node, orig_id)
end

#iso_id_params_add(node) ⇒ Object



112
113
114
115
# File 'lib/metanorma/jis/front.rb', line 112

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

#iso_id_params_core(node) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/metanorma/jis/front.rb', line 99

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



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

def (node, xml)
  publishers = node.attr("publisher") || "JIS"
  csv_split(publishers)&.each do |p|
    xml.contributor do |c|
      c.role type: "author"
      c.organization do |a|
        organization(a, p, false, node, !node.attr("publisher"))
      end
    end
  end
  node.attr("doctype") == "expert-commentary" and
    personal_author(node, xml)
end


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/metanorma/jis/front.rb', line 36

def (node, xml)
  pub = node.attr("copyright-holder") || node.attr("publisher") || "JIS"
  csv_split(pub)&.each do |p|
    xml.copyright do |c|
      c.from (node.attr("copyright-year") || Date.today.year)
      c.owner do |owner|
        owner.organization do |o|
          organization(o, p, true, node,
                       !node.attr("copyright-holder") ||
                       node.attr("publisher"))
        end
      end
    end
  end
end

#metadata_id(node, xml) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/metanorma/jis/front.rb', line 63

def (node, xml)
  node.attr("docidentifier") || node.attr("docnumber") or
    @fatalerror << "No docnumber attribute supplied"
  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



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

def (node, xml)
  publishers = node.attr("publisher") || "JIS"
  csv_split(publishers)&.each do |p|
    xml.contributor do |c|
      c.role type: "publisher"
      c.organization do |a|
        organization(a, p, true, node, !node.attr("publisher"))
      end
    end
  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

#org_abbrevObject



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

def org_abbrev
  super.merge("Japanese Industrial Standards" => "JIS")
end

#pdf_converter(node) ⇒ Object



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

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



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

def presentation_xml_converter(node)
  if node.nil?
    IsoDoc::JIS::PresentationXMLConvert.new({})
  else
    IsoDoc::JIS::PresentationXMLConvert.new(doc_extract_attributes(node))
  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

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



12
13
14
15
16
17
# File 'lib/metanorma/jis/validate.rb', line 12

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



52
53
54
55
56
57
58
59
60
61
# File 'lib/metanorma/jis/front.rb', line 52

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



19
20
21
22
23
# File 'lib/metanorma/jis/validate.rb', line 19

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