Created
June 5, 2013 19:29
-
-
Save fitzhaile/5716533 to your computer and use it in GitHub Desktop.
Base 64 encode Sass script
This file contains 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
# 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