Class: Serenity::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/serenity/template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, output) ⇒ Template

Returns a new instance of Template.



8
9
10
11
# File 'lib/serenity/template.rb', line 8

def initialize(template, output)
  FileUtils.cp(template, output)
  @template = output
end

Instance Attribute Details

#templateObject

Returns the value of attribute template.



6
7
8
# File 'lib/serenity/template.rb', line 6

def template
  @template
end

Instance Method Details

#process(context) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/serenity/template.rb', line 13

def process context
  tmpfiles = []
  Zip::File.open(@template) do |zipfile|
    %w(content.xml styles.xml).each do |xml_file|
      content = zipfile.read(xml_file)

      # Images replacement
      images_replacements = ImagesProcessor.new(content, context).generate_replacements
      images_replacements.each do |r|
        zipfile.replace(r.first, r.last)
      end

      odteruby = OdtEruby.new(XmlReader.new(content))
      out = odteruby.evaluate(context)
      out.force_encoding Encoding.default_external

      tmpfiles << (file = Tempfile.new("serenity"))
      file << out
      file.close

      zipfile.replace(xml_file, file.path)
    end
  end
end