|
#!/usr/bin/env ruby |
|
#-*- encoding: utf-8 -*- |
|
|
|
$VERBOSE = nil |
|
$KCODE = 'u' |
|
|
|
usage = <<END_OF_USAGE |
|
|
|
#{$0} |
|
========================= |
|
|
|
導入 |
|
------------------------- |
|
Ruby 1.9系を推奨(1.8.7でも動作する)。 |
|
kramdownが必要なので `gem install kramdown` としてインストールしておく。 |
|
|
|
基本的な使い方 |
|
------------------------- |
|
`ruby #{$0} sample.md >sample.html` を実行する。 |
|
`chmod 755 #{$0}` してから `./#{$0} sample.md >sample.html` としてもよい。 |
|
`ruby #{$0} sample.md -o sample.html` で出力ファイルの指定も可能。 |
|
|
|
まとめて変換 |
|
------------------------- |
|
`ruby #{$0} -t *.md` でまとめて `*.md` を `*.html` に変換する。 |
|
このオプションは前述のファイル指定と併用できないので注意。 |
|
|
|
ヘッダ・フッタの指定 |
|
------------------------- |
|
`ruby #{$0} -h header.tpl -f footer.tpl` でヘッダ・フッタを指定できる。 |
|
このオプションは他のオプションと併用可能。 |
|
|
|
END_OF_USAGE |
|
|
|
header = <<END_OF_HEADER |
|
<html> |
|
<head> |
|
<link href="http://csswizardry.com/inuitcss/inuit.css/core/css/inuit.css" rel="stylesheet"> |
|
<link href="http://twitter.github.com/bootstrap/assets/js/google-code-prettify/prettify.css" rel="stylesheet"> |
|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> |
|
<script src="http://twitter.github.com/bootstrap/assets/js/google-code-prettify/prettify.js"></script> |
|
<script> |
|
// make sure jQuery is loaded before you run this function, |
|
// probably a good idea to create an application.js loaded |
|
// as last js file |
|
$(function() { |
|
// make code pritty |
|
window.prettyPrint && prettyPrint(); |
|
}); |
|
</script> |
|
<style> |
|
body { |
|
margin: 2em 5em; |
|
} |
|
</style> |
|
</head> |
|
<body> |
|
END_OF_HEADER |
|
|
|
footer = <<END_OF_FOOTER |
|
</body> |
|
</html> |
|
END_OF_FOOTER |
|
|
|
require 'optparse' |
|
require 'pathname' |
|
require 'rubygems' |
|
require 'kramdown' |
|
|
|
target_mode = false |
|
header_file = nil |
|
footer_file = nil |
|
output_file = nil |
|
|
|
ARGV.options {|opts| |
|
opts.on("-o", "--output OUTPUT", "出力ファイルを指定する。") {|name| |
|
output_file = name |
|
} |
|
opts.on("-h", "--header HEADER", "ヘッダファイルを指定する。") {|name| |
|
header_file = name |
|
} |
|
opts.on("-f", "--footer FOOTER", "フッタファイルを指定する。") {|name| |
|
footer_file = name |
|
} |
|
opts.on("-t", "--target", "入力ファイルを指定するフラグ。") {|name| |
|
target_mode = true |
|
} |
|
opts.parse! |
|
} |
|
|
|
if ARGV.empty? |
|
puts usage |
|
exit 0 |
|
end |
|
if header_file |
|
header = File.read(header_file) |
|
end |
|
if footer_file |
|
footer = File.read(footer_file) |
|
end |
|
|
|
def convert(body, header, footer) |
|
output = String.new |
|
output << header << "\n" |
|
output << Kramdown::Document.new(body).to_html << "\n" |
|
output << footer << "\n" |
|
output |
|
end |
|
|
|
if target_mode |
|
# multiple mode |
|
ARGV.each {|name| |
|
output_file = Pathname(name).sub(/\.\w+$/, "").to_s |
|
output_file << ".html" |
|
puts "#{name} => #{output_file}" |
|
File.open(output_file, "w") {|f| |
|
f.puts convert(File.read(name), header, footer) |
|
} |
|
} |
|
else |
|
# single mode |
|
if output_file |
|
ARGV.each {|name| |
|
puts "#{name} => #{output_file}" |
|
File.open(output_file, "w") {|f| |
|
f.puts convert(File.read(name), header, footer) |
|
} |
|
} |
|
else |
|
ARGV.each {|name| |
|
puts convert(File.read(name), header, footer) |
|
} |
|
end |
|
end |