Module: ODFWriter::Images

Included in:
Document
Defined in:
lib/odf_writer/images.rb

Overview

Images: replace images

Constant Summary collapse

IMAGE_DIR_NAME =

constants

"Pictures"

Instance Method Summary collapse

Instance Method Details

#avoid_duplicate_image_names(content) ⇒ Object

avoid_duplicate_image_names

newer versions of LibreOffice can’t open files with duplicates image names



78
79
80
81
82
83
84
85
86
# File 'lib/odf_writer/images.rb', line 78

def avoid_duplicate_image_names(content)

  nodes = content.xpath("//draw:frame[@draw:name]")
  
  nodes.each_with_index do |node, i|
    node.attribute('name').value = "pic_#{i}"
  end
  
end

#find_image_name_matches(content) ⇒ Object

find_image_name_matches



43
44
45
46
47
48
49
50
51
52
# File 'lib/odf_writer/images.rb', line 43

def find_image_name_matches(content)

  @images.each_pair do |image_name, img_data|
    if node = content.xpath("//draw:frame[@draw:name='#{image_name}']/draw:image").first
      placeholder_path = node.attribute('href').value
      @image_names_replacements[path] = ::File.join(IMAGE_DIR_NAME, ::File.basename(placeholder_path))
    end
  end
  
end

#include_image_files(file) ⇒ Object

include_image_files



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/odf_writer/images.rb', line 59

def include_image_files(file)

  return if @images.empty?
  
  @image_names_replacements.each_pair do |path, template_image|
  
    file.output_stream.put_next_entry(template_image)
    file.output_stream.write ::File.read(path)
    
  end
  
end