class DocxTemplate::Docx
Constants
- DOCUMENT_FILE_PATH
- HEADER_FILE_PATH
- IMAGES_DIR_PATH
Attributes
dest_path[R]
file_path[R]
header_replacer_list[R]
image_replacer_list[R]
text_replacer_list[R]
Public Class Methods
new(file_path)
click to toggle source
# File lib/docx_template/docx.rb, line 13 def initialize(file_path) @file_path = file_path @text_replacer_list = [] @image_replacer_list = [] @header_replacer_list = [] end
Public Instance Methods
replace_header(src_text, dest_text, multiple_occurances=false)
click to toggle source
# File lib/docx_template/docx.rb, line 29 def replace_header(src_text, dest_text, multiple_occurances=false) @header_replacer_list << EntityReplacer.new(src_text, dest_text, multiple_occurances) end
replace_image(src_image_file_name, dest_image_file)
click to toggle source
# File lib/docx_template/docx.rb, line 24 def replace_image(src_image_file_name, dest_image_file) replacer = EntityReplacer.new(src_image_file_name, dest_image_file, false) @image_replacer_list << replacer end
replace_text(src_text, dest_text, multiple_occurances=false)
click to toggle source
# File lib/docx_template/docx.rb, line 20 def replace_text(src_text, dest_text, multiple_occurances=false) @text_replacer_list << EntityReplacer.new(src_text, dest_text, multiple_occurances) end
save(dest_path=Dir.mktmpdir)
click to toggle source
# File lib/docx_template/docx.rb, line 33 def save(dest_path=Dir.mktmpdir) @dest_path = dest_path buffer = nil Zip::File.open(@file_path) do |zip_file| buffer = Zip::OutputStream.write_buffer do |out| exclusion_files_list = derive_exclusion_file_list prepare_rest_of_archive(zip_file, out, exclusion_files_list) # text part unless @text_replacer_list.empty? replace_content(out, DOCUMENT_FILE_PATH, zip_file, @text_replacer_list) end # image part @image_replacer_list.each do |replacer| next unless File.exist?(replacer.dest_entity) # silently skip non-existent files out.put_next_entry("#{IMAGES_DIR_PATH}/#{replacer.src_entity}") out.write File.read(replacer.dest_entity) end # header part unless @header_replacer_list.empty? replace_content(out, HEADER_FILE_PATH, zip_file, @header_replacer_list) end end end File.open(dest_path, "w") {|f| f.write(buffer.string) } end
Private Instance Methods
derive_exclusion_file_list()
click to toggle source
# File lib/docx_template/docx.rb, line 75 def derive_exclusion_file_list e_list = [] unless @text_replacer_list.empty? e_list << DOCUMENT_FILE_PATH end @image_replacer_list.each do |replacer| e_list << "#{IMAGES_DIR_PATH}/#{replacer.src_entity}" if File.exist?(replacer.dest_entity) end e_list end
prepare_rest_of_archive(zip_file, out, exclude_list)
click to toggle source
# File lib/docx_template/docx.rb, line 66 def prepare_rest_of_archive(zip_file, out, exclude_list) zip_file.entries.each do |e| unless exclude_list.include?(e.name) out.put_next_entry(e.name) out.write e.get_input_stream.read end end end
replace_content(out, dest_file, zip_file, list)
click to toggle source
Encoding should be handled by the user, check spec for examples
# File lib/docx_template/docx.rb, line 87 def replace_content(out, dest_file, zip_file, list) out.put_next_entry(dest_file) string_buffer = zip_file.read(dest_file) list.each do |replacer| # Dont abort on failure begin string_buffer.gsub!(replacer.src_entity, replacer.dest_entity) rescue Exception => e puts "Exception: #{e}" end end out.write string_buffer end