#!/usr/bin/env ruby require "yaml" require "optparse" require "metanorma" require "uuidtools" require "uri" registry = Metanorma::Registry.instance puts "[metanorma] detecting backends:" Metanorma::CLI::SUPPORTED_GEMS.each do |backend| begin puts backend require backend rescue LoadError puts "[metanorma] backend #{backend} not present" end end puts options = {} def uuid() UUIDTools::UUID.random_create end def extract_documents(m) return [] unless m and m["sections"] ret = [] m["sections"].each do |s| ret << s if s["file"] ret << extract_documents(s) if s["sections"] end ret.flatten end def hyperlink(link, suffix) return unless link link.sub(/\.adoc(?=$|#)/, ".#{suffix}") end def iterate(sections, options) return "" unless sections ret = "" Array(sections).each do |m| ret+= "\n" title = m["title"] && m["number"] ? "#{m['number']}. #{m['title']}" : ( m["number"] || m["title"] || "—" ) ret+= "#{title}\n" ret+= "

#{m["type"]}

\n" if m["type"] if m["file"] ret += "

" file = hyperlink(m["file"], "html") ret += "HTML |" if file file = hyperlink(m["file"], "pdf") ret += "PDF |" if file file = hyperlink(m["file"], "xml") ret += "XML " if file if options[:relaton] file = URI.escape("#{options[:relaton]}/id/#{m['number']}.xml") ret += "| Relaton XML " end ret += "

\n" end ret+= "

#{m['description']}

\n" if m["description"] if m["revdate"] if m["status"] ret+= "

STAGE: #{m["status"]} (#{m['revdate']})

\n" else ret+= "

#{m['revdate']}

\n" end else if m["status"] ret+= "

STAGE: #{m["status"]}

\n" end end ret += iterate(m["sections"], options) ret+= "
\n" end ret end opt_parser = OptionParser.new do |opts| opts.banner += " " opts.on( '-t', '--type TYPE', "Type of standard to generate" ) { |v| options[:type] = v.to_sym } opts.on( '-r', '--require LIBRARY', 'Require LIBRARY prior to execution' ) { |v| options[:require] ||= [] options[:require] << v } opts.on( '-R dir', '--relaton', "Insert links to relaton XML, generated by relaton-server-csd, with root directory dir (/id/xxxx is supplied)" ) { |v| options[:relaton] = v } opts.on_tail("-h", "--help", "Show this message") do puts opts exit end end opt_parser.parse!(ARGV) options[:filename] = ARGV.pop if options[:require] options[:require].each do |r| require r end end manifest = YAML.load_file(options[:filename]) documents = extract_documents(manifest) # TODO real lookup of namespaces and root elements out = <<~END <#{options[:type]}-standard xmlns="http://riboseinc.com/isoxml"> #{manifest["title"]} END # TODO leave in anchor references? # documents.each do |d| out += <<~END #{d['title']} #{d['number']} #{d['description']} END end out += <<~END END out+=iterate(manifest["sections"], options) out+= <<~END <#{options[:type]}-standard> END outfilename = options[:filename].sub(/\.[^.]+$/, ".xml") File.open(outfilename, "w") { |f| f.write out } processor = registry.find_processor(options[:type].to_sym) ext = :html file_extension = "html" || processor.output_formats[ext] outfilename = options[:filename].sub(/\.[^.]+$/, ".#{file_extension}") isodoc_options = { suppressheadingnumbers: true } isodoc_options[:htmlstylesheet] = manifest["htmlstylesheet"] if manifest["htmlstylesheet"] processor.output(out, outfilename, ext, isodoc_options)