Class: Metanorma::Bipm::Converter

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

Constant Summary collapse

BIPM_LOG_MESSAGES =
{
  # rubocop:disable Naming/VariableNumber
  "BIPM_1": { category: "Document Attributes",
              error: "%s is not a recognised committee",
              severity: 1 },

  "BIPM_2": { category: "Document Attributes",
              error: "%s is not a recognised si-aspect",
              severity: 1 },
}.freeze

Instance Method Summary collapse

Instance Method Details

#annex_attrs_preprocess(attrs, node) ⇒ Object



74
75
76
77
# File 'lib/metanorma/bipm/converter.rb', line 74

def annex_attrs_preprocess(attrs, node)
  node.option?("unnumbered") and attrs[:unnumbered] = true
  super
end

#clause_attrs_preprocess(attrs, node) ⇒ Object



69
70
71
72
# File 'lib/metanorma/bipm/converter.rb', line 69

def clause_attrs_preprocess(attrs, node)
  node.option?("unnumbered") and attrs[:unnumbered] = true
  super
end

#committee_number_or_name?(node, type, suffix) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/metanorma/bipm/front.rb', line 23

def committee_number_or_name?(node, type, suffix)
  node.attr("#{type}-number#{suffix}") || node.attr("#{type}#{suffix}") ||
    node.attr("#{type}-en#{suffix}") || node.attr("#{type}-fr#{suffix}")
end

#committee_org_prep_agency(node, type, agency, agency_arr, agency_abbr) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/metanorma/bipm/front.rb', line 28

def committee_org_prep_agency(node, type, agency, agency_arr, agency_abbr)
  i = 1
  suffix = ""
  while committee_number_or_name?(node, type, suffix)
    agency_arr << (node.attr("#{type}-agency#{suffix}") || agency)
    agency_abbr << node.attr("#{type}-agency-abbr#{suffix}")
    i += 1
    suffix = "_#{i}"
  end
  [agency_arr, agency_abbr]
end

#configurationObject



10
11
12
# File 'lib/metanorma/bipm/converter.rb', line 10

def configuration
  Metanorma::Bipm.configuration
end

#contrib_committee_subdiv(xml, comm) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/metanorma/bipm/front.rb', line 4

def contrib_committee_subdiv(xml, comm)
  contributors_committees_filter_empty?(comm) and return
  xml.subdivision **attr_code(type: comm[:subdivtype],
                              subtype: comm[:type]) do |o|
    add_noko_elem(o, "name", comm[:name])
    add_noko_elem(o, "name", comm[:name_en], language: "en")
    add_noko_elem(o, "name", comm[:name_fr], language: "fr")
    add_noko_elem(o, "abbreviation", comm[:abbr])
    add_noko_elem(o, "identifier", comm[:ident])
  end
end

#contributors_committees_filter_empty?(committee) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
# File 'lib/metanorma/bipm/front.rb', line 16

def contributors_committees_filter_empty?(committee)
  (committee[:name].nil? || committee[:name].empty?) &&
    (committee[:name_en].nil? || committee[:name_en].empty?) &&
    (committee[:name_fr].nil? || committee[:name_fr].empty?) &&
    committee[:ident].nil?
end

#date_range(date) ⇒ Object



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

def date_range(date)
  from = date.at("./from")
  to = date.at("./to")
  on = date.at("./on")
  return date.text unless from || on || to
  return on.text.sub(/-.*$/, "") if on

  ret = "#{from.text.sub(/-.*$/, '')}&#x2013;"
  ret += to.text.sub(/-.*$/, "") if to
  ret
end

#default_publisherObject



18
19
20
# File 'lib/metanorma/bipm/converter.rb', line 18

def default_publisher
  org_name_long
end

#document(node) ⇒ Object



79
80
81
82
# File 'lib/metanorma/bipm/converter.rb', line 79

def document(node)
  @jcgm = node.attr("committee-acronym") == "JCGM"
  super
end

#extract_org_attrs_complex(node, opts, source, suffix) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/metanorma/bipm/front.rb', line 40

def extract_org_attrs_complex(node, opts, source, suffix)
  ident = node.attr("#{source}-acronym#{suffix}")
  ret = super.merge(name_en: node.attr("#{source}-en#{suffix}"),
                    name_fr: node.attr("#{source}-fr#{suffix}"))
  ident and ret[:ident] = ident
  ret
end

#format_ref(ref, type) ⇒ Object



64
65
66
67
# File 'lib/metanorma/bipm/converter.rb', line 64

def format_ref(ref, type)
  ref = ref.sub(/^BIPM /, "") if type == "BIPM"
  super
end

#html_converter(node) ⇒ Object



93
94
95
# File 'lib/metanorma/bipm/converter.rb', line 93

def html_converter(node)
  IsoDoc::Bipm::HtmlConvert.new(html_extract_attributes(node))
end

#inline_anchor_xref_attrs(node) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/metanorma/bipm/converter.rb', line 40

