lib/review/epubmaker.rb in review-5.3.0 vs lib/review/epubmaker.rb in review-5.4.0

- old
+ new

@@ -1,6 +1,6 @@ -# Copyright (c) 2010-2021 Kenshi Muto and Masayoshi Takahashi +# Copyright (c) 2010-2022 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". @@ -73,13 +73,17 @@ def execute(*args) cmd_config, yamlfile, exportfile = parse_opts(args) error! "#{yamlfile} not found." unless File.exist?(yamlfile) - @config = ReVIEW::Configure.create(maker: 'epubmaker', - yamlfile: yamlfile, - config: cmd_config) + begin + @config = ReVIEW::Configure.create(maker: 'epubmaker', + yamlfile: yamlfile, + config: cmd_config) + rescue ReVIEW::ConfigError => e + error! e.message + end @producer = ReVIEW::EPUBMaker::Producer.new(@config) update_log_level debug("Loaded yaml file (#{yamlfile}).") @basedir = File.absolute_path(File.dirname(yamlfile)) @@ -225,11 +229,11 @@ allow_exts ||= @config['image_ext'] FileUtils.mkdir_p(destdir) if @config['epubmaker']['verify_target_images'].present? @config['epubmaker']['force_include_images'].each do |file| unless File.exist?(file) - if file !~ /\Ahttps?:/ + unless /\Ahttps?:/.match?(file) warn "#{file} is not found, skip." end next end basedir = File.dirname(file) @@ -255,11 +259,11 @@ dir.each do |fname| next if fname.start_with?('.') 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 + elsif /\.(#{allow_exts.join('|')})\Z/i.match?(fname) FileUtils.mkdir_p(destdir) debug("Copy #{resdir}/#{fname} to the temporary directory.") FileUtils.cp(File.join(resdir, fname), destdir, preserve: true) end end @@ -313,19 +317,29 @@ def build_part(part, basetmpdir, htmlfile) 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) - + @body = ReVIEW::Template.generate(path: template_name(localfile: '_part_body.html.erb', systemfile: 'html/_part_body.html.erb'), binding: binding) @language = @producer.config['language'] @stylesheets = @producer.config['stylesheet'] f.write ReVIEW::Template.generate(path: template_name, binding: binding) end end - def template_name + def template_name(localfile: 'layout.html.erb', systemfile: nil) + if @basedir + layoutfile = File.join(@basedir, 'layouts', localfile) + if File.exist?(layoutfile) + return layoutfile + end + end + + if systemfile + return systemfile + end + if @producer.config['htmlversion'].to_i == 5 './html/layout-html5.html.erb' else './html/layout-xhtml1.html.erb' end @@ -374,19 +388,19 @@ write_buildlogtxt(basetmpdir, htmlfile, filename) debug("Create #{htmlfile} from #{filename}.") if @config['params'].present? warn %Q('params:' in config.yml is obsoleted.) - if @config['params'] =~ /stylesheet=/ + if /stylesheet=/.match?(@config['params']) warn %Q(stylesheets should be defined in 'stylesheet:', not in 'params:') end end begin @converter.convert(filename, File.join(basetmpdir, htmlfile)) write_info_body(basetmpdir, id, htmlfile, ispart, chaptype) remove_hidden_title(basetmpdir, htmlfile) - rescue => e + rescue StandardError => e @compile_errors = true error "compile error in #{filename} (#{e.class})" error e.message end end @@ -434,15 +448,15 @@ warn "#{filename} is discarded because there is no heading. Use `=[notoc]' or `=[nodisp]' to exclude headlines from the table of contents." return end properties = detect_properties(path) - if properties.present? - prop_str = ',properties=' + properties.join(' ') - else - prop_str = '' - end + prop_str = if properties.present? + ',properties=' + properties.join(' ') + else + '' + end first = true headlines.each do |headline| if ispart.present? && headline['level'] == 1 headline['level'] = 0 end @@ -543,28 +557,15 @@ true end def build_titlepage(basetmpdir, htmlfile) - # TODO: should be created via epubcommon @title = h(@config.name_of('booktitle')) File.open(File.join(basetmpdir, htmlfile), 'w') do |f| - @body = '' - @body << %Q(<div class="titlepage">\n) - @body << %Q(<h1 class="tp-title">#{h(@config.name_of('booktitle'))}</h1>\n) - if @config['subtitle'] - @body << %Q(<h2 class="tp-subtitle">#{h(@config.name_of('subtitle'))}</h2>\n) - end - if @config['aut'] - @body << %Q(<h2 class="tp-author">#{h(@config.names_of('aut').join(ReVIEW::I18n.t('names_splitter')))}</h2>\n) - end - if @config['pbl'] - @body << %Q(<h3 class="tp-publisher">#{h(@config.names_of('pbl').join(ReVIEW::I18n.t('names_splitter')))}</h3>\n) - end - @body << '</div>' - + @body = ReVIEW::Template.generate(path: template_name(localfile: '_titlepage.html.erb', systemfile: 'html/_titlepage.html.erb'), binding: binding) @language = @producer.config['language'] @stylesheets = @producer.config['stylesheet'] + f.write ReVIEW::Template.generate(path: template_name, binding: binding) end end def copy_backmatter(basetmpdir)