class OdtHandlebars::OdtFill

Constants

FIELD_MATCHER

Public Class Methods

new(infile,outfile,fill) click to toggle source
# File lib/odt_handlebars/odt_fill.rb, line 9
def initialize(infile,outfile,fill)
  @infile=infile
  @outfile=outfile
  @fill=fill
end

Public Instance Methods

replace() click to toggle source
# File lib/odt_handlebars/odt_fill.rb, line 15
def replace
  unpack
end
replace_handlebars(raw_content,placeholders) click to toggle source
# File lib/odt_handlebars/odt_fill.rb, line 42
def replace_handlebars(raw_content,placeholders)
  content=OdtCleaner.clean(raw_content)
  doc=Nokogiri.parse(content)
  rows=doc.xpath("//table:table-row/*[starts-with(.,'{{/each')]")
  rows.each do |row| row.parent.replace(row.to_str) end
  rows=doc.xpath("//table:table-row/*[starts-with(.,'{{#each')]")
  rows.each do |row| row.parent.replace(row.to_str) end
  fields=doc.xpath("//text:p/*")
  handlebars = Handlebars::Context.new
  template = handlebars.compile(doc.to_s)
  out=template.call(placeholders)
  out
end
unpack() click to toggle source
# File lib/odt_handlebars/odt_fill.rb, line 19
def unpack
  File.open(@outfile,"w") do |outfile|
    Zip::OutputStream.write_buffer(outfile) do |outzip|
      Zip::File.open(@infile) do |inzip|
        inzip.entries.each do |e|
          next if e.file_type_is?(:directory)
          if e.name == "content.xml"
            content = e.get_input_stream.read
            output=replace_handlebars(content,@fill)
            outzip.put_next_entry(e.name)
            outzip.write output
          else
            outzip.put_next_entry(e.name)
            outzip.write e.get_input_stream.read
          end
        end
      end
    end
  end
  @outfile
end