dataURIを作る ref: http://qiita.com/items/3cd7d6d4c250c382d031
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
#!/usr/bin/env ruby | |
require 'base64' | |
require 'rubygems' | |
require 'mime/types' | |
require 'optparse' | |
##初期化 | |
with=false | |
####引数処理 | |
opt = OptionParser.new | |
opt.on( | |
"--tag=[TAG]", | |
"タグに埋め込んだ状態で出力する.デフォルト<img>(MIME判別) "){|arg| | |
case arg | |
when nil | |
with = "img" | |
when false | |
with = false | |
else | |
with = arg | |
end | |
} | |
opt.on("--css", "CSS埋め込み用に展開する .url(\"data:image/png;base64,XXXX\") "){ |arg| with = "css"|| arg } | |
opt.parse!(ARGV) | |
###メイン処理 | |
data_type = MIME::Types.type_for(ARGF.path).first.to_s | |
if with != false then | |
with = "script" if data_type =~/script/i | |
with = "img" if data_type =~/image/i and not with=="css" | |
#with = "embed" if data_type =~/flash/i # no browser support swf in data uri scheme | |
with = "svg" if data_type =~/svg/i | |
end | |
data_str = Base64.encode64(ARGF.read).gsub /\n/,"" | |
src_str = "data:#{data_type};base64,#{data_str}" | |
##出力処理 | |
case with | |
when "img" | |
puts "<img src=\"#{src_str}\" />" | |
when "script" | |
puts "<script src=\"#{src_str}\"></script>" | |
when "css" | |
puts "url(\"#{src_str}\"" | |
when "object" | |
puts "<object data='#{src_str}' type='#{data_type}'> </object>" | |
when "embed" | |
puts "<embed src='#{src_str}' type='#{data_type}' />" | |
when false | |
puts src_str | |
else | |
puts "<#{with} src=\"#{src_str}\" />" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment