Created
May 10, 2015 01:50
-
-
Save activeshadow/5fc31de3658bb87b3074 to your computer and use it in GitHub Desktop.
Embed all images directly in HTML file (OS X)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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