module MailEngine
class HtmlDocumentAssetsReplacer
class << self
def check! file_paths
html_file_pathes = file_paths.select { |path| path =~ /\/[^_\/]+\.htm(l)?$/i }
raise "No html was passed in." if html_file_pathes.blank?
raise "Please only include one html file in the zip file." if html_file_pathes.size > 1
html_file_pathes.first
end
# MailEngine::HtmlDocumentAssetsReplacer.process(mail_template, file_paths)
def process mail_template, file_paths, hostname
raise "Host should not include http://" if hostname =~ /^http:\/\//i
# check if there is a html file
html_file_path = check! file_paths
# persist files into databases.
other_file_paths = file_paths - Array.wrap(html_file_path)
stored_file_objects = other_file_paths.map do |path|
f = File.open(path)
mail_template.mail_template_files.create :file => f, :size => File.size(f)
end
### replace images and csses from the html document.
# possible will use this in the future to take care of encoding problem:
# doc = Nokogiri.XML('', nil, 'EUC-JP')
doc = Nokogiri::HTML(File.read(html_file_path))
doc.css('img, link').each do |element|
# FIXME seems will have the problem if have duplicated filename in different dir
case
when element.name == "link" && substitution_object = stored_file_objects.detect { |file_object| file_object.attributes["file"] == File.basename(element['href']) }
element['href'] = "http://#{File.join(hostname, substitution_object.file.url)}"
when element.name == "img" && substitution_object = stored_file_objects.detect { |file_object| file_object.attributes["file"] == File.basename(element['src']) }
element['src'] = "http://#{File.join(hostname, substitution_object.file.url)}"
end
end
doc.to_html
end
def replace_resource_filename_in_html html, original_filename, new_filename
return false if original_filename.blank? or new_filename.blank?
doc = Nokogiri::HTML(html)
doc.css('img, link').each do |element|
# FIXME seems will have the problem if have duplicated filename in different dir
next if element.name == "link" && element['href'].blank?
next if element.name == "img" && element['src'].blank?
case
when element.name == "link" && File.basename(element['href']) == original_filename
element['href'] = element['href'].sub(original_filename, new_filename)
when element.name == "img" && File.basename(element['src']) == original_filename
element['src'] = element['src'].sub(original_filename, new_filename)
end
end
doc.to_html
end
def replace_resource_url_in_html html, original_file_url, new_file_url
return false if original_file_url.blank? or new_file_url.blank?
doc = Nokogiri::HTML(html)
doc.css('img, link').each do |element|
# FIXME seems will have the problem if have duplicated filename in different dir
next if element.name == "link" && element['href'].blank?
next if element.name == "img" && element['src'].blank?
case
when element.name == "link" && element['href'].include?(original_file_url)
element['href'] = element['href'].sub(original_file_url, new_file_url)
when element.name == "img" && element['src'].include?(original_file_url)
element['src'] = element['src'].sub(original_file_url, new_file_url)
end
end
doc.to_html
end
end
end
end