Class: Sablon::Processor::Document::ImageBlock

Inherits:
Block
  • Object
show all
Defined in:
lib/sablon/processor/document/blocks.rb

Instance Attribute Summary

Attributes inherited from Block

#end_field, #start_field

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Block

#body, enclosed_by, #end_node, #initialize, parent, parent_selector, #process, #remove_control_elements, #start_node

Constructor Details

This class inherits a constructor from Sablon::Processor::Document::Block

Class Method Details

.encloses?(start_field, end_field) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/sablon/processor/document/blocks.rb', line 90

def self.encloses?(start_field, end_field)
  start_field.expression.start_with?('@')
end

Instance Method Details

#replace(image) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# 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 = if File.extname(image.name) == '.svg'
        node.at_xpath('.//asvg:svgBlip', asvg: 'http://schemas.microsoft.com/office/drawing/2016/SVG/main')
      else
        node.at_xpath('.//a:blip', a: 'http://schemas.openxmlformats.org/drawingml/2006/main')
      end
      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