Class: Metanorma::UN::Converter

Inherits:
Standoc::Converter
  • Object
show all
Defined in:
lib/metanorma/un/validate.rb,
lib/metanorma/un/converter.rb

Constant Summary collapse

XML_ROOT_TAG =
"un-standard".freeze
XML_NAMESPACE =
"https://www.metanorma.org/ns/un".freeze
PREFACE_CLAUSE_NAMES =
%w(donotrecognise-foreword
acknowledgements).freeze

Instance Method Summary collapse

Instance Method Details

#admonition_attrs(node) ⇒ Object



221
222
223
224
# File 'lib/metanorma/un/converter.rb', line 221

def admonition_attrs(node)
  attr_code(super.merge(unnumbered: node.option?("unnumbered"),
                        subsequence: node.attr("subsequence")))
end

#asciidoc_sub(text) ⇒ Object



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

def asciidoc_sub(text)
  Metanorma::Utils::asciidoc_sub(text)
end

#bibdata_validate(doc) ⇒ Object



9
10
11
# File 'lib/metanorma/un/validate.rb', line 9

def bibdata_validate(doc)
  stage_validate(doc)
end

#boilerplate_file(_xmldoc) ⇒ Object



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

def boilerplate_file(_xmldoc)
  File.join(@libdir, "boilerplate.adoc")
end

#content_validate(doc) ⇒ Object



4
5
6
7
# File 'lib/metanorma/un/validate.rb', line 4

def content_validate(doc)
  super
  bibdata_validate(doc.root)
end

#default_publisherObject



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

def default_publisher
  "United Nations"
end

#doc_converter(node) ⇒ Object



177
178
179
# File 'lib/metanorma/un/converter.rb', line 177

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

#doc_extract_attributes(node) ⇒ Object



163
164
165
# File 'lib/metanorma/un/converter.rb', line 163

def doc_extract_attributes(node)
  super.merge(toc: node.attributes.has_key?("toc"))
end

#doctype(node) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/metanorma/un/converter.rb', line 126

def doctype(node)
  d = super
  unless %w{plenary recommendation addendum communication corrigendum
            reissue agenda budgetary sec-gen-notes expert-report
            resolution plenary-attachment}.include? d
    @log.add("Document Attributes", nil,
             "#{d} is not a legal document type: reverting to '#{@default_doctype}'")
    d = @default_doctype
  end
  d
end

#html_converter(node) ⇒ Object



173
174
175
# File 'lib/metanorma/un/converter.rb', line 173

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

#html_extract_attributes(node) ⇒ Object



159
160
161
# File 'lib/metanorma/un/converter.rb', line 159

def html_extract_attributes(node)
  super.merge(toc: node.attributes.has_key?("toc"))
end

#init_misc(node) ⇒ Object



121
122
123
124
# File 'lib/metanorma/un/converter.rb', line 121

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

#makexml(node) ⇒ Object



114
115
116
117
118
119
# File 'lib/metanorma/un/converter.rb', line 114

def makexml(node)
  @draft = node.attributes.has_key?("draft")
  @no_number_subheadings =
    node.attributes.has_key?("do-not-number-subheadings")
  super
end

#metadata_committee(node, xml) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/metanorma/un/converter.rb', line 22

def (node, xml)
  return unless node.attr("committee")

  xml.editorialgroup do |a|
    a.committee node.attr("committee"),
                **attr_code(type: node.attr("committee-type"))
    i = 2
    while node.attr("committee_#{i}")
      a.committee node.attr("committee_#{i}"),
                  **attr_code(type: node.attr("committee-type_#{i}"))
      i += 1
    end
  end
end

#metadata_distribution(node, xml) ⇒ Object



67
68
69
# File 'lib/metanorma/un/converter.rb', line 67

def (node, xml)
  xml.distribution node.attr("distribution") if node.attr("distribution")
end

#metadata_ext(node, xml) ⇒ Object



103
104
105
106
107
108
# File 'lib/metanorma/un/converter.rb', line 103

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

#metadata_id(node, xml) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/metanorma/un/converter.rb', line 54

def (node, xml)
  dn = node.attr("docnumber")
  if docstatus = node.attr("status")
    abbr = IsoDoc::UN::Metadata.new("en", "Latn", "", @i18n)
      .stage_abbr(docstatus)
    dn = "#{dn}(#{abbr})" unless abbr.empty?
  end
  id = node.attr("docidentifier") and dn = id
  xml.docidentifier primary: "true" do |i|
    i << dn
  end
end

#metadata_language(node, xml) ⇒ Object



92
93
94
95
96
# File 'lib/metanorma/un/converter.rb', line 92

def (node, xml)
  languages = node&.attr("language")&.split(/, */) ||
    %w(ar ru en fr zh es)
  languages.each { |l| xml.language l }
