Created
November 21, 2010 08:32
-
-
Save hitode909/708573 to your computer and use it in GitHub Desktop.
kindlegen用のhtmlを作る
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 '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