#!/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}" ) { |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", ) { options[:version] = true } opts.on_tail("-h", "--help", "Show this message") do puts opts exit end end opt_parser.parse!(ARGV) # TODO: move this to individual Processors if options[:version] case options[:type] when :iso puts "Asciidoctor::ISO #{Asciidoctor::ISO::VERSION}/IsoDoc #{IsoDoc::VERSION}" when :gb puts "Asciidoctor::Gb #{Asciidoctor::Gb::VERSION}" when :csd puts "Asciidoctor::Csd #{Asciidoctor::Csd::VERSION}" when :csand puts "Asciidoctor::Csand #{Asciidoctor::Csand::VERSION}" when :m3d puts "Asciidoctor::M3d #{Asciidoctor::M3d::VERSION}" when :rsd puts "Asciidoctor::Rsd #{Asciidoctor::Rsd::VERSION}" end exit 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) file = File.read(options[:filename], encoding: "utf-8") isodoc = processor.input_to_isodoc(file) options[:extension_keys].each do |ext| file_extension = processor.output_formats[ext] outfilename = options[:filename].sub(/\.[^.]+$/, ".#{file_extension}") # puts "made outfilename #{outfilename}" processor.output(isodoc, outfilename, ext) end end