bin/review-epubmaker-ng in review-1.1.0 vs bin/review-epubmaker-ng in review-1.2.0

- old
+ new

@@ -1,176 +1,23 @@ #!/usr/bin/env ruby # encoding: utf-8 # -# Copyright (c) 2010 Kenshi Muto and Masayoshi Takahashi +# Copyright (c) 2010-2013 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 'pathname' bindir = Pathname.new(__FILE__).realpath.dirname $LOAD_PATH.unshift((bindir + '../lib').realpath) -require 'review' -require 'epubmaker' -include EPUBMaker +require 'review/epubmaker' -def main - if ARGV.size != 1 - puts "Usage: #{$0} configfile" - exit 0 - end - - yamlfile = ARGV[0] - @params = ReVIEW::Configure.values.merge(YAML.load_file(yamlfile)) - @epub = Producer.new(@params) - @epub.load(yamlfile) - @params = @epub.params - - bookname = @params["bookname"] - - if File.exist?("#{bookname}.epub") - STDERR.puts "#{bookname}.epub exists. Please remove or rename it first." - exit 1 - end - if @params["debug"] && File.exist?("#{bookname}") - STDERR.puts "#{bookname} debug directory exists. Please remove or rename it first." - exit 1 - end - - basetmp = Dir.mktmpdir - - FileUtils.cp(@params["cover"], "#{basetmp}/#{@params["cover"]}") if !@params["cover"].nil? && File.exist?(@params["cover"]) - - if @params["titlepage"] # FIXME - FileUtils.cp(@params["titlepage"], "#{basetmp}/#{@params["titlepage"]}") - @epub.contents.push(Content.new("id" => "title", "file" => @params["titlepage"], "title" => @epub.res.v("titlepagetitle"))) - end - - makeBody(basetmp, yamlfile) - - if @params["colophon"] - if @params["colophon"].instance_of?(String) - FileUtils.cp(@params["colophon"], "#{basetmp}/colophon.#{@params["htmlext"]}") - else - File.open("#{basetmp}/colophon.#{@params["htmlext"]}", "w") {|f| @epub.colophon(f) } - end - @epub.contents.push(Content.new("id" => "colophon", "file" => "colophon.#{@params["htmlext"]}", "title" => @epub.res.v("colophontitle"))) - end - - epubtmpdir = @params["debug"].nil? ? nil : "#{Dir.pwd}/#{bookname}" - unless @params["debug"].nil? - Dir.mkdir(bookname) - end - @epub.produce("#{bookname}.epub", basetmp, epubtmpdir) - - FileUtils.rm_r(basetmp) +rv = ReVIEW::EPUBMaker.new +if ARGV.size < 1 || !File.exist?(ARGV[0]) + STDERR.puts "Usage: #{$0} YAML_filename [export_filename]" + exit 0 end -def makeBody(basetmp, yamlfile) - pre = 0 - body = 0 - post = 0 - @manifeststr = "" - @ncxstr = "" - @tocdesc = Array.new - toccount = 2 - - if File.exists?("PREDEF") - File.open("PREDEF") do |chaps| - chaps.each_line do |l| - pre += 1 - next if l =~ /\A#/ - fork { - STDOUT.reopen("#{basetmp}/pre#{pre}.#{@params["htmlext"]}") - exec("review-compile --target=html --yaml=#{yamlfile} #{l}") - } - Process.waitall - getanchors(basetmp, "pre#{pre}.#{@params["htmlext"]}") - end - end - end - - if File.exists?("CHAPS") - File.open("CHAPS") do |chaps| - chaps.each_line do |l| - body += 1 - next if l =~ /\A#/ - fork { - STDOUT.reopen("#{basetmp}/chap#{body}.#{@params["htmlext"]}") - exec("review-compile --target=html --yaml=#{yamlfile} #{l}") - } - Process.waitall - getanchors(basetmp, "chap#{body}.#{@params["htmlext"]}") - end - end - end - - if File.exists?("POSTDEF") - File.open("POSTDEF") do |chaps| - chaps.each_line do |l| - post += 1 - next if l =~ /^#/ - fork { - STDOUT.reopen("#{basetmp}/post#{post}.#{@params["htmlext"]}") - exec("review-compile --target=html --yaml=#{yamlfile} #{l}") - } - Process.waitall - getanchors(basetmp, "post#{post}.#{@params["htmlext"]}") - end - end - end - if File.exist?("images") - Dir.mkdir("#{basetmp}/images") - copyImagesToDir("images", "#{basetmp}/images") - end -end - -def getanchors(basetmp, filename) - File.open("#{basetmp}/#{filename}") do |f| - firsttime = true - f.each_line do |l| - if l =~ /\A<h(\d)><a id=\"(.+?)\" \/>(.+?)<\/h/ - # level, ID, content - level = $1.to_i - id = $2 - title = $3 - if level == 1 && !firsttime.nil? - @epub.contents.push(Content.new({ - "id" => "#{filename.sub(".#{@params["htmlext"]}", '')}", - "file" => "#{filename}", - "level" => level, - "title" => title - })) - firsttime = nil - else - @epub.contents.push(Content.new({ - "id" => "#{filename.sub(".#{@params["htmlext"]}", '')}-#{id}", - "file" => "#{filename}##{id}", - "level" => level, - "title" => title - })) - end - end - end - end -end - -def copyImagesToDir(dirname, copybase) - Dir.open(dirname) do |dir| - dir.each do |fname| - next if fname =~ /\A\./ - if FileTest.directory?("#{dirname}/#{fname}") - copyImagesToDir("#{dirname}/#{fname}", "#{copybase}/#{fname}") - else - if fname =~ /\.(png|gif|jpg|jpeg|svg)\Z/i - Dir.mkdir(copybase) unless File.exist?(copybase) - FileUtils.cp "#{dirname}/#{fname}", copybase - end - end - end - end -end - -main +bookname = (ARGV.size > 1) ? ARGV[1] : nil +rv.produce(ARGV[0], bookname)