class AnyStyle::CLI::Commands::Base
Attributes
options[R]
output_folder[R]
Public Class Methods
new(options)
click to toggle source
# File lib/anystyle/cli/commands/base.rb, line 7 def initialize(options) @options = options end
Public Instance Methods
each_format(&block)
click to toggle source
# File lib/anystyle/cli/commands/base.rb, line 27 def each_format(&block) options[:format].each(&block) end
extsub(path, new_extname)
click to toggle source
# File lib/anystyle/cli/commands/base.rb, line 59 def extsub(path, new_extname) basename = path.basename(path.extname) path.dirname.join("#{basename}#{new_extname}") end
find(input, opts = {})
click to toggle source
# File lib/anystyle/cli/commands/base.rb, line 31 def find(input, opts = {}) AnyStyle.find(input, format: :wapiti, layout: opts[:layout], crop: opts[:crop].nil? ? nil : opts[:crop].map(&:to_i)) end
format(dataset, fmt)
click to toggle source
# File lib/anystyle/cli/commands/base.rb, line 42 def format(dataset, fmt) case fmt when 'bib' AnyStyle.parser.format_bibtex(dataset).to_s when 'csl' JSON.pretty_generate AnyStyle.parser.format_csl(dataset) when 'json' JSON.pretty_generate AnyStyle.parser.format_hash(dataset) when 'ref', 'txt' dataset.to_txt when 'xml' dataset.to_xml(indent: 2).to_s else raise ArgumentError, "format not supported: #{fmt}" end end
overwrite?()
click to toggle source
# File lib/anystyle/cli/commands/base.rb, line 23 def overwrite? !!options[:overwrite] end
parse(input)
click to toggle source
# File lib/anystyle/cli/commands/base.rb, line 38 def parse(input) AnyStyle.parse(input, format: :wapiti) end
report(error, file)
click to toggle source
# File lib/anystyle/cli/commands/base.rb, line 94 def report(error, file) STDERR.puts "Error processing `#{file}'" STDERR.puts " #{error.message}" STDERR.puts " #{error.backtrace[0]}" STDERR.puts " #{error.backtrace[1]}" STDERR.puts " ..." end
run(args, params)
click to toggle source
# File lib/anystyle/cli/commands/base.rb, line 11 def run(args, params) raise NotImplementedYet end
say(*args)
click to toggle source
# File lib/anystyle/cli/commands/base.rb, line 90 def say(*args) STDERR.puts(*args) if verbose? end
set_output_folder(path)
click to toggle source
# File lib/anystyle/cli/commands/base.rb, line 72 def set_output_folder(path) case path when nil, '-' options[:stdout] = true else @output_folder = Pathname.new(path).expand_path end ensure unless @output_folder.nil? if @output_folder.exist? raise ArgumentError, "not a directory: #{path}" unless @output_folder.directory? else @output_folder.mkdir end end end
stdout?()
click to toggle source
# File lib/anystyle/cli/commands/base.rb, line 19 def stdout? !!options[:stdout] end
transpose(path, base_path)
click to toggle source
# File lib/anystyle/cli/commands/base.rb, line 64 def transpose(path, base_path) if output_folder.nil? path else output_folder.join(path.relative_path_from(base_path)) end end
verbose?()
click to toggle source
# File lib/anystyle/cli/commands/base.rb, line 15 def verbose? !!options[:verbose] end
walk(input) { |file, path| ... }
click to toggle source
# File lib/anystyle/cli/commands/base.rb, line 102 def walk(input) path = Pathname(input).expand_path raise ArgumentError, "path does not exist: #{input}" unless path.exist? if path.directory? path.each_child do |file| begin yield file, path unless file.directory? rescue => e report e, file.relative_path_from(path) end end else begin yield path, path.dirname rescue => e report e, path.basename end end end
write(content, path, base_path)
click to toggle source
# File lib/anystyle/cli/commands/base.rb, line 123 def write(content, path, base_path) if stdout? STDOUT.puts(content) else path = transpose(path, base_path) if !overwrite? && path.exist? raise RuntimeError, "file exists, use --overwrite to force saving: #{path}" end File.write path, content end end