#!/usr/bin/env ruby # encoding: utf-8 # # Copyright (c) 2010 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 'tmpdir' require 'yaml' require 'fileutils' def error(msg) $stderr.puts "#{File.basename($0, '.*')}: error: #{msg}" exit 1 end def usage $stderr.puts "Usage: #{$0} configfile" exit 0 end def check_book(values) pdf_file = values["bookname"]+".pdf" if File.exists? pdf_file error "file already exists:#{pdf_file}" end end def build_path(values) if values["debug"].nil? Dir.mktmpdir+"/#{values["bookname"]}" else "./#{values["bookname"]}" end end def main usage if ARGV.size != 1 yamlfile = ARGV[0] values = YAML.load_file(yamlfile) check_book(values) basedir = Dir.pwd path = build_path(values) bookname = values["bookname"] Dir.mkdir(path) pre = 0 body = 0 post = 0 @pre_str = "" @chap_str = "" @post_str = "" toccount = 2 if File.exists?("PREDEF") File.open("PREDEF") {|chaps| chaps.each_line {|l| next if l =~ /^#/ pre = pre + 1 toccount = toccount + 1 fork { STDOUT.reopen("#{path}/pre#{pre}.tex") exec("review-compile --target=latex --level=1 #{values["params"]} #{l}") } Process.waitall @pre_str << %Q|\\input{pre#{pre}.tex}\n| } } end if File.exists?("CHAPS") File.open("CHAPS") {|chaps| chaps.each_line {|l| body = body + 1 toccount = toccount + 1 next if l =~ /^#/ fork { STDOUT.reopen("#{path}/chap#{body}.tex") exec("review-compile --target=latex --level=#{values["secnolevel"]} #{values["params"]} #{l}") } Process.waitall @chap_str << %Q|\\input{chap#{body}.tex}\n| } } end if File.exists?("POSTDEF") File.open("POSTDEF") {|chaps| chaps.each_line {|l| next if l =~ /^#/ post = post + 1 toccount = toccount + 1 fork { STDOUT.reopen("#{path}/post#{post}.tex") exec("review-compile --target=latex --level=1 #{values["params"]} #{l}") } Process.waitall @post_str << %Q|\\input{post#{post}.tex}\n| } } end values["pre_str"] = @pre_str values["chap_str"] = @chap_str values["post_str"] = @post_str values["usepackage"] = "" if values["texstyle"] values["usepackage"] = "\\usepackage{#{values['texstyle']}}" end copy_images("./images", "#{path}/images") copyStyToDir(Dir.pwd + "/sty", path) Dir.chdir(path) { template = get_template(values) File.open("./book.tex", "wb"){|f| f.write(template)} ## do compile fork { exec("platex book.tex") } Process.waitall fork { exec("platex book.tex") } Process.waitall fork { exec("dvipdfmx -d 5 book.dvi") } Process.waitall } FileUtils.cp("#{path}/book.pdf", "#{basedir}/#{bookname}.pdf") end def copy_images(from, to) if File.exist?(from) Dir.mkdir(to) copyImagesToDir(from, to) Dir.chdir(to) { fork { exec("ebb *.png *.jpg") } } Process.waitall end end def get_template(values) str = <