Sha256: e0da1bbf1c15bad556b75bf3fc8583bd2b2d7e7f518da8ed95033c670f98584a

Contents?: true

Size: 1.77 KB

Versions: 8

Compression:

Stored size: 1.77 KB

Contents

module Sablon
  class Template
    def initialize(path)
      @path = path
    end

    # Same as +render_to_string+ but writes the processed template to +output_path+.
    def render_to_file(output_path, context, properties = {})
      File.open(output_path, 'wb') do |f|
        f.write render_to_string(context, properties)
      end
    end

    # Process the template. The +context+ hash will be available in the template.
    def render_to_string(context, properties = {})
      render(context, properties).string
    end

    private
    def render(context, properties = {})
      Sablon::Numbering.instance.reset!
      Zip.sort_entries = true # required to process document.xml before numbering.xml
      Zip::OutputStream.write_buffer(StringIO.new) do |out|
        Zip::File.open(@path).each do |entry|
          entry_name = entry.name
          out.put_next_entry(entry_name)
          content = entry.get_input_stream.read
          if entry_name == 'word/document.xml'
            out.write(process(Processor::Document, content, context, properties))
          elsif entry_name =~ /word\/header\d*\.xml/ || entry_name =~ /word\/footer\d*\.xml/
            out.write(process(Processor::Document, content, context))
          elsif entry_name == 'word/numbering.xml'
            out.write(process(Processor::Numbering, content))
          else
            out.write(content)
          end
        end
      end
    end

    # process the sablon xml template with the given +context+.
    #
    # IMPORTANT: Open Office does not ignore whitespace around tags.
    # We need to render the xml without indent and whitespace.
    def process(processor, content, *args)
      document = Nokogiri::XML(content)
      processor.process(document, *args).to_xml(indent: 0, save_with: 0)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
sablon-0.0.21 lib/sablon/template.rb
sablon-0.0.20 lib/sablon/template.rb
sablon-0.0.19 lib/sablon/template.rb
sablon-0.0.19.beta5 lib/sablon/template.rb
sablon-0.0.19.beta4 lib/sablon/template.rb
sablon-0.0.19.beta3 lib/sablon/template.rb
sablon-0.0.19.beta2 lib/sablon/template.rb
sablon-0.0.19.beta1 lib/sablon/template.rb