Skip to content

Instantly share code, notes, and snippets.

@takuya
Created August 17, 2012 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takuya/3381695 to your computer and use it in GitHub Desktop.
Save takuya/3381695 to your computer and use it in GitHub Desktop.
#!/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