bin/gepuber
require 'gepub'
require 'optparse'
def usage
STDERR.print "gepuber [-d destination ] [-c configfilename] <source-directory>\r\n"
exit 1
end
def srccheck(srcdir, configfilename)
if !File.exist?(srcdir) || !File.directory?(srcdir)
STDERR.print "#{srcdir} is not a directory"
exit 1
end
if !File.exist?(File.join(srcdir, configfilename))
STDERR.print "#{configfilename} does not exists in#{srcdir}."
exit 1
end
end
def destcheck(destdir)
if (File.exist?(destdir) && !File.directory?(destdir))
STDERR.print "#{destdir} is not a directory\n"
exit 1
end
end
destbasedir = "."
configfilename = 'gepuber.conf'
opt = OptionParser.new
usage if ARGV.length < 1
opt.on('-d [directory]') { |dir|
destbasedir = dir
}
opt.on('-c [configfilename]') { |name|
configfilename = name
}
destbasedir = File.expand_path(destbasedir)
srcdir = opt.parse(ARGV)[0]
srccheck(srcdir, configfilename)
epubname_to_generate = 'gepuber_generated.epub'
Dir.chdir(srcdir)
begin
File.open(configfilename, 'rb') {
|io|
GEPUB::Builder.new {
table_of_contents = {}
(class<< self;self;end).send(
:define_method,
:epub_name, Proc.new { |name|
epubname_to_generate = name
})
eval "#{io.read}"
resources {
Dir.glob('*.css') {
|f|
file f
}
Dir.glob('css/*.css') {
|f|
file f
}
Dir.glob('img/*') {
|f|
if File.basename(f) == 'cover.jpg'
cover_image f
else
file f
end
}
ordered {
Dir.glob('[0-9]*.{xhtml,html}') {
|f|
file f
if table_of_contents[f]
heading table_of_contents[f]
end
}
}
}
}.generate_epub(File.join(destbasedir, epubname_to_generate))
}
end