Class: Asciidoctor::Csd::Converter

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

Overview

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

Constant Summary collapse

ONE_SYMBOLS_WARNING =
"ISO style: only one Symbols and Abbreviated "\
"Terms section in the standard".freeze
NON_DL_SYMBOLS_WARNING =
"ISO style: Symbols and Abbreviated Terms can "\
"only contain a definition list".freeze
SEQ =

spec of permissible section sequence we skip normative references, it goes to end of list

[
    {
      msg: "Initial section must be (content) Foreword",
      val:  [{ tag: "foreword", title: "Foreword" }],
    },
    {
      msg: "Prefatory material must be followed by (clause) Scope",
      val:  [{ tag: "introduction", title: "Introduction" },
             { tag: "clause", title: "Scope" }],
    },
    {
      msg: "Prefatory material must be followed by (clause) Scope",
      val: [{ tag: "clause", title: "Scope" }],
    },
    {
      msg: "Normative References must be followed by "\
      "Terms and Definitions",
      val: [
        { tag: "terms", title: "Terms and definitions" },
        { tag: "clause", title: "Terms and definitions" },
        {
          tag: "terms",
          title: "Terms, definitions, symbols and abbreviated terms",
        },
        {
          tag: "clause",
          title: "Terms, definitions, symbols and abbreviated terms",
        },
      ],
    },
].freeze
SECTIONS_XPATH =
"//foreword | //introduction | //sections/terms | .//annex | "\
"//sections/definitions | //sections/clause | //references[not(parent::clause)] | "\
"//clause[descendant::references][not(parent::clause)]".freeze

Instance Method Summary collapse

Constructor Details

#initialize(backend, opts) ⇒ Converter

Returns a new instance of Converter.



21
22
23
# File 'lib/asciidoctor/csd/converter.rb', line 21

def initialize(backend, opts)
  super
end

Instance Method Details

#bibdata_validate(doc) ⇒ Object



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

def bibdata_validate(doc)
  doctype_validate(doc)
  stage_validate(doc)
end

#content_validate(doc) ⇒ Object



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

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

#doctype(node) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/asciidoctor/csd/front.rb', line 70

def doctype(node)
  d = node.attr("doctype")
  unless ::Metanorma::Csd::DOCSUFFIX.keys.include?(d)
    warn "#{d} is not a legal document type: reverting to 'standard'"
    d = "standard"
  end
  d
end

#doctype_validate(xmldoc) ⇒ Object



14
15
16
17
18
19
# File 'lib/asciidoctor/csd/validate.rb', line 14

def doctype_validate(xmldoc)
  doctype = xmldoc&.at("//bibdata/ext/doctype")&.text
  %w(directive guide specification standard report administrative amendment
  technical-corrigendum advisory).include? doctype or
    warn "Document Attributes: #{doctype} is not a recognised document type"
end

#document(node) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/asciidoctor/csd/converter.rb', line 38

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
    word_converter(node).convert filename
    pdf_converter(node).convert filename
  end
  @files_to_delete.each { |f| FileUtils.rm f }
  ret
end

#html_converter(node) ⇒ Object



71
72
73
# File 'lib/asciidoctor/csd/converter.rb', line 71

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

#makexml(node) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/asciidoctor/csd/converter.rb', line 25

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

#metadata_author(node, xml) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/asciidoctor/csd/front.rb', line 10

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

#metadata_committee(node, xml) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/asciidoctor/csd/front.rb', line 29

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


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

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 "CalConnect"
      end
    end
  end
end

#metadata_id(node, xml) ⇒ Object



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

def (node, xml)
  id = node.attr("docnumber") || "???"
  prefix = prefix_id(node)
  id = "#{prefix} #{id}"
  year = node.attr("copyright-year")
  id += ":#{year}" if year
  xml.docidentifier id, **{type: "csd"}
  xml.docnumber node.attr("docnumber")
end

#metadata_publisher(node, xml) ⇒ Object



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

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

#metadata_status(node, xml) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/asciidoctor/csd/front.rb', line 43

def (node, xml)
  status = node.attr("status")
  unless status && ::Metanorma::Csd::DOCSTATUS.keys.include?(status)
    warn "#{status} is not a legal status"
  end
  super
end

#pdf_converter(node) ⇒ Object



75
76
77
# File 'lib/asciidoctor/csd/converter.rb', line 75

def pdf_converter(node)
  IsoDoc::Csd::PdfConvert.new(html_extract_attributes(node))
end

#prefix_id(node) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/asciidoctor/csd/front.rb', line 51

