class Metanorma::Cli::Command
Public Instance Methods
collection(filename = nil)
click to toggle source
# File lib/metanorma/cli/command.rb, line 67 def collection(filename = nil) if filename opts = options opts[:format] &&= opts[:format].split(",").map &:to_sym opts[:compile] = filter_compile_options(opts) coll = Metanorma::Collection.parse filename coll.render opts else UI.say("Need to specify a file to process") end rescue ArgumentError => e UI.say e.message end
compile(file_name = nil)
click to toggle source
# File lib/metanorma/cli/command.rb, line 42 def compile(file_name = nil) if file_name && !options[:version] documents = select_wildcard_documents(file_name) || [file_name] documents.each { |document| compile_document(document, options.dup) } elsif options[:version] invoke(:version, [], type: options[:type], format: options[:format]) elsif options.keys.size >= 2 UI.say("Need to specify a file to process") else invoke :help end end
list_doctypes(type = nil)
click to toggle source
# File lib/metanorma/cli/command.rb, line 99 def list_doctypes(type = nil) processors = backend_processors if type && processors[type.to_sym] processors = { type.to_sym => processors[type.to_sym] } end print_doctypes_table(processors) end
list_extensions(type = nil)
click to toggle source
# File lib/metanorma/cli/command.rb, line 92 def list_extensions(type = nil) single_type_extensions(type) || all_type_extensions rescue LoadError UI.say("Couldn't load #{type}, please provide a valid type!") end
new(name)
click to toggle source
# File lib/metanorma/cli/command.rb, line 21 def new(name) create_new_document(name, options) end
version()
click to toggle source
# File lib/metanorma/cli/command.rb, line 84 def version Metanorma::Cli.load_flavors backend_version(options[:type]) || supported_backends rescue NameError => error UI.say(error) end
Private Instance Methods
all_type_extensions()
click to toggle source
# File lib/metanorma/cli/command.rb, line 128 def all_type_extensions Metanorma::Cli.load_flavors message = "Supported extensions per type: \n" Metanorma::Registry.instance.processors.each do |type_sym, processor| format_keys = processor.output_formats.keys message += " #{type_sym}: #{join_keys(format_keys)}.\n" end UI.say(message) end
backend_processors()
click to toggle source
# File lib/metanorma/cli/command.rb, line 146 def backend_processors @backend_processors ||= ( Metanorma::Cli.load_flavors Metanorma::Registry.instance.processors ) end
backend_version(type)
click to toggle source
# File lib/metanorma/cli/command.rb, line 140 def backend_version(type) if type UI.say(find_backend(type).version) end end
compile_document(filename, options)
click to toggle source
# File lib/metanorma/cli/command.rb, line 208 def compile_document(filename, options) Metanorma::Cli.load_flavors errors = Metanorma::Cli::Compiler.compile(filename, options) errors.each { |error| Util.log(error, :error) } exit(1) if errors.any? end
create_new_document(name, options)
click to toggle source
# File lib/metanorma/cli/command.rb, line 173 def create_new_document(name, options) Metanorma::Cli::Generator.run( name, type: options[:type], doctype: options[:doctype], template: options[:template], overwrite: options[:overwrite], ) end
find_backend(type)
click to toggle source
# File lib/metanorma/cli/command.rb, line 153 def find_backend(type) load_flavours(type) Metanorma::Registry.instance.find_processor(type&.to_sym) end
join_keys(keys)
click to toggle source
# File lib/metanorma/cli/command.rb, line 169 def join_keys(keys) [keys[0..-2].join(", "), keys.last].join(" and ") end
load_flavours(type)
click to toggle source
# File lib/metanorma/cli/command.rb, line 183 def load_flavours(type) Metanorma::Cli.load_flavors unless Metanorma::Registry.instance.find_processor(type&.to_sym) require "metanorma-#{type}" end end
print_doctypes_table(processors)
click to toggle source
# File lib/metanorma/cli/command.rb, line 190 def print_doctypes_table(processors) table_data = processors.map do |type_sym, processor| [ type_sym.to_s, processor.input_format, join_keys(processor.output_formats.keys), ] end UI.table(["Type", "Input", "Supported output format"], table_data) end
select_wildcard_documents(filename)
click to toggle source
# File lib/metanorma/cli/command.rb, line 202 def select_wildcard_documents(filename) if filename.include?("*") Dir.glob(Pathname.new(filename)) end end
single_type_extensions(type)
click to toggle source
# File lib/metanorma/cli/command.rb, line 120 def single_type_extensions(type) return false unless type format_keys = find_backend(type).output_formats.keys UI.say("Supported extensions: #{join_keys(format_keys)}.") true end
supported_backends()
click to toggle source
# File lib/metanorma/cli/command.rb, line 158 def supported_backends UI.say("Metanorma #{Metanorma::VERSION}") UI.say("Metanorma::Cli #{VERSION}") Metanorma::Cli.load_flavors Metanorma::Registry.instance.processors.map do |type, processor| UI.say(processor.version) end end