#!/usr/bin/env ruby
require 'base64'
require 'rubygems'
require 'mime/types'
require 'optparse'
##初期化
with=false
####引数処理
opt = OptionParser.new
opt.on(
"--tag=[TAG]",
"タグに埋め込んだ状態で出力する.デフォルト(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 ""
when "script"
puts ""
when "css"
puts "url(\"#{src_str}\""
when "object"
puts ""
when "embed"
puts ""
when false
puts src_str
else
puts "<#{with} src=\"#{src_str}\" />"
end