class Html2Docx::Relation

Public Class Methods

new(options = {}) click to toggle source
# File lib/Html2Docx/relation.rb, line 3
def initialize(options = {})
  @relation_file = nil
  @relation = nil
  @relations = []
  @last_relation_id = 1
  @internal_links = {}
  @external_links = {}
  @images = {}
  @unique_image_id = 0

  if options[:main_relation]
    @relation_file = File.join(options.fetch(:temp), 'word', '_rels', 'document2.xml.rels')
    @relation = File.open(@relation_file) { |f| Nokogiri::XML(f) }
    @last_relation_id = @relation.css('Relationship').last.attr('Id').to_i
  else
    @relation_file = File.join(options.fetch(:temp), 'word', '_rels', options.fetch(:file_name))
    @relation = create_relation_file
  end

  @relations = @relation.at_css('Relationships')

  @relation.at_css('Relationship').children.each do |children|
    children.remove
  end
end

Public Instance Methods

add_image(image, media_path) click to toggle source
# File lib/Html2Docx/relation.rb, line 97
def add_image(image, media_path)
  real_path    = image[:path]
  image_name   = image[:path].split('/').last
  current_path = File.join(media_path, image_name)

  if real_path.start_with? 'http'
    request    = Typhoeus::Request.new(real_path)
    image_file = File.open(current_path, 'wb+')

    request.on_headers do |response|
      if response.code != 200
        raise "Image not found! Image Path: #{real_path}"
      end
    end

    request.on_body do |data|
      image_file.write(data)
    end

    request.on_complete do |response|
      image_file.close
    end

    request.run
  else
    if File.exist? real_path
      FileUtils.cp real_path, current_path
    else
      raise "Image not found! Image Path: #{real_path}"
    end
  end

  relation_image_path = File.join('/', 'media', image_name)

  add_image_relation(relation_image_path)
end
add_image_relation(relation_image_path) click to toggle source
# File lib/Html2Docx/relation.rb, line 134
def add_image_relation(relation_image_path)
  image_id = "iId#{get_uniq_image_id}"

  @images[image_id] = {
    type: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
    target: relation_image_path
  }

  image_id
end
create_relation_file() click to toggle source
# File lib/Html2Docx/relation.rb, line 29
def create_relation_file
  document = Nokogiri::XML::Document.new
  document.encoding = 'UTF-8'
  relations_tag = Nokogiri::XML::Node.new('Relationships', document)
  relations_tag['xmlns'] = 'http://schemas.openxmlformats.org/package/2006/relationships'
  document.add_child relations_tag
  document
end
get_latest_image_id() click to toggle source
# File lib/Html2Docx/relation.rb, line 145
def get_latest_image_id
  @images.keys.max.to_i
end
get_uniq_image_id() click to toggle source
# File lib/Html2Docx/relation.rb, line 93
def get_uniq_image_id
  @unique_image_id = @unique_image_id + 1
end
render() click to toggle source
# File lib/Html2Docx/relation.rb, line 149
def render
  @external_links.each do |key, value|
    external_link_relation = Nokogiri::XML::Node.new('Relationship', @relation)
    external_link_relation['Type'] = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink'
    external_link_relation['Target'] = value
    external_link_relation['TargetMode'] = 'External'
    external_link_relation['Id'] = key

    @relation.root.add_child(external_link_relation)
  end

  @images.each do |key, value|
    image_relation = Nokogiri::XML::Node.new('Relationship', @relation)
    image_relation['Type'] = value[:type]
    image_relation['Target'] = value[:target]
    image_relation['Id'] = key

    @relation.root.add_child(image_relation)
  end

  File.open(@relation_file, 'w') { |f| f.write(Helpers::NokogiriHelper.to_xml(@relation)) }
end