lib/review/epubmaker.rb in review-1.5.0 vs lib/review/epubmaker.rb in review-1.6.0

- old
+ new

@@ -16,99 +16,101 @@ class EPUBMaker include ::EPUBMaker include REXML def initialize - @epub = nil + @producer = nil @tochtmltxt = "toc-html.txt" @buildlogtxt = "build-log.txt" end def log(s) - puts s unless @params["debug"].nil? + puts s if @params["debug"].present? end def load_yaml(yamlfile) @params = ReVIEW::Configure.values.merge(YAML.load_file(yamlfile)) # FIXME:設定がRe:VIEW側とepubmaker/producer.rb側の2つに分かれて面倒 - @epub = Producer.new(@params) - @epub.load(yamlfile) - @params = @epub.params + @producer = Producer.new(@params) + @producer.load(yamlfile) + @params = @producer.params end def produce(yamlfile, bookname=nil) load_yaml(yamlfile) I18n.setup(@params["language"]) bookname = @params["bookname"] if bookname.nil? booktmpname = "#{bookname}-epub" log("Loaded yaml file (#{yamlfile}). I will produce #{bookname}.epub.") - File.unlink("#{bookname}.epub") if File.exist?("#{bookname}.epub") - FileUtils.rm_rf(booktmpname) if @params["debug"] && File.exist?(booktmpname) + FileUtils.rm_f("#{bookname}.epub") + FileUtils.rm_rf(booktmpname) if @params["debug"] basetmpdir = Dir.mktmpdir("#{bookname}-", Dir.pwd) begin log("Created first temporary directory as #{basetmpdir}.") - log("Call hook_beforeprocess. (#{@params["epubmaker"]["hook_beforeprocess"]})") - call_hook(@params["epubmaker"]["hook_beforeprocess"], basetmpdir) + call_hook("hook_beforeprocess", basetmpdir) + ## copy all files into basetmpdir copy_stylesheet(basetmpdir) copy_frontmatter(basetmpdir) - log("Call hook_afterfrontmatter. (#{@params["epubmaker"]["hook_afterfrontmatter"]})") - call_hook(@params["epubmaker"]["hook_afterfrontmatter"], basetmpdir) + call_hook("hook_afterfrontmatter", basetmpdir) build_body(basetmpdir, yamlfile) - log("Call hook_afterbody. (#{@params["epubmaker"]["hook_afterbody"]})") - call_hook(@params["epubmaker"]["hook_afterbody"], basetmpdir) + call_hook("hook_afterbody", basetmpdir) copy_backmatter(basetmpdir) - log("Call hook_afterbackmatter. (#{@params["epubmaker"]["hook_afterbackmatter"]})") - call_hook(@params["epubmaker"]["hook_afterbackmatter"], basetmpdir) + call_hook("hook_afterbackmatter", basetmpdir) + ## push contents in basetmpdir into @producer push_contents(basetmpdir) - if !@params["epubmaker"]["verify_target_images"].nil? + if @params["epubmaker"]["verify_target_images"].present? verify_target_images(basetmpdir) copy_images(@params["imagedir"], basetmpdir) else copy_images(@params["imagedir"], "#{basetmpdir}/images") end copy_resources("covers", "#{basetmpdir}/images") copy_resources("adv", "#{basetmpdir}/images") copy_resources(@params["fontdir"], "#{basetmpdir}/fonts", @params["font_ext"]) - log("Call hook_aftercopyimage. (#{@params["epubmaker"]["hook_aftercopyimage"]})") - call_hook(@params["epubmaker"]["hook_aftercopyimage"], basetmpdir) + call_hook("hook_aftercopyimage", basetmpdir) - @epub.import_imageinfo("#{basetmpdir}/images", basetmpdir) - @epub.import_imageinfo("#{basetmpdir}/fonts", basetmpdir, @params["font_ext"]) + @producer.import_imageinfo("#{basetmpdir}/images", basetmpdir) + @producer.import_imageinfo("#{basetmpdir}/fonts", basetmpdir, @params["font_ext"]) - epubtmpdir = @params["debug"].nil? ? nil : "#{Dir.pwd}/#{booktmpname}" - Dir.mkdir(booktmpname) unless @params["debug"].nil? + epubtmpdir = nil + if @params["debug"].present? + epubtmpdir = "#{Dir.pwd}/#{booktmpname}" + Dir.mkdir(epubtmpdir) + end log("Call ePUB producer.") - @epub.produce("#{bookname}.epub", basetmpdir, epubtmpdir) + @producer.produce("#{bookname}.epub", basetmpdir, epubtmpdir) log("Finished.") ensure FileUtils.remove_entry_secure basetmpdir if @params["debug"].nil? end end - def call_hook(filename, *params) - if !filename.nil? && File.exist?(filename) && FileTest.executable?(filename) + def call_hook(hook_name, *params) + filename = @params["epubmaker"][hook_name] + log("Call #{hook_name}. (#{filename})") + if filename.present? && File.exist?(filename) && FileTest.executable?(filename) if ENV["REVIEW_SAFE_MODE"].to_i & 1 > 0 warn "hook is prohibited in safe mode. ignored." else system(filename, *params) end end end def verify_target_images(basetmpdir) - @epub.contents.each do |content| + @producer.contents.each do |content| if content.media == "application/xhtml+xml" File.open("#{basetmpdir}/#{content.file}") do |f| Document.new(File.new(f)).each_element("//img") do |e| @params["epubmaker"]["force_include_images"].push(e.attributes["src"]) @@ -131,19 +133,19 @@ end def copy_images(resdir, destdir, allow_exts=nil) return nil unless File.exist?(resdir) allow_exts = @params["image_ext"] if allow_exts.nil? - FileUtils.mkdir_p(destdir) unless FileTest.directory?(destdir) - if !@params["epubmaker"]["verify_target_images"].nil? + FileUtils.mkdir_p(destdir) + if @params["epubmaker"]["verify_target_images"].present? @params["epubmaker"]["force_include_images"].each do |file| unless File.exist?(file) warn "#{file} is not found, skip." if file !~ /\Ahttp[s]?:/ next end basedir = File.dirname(file) - FileUtils.mkdir_p("#{destdir}/#{basedir}") unless FileTest.directory?("#{destdir}/#{basedir}") + FileUtils.mkdir_p("#{destdir}/#{basedir}") log("Copy #{file} to the temporary directory.") FileUtils.cp(file, "#{destdir}/#{basedir}") end else recursive_copy_files(resdir, destdir, allow_exts) @@ -151,11 +153,11 @@ end def copy_resources(resdir, destdir, allow_exts=nil) return nil unless File.exist?(resdir) allow_exts = @params["image_ext"] if allow_exts.nil? - FileUtils.mkdir_p(destdir) unless FileTest.directory?(destdir) + FileUtils.mkdir_p(destdir) recursive_copy_files(resdir, destdir, allow_exts) end def recursive_copy_files(resdir, destdir, allow_exts) Dir.open(resdir) do |dir| @@ -163,11 +165,11 @@ next if fname =~ /\A\./ if FileTest.directory?("#{resdir}/#{fname}") recursive_copy_files("#{resdir}/#{fname}", "#{destdir}/#{fname}", allow_exts) else if fname =~ /\.(#{allow_exts.join("|")})\Z/i - FileUtils.mkdir_p(destdir) unless File.exist?(destdir) + FileUtils.mkdir_p(destdir) log("Copy #{resdir}/#{fname} to the temporary directory.") FileUtils.cp("#{resdir}/#{fname}", destdir) end end end @@ -233,19 +235,19 @@ def build_chap(chap, base_path, basetmpdir, yamlfile, ispart=nil) filename = "" chaptype = "body" - if !ispart.nil? + if ispart.present? chaptype = "part" elsif chap.on_PREDEF? chaptype = "pre" elsif chap.on_APPENDIX? chaptype = "post" end - if !ispart.nil? + if ispart.present? filename = chap.path else filename = Pathname.new(chap.path).relative_path_from(base_path).to_s end id = filename.sub(/\.re\Z/, "") @@ -314,11 +316,11 @@ if properties.present? prop_str = ",properties="+properties.join(" ") end first = true headlines.each do |headline| - headline["level"] = 0 if !ispart.nil? && headline["level"] == 1 + headline["level"] = 0 if ispart.present? && headline["level"] == 1 if first.nil? write_tochtmltxt(basetmpdir, "#{headline["level"]}\t#{filename}##{headline["id"]}\t#{headline["title"]}\tchaptype=#{chaptype}") else write_tochtmltxt(basetmpdir, "#{headline["level"]}\t#{filename}\t#{headline["title"]}\tforce_include=true,chaptype=#{chaptype}#{prop_str}") first = nil @@ -332,11 +334,11 @@ force_include = nil customid = nil chaptype = nil properties = nil level, file, title, custom = l.chomp.split("\t") - unless custom.nil? + if custom.present? # custom setting vars = custom.split(/,\s*/) vars.each do |var| k, v = var.split("=") case k @@ -359,44 +361,44 @@ hash["id"] = customid end if properties.present? hash["properties"] = properties.split(" ") end - @epub.contents.push(Content.new(hash)) + @producer.contents.push(Content.new(hash)) end end end def copy_stylesheet(basetmpdir) if @params["stylesheet"].size > 0 @params["stylesheet"].each do |sfile| FileUtils.cp(sfile, basetmpdir) - @epub.contents.push(Content.new("file" => sfile)) + @producer.contents.push(Content.new("file" => sfile)) end end end def copy_frontmatter(basetmpdir) - FileUtils.cp(@params["cover"], "#{basetmpdir}/#{File.basename(@params["cover"])}") if !@params["cover"].nil? && File.exist?(@params["cover"]) + FileUtils.cp(@params["cover"], "#{basetmpdir}/#{File.basename(@params["cover"])}") if @params["cover"].present? && File.exist?(@params["cover"]) if @params["titlepage"] if @params["titlefile"].nil? build_titlepage(basetmpdir, "titlepage.#{@params["htmlext"]}") else FileUtils.cp(@params["titlefile"], "#{basetmpdir}/titlepage.#{@params["htmlext"]}") end - write_tochtmltxt(basetmpdir, "1\ttitlepage.#{@params["htmlext"]}\t#{@epub.res.v("titlepagetitle")}\tchaptype=pre") + write_tochtmltxt(basetmpdir, "1\ttitlepage.#{@params["htmlext"]}\t#{@producer.res.v("titlepagetitle")}\tchaptype=pre") end - if !@params["originaltitlefile"].nil? && File.exist?(@params["originaltitlefile"]) + if @params["originaltitlefile"].present? && File.exist?(@params["originaltitlefile"]) FileUtils.cp(@params["originaltitlefile"], "#{basetmpdir}/#{File.basename(@params["originaltitlefile"])}") - write_tochtmltxt(basetmpdir, "1\t#{File.basename(@params["originaltitlefile"])}\t#{@epub.res.v("originaltitle")}\tchaptype=pre") + write_tochtmltxt(basetmpdir, "1\t#{File.basename(@params["originaltitlefile"])}\t#{@producer.res.v("originaltitle")}\tchaptype=pre") end - if !@params["creditfile"].nil? && File.exist?(@params["creditfile"]) + if @params["creditfile"].present? && File.exist?(@params["creditfile"]) FileUtils.cp(@params["creditfile"], "#{basetmpdir}/#{File.basename(@params["creditfile"])}") - write_tochtmltxt(basetmpdir, "1\t#{File.basename(@params["creditfile"])}\t#{@epub.res.v("credittitle")}\tchaptype=pre") + write_tochtmltxt(basetmpdir, "1\t#{File.basename(@params["creditfile"])}\t#{@producer.res.v("credittitle")}\tchaptype=pre") end end def build_titlepage(basetmpdir, htmlfile) File.open("#{basetmpdir}/#{htmlfile}", "w") do |f| @@ -425,30 +427,30 @@ end def copy_backmatter(basetmpdir) if @params["profile"] FileUtils.cp(@params["profile"], "#{basetmpdir}/#{File.basename(@params["profile"])}") - write_tochtmltxt(basetmpdir, "1\t#{File.basename(@params["profile"])}\t#{@epub.res.v("profiletitle")}\tchaptype=post") + write_tochtmltxt(basetmpdir, "1\t#{File.basename(@params["profile"])}\t#{@producer.res.v("profiletitle")}\tchaptype=post") end if @params["advfile"] FileUtils.cp(@params["advfile"], "#{basetmpdir}/#{File.basename(@params["advfile"])}") - write_tochtmltxt(basetmpdir, "1\t#{File.basename(@params["advfile"])}\t#{@epub.res.v("advtitle")}\tchaptype=post") + write_tochtmltxt(basetmpdir, "1\t#{File.basename(@params["advfile"])}\t#{@producer.res.v("advtitle")}\tchaptype=post") end if @params["colophon"] if @params["colophon"].instance_of?(String) # FIXME:このやり方はやめる? FileUtils.cp(@params["colophon"], "#{basetmpdir}/colophon.#{@params["htmlext"]}") else - File.open("#{basetmpdir}/colophon.#{@params["htmlext"]}", "w") {|f| @epub.colophon(f) } + File.open("#{basetmpdir}/colophon.#{@params["htmlext"]}", "w") {|f| @producer.colophon(f) } end - write_tochtmltxt(basetmpdir, "1\tcolophon.#{@params["htmlext"]}\t#{@epub.res.v("colophontitle")}\tchaptype=post") + write_tochtmltxt(basetmpdir, "1\tcolophon.#{@params["htmlext"]}\t#{@producer.res.v("colophontitle")}\tchaptype=post") end if @params["backcover"] FileUtils.cp(@params["backcover"], "#{basetmpdir}/#{File.basename(@params["backcover"])}") - write_tochtmltxt(basetmpdir, "1\t#{File.basename(@params["backcover"])}\t#{@epub.res.v("backcovertitle")}\tchaptype=post") + write_tochtmltxt(basetmpdir, "1\t#{File.basename(@params["backcover"])}\t#{@producer.res.v("backcovertitle")}\tchaptype=post") end end def write_tochtmltxt(basetmpdir, s) File.open("#{basetmpdir}/#{@tochtmltxt}", "a") do |f| @@ -513,34 +515,34 @@ @headlines = headlines end def tag_start(name, attrs) if name =~ /\Ah(\d+)/ - unless @level.nil? + if @level.present? raise "#{name}, #{attrs}" end @level = $1.to_i - @id = attrs["id"] if !attrs["id"].nil? + @id = attrs["id"] if attrs["id"].present? elsif !@level.nil? - if name == "img" && !attrs["alt"].nil? + if name == "img" && attrs["alt"].present? @content << attrs["alt"] - elsif name == "a" && !attrs["id"].nil? + elsif name == "a" && attrs["id"].present? @id = attrs["id"] end end end def tag_end(name) if name =~ /\Ah\d+/ - @headlines.push({"level" => @level, "id" => @id, "title" => @content}) unless @id.nil? + @headlines.push({"level" => @level, "id" => @id, "title" => @content}) if @id.present? @content = "" @level = nil @id = nil end end def text(text) - unless @level.nil? + if @level.present? @content << text.gsub("\t", " ") # FIXME:区切り文字 end end end end