Module: Documentalist::ODFMerge
- Includes:
- Dependencies
- Defined in:
- lib/backends/odf_merge.rb
Overview
This module provides open document merge functionality
Class Method Summary collapse
- .get_contents(odt_file) ⇒ Object
- .merge_string(string, options = {}) ⇒ Object
- .merge_template(template, options = {}) ⇒ Object
Methods included from Dependencies
Class Method Details
.get_contents(odt_file) ⇒ Object
27 28 29 30 31 |
# File 'lib/backends/odf_merge.rb', line 27 def self.get_contents(odt_file) contents = String.new Zip::ZipFile.open(odt_file) { |zip| contents = zip.read("content.xml") } contents.gsub("<%", "<%").gsub("%>", "%>") end |
.merge_string(string, options = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/backends/odf_merge.rb', line 15 def self.merge_string(string, = {}) locals = [:locals] if locals and locals.is_a? Hash locals.each do |k,v| instance_variable_set("@#{k.to_s}".to_sym, v) end end ERB.new(string).result(binding) end |
.merge_template(template, options = {}) ⇒ Object
33 34 35 36 37 38 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 |
# File 'lib/backends/odf_merge.rb', line 33 def self.merge_template(template, = {}) # Get template contents tmp_contents= Tempfile.new("officer-contents") tmp_contents.write(merge_string(get_contents(template), :locals => [:locals])) tmp_contents.close # Copy the template so we can merge the data into the copy tmp_merged_template = File.join(Dir.tmpdir, "merged-template-#{rand(10**9)}#{File.extname(template)}") FileUtils.cp(template, tmp_merged_template) # Stuff the merged contents.xml into the OpenDocument zip Zip::ZipFile.open(tmp_merged_template) do |zip| zip.replace("content.xml", tmp_contents.path) zip.commit end # Remove the merged contents.xml tmp_contents.unlink # Manages the converted file depending on the context if [:to] if File.extname([:to]) == File.extname(template) FileUtils.mv(tmp_merged_template, [:to]) else Documentalist.convert(tmp_merged_template, :to => [:to]) FileUtils.rm(tmp_merged_template) end else FileUtils.rm(template) FileUtils.mv(tmp_merged_template, template) end end |