def inline_anchor_xref_attrs(node)
  flags = %w(pagenumber nosee nopage).each_with_object({}) do |w, m|
    if /#{w}%/.match?(node.text)
      node.text = node.text.sub(/#{w}%/, "")
      m[w] = true
    end
  end
  ret = super
  flags.each_key { |k| ret[k.to_sym] = true }
  ret
end

#log_messagesObject

rubocop:enable Naming/VariableNumber



16
17
18
# File 'lib/metanorma/bipm/log.rb', line 16

def log_messages
  super.merge(BIPM_LOG_MESSAGES)
end

#metadata_relations(node, xml) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/metanorma/bipm/front.rb', line 48

def (node, xml)
  super
  relation_supersedes_self(node, xml, "")
  i = 2
  while relation_supersedes_self(node, xml, "_#{i}")
    i += 1
  end
end

#org_abbrevObject



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

def org_abbrev
  { org_name_long => configuration.organization_name_short }
end

#org_name_longObject



14
15
16
# File 'lib/metanorma/bipm/converter.rb', line 14

def org_name_long
  configuration.organization_name_long[@lang]
end

#outputs(node, ret) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/metanorma/bipm/converter.rb', line 84

def outputs(node, ret)
  File.open("#{@filename}.xml", "w:UTF-8") { |f| f.write(ret) }
  presentation_xml_converter(node).convert("#{@filename}.xml")
  html_converter(node).convert("#{@filename}.presentation.xml",
                               nil, false, "#{@filename}.html")
  pdf_converter(node)&.convert("#{@filename}.presentation.xml",
                               nil, false, "#{@filename}.pdf")
end

#pdf_converter(node) ⇒ Object



104
105
106
107
# File 'lib/metanorma/bipm/converter.rb', line 104

def pdf_converter(node)
  node.attr("no-pdf") and return nil
  IsoDoc::Bipm::PdfConvert.new(pdf_extract_attributes(node))
end

#personal_role(node, xml, suffix) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/metanorma/bipm/front.rb', line 79

def personal_role(node, xml, suffix)
  role = node.attr("role#{suffix}") || "author"
  unless %w(author editor).include?(role.downcase)
    desc = role
    role = "editor"
  end
  xml.role type: role.downcase do |d|
    d << desc # can be empty
  end
end

#presentation_xml_converter(node) ⇒ Object



97
98
99
100
101
102
# File 'lib/metanorma/bipm/converter.rb', line 97

def presentation_xml_converter(node)
  IsoDoc::Bipm::PresentationXMLConvert
    .new(html_extract_attributes(node)
    .merge(output_formats: ::Metanorma::Bipm::Processor.new
    .output_formats))
end

#relation_supersedes_self(node, xml, suffix) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/metanorma/bipm/front.rb', line 57

def relation_supersedes_self(node, xml, suffix)
  d = node.attr("supersedes-date#{suffix}")
  draft = node.attr("supersedes-draft#{suffix}")
  edition = node.attr("supersedes-edition#{suffix}")
  return false unless d || draft || edition

  relation_supersedes_self1(xml, d, edition, draft)
end

#relation_supersedes_self1(xml, date, edition, draft) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/metanorma/bipm/front.rb', line 66

def relation_supersedes_self1(xml, date, edition, draft)
  xml.relation type: "supersedes" do |r|
    r.bibitem do |b|
      add_noko_elem(b, "date", date,
                    type: edition ? "published" : "circulated")
      add_noko_elem(b, "edition", edition)
      draft and b.version do |v|
        add_noko_elem(v, "draft", draft)
      end
    end
  end
end

#sectiontype(node, level = true) ⇒ Object



34
35
36
37
38
# File 'lib/metanorma/bipm/converter.rb', line 34

def sectiontype(node, level = true)
  ret = sectiontype1(node)
  ret == "terms and definitions" and return ret
  super
end

#sectiontype_streamline(ret) ⇒ Object



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

def sectiontype_streamline(ret)
  case ret
  when "introduction" then @jcgm ? "introduction" : "clause"
  else
    super
  end
end

#title(node, xml) ⇒ Object



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

def title(node, xml)
  ["en", "fr"].each do |lang|
    add_title_xml(xml, node.attr("title-#{lang}"), lang, "title-main")
    %w(cover appendix annex part subpart provenance).each do |w|
      typed_title(node, xml, lang, w)
    end
    title_nums(node, xml, lang)
  end
end

#title_num_prefix(key, value, xml, lang) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/metanorma/bipm/front.rb', line 120

def title_num_prefix(key, value, xml, lang)
  lookup = if @document_scheme == "2019"
             { part: "level4_ancillary",
               subpart: "level5_ancillary",
               appendix: "level3_ancillary_2019",
               annex: "level2_ancillary_2019" }
           else
             { part: "level4_ancillary",
               subpart: "level5_ancillary",
               appendix: "level3_ancillary",
               annex: "level2_ancillary" }
           end
  prefix = @i18n_cache[lang].get.dig(lookup[key]) or return
  value && !value.empty? or return
  title = "#{prefix}&#xa0;#{value}"
  add_title_xml(xml, title, lang, "title-#{key}-prefix")
end

#title_nums(node, xml, lang) ⇒ Object



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

def title_nums(node, xml, lang)
  @i18n_cache ||= {}
  @i18n_cache[lang] ||= ::IsoDoc::Bipm::I18n.new(lang, ::Metanorma::Utils::default_script(lang))
  ret = title_nums_prep(node)
  ret.each do |k, v|
    title_num_prefix(k, v, xml, lang)
  end
end

#title_nums_prep(node) ⇒ Object



114
115
116
117
118
# File 'lib/metanorma/bipm/front.rb', line 114

def title_nums_prep(node)
  part, subpart = node.attr("partnumber")&.split("-")
  { part:, subpart:, appendix: node.attr("appendix-id"),
    annex: node.attr("annex-id") }
end

#typed_title(node, xml, lang, type) ⇒ Object



100
101
102
103
# File 'lib/metanorma/bipm/front.rb', line 100

def typed_title(node, xml, lang, type)
  title = node.attr("title-#{type}-#{lang}") or return
  add_title_xml(xml, title, lang, "title-#{type}")
end