def prefix_id(node)
  prefix = "CC"
  typesuffix = ::Metanorma::Csd::DOCSUFFIX[node.attr("doctype")] || ""
  prefix += "/#{typesuffix}" unless typesuffix.empty?
  status = ::Metanorma::Csd::DOCSTATUS[node.attr("status")] || ""
  prefix += "/#{status}" unless status.empty?
  prefix
end

#section_validate(doc) ⇒ Object



6
7
8
9
10
11
# File 'lib/asciidoctor/csd/validate_section.rb', line 6

def section_validate(doc)
  advisory = doc.root.at("//bibdata/ext[doctype = 'advisory']")
  symbols_validate(doc.root) unless advisory
  sections_sequence_validate(doc.root) unless advisory
  super
end

#sections_cleanup(x) ⇒ Object



60
61
62
63
64
65
# File 'lib/asciidoctor/csd/converter.rb', line 60

def sections_cleanup(x)
  super
  x.xpath("//*[@inline-header]").each do |h|
    h.delete("inline-header")
  end
end

#sections_sequence_validate(root) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/asciidoctor/csd/validate_section.rb', line 80

def sections_sequence_validate(root)
  f = root.xpath(SECTIONS_XPATH)
  names = f.map { |s| { tag: s.name, title: s&.at("./title")&.text } }
  names = seqcheck(names, SEQ[0][:msg], SEQ[0][:val]) || return
  n = names[0]
  names = seqcheck(names, SEQ[1][:msg], SEQ[1][:val]) || return
  if n == { tag: "introduction", title: "Introduction" }
    names = seqcheck(names, SEQ[2][:msg], SEQ[2][:val]) || return
  end
  names = seqcheck(names, SEQ[3][:msg], SEQ[3][:val]) || return
  n = names.shift
  if n == { tag: "definitions", title: nil }
    n = names.shift || return
  end
  unless n
    warn "ISO style: Document must contain at least one clause"
    return
  end
  n[:tag] == "clause" ||
    warn("ISO style: Document must contain clause after "\
         "Terms and Definitions")
  n == { tag: "clause", title: "Scope" } &&
    warn("ISO style: Scope must occur before Terms and Definitions")
  n = names.shift || return
  while n[:tag] == "clause"
    n[:title] == "Scope" &&
      warn("ISO style: Scope must occur before Terms and Definitions")
    n = names.shift || return
  end
  unless n[:tag] == "annex" || n[:tag] == "references"
    warn "ISO style: Only annexes and references can follow clauses"
  end
  while n[:tag] == "annex"
    n = names.shift
    if n.nil?
      warn("ISO style: Document must include (references) "\
           "Normative References")
      return
    end
  end
  n == { tag: "references", title: "Normative References" } ||
    warn("ISO style: Document must include (references) "\
         "Normative References")
  n = names.shift
  n == { tag: "references", title: "Bibliography" } ||
    warn("ISO style: Final section must be (references) Bibliography")
  names.empty? ||
    warn("ISO style: There are sections after the final Bibliography")
end

#seqcheck(names, msg, accepted) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/asciidoctor/csd/validate_section.rb', line 31

def seqcheck(names, msg, accepted)
  n = names.shift
  unless accepted.include? n
    warn "ISO style: #{msg}"
    names = []
  end
  names
end

#stage_validate(xmldoc) ⇒ Object



21
22
23
24
25
26
# File 'lib/asciidoctor/csd/validate.rb', line 21

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



67
68
69
# File 'lib/asciidoctor/csd/converter.rb', line 67

def style(n, t)
  return
end

#style_warning(node, msg, text) ⇒ Object



130
131
132
133
134
135
# File 'lib/asciidoctor/csd/validate_section.rb', line 130

def style_warning(node, msg, text)
  return if @novalid
  w = "ISO style: WARNING (#{Standoc::Utils::current_location(node)}): #{msg}"
  w += ": #{text}" if text
  warn w
end

#symbols_validate(root) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/asciidoctor/csd/validate_section.rb', line 19

def symbols_validate(root)
  f = root.xpath("//definitions")
  f.empty? && return
  (f.size == 1) || warn(ONE_SYMBOLS_WARNING)
  f.first.elements.each do |e|
    unless e.name == "dl"
      warn(NON_DL_SYMBOLS_WARNING)
      return
    end
  end
end

#validate(doc) ⇒ Object



54
55
56
57
58
# File 'lib/asciidoctor/csd/converter.rb', line 54

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

#word_converter(node) ⇒ Object



79
80
81
# File 'lib/asciidoctor/csd/converter.rb', line 79

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