require "asciidoctor" require "asciidoctor/rsd" require "asciidoctor/standoc/converter" require "isodoc/rsd/html_convert" require "isodoc/rsd/word_convert" require "fileutils" module Asciidoctor module Rsd # A {Converter} implementation that generates RSD output, and a document # schema encapsulation of the document for validation # class Converter < Standoc::Converter register_for "rsd" def initialize(backend, opts) super @libdir = File.dirname(__FILE__) end def metadata_author(node, xml) xml.contributor do |c| c.role **{ type: "author" } c.organization do |a| a.name "Ribose" end end end def metadata_publisher(node, xml) xml.contributor do |c| c.role **{ type: "publisher" } c.organization do |a| a.name "Ribose" end end end def metadata_committee(node, xml) 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 def metadata_status(node, xml) xml.status(**{ format: "plain" }) { |s| s << node.attr("status") } end def metadata_id(node, xml) docstatus = node.attr("status") dn = node.attr("docnumber") if docstatus abbr = IsoDoc::Rsd::Metadata.new("en", "Latn", {}). status_abbr(docstatus) dn = "#{dn}(#{abbr})" unless abbr.empty? end node.attr("copyright-year") and dn += ":#{node.attr("copyright-year")}" xml.docidentifier dn, **{type: "rsd"} xml.docnumber { |i| i << node.attr("docnumber") } end def metadata_copyright(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 "Ribose" end end end end def metadata_security(node, xml) security = node.attr("security") || return xml.security security end def metadata(node, xml) super metadata_security(node, xml) end def title_validate(root) nil end def makexml(node) result = ["\n"] @draft = node.attributes.has_key?("draft") result << noko { |ixml| front node, ixml } result << noko { |ixml| middle node, ixml } result << "" result = textcleanup(result.flatten * "\n") ret1 = cleanup(Nokogiri::XML(result)) validate(ret1) ret1.root.add_namespace(nil, RSD_NAMESPACE) ret1 end def doctype(node) d = node.attr("doctype") unless %w{policy-and-procedures best-practices supporting-document report legal directives proposal standard}.include? d warn "#{d} is not a legal document type: reverting to 'standard'" d = "standard" end d end def pdf_convert(filename) url = "#{Dir.pwd}/#{filename}.html" pdfjs = File.join(File.dirname(__FILE__), 'pdf.js') system "export NODE_PATH=$(npm root --quiet -g); node #{pdfjs} file://#{url} #{filename}.pdf" end 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_convert(filename.sub(/\.xml$/, "")) unless node.attr("nodoc") end @files_to_delete.each { |f| FileUtils.rm f } ret end def validate(doc) content_validate(doc) schema_validate(formattedstr_strip(doc.dup), File.join(File.dirname(__FILE__), "rsd.rng")) end def rsd_html_path(file) File.join(File.dirname(__FILE__), File.join("html", file)) end def sections_cleanup(x) super x.xpath("//*[@inline-header]").each do |h| h.delete("inline-header") end end def style(n, t) return end def html_converter(node) IsoDoc::Rsd::HtmlConvert.new(html_extract_attributes(node)) end def word_converter(node) IsoDoc::Rsd::WordConvert.new(doc_extract_attributes(node)) end end end end