class Relaton::Cli::BaseConvertor
Constants
- FILENAME_BAD_CHARS
From gavinmiller.io/2016/creating-a-secure-sanitization-function/
Attributes
file[R]
options[R]
outdir[R]
overwrite[R]
writable[R]
Public Class Methods
new(file, options = {})
click to toggle source
# File lib/relaton/cli/base_convertor.rb, line 9 def initialize(file, options = {}) @file = file @options = options @outdir = options.fetch(:outdir, nil) @writable = options.fetch(:write, true) @overwrite = options.fetch(:overwrite, true) @default_filelabel = 0 install_dependencies(options[:require] || []) end
to_html(file, style = nil, template = nil)
click to toggle source
Convert to HTML
This interface expect us to provide Relaton
collection XML as XML/RXL, and necessary styels / templates then it will be used convert that collection to HTML.
@param file [String] Relaton
collection file path @param style [String] Stylesheet file path for styles @param template [String] The liquid tempalte directory
@return [String] HTML
# File lib/relaton/cli/base_convertor.rb, line 37 def self.to_html(file, style = nil, template = nil) new( file, style: style || File.join(File.dirname(__FILE__), "../../../templates/index-style.css"), template: template || File.join(File.dirname(__FILE__), "../../../templates/"), extension: "html" ).to_html end
Public Instance Methods
to_html()
click to toggle source
@return [String] HTML
# File lib/relaton/cli/base_convertor.rb, line 21 def to_html content = convert_to_html write_to_a_file(content) end
Private Instance Methods
collection_filename(identifier)
click to toggle source
# File lib/relaton/cli/base_convertor.rb, line 127 def collection_filename(identifier) File.join( outdir, [@options[:prefix], identifier, extension].compact.join("") ) end
convert_and_write(content, format)
click to toggle source
# File lib/relaton/cli/base_convertor.rb, line 77 def convert_and_write(content, format) content = convert_content(content) write_to_a_file(item_output(content, format)) write_to_file_collection(content, format.to_sym) end
convert_to_html()
click to toggle source
@return [String] HTML
# File lib/relaton/cli/base_convertor.rb, line 51 def convert_to_html Relaton::Cli::XmlToHtmlRenderer.render( xml_content(file), stylesheet: options[:style], liquid_dir: options[:template] ) end
docidentifier_code(docidentifier)
click to toggle source
# File lib/relaton/cli/base_convertor.rb, line 115 def docidentifier_code(docidentifier) return "" if docidentifier.nil? FILENAME_BAD_CHARS.reduce(docidentifier.downcase) do |result, bad_char| result.gsub(bad_char, "-") end end
extension()
click to toggle source
# File lib/relaton/cli/base_convertor.rb, line 123 def extension @extension ||= [".", options.fetch(:extension, default_ext)].join end
extract_docid(item)
click to toggle source
# File lib/relaton/cli/base_convertor.rb, line 103 def extract_docid(item) @default_filelabel += 1 item.docidentifier.nil? && (return @default_filelabel.to_s) # item.docidentifier.is_a?(Array) or return @default_filelabel.to_s item.docidentifier.empty? && (return @default_filelabel.to_s) docidentifier_code(item.docidentifier) end
install_dependencies(dependencies)
click to toggle source
# File lib/relaton/cli/base_convertor.rb, line 66 def install_dependencies(dependencies) dependencies.each { |dependency| require(dependency) } end
item_output(content, format)
click to toggle source
# File lib/relaton/cli/base_convertor.rb, line 70 def item_output(content, format) case format.to_sym when :to_yaml then content.to_yaml when :to_xml then content.to_xml(date_format: :full, bibdata: true) end end
write_to_a_file(content, outfile = nil)
click to toggle source
# File lib/relaton/cli/base_convertor.rb, line 83 def write_to_a_file(content, outfile = nil) outfile ||= Pathname.new(file).sub_ext(extension).to_s if !File.exists?(outfile) || overwrite File.open(outfile, "w:utf-8") do |file| file.write(content) end end end
write_to_file_collection(content, format)
click to toggle source
# File lib/relaton/cli/base_convertor.rb, line 93 def write_to_file_collection(content, format) if outdir && content.is_a?(Relaton::Bibcollection) FileUtils.mkdir_p(outdir) content.items_flattened.each do |item| collection = collection_filename(extract_docid(item)) write_to_a_file(item_output(item, format), collection) end end end
xml_content(file)
click to toggle source
@param file [String] path to a file @return [String] the file's content @return [String] HTML
# File lib/relaton/cli/base_convertor.rb, line 62 def xml_content(file) File.read(file, encoding: "utf-8") end