Base 64 encode Sass script
# Additional SASS Script functions | |
require 'sass' | |
require 'base64' | |
module Sass::Script::Functions | |
def base64encode(string) | |
assert_type string, :String | |
file = "."+string.value | |
data = File.open(file, "rb") {|io| io.read} | |
data = [data].flatten.pack("m").gsub("\n","") | |
Sass::Script::String.new("'data:#{mime_type(string,nil)};base64,#{data}'") | |
end | |
declare :base64encode, :args => [:string] | |
end | |
private | |
def mime_type(path, mime_type = nil) | |
return mime_type if mime_type | |
path = "#{path}" | |
if path.include?('.png') | |
'image/png' | |
elsif path.include?('.jp') | |
'image/jpeg' | |
elsif path.include?('.gif') | |
'image/gif' | |
elsif path.include?('.svg') | |
'image/svg+xml' | |
elsif path.include?('.otf') | |
'font/opentype' | |
elsif path.include?('.eot') | |
'application/vnd.ms-fontobject' | |
elsif path.include?('.ttf') | |
'font/truetype' | |
elsif path.include?('.woff') | |
'application/x-font-woff' | |
elsif path.include?('.off') | |
'font/openfont' | |
elsif path.include?(/\.([a-zA-Z]+)$/) | |
"image/#{Regexp.last_match(1).downcase}" | |
else | |
raise "A mime type could not be determined for #{path}, please specify one explicitly." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment