Class: Coradoc::Converter
- Inherits:
-
Object
- Object
- Coradoc::Converter
- Defined in:
- lib/coradoc/converter.rb
Defined Under Namespace
Modules: CommonInputOutputMethods Classes: ConverterArgumentError, NoInputPathError, NoOutputPathError, NoProcessorError
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#input ⇒ Object
Returns the value of attribute input.
-
#output ⇒ Object
Returns the value of attribute output.
Class Method Summary collapse
Instance Method Summary collapse
- #convert(data = nil) ⇒ Object
-
#initialize(input = nil, output = nil, **config) ⇒ Converter
constructor
A new instance of Converter.
- #input_processor ⇒ Object
- #output_processor ⇒ Object
Constructor Details
#initialize(input = nil, output = nil, **config) ⇒ Converter
Returns a new instance of Converter.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/coradoc/converter.rb', line 7 def initialize(input = nil, output = nil, **config) @input = input || $stdin @output = output || $stdout @config = { input_options: {}, output_options: {}, }.merge(config) yield if block_given? end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
5 6 7 |
# File 'lib/coradoc/converter.rb', line 5 def config @config end |
#input ⇒ Object
Returns the value of attribute input.
5 6 7 |
# File 'lib/coradoc/converter.rb', line 5 def input @input end |
#output ⇒ Object
Returns the value of attribute output.
5 6 7 |
# File 'lib/coradoc/converter.rb', line 5 def output @output end |
Class Method Details
.call(*args, **kwargs, &block) ⇒ Object
19 20 21 |
# File 'lib/coradoc/converter.rb', line 19 def self.call(*args, **kwargs, &block) new(*args, **kwargs, &block).convert end |
Instance Method Details
#convert(data = nil) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/coradoc/converter.rb', line 39 def convert(data = nil) input_id = input_processor.processor_id output_id = output_processor.processor_id unless data input = self.input input = File.open(input, "rb") if input.is_a? String data = input.read input_path = input.path if input.respond_to? :path end # Some input processors may prefer filenames if input_processor.respond_to? :processor_wants_filenames unless input.respond_to? :path raise NoInputPathError, "no input path given, but #{input_processor} wants that " + "form. Ensure you don't read from standard input." end data = input.path end # We may need to configure destination path. output = self.output if output.is_a? String FileUtils.mkdir_p(File.dirname(output)) output = File.open(output, "wb") end output_path = output.path if output.respond_to?(:path) = config[:input_options] = .merge(destination: output_path) if output_path = .merge(sourcedir: File.dirname(input_path)) if input_path data = input_processor.processor_execute(data, ) # Two options are possible at this point: # Either we have a document we want to write to some output, or # we have a Hash, that contains a list of files and their # documents (where a nil key denotes the main file). Let's normalize # those cases. data = { nil => data } unless data.is_a? Hash # Let's check an edge case of non-nil keys and no output path if !output_path && data.keys.any? { |i| !i.nil? } raise NoOutputPathError, "no output path given, while wanting to write multiple files" end data = output_processor.processor_execute(data, config[:output_options]) if input_processor.respond_to?(:processor_postprocess) data = input_processor.processor_postprocess( data, .merge(output_processor: output_id) ) end # Now we have all, let's write. data.each do |filename, content| if filename.nil? file = output else dirname = File.dirname(output_path) file = "#{dirname}/#{filename}" FileUtils.mkdir_p(File.dirname(file)) file = File.open(file, "wb") end file.write(content) file.close end end |
#input_processor ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/coradoc/converter.rb', line 23 def input_processor if config[:input_processor] Input[config[:input_processor]] else Input.select_processor(input) end end |
#output_processor ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/coradoc/converter.rb', line 31 def output_processor if config[:output_processor] Output[config[:output_processor]] else Output.select_processor(output) end end |