class Asciidoctor::Csd::Converter

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

Constants

NON_DL_SYMBOLS_WARNING
ONE_SYMBOLS_WARNING
SECTIONS_XPATH
SEQ

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

XML_NAMESPACE
XML_ROOT_TAG

Public Class Methods

new(backend, opts) click to toggle source
Calls superclass method
# File lib/asciidoctor/csd/converter.rb, line 22
def initialize(backend, opts)
  super
end

Public Instance Methods

bibdata_validate(doc) click to toggle source
# File lib/asciidoctor/csd/validate.rb, line 9
def bibdata_validate(doc)
  doctype_validate(doc)
  stage_validate(doc)
end
content_validate(doc) click to toggle source
Calls superclass method
# File lib/asciidoctor/csd/validate.rb, line 4
def content_validate(doc)
  super
  bibdata_validate(doc.root)
end
doctype(node) click to toggle source
# File lib/asciidoctor/csd/front.rb, line 70
def doctype(node)
  d = node.attr("doctype")
  unless ::Metanorma::Csd::DOCSUFFIX.keys.include?(d)
    @log.add("Document Attributes", nil,
             "#{d} is not a legal document type: reverting to 'standard'")
    d = "standard"
  end
  d
end
doctype_validate(xmldoc) click to toggle source
# 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
  @log.add("Document Attributes", nil, "#{doctype} is not a recognised document type")
end
document(node) click to toggle source
# File lib/asciidoctor/csd/converter.rb, line 26
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
  @log.write(@localdir + @filename + ".err") unless @novalid
  @files_to_delete.each { |f| FileUtils.rm f }
  ret
end
html_converter(node) click to toggle source
# File lib/asciidoctor/csd/converter.rb, line 60
def html_converter(node)
  IsoDoc::Csd::HtmlConvert.new(html_extract_attributes(node))
end
metadata_author(node, xml) click to toggle source
# File lib/asciidoctor/csd/front.rb, line 10
def metadata_author(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) click to toggle source
# File lib/asciidoctor/csd/front.rb, line 29
def metadata_committee(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
metadata_id(node, xml) click to toggle source
# File lib/asciidoctor/csd/front.rb, line 60
def metadata_id(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) click to toggle source
# File lib/asciidoctor/csd/front.rb, line 20
def metadata_publisher(node, xml)
  xml.contributor do |c|
    c.role **{ type: "publisher" }
    c.organization do |a|
      a.name "CalConnect"
    end
  end
end
metadata_status(node, xml) click to toggle source
Calls superclass method
# File lib/asciidoctor/csd/front.rb, line 43
def metadata_status(node, xml)
  status = node.attr("status")
  unless status && ::Metanorma::Csd::DOCSTATUS.keys.include?(status)
    @log.add("Document Attributes", nil, "#{status} is not a legal status")
  end
  super
end
pdf_converter(node) click to toggle source
# File lib/asciidoctor/csd/converter.rb, line 64
def pdf_converter(node)
  IsoDoc::Csd::PdfConvert.new(html_extract_attributes(node))
end
prefix_id(node) click to toggle source
# 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) click to toggle source
Calls superclass method
# 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) click to toggle source
Calls superclass method
# File lib/asciidoctor/csd/converter.rb, line 49
def sections_cleanup(x)
  super
  x.xpath("//*[@inline-header]").each do |h|
    h.delete("inline-header")
  end
end
sections_sequence_validate(root) click to toggle source
# 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
    @log.add("Style", nil, "Document must contain at least one clause")
    return
  end
  n[:tag] == "clause" ||
    @log.add("Style", nil, "Document must contain clause after "\
         "Terms and Definitions")
  n == { tag: "clause", title: "Scope" } &&
    @log.add("Style", nil, "Scope must occur before Terms and Definitions")
  n = names.shift || return
  while n[:tag] == "clause"
    n[:title] == "Scope" &&
      @log.add("Style", nil, "Scope must occur before Terms and Definitions")
    n = names.shift || return
  end
  unless n[:tag] == "annex" || n[:tag] == "references"
    @log.add("Style", nil, "Only annexes and references can follow clauses")
  end
  while n[:tag] == "annex"
    n = names.shift
    if n.nil?
      @log.add("Style", nil, "Document must include (references) "\
           "Normative References")
      return
    end
  end
  n == { tag: "references", title: "Normative References" } ||
    @log.add("Style", nil, "Document must include (references) "\
         "Normative References")
  n = names.shift
  n == { tag: "references", title: "Bibliography" } ||
    @log.add("Style", nil, "Final section must be (references) Bibliography")
  names.empty? ||
    @log.add("Style", nil, "There are sections after the final Bibliography")
end
seqcheck(names, msg, accepted) click to toggle source
# File lib/asciidoctor/csd/validate_section.rb, line 31
def seqcheck(names, msg, accepted)
  n = names.shift
  unless accepted.include? n
    @log.add("Style", nil, msg)
    names = []
  end
  names
end
stage_validate(xmldoc) click to toggle source
# 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
  @log.add("Document Attributes", nil, "#{stage} is not a recognised status")
end
style(n, t) click to toggle source
# File lib/asciidoctor/csd/converter.rb, line 56
def style(n, t)
  return
end
style_warning(node, msg, text = nil) click to toggle source
# File lib/asciidoctor/csd/validate_section.rb, line 130
def style_warning(node, msg, text = nil)
  return if @novalid
  w = msg
  w += ": #{text}" if text
  @log.add("Style", node, w)
end
symbols_validate(root) click to toggle source
# File lib/asciidoctor/csd/validate_section.rb, line 19
def symbols_validate(root)
  f = root.xpath("//definitions")
  f.empty? && return
  (f.size == 1) || @log.add("Style", f.first, ONE_SYMBOLS_WARNING)
  f.first.elements.each do |e|
    unless e.name == "dl"
      @log.add("Style", f.first, NON_DL_SYMBOLS_WARNING)
      return
    end
  end
end
validate(doc) click to toggle source
# File lib/asciidoctor/csd/converter.rb, line 43
def validate(doc)
  content_validate(doc)
  schema_validate(formattedstr_strip(doc.dup),
                  File.join(File.dirname(__FILE__), "csd.rng"))
end
word_converter(node) click to toggle source
# File lib/asciidoctor/csd/converter.rb, line 68
def word_converter(node)
  IsoDoc::Csd::WordConvert.new(doc_extract_attributes(node))
end