Skip to content

Instantly share code, notes, and snippets.

@hitode909
Created November 21, 2010 08:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hitode909/708573 to your computer and use it in GitHub Desktop.
Save hitode909/708573 to your computer and use it in GitHub Desktop.
kindlegen用のhtmlを作る
#!/usr/bin/env ruby
require 'optparse'
def tag(name, attr = nil, content = nil)
unless attr or content
return "<#{name}>"
end
if not attr and content
return "<#{name}>#{content}</#{name}>"
end
# has attr
attr_str = attr.each_pair.map{|k,v| "#{k}=\"#{v}\""}.join(" ")
unless content
return "<#{name} #{attr_str}>"
end
return "<#{name} #{attr_str}>#{content}</#{name}>"
end
banner = <<EOF
usage
jpg2html.rb *.jpg
EOF
config = { }
parser = OptionParser.new(banner) {|opt|
opt.on('--title TITLE', 'specify title') {|v|
config[:title] = v
}
opt.parse!(ARGV)
}
file = config[:title] or 'out.html'
if ARGV.empty?
puts parser.to_s
exit(1)
end
filename = (config[:title] || 'out') + '.html'
open(filename, 'w') {|f|
f.puts tag('html')
f.puts tag('head')
f.puts tag('meta', "http-equiv" => "Content-Type", "content" => "text/html; charset=utf-8")
f.puts tag('title', nil, config[:title]) if config[:title]
f.puts tag('/head')
f.puts tag('body')
ARGV.each{|file|
f.puts tag("img", "src" => File.expand_path(file))
f.puts tag("br")
}
f.puts tag('/body')
f.puts tag('/html')
}
puts filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment