Class: Metanorma::Itu::Converter

Inherits:
Standoc::Converter
  • Object
show all
Defined in:
lib/metanorma/itu/log.rb,
lib/metanorma/itu/front.rb,
lib/metanorma/itu/front_id.rb,
lib/metanorma/itu/converter.rb,
lib/metanorma/itu/front_contrib.rb

Constant Summary collapse

ITU_LOG_MESSAGES =
{
  # rubocop:disable Naming/VariableNumber
  "ITU_1": { category: "Document Attributes",
             error: "%s is not a recognised document type",
             severity: 2 },
  "ITU_2": { category: "Document Attributes",
             error: "%s is not a recognised status",
             severity: 2 },
  "ITU_3": { category: "Document Attributes",
             error: "Title includes series name %s",
             severity: 2 },
  "ITU_4": { category: "Style",
             error: "Requirement possibly in preface: %s",
             severity: 2 },
  "ITU_6": { category: "Document Attributes",
             error: "Recommendation Status %s inconsistent with AAP",
             severity: 2 },
  "ITU_7": { category: "Document Attributes",
             error: "Recommendation Status %s inconsistent with TAP",
             severity: 2 },
  "ITU_8": { category: "Style",
             error: "%s does not match ITU document identifier conventions",
             severity: 2 },
  "ITU_9": { category: "Style",
             error: "Unnumbered clause out of place",
             severity: 2 },
  "ITU_10": { category: "Style",
              error: "No Summary has been provided",
              severity: 2 },
  "ITU_11": { category: "Style",
              error: "No Keywords have been provided",
              severity: 2 },
  "ITU_12": { category: "Style",
              error: "(terms) %s: %s",
              severity: 2 },
}.freeze
ITULANG =
{ "en" => "E", "fr" => "F", "ar" => "A", "es" => "S",
"zh" => "C", "ru" => "R" }.freeze

Instance Method Summary collapse

Instance Method Details

#abstract_parse(attrs, xml, node) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/metanorma/itu/converter.rb', line 100

def abstract_parse(attrs, xml, node)
  xml.abstract **attr_code(attrs) do |xml_section|
    # xml_section.title { |name| name << node.title }
    add_noko_elem(xml_section, "title", node.title)
    xml_section << node.content
  end
end

#clause_parse(attrs, xml, node) ⇒ Object



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

def clause_parse(attrs, xml, node)
  node.option?("unnumbered") and attrs[:unnumbered] = true
  case sectiontype1(node)
  when "conventions" then attrs = attrs.merge(type: "conventions")
  when "history", "source"
    attrs[:preface] and attrs = attrs.merge(type: sectiontype1(node))
  end
  super
end

#committee_contributors(node, xml, agency, opt) ⇒ Object



14
15
16
17
# File 'lib/metanorma/itu/front_contrib.rb', line 14

def committee_contributors(node, xml, agency, opt)
  (node) or return
  super
end

#default_publisherObject



4
5
6
# File 'lib/metanorma/itu/front_contrib.rb', line 4

def default_publisher
  @i18n.get["ITU"] || @i18n.international_telecommunication_union
end

#doc_converter(node) ⇒ Object



137
138
139
# File 'lib/metanorma/itu/converter.rb', line 137

def doc_converter(node)
  IsoDoc::Itu::WordConvert.new(doc_extract_attributes(node))
end

#doc_extract_attributes(node) ⇒ Object



117
118
119
120
# File 'lib/metanorma/itu/converter.rb', line 117

def doc_extract_attributes(node)
  super.merge(hierarchicalassets:
              node.attr("hierarchical-object-numbering"))
end

#document_scheme(node) ⇒ Object



108
109
110
# File 'lib/metanorma/itu/converter.rb', line 108

def document_scheme(node)
  super || "current"
end

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



23
24
25
# File 'lib/metanorma/itu/front_contrib.rb', line 23

def extract_org_attrs_complex(node, opts, source, suffix)
  super.merge(ident: node.attr("#{source}-acronym#{suffix}")).compact
end

#full_committee_id(contrib) ⇒ Object



27
# File 'lib/metanorma/itu/front_contrib.rb', line 27

def full_committee_id(contrib); end

#group_period(node, prefix, suffix) ⇒ Object



77
78
79
80
81
82
# File 'lib/metanorma/itu/front.rb', line 77

def group_period(node, prefix, suffix)
  s = node.attr("#{prefix}group-year-start#{suffix}") ||
    Date.today.year - (Date.today.year % 2)
  e = node.attr("#{prefix}group-year-end#{suffix}") || s.to_i + 2
  [s, e]
end

#html_converter(node) ⇒ Object



129
130
131
# File 'lib/metanorma/itu/converter.rb', line 129

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

#html_extract_attributes(node) ⇒ Object



112
113
114
115
# File 'lib/metanorma/itu/converter.rb', line 112

def html_extract_attributes(node)
  super.merge(hierarchicalassets:
              node.attr("hierarchical-object-numbering"))
end

#init(node) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/metanorma/itu/converter.rb', line 16

def init(node)
  super
  @smartquotes = node.attr("smartquotes") == "true"
  @no_insert_missing_sections = doctype(node) != "recommendation" ||
    node.attr("legacy-do-not-insert-missing-sections") ||
    node.attr("document-schema") == "legacy"
end

#init_metadata(node) ⇒ Object



24
25
26
27
# File 'lib/metanorma/itu/converter.rb', line 24

def (node)
  super
  @default_doctype = "recommendation"
end

#iso_id(node, xml) ⇒ Object



23
24
25
26
# File 'lib/metanorma/itu/front_id.rb', line 23

def iso_id(node, xml)
  add_noko_elem(xml, "docidentifier",
                node.attr("common-text-docnumber"), type: "ISO")
end

#itu_contrib_id(node) ⇒ Object



52
53
54
55
56
# File 'lib/metanorma/itu/front_id.rb', line 52

def itu_contrib_id(node)
  group = node.attr("group-acronym") ||
    node.attr("group")&.sub("Study Group ", "SG") || "XXX"
  "#{group}-C#{node.attr('docnumber')}"
end

#itu_id1(node, lang) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/metanorma/itu/front_id.rb', line 31

def itu_id1(node, lang)
  bureau = node.attr("bureau") || "T"
  id = case doctype(node)
       when "service-publication"
         itu_service_pub_id(node)
       when "contribution"
         itu_contrib_id(node)
       else
         "ITU-#{bureau} #{node.attr('docnumber')}"
       end
  itu_lang(id, lang)
end

#itu_lang(id, lang) ⇒ Object



44
45
46
# File 'lib/metanorma/itu/front_id.rb', line 44

def itu_lang(id, lang)
  id + (lang ? "-#{ITULANG[@lang]}" : "")
end

#itu_service_pub_id(node) ⇒ Object



48
49
50
# File 'lib/metanorma/itu/front_id.rb', line 48

def itu_service_pub_id(node)
  @i18n.annex_to_itu_ob_abbrev.sub(/%/, node.attr("docnumber"))
end

#log_messagesObject

rubocop:enable Naming/VariableNumber



42
43
44
# File 'lib/metanorma/itu/log.rb', line 42

def log_messages
  super.merge(ITU_LOG_MESSAGES)
end

#metadata_committee_prep(node) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/metanorma/itu/front_contrib.rb', line 29

def (node)
  a = node.attributes.dup
  a.each do |k, v|
    /group(type|acronym)/.match?(k) and
      node.set_attr(k.sub(/group(type|acronym)/, "group-\\1"), v)
    /group(yearstart|yearend)/.match?(k) and
      node.set_attr(k.sub(/groupyear(start|end)/, "group-year-\\1"), v)
  end
end

#metadata_committee_types(_node) ⇒ Object



19
20
21
# File 'lib/metanorma/itu/front_contrib.rb', line 19

def (_node)
  %w(bureau sector group subgroup workgroup)
end

#metadata_contribution(node, xml) ⇒ Object



134
135
136
137
138
139
# File 'lib/metanorma/itu/front.rb', line 134

def (node, xml)
  %w(timing).each do |k|
    add_noko_elem(xml, k, node.attr(k))
    # a = node.attr(k) and xml.send k, a
  end
end

#metadata_ext(node, xml) ⇒ Object



173
174
175
176
177
178
179
180
181
# File 'lib/metanorma/itu/front.rb', line 173

def (node, xml)
  super
  (node, xml)
  (node, xml)
  (node, xml)
  (node, xml)
  (node, xml)
  (node, xml)
end

#metadata_id_docidentifier(node, xml) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/metanorma/itu/front_id.rb', line 67

def (node, xml)
  super
  add_noko_elem(xml, "docidentifier",
                itu_lang(node.attr("docidentifier"), true),
                boilerplate: true,
                type: "ITU-lang")
end

#metadata_id_nonprimary(node, xml) ⇒ Object



4
5
6
7
8
9
# File 'lib/metanorma/itu/front_id.rb', line 4

def (node, xml)
  provisional_id(node, xml)
  td_id(node, xml)
  recommendation_id(node, xml)
  iso_id(node, xml)
end

#metadata_id_primary(node, xml) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/metanorma/itu/front_id.rb', line 58

def (node, xml)
  node.attr("docnumber") or return
  add_noko_elem(xml, "docidentifier",
                itu_id1(node, false),
                type: "ITU", primary: "true")
  add_noko_elem(xml, "docidentifier",
                itu_id1(node, true), type: "ITU-lang")
end

#metadata_id_primary_type(_node) ⇒ Object



75
76
77
# File 'lib/metanorma/itu/front_id.rb', line 75

def (_node)
  "ITU"
end

#metadata_ip_notice(node, xml) ⇒ Object



109
110
111
112
113
# File 'lib/metanorma/itu/front.rb', line 109

def (node, xml)
  add_noko_elem(xml, "ip-notice-received",
                node.attr("ip-notice-received") || "false")
  # xml.ip_notice_received (node.attr("ip-notice-received") || "false")
end

#metadata_keywords(node, xml) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/metanorma/itu/converter.rb', line 81

def (node, xml)
  node.attr("keywords") or return
  node.attr("keywords").split(/, */).sort.each_with_index do |kw, i|
    kw_out = i.zero? ? Metanorma::Utils.strict_capitalize_first(kw) : kw
    add_noko_elem(xml, "keyword", kw_out)
    # xml.keyword kw_out
  end
end

#metadata_meeting(mtg, acronym, xml) ⇒ Object



127
128
129
130
131
132
# File 'lib/metanorma/itu/front.rb', line 127

def (mtg, acronym, xml)
  add_noko_elem(xml, "meeting", mtg, acronym: acronym)
  # xml.meeting **attr_code(acronym: acronym) do |m|
  #  m << mtg
  # end
end

#metadata_meeting_date(val, xml) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/metanorma/itu/front.rb', line 141

def (val, xml)
  xml.meeting_date do |m|
    d = val.split("/")
    if d.size > 1
      add_noko_elem(m, "from", d[0])
      # m.from d[0]
      add_noko_elem(m, "to", d[1])
      # m.to d[1]
    else
      add_noko_elem(m, "on", d[0])
      # m.on d[0]
    end
  end
end

#metadata_question(node, xml) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/metanorma/itu/front.rb', line 63

def (node, xml)
  vals = csv_split(node.attr("question"), ",").map do |s1|
    t, v = s1.split(":", 2).map(&:strip)
    { id: t, value: v }
  end
  vals.each do |v|
    xml.question do |q|
      add_noko_elem(q, "identifier", v[:id])
      # q.identifier a
      add_noko_elem(q, "name", v[:value]) # q.name a
    end
  end
end

#metadata_recommendationstatus(node, xml) ⇒ Object



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

def (node, xml)
  node.attr("recommendation-from") || node.attr("approval-process") or
    return
  xml.recommendationstatus do |s|
    add_noko_elem(s, "from", node.attr("recommendation-from"))
    add_noko_elem(s, "to", node.attr("recommendation-to"))
    node.attr("approval-process") and
      add_noko_elem(s, "approvalstage", node.attr("approval-status"),
                    process: node.attr("approval-process"))
    # s.approvalstage **{ process: node.attr("approval-process") } do |x|
    #  x << node.attr("approval-status")
    # end
  end
end

#metadata_series(node, xml) ⇒ Object



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

def (node, xml)
  { series: "main", series1: "secondary", series2: "tertiary" }
    .each do |k, v|
    node.attr(k.to_s) and
      xml.series **{ type: v } do |s|
        add_noko_elem(s, "title", node.attr(k.to_s))
      end
  end
end

#metadata_status(node, xml) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/metanorma/itu/front.rb', line 8

