class Sheng::Docx
Constants
- WMLFileNamePatterns
Attributes
errors[R]
Public Class Methods
new(input_file_path, params)
click to toggle source
# File lib/sheng/docx.rb, line 21 def initialize(input_file_path, params) @input_zip_file = Zip::File.new(input_file_path) @data_set = DataSet.new(params) @errors = {} rescue Zip::Error => e raise InvalidFile.new(e.message) end
Public Instance Methods
generate(path, force: false)
click to toggle source
# File lib/sheng/docx.rb, line 45 def generate(path, force: false) if File.exists?(path) && !force raise OutputPathAlreadyExists, "File at #{path} already exists" end output_buffer = generate_output_buffer if errors.present? raise MergeError.new(errors) end File.open(path, "w") { |f| f.write(output_buffer.string) } end
required_hash()
click to toggle source
# File lib/sheng/docx.rb, line 41 def required_hash wml_files.inject({}) { |memo, wml| Sheng::Support.merge_required_hashes(memo, wml.required_hash) } end
to_tree()
click to toggle source
# File lib/sheng/docx.rb, line 37 def to_tree wml_files.map { |wml| { :file => wml.filename, :tree => wml.to_tree } } end
wml_files()
click to toggle source
# File lib/sheng/docx.rb, line 29 def wml_files @wml_files ||= @input_zip_file.entries.map do |entry| if is_wml_file?(entry.name) WMLFile.new(entry.name, entry.get_input_stream) end end.compact end
Private Instance Methods
generate_output_buffer()
click to toggle source
# File lib/sheng/docx.rb, line 61 def generate_output_buffer Zip::OutputStream.write_buffer do |out| begin @input_zip_file.entries.each do |entry| write_converted_zip_file_to_buffer(entry, out) end ensure out.close_buffer end end end
is_wml_file?(file_name)
click to toggle source
# File lib/sheng/docx.rb, line 85 def is_wml_file?(file_name) WMLFileNamePatterns.any? { |regex| file_name.match(regex) } end
write_converted_zip_file_to_buffer(entry, buffer)
click to toggle source
# File lib/sheng/docx.rb, line 73 def write_converted_zip_file_to_buffer(entry, buffer) contents = entry.get_input_stream.read buffer.put_next_entry(entry.name) if is_wml_file?(entry.name) wml_file = WMLFile.new(entry.name, contents) buffer.write wml_file.interpolate(@data_set) errors.merge!(wml_file.errors) else buffer.write contents end end