Created
June 6, 2012 15:29
-
-
Save ouyangzhiping/2882649 to your computer and use it in GitHub Desktop.
makeebooks
This file contains hidden or 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
# encoding: UTF-8 | |
#!/usr/bin/env ruby | |
# This script convers markdown book to one of the serveral e-book | |
# formats supported with calibre (http://calibre-ebook.com) | |
# | |
# Samples: | |
# | |
# Build e-book for amazon kindle for english and russian languages | |
# $ make-ebook en ru | |
# or | |
# $ FORMAT=mobi make-ebook en ru | |
# | |
# Build e-book in 'epub' format for russian only | |
# $ FORMAT=epub make-ebook ru | |
#require 'rubygems' | |
require 'rdiscount' | |
#require 'ruby-debug' | |
if ARGV.length == 0 | |
puts "you need to specify at least one language. For example: makeebooks en" | |
exit | |
end | |
format = ENV['FORMAT'] || 'mobi' | |
puts "using .#{format} (you can change it via FORMAT environment variable. try 'mobi' or 'epub')" | |
ARGV.each do |lang| | |
puts "convert content for '#{lang}' language" | |
if lang == 'ko' | |
figure_title = '그림' | |
elsif lang == 'ru' | |
figure_title = 'Рисунок' | |
else | |
figure_title = 'Figure' | |
end | |
book_content = %(<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Pro Git - professional version control</title></head><body>) | |
dir = File.expand_path(File.join(File.dirname(__FILE__), lang)) | |
Dir[File.join(dir, '**', '*.markdown')].sort.each do |input| | |
puts "processing #{input}" | |
content = File.read(input) | |
content.gsub!(/Insert\s+(.*)(\.png)\s*\n?\s*#{figure_title}\s+(.*)/, '') | |
book_content << RDiscount.new(content).to_html | |
end | |
book_content << "</body></html>" | |
File.open("progit.#{lang}.html", 'w') do |output| | |
output.write(book_content) | |
end | |
system('ebook-convert', "progit.#{lang}.html", "progit.#{lang}.#{format}", | |
'--cover', 'ebooks/cover.png', | |
'--authors', 'Scott Chacon', | |
'--comments', "licensed under the Creative Commons Attribution-Non Commercial-Share Alike 3.0 license", | |
'--level1-toc', '//h:h1', | |
'--level2-toc', '//h:h2', | |
'--level3-toc', '//h:h3', | |
'--language', lang) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment