#!/usr/bin/env ruby require "optparse" require "metanorma" registry = Metanorma::Registry.instance supported_gem_paths = [ "asciidoctor-rfc", "asciidoctor-iso", "asciidoctor-gb", "asciidoctor-csd", "asciidoctor-csand", "asciidoctor-m3d", "asciidoctor-rsd" ] puts "[metanorma] detecting backends:" supported_gem_paths.each do |backend| begin puts backend require backend rescue LoadError puts "[metanorma] backend #{backend} not present" end end puts options = { format: :asciidoc } opt_parser = OptionParser.new do |opts| opts.banner += " " opts.on( '-t', '--type TYPE', "Type of standard to generate: #{registry.supported_backends.join(", ")}" ) { |v| options[:type] = v.to_sym } opts.on( '-x', '--extensions EXT1,EXT2,...', Array, "Type of extension to generate per type: #{registry.output_formats}\n"\ "In addition, xml (outside of rfc2, rfc3) generates IsoDoc XML" ) { |v| options[:extension_keys] = v.map(&:to_sym) } opts.on( '-f', '--format FORMAT', 'Format of source file: asciidoc (current default, only format supported)' ) { |v| options[:format] = v.to_sym } opts.on( '-r', '--require LIBRARY', 'Require LIBRARY prior to execution' ) { |v| options[:require] ||= [] options[:require] << v } opts.on( '-v', '--version', "Print version of code (accompanied with -t)", ) { options[:version] = true } opts.on_tail("-h", "--help", "Show this message") do puts opts exit end end opt_parser.parse!(ARGV) if options[:version] case options[:format] when :asciidoc processor = registry.find_processor(options[:type].to_sym) puts processor.version exit end end options[:filename] = ARGV.pop unless options[:type] puts "[metanorma] Error: Please specify a standard type." puts opt_parser.help exit 0 end unless registry.supported_backends.include? options[:type] puts "[metanorma] Error: #{options[:type]} is not a supported standard type." exit 0 end unless options[:format] == :asciidoc puts "[metanorma] Error: Only source file format currently supported is 'asciidoc'." exit 0 end unless options[:filename] puts "[metanorma] Error: Need to specify a file to process." exit 0 end if options[:require] options[:require].each do |r| require r end end case options[:format] when :asciidoc processor = registry.find_processor(options[:type].to_sym) options[:extension_keys] ||= processor.output_formats.inject([]) do |memo, (k, _)| memo << k; memo end extensions = options[:extension_keys].inject([]) do |memo, e| if processor.output_formats[e] || e == :xml then memo << e else warn "[metanorma] Error: #{e} format is not supported for this standard" end memo end exit 0 if extensions.empty? file = File.read(options[:filename], encoding: "utf-8") isodoc = processor.input_to_isodoc(file) isodoc_options = processor.extract_options(file) extensions.each do |ext| file_extension = ext == :xml ? "xml" : processor.output_formats[ext] outfilename = options[:filename].sub(/\.[^.]+$/, ".#{file_extension}") if ext == :xml File.open(outfilename, "w:UTF-8") { |f| f.write(isodoc) } else processor.output(isodoc, outfilename, ext, isodoc_options) end end end