lib/review/epubmaker.rb in review-3.1.0 vs lib/review/epubmaker.rb in review-3.2.0

- old
+ new

@@ -1,6 +1,6 @@ -# Copyright (c) 2010-2018 Kenshi Muto and Masayoshi Takahashi +# Copyright (c) 2010-2019 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". @@ -62,11 +62,49 @@ @producer = Producer.new(@config) @producer.load(yamlfile) @config = @producer.config @config.maker = 'epubmaker' + end + + def self.execute(*args) + self.new.execute(*args) + end + + def parse_opts(args) + cmd_config = {} + opts = OptionParser.new + + opts.banner = 'Usage: review-epubmaker [options] configfile [export_filename]' + opts.version = ReVIEW::VERSION + opts.on('--help', 'Prints this message and quit.') do + puts opts.help + exit 0 + end + opts.on('--[no-]debug', 'Keep temporary files.') { |debug| cmd_config['debug'] = debug } + + opts.parse!(args) + if args.size < 1 || args.size > 2 + puts opts.help + exit 0 + end + + [cmd_config, args[0], args[1]] + end + + def execute(*args) + @config = ReVIEW::Configure.values + @config.maker = 'epubmaker' + cmd_config, yamlfile, exportfile = parse_opts(args) + error "#{yamlfile} not found." unless File.exist?(yamlfile) + + load_yaml(yamlfile) + @config.deep_merge!(cmd_config) update_log_level + log("Loaded yaml file (#{yamlfile}).") + + produce(yamlfile, exportfile) end def update_log_level if @config['debug'] @logger.level = Logger::DEBUG @@ -87,20 +125,19 @@ Dir.mktmpdir("#{@config['bookname']}-epub-") end end def produce(yamlfile, bookname = nil) - load_yaml(yamlfile) I18n.setup(@config['language']) bookname ||= @config['bookname'] booktmpname = "#{bookname}-epub" begin @config.check_version(ReVIEW::VERSION) rescue ReVIEW::ConfigError => e warn e.message end - log("Loaded yaml file (#{yamlfile}). I will produce #{bookname}.epub.") + log("#{bookname}.epub will be created.") FileUtils.rm_f("#{bookname}.epub") if @config['debug'] FileUtils.rm_rf(booktmpname) end