class Sablon::Statement::Image

Public Instance Methods

evaluate(env) click to toggle source
# File lib/sablon/operations.rb, line 124
def evaluate(env)
  image = image_reference.evaluate(env.context)
  set_local_rid(env, image) if image
  block.replace(image)
end

Private Instance Methods

set_local_rid(env, image) click to toggle source
# File lib/sablon/operations.rb, line 132
def set_local_rid(env, image)
  if image.rid_by_file.keys.empty?
    # Only add the image once, it is reused afterwards
    rel_attr = {
      Type: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image'
    }
    rid = env.document.add_media(image.name, image.data, rel_attr)
    image.rid_by_file[env.document.current_entry] = rid
  elsif image.rid_by_file[env.document.current_entry].nil?
    # locate an existing relationship and duplicate it
    entry = image.rid_by_file.keys.first
    value = image.rid_by_file[entry]
    #
    rel = env.document.find_relationship_by('Id', value, entry)
    rid = env.document.add_relationship(rel.attributes)
    image.rid_by_file[env.document.current_entry] = rid
  end
  #
  image.local_rid = image.rid_by_file[env.document.current_entry]
end