#!/usr/bin/env ruby
# encoding: utf-8
#
# Copyright (c) 2010 Kenshi Muto and Masayoshi Takahashi
#
# This program is free software.
# You can distribute or modify this program under the terms of
# the GNU LGPL, Lesser General Public License version 2.1.
# For details of the GNU LGPL, see the file "COPYING".
#
require 'tmpdir'
require 'fileutils'
require 'yaml'
require 'optparse'
require 'rexml/document'
require 'time'
require 'pathname'
bindir = Pathname.new(__FILE__).realpath.dirname
$LOAD_PATH.unshift((bindir + '../lib').realpath)
require 'uuid'
require 'review'
require 'review/i18n'
require 'review/htmlutils'
include ReVIEW::HTMLUtils
$essential_files = ['top', 'toc', 'colophon']
def main
if ARGV.size != 1
puts "Usage: #{$0} configfile"
exit 0
end
yamlfile = ARGV[0]
values = ReVIEW::Configure.values.merge(YAML.load_file(yamlfile))
bookname = values["bookname"]
$essential_files <<= bookname
tmp = values["debug"].nil? ? Dir.mktmpdir : "."
@bookdir = "#{tmp}/#{bookname}-epub"
@epubversion = values["epubversion"] || 2
@htmlversion = values["htmlversion"] || 4
if @epubversion == 3
## if epubversion is 3, htmlversion should be 5
@htmlversion = 5
end
if File.exist?("#{bookname}.epub")
STDERR.puts "#{bookname}.epub exists. Please remove or rename first."
exit 1
end
if File.exist?(@bookdir)
STDERR.puts "#{@bookdir} directory exists. Please remove or rename first."
exit 1
end
@identifier = values["urnid"].nil? ? "urn:uuid:#{UUID.create}" : values["urnid"]
Dir.mkdir(@bookdir)
# MIME type
File.open("#{@bookdir}/mimetype", "w") {|f|
f.write "application/epub+zip"
}
Dir.mkdir("#{@bookdir}/OEBPS")
# XHTML
@manifeststr = ""
@ncxstr = ""
@tocdesc = Array.new
basedir = Dir.pwd
base_path = Pathname.new(basedir)
ReVIEW::Book.load(basedir).parts.each do |part|
if part.name.present?
if part.file?
filename = (base_path + part.path).to_s
output_chaps_by_file(filename, values)
htmlfile = File.basename(filename.chomp.strip,".*")+".html"
else
htmlfile = "part_#{part.number}.html"
make_part_page(part, htmlfile, values)
end
file_id = "part_#{part.number}"
part_name = "#{ReVIEW::I18n.t("part", part.number)} #{part.name.strip}"
@tocdesc << [1, htmlfile, nil, part_name]
@manifeststr << %Q(
#{values["booktitle"]}
EOT if values["pubhistory"] f.puts %Q[#{values["pubhistory"].gsub(/\n/,"
")}
#{values["rights"]}
] end f.puts "" f.puts footer } end end def make_part_page(part, filename, values) File.open("#{@bookdir}/OEBPS/#{filename}", "w") {|f| f.puts <<-EOT EOT doctype = make_doctype() f.puts doctype f.puts <<-EOT