def (node, xml)
  stage = node.attr("status") || node.attr("docstage") || "published"
  stage = "draft" if node.attributes.has_key?("draft")
  xml.status do |s|
    add_noko_elem(s, "stage", stage, abbreviation: node.attr("docstage-abbrev"))
  end
end

#metadata_studyperiod(node, xml) ⇒ Object



165
166
167
168
169
170
171
# File 'lib/metanorma/itu/front.rb', line 165

def (node, xml)
  s, e = group_period(node, "", "")
  xml.studyperiod do |p|
    add_noko_elem(p, "start", s.to_s)
    add_noko_elem(p, "end", e.to_s)
  end
end

#metadata_techreport(node, xml) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/metanorma/itu/front.rb', line 115

def (node, xml)
  a = node.attr("meeting") and
    (a, node.attr("meeting-acronym"), xml)
  add_noko_elem(xml, "meeting_place", node.attr("meeting-place"))
  # a = node.attr("meeting-place") and xml.meeting_place a
  a = node.attr("meeting-date") and (a, xml)
  add_noko_elem(xml, "intended_type", node.attr("intended-type"))
  # a = node.attr("intended-type") and xml.intended_type a
  add_noko_elem(xml, "source", node.attr("source"))
  # a = node.attr("source") and xml.source a
end

#ol_attrs(node) ⇒ Object



29
30
31
32
33
# File 'lib/metanorma/itu/converter.rb', line 29

def ol_attrs(node)
  ret = super
  ret.delete(:type)
  ret.merge(class: node.attr("class"))
end

#org_abbrevObject



8
9
10
11
12
# File 'lib/metanorma/itu/front_contrib.rb', line 8

def org_abbrev
  if @i18n.get["ITU"]
    { @i18n.international_telecommunication_union => @i18n.get["ITU"] }
  else {} end
end

#org_attrs_add_committees(node, ret, opts, opts_orig) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/metanorma/itu/front_contrib.rb', line 39

def org_attrs_add_committees(node, ret, opts, opts_orig)
  opts_orig[:groups]&.each_with_index do |g, i|
    i.zero? and next
    contributors_committees_pad_multiples(ret.first, node, g)
    opts = committee_contrib_org_prep(node, g, nil, opts_orig)
    ret << org_attrs_parse_core(node, opts).map do |x|
      x.merge(subdivtype: opts[:subdivtype])
    end
  end
  contributors_committees_nest1(ret)
end

#other_title_defaultlang(node, xml, type) ⇒ Object



50
51
52
53
# File 'lib/metanorma/itu/front.rb', line 50

def other_title_defaultlang(node, xml, type)
  a = node.attr(type) || node.attr("#{type}-#{@lang}")
  add_title_xml(xml, a, @lang, type.sub(/-title/, ""))
end

#other_title_otherlangs(node, xml, type) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/metanorma/itu/front.rb', line 55

def other_title_otherlangs(node, xml, type)
  node.attributes.each do |k, v|
    m = /^#{type}-(?<lang>.+)$/.match(k) or next
    m[:lang] == @lang and next
    add_title_xml(xml, v, m[:lang], type.sub(/-title/, ""))
  end
end

#outputs(node, ret) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/metanorma/itu/converter.rb', line 35

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")
  doc_converter(node).convert("#{@filename}.presentation.xml",
                              nil, false, "#{@filename}.doc")
  node.attr("no-pdf") or
    pdf_converter(node)&.convert("#{@filename}.presentation.xml",
                                 nil, false, "#{@filename}.pdf")
end

#pdf_converter(node) ⇒ Object



133
134
135
# File 'lib/metanorma/itu/converter.rb', line 133

def pdf_converter(node)
  IsoDoc::Itu::PdfConvert.new(pdf_extract_attributes(node))
end

#personal_role(node, contrib, suffix) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/metanorma/itu/front.rb', line 156

def personal_role(node, contrib, suffix)
  if node.attr("role#{suffix}")&.downcase == "rapporteur"
    add_noko_elem(contrib, "role", "raporteur", type: "editor")
    # contrib.role "raporteur", **{ type: "editor" }
  else
    super
  end
end

#presentation_xml_converter(node) ⇒ Object



122
123
124
125
126
127
# File 'lib/metanorma/itu/converter.rb', line 122

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

#provisional_id(node, xml) ⇒ Object



