class Sablon::Processor::Document::ImageBlock

Public Class Methods

encloses?(start_field, end_field) click to toggle source
# File lib/sablon/processor/document/blocks.rb, line 90
def self.encloses?(start_field, end_field)
  start_field.expression.start_with?('@')
end

Public Instance Methods

replace(image) click to toggle source
# File lib/sablon/processor/document/blocks.rb, line 94
def replace(image)
  # we need to include the start and end nodes incase the image is
  # inline with the merge fields
  nodes = [start_node] + body + [end_node]
  #
  if image
    nodes.each do |node|
      pic_prop = node.at_xpath('.//pic:cNvPr', pic: 'http://schemas.openxmlformats.org/drawingml/2006/picture')
      pic_prop.attributes['name'].value = image.name if pic_prop
      blip = node.at_xpath('.//a:blip', a: 'http://schemas.openxmlformats.org/drawingml/2006/main')
      blip.attributes['embed'].value = image.local_rid if blip
      drawing_size = node.at_xpath('.//wp:extent')

  # if image properties are defined, the size of the placeholder image
  # should be replaced with the actual values
      if image.width && image.height
        drawing_size.attributes['cx'].value = image.width.to_s if drawing_size
        drawing_size.attributes['cy'].value = image.height.to_s if drawing_size
        pic_size = node.at_xpath('.//a:xfrm//a:ext', a: 'http://schemas.openxmlformats.org/drawingml/2006/main')
        pic_size.attributes['cx'].value = image.width.to_s if pic_size
        pic_size.attributes['cy'].value = image.height.to_s if pic_size
      end
    end
  end
  #
  start_field.remove
  end_field.remove
end