end

#metadata_session(node, xml) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/metanorma/un/converter.rb', line 71

def (node, xml)
  xml.session do |session|
    session.number node.attr("session") if node.attr("session")
    session.date node.attr("session-date") if node.attr("session-date")
    node.attr("item-number")&.split(/, */)&.each do |i|
      session.item_number i
    end
    node.attr("item-name")&.split(/, */)&.each do |i|
      session.item_name i
    end
    node.attr("subitem-name")&.split(/, */)&.each do |i|
      session.subitem_name i
    end
    node.attr("collaborator") and
      session.collaborator node.attr("collaborator")
    session.id node.attr("agenda-id") if node.attr("agenda-id")
    node.attr("item-footnote") and
      session.item_footnote node.attr("item-footnote")
  end
end

#metadata_submission_language(node, xml) ⇒ Object



98
99
100
101
# File 'lib/metanorma/un/converter.rb', line 98

def (node, xml)
  languages = node&.attr("submissionlanguage")&.split(/, */) || []
  languages.each { |l| xml.submissionlanguage l }
end

#no_number_subheadings(xmldoc) ⇒ Object



193
194
195
196
197
198
199
200
# File 'lib/metanorma/un/converter.rb', line 193

def no_number_subheadings(xmldoc)
  xmldoc.xpath("//sections/clause | " \
               "//sections/definitions | //annex").each do |s|
    s.xpath(".//clause | .//definitions").each do |c|
      c["unnumbered"] = true
    end
  end
end

#outputs(node, ret) ⇒ Object



138
139
140
141
142
143
144
145
146
147
# File 'lib/metanorma/un/converter.rb', line 138

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

#para_to_clause(xmldoc) ⇒ Object



202
203
204
205
# File 'lib/metanorma/un/converter.rb', line 202

def para_to_clause(xmldoc)
  %w(plenary agenda budgetary).include?(@doctype) or
    para_to_clause1(xmldoc)
end

#para_to_clause1(xmldoc) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/metanorma/un/converter.rb', line 207

def para_to_clause1(xmldoc)
  xmldoc.xpath("//clause/p | //annex/p").each do |p|
    cl = Nokogiri::XML::Node.new("clause", xmldoc)
    cl["id"] = p["id"]
    cl["inline-header"] = "true"
    p["id"] = "_#{UUIDTools::UUID.random_create}"
    p.replace(cl)
    p.parent = cl
    while (n = cl.next_element) && !%w(p clause).include?(n.name)
      n.parent = cl
    end
  end
end

#pdf_converter(node) ⇒ Object



181
182
183
184
185
# File 'lib/metanorma/un/converter.rb', line 181

def pdf_converter(node)
  return nil if node.attr("no-pdf")

  IsoDoc::UN::PdfConvert.new(pdf_extract_attributes(node))
end

#presentation_xml_converter(node) ⇒ Object



167
168
169
170
171
# File 'lib/metanorma/un/converter.rb', line 167

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

#sections_cleanup(xmldoc) ⇒ Object



187
188
189
190
191
# File 'lib/metanorma/un/converter.rb', line 187

def sections_cleanup(xmldoc)
  super
  no_number_subheadings(xmldoc) if @no_number_subheadings
  para_to_clause(xmldoc)
end

#sectiontype_streamline(ret) ⇒ Object



229
230
231
232
233
234
235
# File 'lib/metanorma/un/converter.rb', line 229

def sectiontype_streamline(ret)
  case ret
  when "foreword", "introduction" then "donotrecognise-foreword"
  else
    super
  end
end

#stage_validate(xmldoc) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/metanorma/un/validate.rb', line 13

def stage_validate(xmldoc)
  stage = xmldoc&.at("//bibdata/status/stage")&.text
  %w(proposal working-draft committee-draft draft-standard final-draft
     published withdrawn).include? stage or
    @log.add("Document Attributes", nil,
             "#{stage} is not a recognised status")
end

#style(_node, _text) ⇒ Object



155
156
157
# File 'lib/metanorma/un/converter.rb', line 155

def style(_node, _text)
  nil
end

#title(node, xml) ⇒ Object



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

def title(node, xml)
  ["en"].each do |lang|
    xml.title type: "main", language: lang, format: "text/plain" do |t|
      t << (asciidoc_sub(node.attr("title")) || node.title)
    end
    node.attr("subtitle") and
      xml.title type: "subtitle", language: lang,
                format: "text/plain" do |t|
        t << asciidoc_sub(node.attr("subtitle"))
      end
  end
end

#title_validate(_root) ⇒ Object



110
111
112
# File 'lib/metanorma/un/converter.rb', line 110

def title_validate(_root)
  nil
end

#validate(doc) ⇒ Object



149
150
151
152
153
# File 'lib/metanorma/un/converter.rb', line 149

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