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

Instance Method Summary collapse

Instance Method Details

#admonition_attrs(node) ⇒ Object



204
205
206
207
208
209
# File 'lib/asciidoctor/unece/converter.rb', line 204

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



173
174
175
# File 'lib/asciidoctor/unece/converter.rb', line 173

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

#doctype(node) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/asciidoctor/unece/converter.rb', line 133

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



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/asciidoctor/unece/converter.rb', line 143

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



177
178
179
# File 'lib/asciidoctor/unece/converter.rb', line 177

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

#html_extract_attributes(node) ⇒ Object



169
170
171
# File 'lib/asciidoctor/unece/converter.rb', line 169

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

#makexml(node) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/asciidoctor/unece/converter.rb', line 120

def makexml(node)
  result = ["<?xml version='1.0' encoding='UTF-8'?>\n<unece-standard>"]
  @draft = node.attributes.has_key?("draft")
  result << noko { |ixml| front node, ixml }
  result << noko { |ixml| middle node, ixml }
  result << "</unece-standard>"
  result = textcleanup(result)
  ret1 = cleanup(Nokogiri::XML(result))
  validate(ret1) unless @novalid
  ret1.root.add_namespace(nil, Metanorma::Unece::DOCUMENT_NAMESPACE)
  ret1
end

#metadata_author(node, xml) ⇒ Object



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

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



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

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


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

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



82
83
84
# File 'lib/asciidoctor/unece/converter.rb', line 82

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

#metadata_ext(node, xml) ⇒ Object



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

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

#metadata_id(node, xml) ⇒ Object



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

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



99
100
101
102
# File 'lib/asciidoctor/unece/converter.rb', line 99

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



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

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



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

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



104
105
106
107
# File 'lib/asciidoctor/unece/converter.rb', line 104

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

#pdf_converter(node) ⇒ Object



185
186
187
# File 'lib/asciidoctor/unece/converter.rb', line 185

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

#sections_cleanup(xmldoc) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/asciidoctor/unece/converter.rb', line 189

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



165
166
167
# File 'lib/asciidoctor/unece/converter.rb', line 165

def style(n, t)
  return
end

#title(node, xml) ⇒ Object



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

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



116
117
118
# File 'lib/asciidoctor/unece/converter.rb', line 116

def title_validate(root)
  nil
end

#validate(doc) ⇒ Object



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

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

#word_converter(node) ⇒ Object



181
182
183
# File 'lib/asciidoctor/unece/converter.rb', line 181

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