lib/review/epubmaker.rb in review-5.1.1 vs lib/review/epubmaker.rb in review-5.2.0
- old
+ new
@@ -25,14 +25,16 @@
require 'review/epubmaker/content'
require 'review/epubmaker/epubv2'
require 'review/epubmaker/epubv3'
require 'review/epubmaker/reviewheaderlistener'
require 'review/makerhelper'
+require 'review/loggable'
module ReVIEW
class EPUBMaker
include MakerHelper
+ include Loggable
include ReVIEW::CallHook
def initialize
@producer = nil
@htmltoc = nil
@@ -40,23 +42,10 @@
@logger = ReVIEW.logger
@img_math = nil
@basedir = nil
end
- def error(msg)
- @logger.error msg
- exit 1
- end
-
- def warn(msg)
- @logger.warn msg
- end
-
- def log(msg)
- @logger.debug(msg)
- end
-
def self.execute(*args)
self.new.execute(*args)
end
def parse_opts(args)
@@ -82,18 +71,18 @@
[cmd_config, args[0], args[1]]
end
def execute(*args)
cmd_config, yamlfile, exportfile = parse_opts(args)
- error "#{yamlfile} not found." unless File.exist?(yamlfile)
+ error! "#{yamlfile} not found." unless File.exist?(yamlfile)
@config = ReVIEW::Configure.create(maker: 'epubmaker',
yamlfile: yamlfile,
config: cmd_config)
@producer = ReVIEW::EPUBMaker::Producer.new(@config)
update_log_level
- log("Loaded yaml file (#{yamlfile}).")
+ debug("Loaded yaml file (#{yamlfile}).")
@basedir = File.absolute_path(File.dirname(yamlfile))
produce(yamlfile, exportfile)
end
@@ -127,27 +116,25 @@
I18n.setup(@config['language'])
bookname ||= @config['bookname']
booktmpname = "#{bookname}-epub"
@img_math = ReVIEW::ImgMath.new(@config)
- begin
- @config.check_version(ReVIEW::VERSION)
- rescue ReVIEW::ConfigError => e
+ unless @config.check_version(ReVIEW::VERSION, exception: false)
warn e.message
end
- log("#{bookname}.epub will be created.")
+ debug("#{bookname}.epub will be created.")
FileUtils.rm_f("#{bookname}.epub")
if @config['debug']
FileUtils.rm_rf(booktmpname)
end
@img_math.cleanup_mathimg
basetmpdir = build_path
begin
- log("Created first temporary directory as #{basetmpdir}.")
+ debug("Created first temporary directory as #{basetmpdir}.")
call_hook('hook_beforeprocess', basetmpdir, base_dir: @basedir)
@htmltoc = ReVIEW::HTMLToc.new(basetmpdir)
## copy all files into basetmpdir
@@ -190,18 +177,18 @@
epubtmpdir = nil
if @config['debug'].present?
epubtmpdir = File.join(basetmpdir, booktmpname)
Dir.mkdir(epubtmpdir)
end
- log('Call ePUB producer.')
+ debug('Call ePUB producer.')
@producer.produce("#{bookname}.epub", basetmpdir, epubtmpdir, base_dir: @basedir)
- log('Finished.')
+ debug('Finished.')
@logger.success("built #{bookname}.epub")
rescue ApplicationError => e
raise if @config['debug']
- error(e.message)
+ error! e.message
ensure
FileUtils.remove_entry_secure(basetmpdir) unless @config['debug']
end
end
@@ -243,12 +230,12 @@
end
next
end
basedir = File.dirname(file)
FileUtils.mkdir_p(File.join(destdir, basedir))
- log("Copy #{file} to the temporary directory.")
- FileUtils.cp(file, File.join(destdir, basedir))
+ debug("Copy #{file} to the temporary directory.")
+ FileUtils.cp(file, File.join(destdir, basedir), preserve: true)
end
else
recursive_copy_files(resdir, destdir, allow_exts)
end
end
@@ -268,22 +255,21 @@
if FileTest.directory?(File.join(resdir, fname))
recursive_copy_files(File.join(resdir, fname), File.join(destdir, fname), allow_exts)
elsif fname =~ /\.(#{allow_exts.join('|')})\Z/i
FileUtils.mkdir_p(destdir)
- log("Copy #{resdir}/#{fname} to the temporary directory.")
- FileUtils.cp(File.join(resdir, fname), destdir)
+ debug("Copy #{resdir}/#{fname} to the temporary directory.")
+ FileUtils.cp(File.join(resdir, fname), destdir, preserve: true)
end
end
end
end
def check_compile_status
return unless @compile_errors
- $stderr.puts 'compile error, No EPUB file output.'
- exit 1
+ error! 'compile error, No EPUB file output.'
end
def build_body(basetmpdir, yamlfile)
@precount = 0
@bodycount = 0
@@ -321,11 +307,11 @@
end
check_compile_status
end
def build_part(part, basetmpdir, htmlfile)
- log("Create #{htmlfile} from a template.")
+ debug("Create #{htmlfile} from a template.")
File.open(File.join(basetmpdir, htmlfile), 'w') do |f|
@part_number = part.number
@part_title = part.name.strip
@body = ReVIEW::Template.generate(path: 'html/_part_body.html.erb', binding: binding)
@@ -382,11 +368,11 @@
return
end
htmlfile = "#{id}.#{@config['htmlext']}"
write_buildlogtxt(basetmpdir, htmlfile, filename)
- log("Create #{htmlfile} from #{filename}.")
+ debug("Create #{htmlfile} from #{filename}.")
if @config['params'].present?
warn %Q('params:' in config.yml is obsoleted.)
if @config['params'] =~ /stylesheet=/
warn %Q(stylesheets should be defined in 'stylesheet:', not in 'params:')
@@ -396,12 +382,12 @@
@converter.convert(filename, File.join(basetmpdir, htmlfile))
write_info_body(basetmpdir, id, htmlfile, ispart, chaptype)
remove_hidden_title(basetmpdir, htmlfile)
rescue => e
@compile_errors = true
- warn "compile error in #{filename} (#{e.class})"
- warn e.message
+ error "compile error in #{filename} (#{e.class})"
+ error e.message
end
end
def remove_hidden_title(basetmpdir, htmlfile)
File.open(File.join(basetmpdir, htmlfile), 'r+') do |f|
@@ -478,11 +464,11 @@
def push_contents(_basetmpdir)
@htmltoc.each_item do |level, file, title, args|
next if level.to_i > @config['toclevel'] && args[:force_include].nil?
- log("Push #{file} to ePUB contents.")
+ debug("Push #{file} to ePUB contents.")
params = { file: file,
level: level.to_i,
title: title,
chaptype: args[:chaptype] }
@@ -502,23 +488,23 @@
def copy_stylesheet(basetmpdir)
return if @config['stylesheet'].empty?
@config['stylesheet'].each do |sfile|
unless File.exist?(sfile)
- error "stylesheet: #{sfile} is not found."
+ error! "stylesheet: #{sfile} is not found."
end
- FileUtils.cp(sfile, basetmpdir)
+ FileUtils.cp(sfile, basetmpdir, preserve: true)
@producer.contents.push(ReVIEW::EPUBMaker::Content.new(file: sfile))
end
end
def copy_static_file(configname, destdir, destfilename: nil)
destfilename ||= @config[configname]
unless File.exist?(@config[configname])
- error "#{configname}: #{@config[configname]} is not found."
+ error! "#{configname}: #{@config[configname]} is not found."
end
FileUtils.cp(@config[configname],
- File.join(destdir, destfilename))
+ File.join(destdir, destfilename), preserve: true)
end
def copy_frontmatter(basetmpdir)
if @config['cover'].present? && File.exist?(@config['cover'])
copy_static_file('cover', basetmpdir)