Class: Asciidoctor::Unece::Converter

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

Overview

A Converter implementation that generates RSD output, and a document schema encapsulation of the document for validation

Constant Summary collapse

XML_ROOT_TAG =
"unece-standard".freeze
XML_NAMESPACE =
"https://www.metanorma.com/ns/unece".freeze

Instance Method Summary collapse

Instance Method Details

#admonition_attrs(node) ⇒ Object



198
199
200
201
202
203
# File 'lib/asciidoctor/unece/converter.rb', line 198

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

#bibdata_validate(doc) ⇒ Object



9
10
11
# File 'lib/asciidoctor/unece/validate.rb', line 9

def bibdata_validate(doc)
  stage_validate(doc)
end

#content_validate(doc) ⇒ Object



4
5
6
7
# File 'lib/asciidoctor/unece/validate.rb', line 4

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

#doc_extract_attributes(node) ⇒ Object



167
168
169
# File 'lib/asciidoctor/unece/converter.rb', line 167

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

#doctype(node) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/asciidoctor/unece/converter.rb', line 127

def doctype(node)
  d = node.attr("doctype")
  unless %w{plenary recommendation addendum communication corrigendum reissue
    agenda budgetary sec-gen-notes expert-report resolution}.include? d
    warn "#{d} is not a legal document type: reverting to 'recommendation'"
    d = "recommendation"
  end
  d
end

#document(node) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/asciidoctor/unece/converter.rb', line 137

def document(node)
  init(node)
  ret1 = makexml(node)
  ret = ret1.to_xml(indent: 2)
  unless node.attr("nodoc") || !node.attr("docfile")
    filename = node.attr("docfile").gsub(/\.adoc/, ".xml").
      gsub(%r{^.*/}, "")
    File.open(filename, "w") { |f| f.write(ret) }
    html_converter(node).convert filename unless node.attr("nodoc")
    word_converter(node).convert filename unless node.attr("nodoc")
    pdf_converter(node).convert filename unless node.attr("nodoc")
  end
  @files_to_delete.each { |f| FileUtils.rm f }
  ret
end

#html_converter(node) ⇒ Object



171
172
173
# File 'lib/asciidoctor/unece/converter.rb', line 171

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

#html_extract_attributes(node) ⇒ Object



163
164
165
# File 'lib/asciidoctor/unece/converter.rb', line 163

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

#makexml(node) ⇒ Object



122
123
124
125
# File 'lib/asciidoctor/unece/converter.rb', line 122

def makexml(node)
  @draft = node.attributes.has_key?("draft")
  super
end

#metadata_author(node, xml) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/asciidoctor/unece/converter.rb', line 18

def (node, xml)
  xml.contributor do |c|
    c.role **{ type: "author" }
    c.organization do |a|
      a.name Metanorma::Unece::ORGANIZATION_NAME_SHORT
    end
  end
end

#metadata_committee(node, xml) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/asciidoctor/unece/converter.rb', line 36

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}") do
      a.committee node.attr("committee_#{i}"),
        **attr_code(type: node.attr("committee-type_#{i}"))
      i += 1
    end
  end
end


72
73
74
75
76
77
78
79
80
81
82
# File 'lib/asciidoctor/unece/converter.rb', line 72

def (node, xml)
  from = node.attr("copyright-year") || Date.today.year
  xml.copyright do |c|
    c.from from
    c.owner do |owner|
      owner.organization do |o|
        o.name Metanorma::Unece::ORGANIZATION_NAME_SHORT
      end
    end
  end
end

#metadata_distribution(node, xml) ⇒ Object



84
85
86
# File 'lib/asciidoctor/unece/converter.rb', line 84

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

#metadata_ext(node, xml) ⇒ Object



111
112
113
114
115
116
# File 'lib/asciidoctor/unece/converter.rb', line 111

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

#metadata_id(node, xml) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/asciidoctor/unece/converter.rb', line 62

def (node, xml)
  dn = node.attr("docnumber")
  if docstatus = node.attr("status")
    abbr = IsoDoc::Unece::Metadata.new("en", "Latn", {}).status_abbr(docstatus)
    dn = "#{dn}(#{abbr})" unless abbr.empty?
  end
  xml.docidentifier { |i| i << dn }
  xml.docnumber { |i| i << node.attr("docnumber") }
end

#metadata_language(node, xml) ⇒ Object



101
102
103
104
# File 'lib/asciidoctor/unece/converter.rb', line 101

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

#metadata_publisher(node, xml) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/asciidoctor/unece/converter.rb', line 27

def (node, xml)
  xml.contributor do |c|
    c.role **{ type: "publisher" }
    c.organization do |a|
      a.name Metanorma::Unece::ORGANIZATION_NAME_SHORT
    end
  end
end

#metadata_session(node, xml) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/asciidoctor/unece/converter.rb', line 88

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 { |i| session.item_number i }
    node&.attr("item-name")&.split(/,[ ]*/)&.each { |i| session.item_name i }
    node&.attr("subitem-name")&.split(/,[ ]*/)&.each { |i| session.subitem_name i }
    session.collaborator node.attr("collaborator") if node.attr("collaborator")
    session.id node.attr("agenda-id") if node.attr("agenda-id")
    session.item_footnote node.attr("item-footnote") if node.attr("item-footnote")
  end
end

#metadata_submission_language(node, xml) ⇒ Object



106
107
108
109
# File 'lib/asciidoctor/unece/converter.rb', line 106

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

#pdf_converter(node) ⇒ Object



179
180
181
# File 'lib/asciidoctor/unece/converter.rb', line 179

def pdf_converter(node)
  IsoDoc::Unece::PdfConvert.new(doc_extract_attributes(node))
end

#sections_cleanup(xmldoc) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/asciidoctor/unece/converter.rb', line 183

def sections_cleanup(xmldoc)
  super
  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 and !%w(p clause).include? n.name
      n.parent = cl
    end
  end
end

#stage_validate(xmldoc) ⇒ Object



13
14
15
16
17
18
# File 'lib/asciidoctor/unece/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
    warn "Document Attributes: #{stage} is not a recognised status"
end

#style(n, t) ⇒ Object



159
160
161
# File 'lib/asciidoctor/unece/converter.rb', line 159

def style(n, t)
  return
end

#title(node, xml) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/asciidoctor/unece/converter.rb', line 50

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

#title_validate(root) ⇒ Object



118
119
120
# File 'lib/asciidoctor/unece/converter.rb', line 118

def title_validate(root)
  nil
end

#validate(doc) ⇒ Object



153
154
155
156
157
# File 'lib/asciidoctor/unece/converter.rb', line 153

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

#word_converter(node) ⇒ Object



175
176
177
# File 'lib/asciidoctor/unece/converter.rb', line 175

def word_converter(node)
  IsoDoc::Unece::WordConvert.new(doc_extract_attributes(node))
end