11
12
13
14
15
# File 'lib/metanorma/itu/front_id.rb', line 11

def provisional_id(node, xml)
  node.attr("provisional-name") or return
  add_noko_elem(xml, "docidentifier",
                node.attr("provisional-name"), type: "ITU-provisional")
end

#recommendation_id(node, xml) ⇒ Object



79
80
81
82
83
84
# File 'lib/metanorma/itu/front_id.rb', line 79

def recommendation_id(node, xml)
  node.attr("recommendationnumber") or return
  node.attr("recommendationnumber").split("/").each do |s|
    add_noko_elem(xml, "docidentifier", s, type: "ITU-Recommendation")
  end
end

#relaton_relationsObject



16
17
18
# File 'lib/metanorma/itu/front.rb', line 16

def relaton_relations
  super + %w(complements)
end

#sectiontype(node, level = true) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/metanorma/itu/converter.rb', line 59

def sectiontype(node, level = true)
  ret = super
  hdr = sectiontype_streamline(node.attr("heading")&.downcase)
  return nil if ret == "terms and definitions" &&
    hdr != "terms and definitions" && node.level > 1
  return nil if ret == "symbols and abbreviated terms" &&
    hdr != "symbols and abbreviated terms" && node.level > 1

  ret
end

#sectiontype_streamline(ret) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/metanorma/itu/converter.rb', line 47

def sectiontype_streamline(ret)
  case ret
  when "definitions", "terms defined elsewhere",
    "terms defined in this recommendation"
    "terms and definitions"
  when "abbreviations and acronyms" then "symbols and abbreviated terms"
  when "references" then "normative references"
  else
    super
  end
end

#structured_id(node, xml) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/metanorma/itu/front_id.rb', line 86

def structured_id(node, xml)
  node.attr("docnumber") or return
  xml.structuredidentifier do |i|
    add_noko_elem(i, "bureau", node.attr("bureau") || "T")
    add_noko_elem(i, "docnumber", node.attr("docnumber"))
    add_noko_elem(i, "annexid", node.attr("annexid"))
    add_noko_elem(i, "amendment", node.attr("amendment-number"))
    add_noko_elem(i, "corrigendum", node.attr("corrigendum-number"))
  end
end

#td_id(node, xml) ⇒ Object



17
18
19
20
21
# File 'lib/metanorma/itu/front_id.rb', line 17

def td_id(node, xml)
  node.attr("td-number") or return
  add_noko_elem(xml, "docidentifier",
                node.attr("td-number"), type: "ITU-TemporaryDocument")
end

#term_def_subclause_parse(attrs, xml, node) ⇒ Object



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

def term_def_subclause_parse(attrs, xml, node)
  case sectiontype1(node)
  when "terms defined in this recommendation"
    term_def_parse(attrs.merge(type: "internal"), xml, node, false)
  when "terms defined elsewhere"
    term_def_parse(attrs.merge(type: "external"), xml, node, false)
  else
    super
  end
end

#title(node, xml) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/metanorma/itu/front.rb', line 39

def title(node, xml)
  title_defaultlang(node, xml)
  title_otherlangs(node, xml)
  title_fallback(node, xml)
  %w(subtitle amendment-title corrigendum-title collection-title
     slogan-title).each do |t|
    other_title_defaultlang(node, xml, t)
    other_title_otherlangs(node, xml, t)
  end
end

#title_defaultlang(node, xml) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/metanorma/itu/front.rb', line 20

def title_defaultlang(node, xml)
  a = node.attr("title") || node.attr("title-#{@lang}") or return
  add_title_xml(xml, a, @lang, "main")
  if a = node.attr("annextitle") || node.attr("annextitle-#{@lang}")
    add_title_xml(xml, a, @lang, "annex")
  end
  title_nums(node, xml, @lang)
end

#title_otherlangs(node, xml) ⇒ Object



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

def title_otherlangs(node, xml)
  node.attributes.each do |k, v|
    /^(?:annex)?title-(?<lang>.+)$/ =~ k or next
    lang == @lang and next
    type = /^annex/.match?(k) ? "annex" : "main"
    add_title_xml(xml, v, lang, type)
    title_nums(node, xml, lang)
  end
end

#title_validate(_root) ⇒ Object



12
13
14
# File 'lib/metanorma/itu/converter.rb', line 12

def title_validate(_root)
  nil
end