Skip to content

Instantly share code, notes, and snippets.

@activeshadow
Created May 10, 2015 01:50
Show Gist options
  • Save activeshadow/5fc31de3658bb87b3074 to your computer and use it in GitHub Desktop.
Save activeshadow/5fc31de3658bb87b3074 to your computer and use it in GitHub Desktop.
Embed all images directly in HTML file (OS X)
require 'base64'
require 'fileutils'
img = /img src="(.*)" /
File.open("#{ARGV[0]}.html") do |i|
File.open("#{ARGV[0]}-embedded.html", 'w') do |o|
i.each_line do |l|
if match = img.match(l)
location = match[1]
basename = File.basename(location)
FileUtils.cp(location, '.')
`sips -Z 1280 #{basename}`
image = File.open(basename, 'rb') do |f|
f.read
end
l.sub!(img, "img src=\"data:image/png;base64,#{Base64.strict_encode64(image)}\" ")
end
o.puts l
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment