lib/relaton/cli/relaton_file.rb in relaton-cli-0.1.8 vs lib/relaton/cli/relaton_file.rb in relaton-cli-0.1.9

- old
+ new

@@ -134,18 +134,22 @@ end.compact end def split_and_write_to_files output_dir = outdir || build_dirname(source) - write_to_collection_file(relaton_collection.to_yaml, output_dir) relaton_collection.items.each do |content| name = build_filename(nil, content.docidentifier) - write_to_file(content.to_xml, output_dir, name) + write_to_file(content.send(output_type), output_dir, name) end end + def output_type + output_format = options[:extension] || "rxl" + (output_format == "rxl" ? "to_xml" : "to_#{output_format}").to_sym + end + def bibdata_instance(document, file) document = clean_nokogiri_document(document) bibdata = Relaton::Bibdata.from_xml(document.root) build_bibdata_relaton(bibdata, file) @@ -190,14 +194,10 @@ def write_to_file(content, directory = nil, output_file = nil) file_with_dir = [directory, output_file || outfile].compact.join("/") File.open(file_with_dir, "w:utf-8") { |file| file.write(content) } end - def write_to_collection_file(content, directory) - write_to_file(content, directory, build_filename(source, nil, "yaml")) - end - def build_dirname(filename) basename = File.basename(filename)&.gsub(/.(xml|rxl)/, "") directory_name = sanitize_string(basename) Dir.mkdir(directory_name) unless File.exists?(directory_name) @@ -208,11 +208,16 @@ identifier ||= Pathname.new(File.basename(file, ".xml")).to_s [sanitize_string(identifier), options[:extension] || ext].join(".") end def sanitize_string(string) - string.downcase.gsub("/", " ").gsub(/^\s+/, "").gsub(/\s+$/, ""). - gsub(/\s+/, "-") + clean_string = replace_bad_characters(string.downcase) + clean_string.gsub(/^\s+/, "").gsub(/\s+$/, "").gsub(/\s+/, "-") + end + + def replace_bad_characters(string) + bad_chars = ["/", "\\", "?", "%", "*", ":", "|", '"', "<", ">", ".", " "] + bad_chars.inject(string.downcase) { |res, char| res.gsub(char, "-") } end end end end