#!/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' require 'erb' require 'pathname' bindir = Pathname.new(__FILE__).realpath.dirname $LOAD_PATH.unshift((bindir + '../lib').realpath) require 'review' 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"]}-pdf" else "./#{values["bookname"]}-pdf" end end def main usage if ARGV.size != 1 yamlfile = ARGV[0] values = ReVIEW::Configure.values.merge(YAML.load_file(yamlfile)) check_book(values) @basedir = Dir.pwd @path = build_path(values) bookname = values["bookname"] Dir.mkdir(@path) @chaps_fnames = Hash.new{|h, key| h[key] = ""} ReVIEW::Book.load(@basedir).parts.each do |part| if part.name.present? if part.file? output_parts(part.name, values) @chaps_fnames["CHAPS"] << %Q|\\input{#{part.name}.tex}\n| else @chaps_fnames["CHAPS"] << %Q|\\part{#{part.name}}\n| end end part.chapters.each do |chap| filename = "#{File.basename(chap.path, ".*")}.tex" output_chaps(filename, values) @chaps_fnames["PREDEF"] << "\\input{#{filename}}\n" if chap.on_PREDEF? @chaps_fnames["CHAPS"] << "\\input{#{filename}}\n" if chap.on_CHAPS? @chaps_fnames["POSTDEF"] << "\\input{#{filename}}\n" if chap.on_POSTDEF? end end values["pre_str"] = @chaps_fnames["PREDEF"] values["chap_str"] = @chaps_fnames["CHAPS"] values["post_str"] = @chaps_fnames["POSTDEF"] values["usepackage"] = "" if values["texstyle"] values["usepackage"] = "\\usepackage{#{values['texstyle']}}" end copy_images("./images", "#{@path}/images") copyStyToDir(Dir.pwd + "/sty", @path) copyStyToDir(Dir.pwd, @path, "tex") Dir.chdir(@path) { template = get_template(values) File.open("./book.tex", "wb"){|f| f.write(template)} ## do compile enc = values["params"].to_s.split(/\s+/).find{|i| i =~ /\A--outencoding=/ } kanji = enc ? enc.split(/=/).last.gsub(/-/, '').downcase : 'utf8' fork { exec("platex -kanji=#{kanji} book.tex") } Process.waitall fork { exec("platex -kanji=#{kanji} book.tex") } Process.waitall fork { exec("platex -kanji=#{kanji} book.tex") } Process.waitall fork { exec("dvipdfmx -d 5 book.dvi") } Process.waitall } FileUtils.cp("#{@path}/book.pdf", "#{@basedir}/#{bookname}.pdf") end def output_chaps(filename, values) fork { STDOUT.reopen("#{@path}/#{filename}") $stderr.puts "compiling #{filename}" exec("review-compile --target=latex --level=#{values["secnolevel"]} --toclevel=#{values["toclevel"]} #{values["params"]} #{filename}") } Process.waitall end def output_parts(filename, values) fork { STDOUT.reopen("#{@path}/#{filename}.tex") $stderr.puts "compiling #{filename}.tex" exec("review-compile --target=latex --level=#{values["secnolevel"]} --toclevel=#{values["toclevel"]} #{values["params"]} #{filename}.re | sed -e s/\\chapter{/\\part{/") } Process.waitall end def copy_images(from, to) if File.exist?(from) Dir.mkdir(to) ReVIEW::MakerHelper.copy_images_to_dir(from, to) Dir.chdir(to) { fork { begin exec("extractbb *.png *.jpg *.pdf */*.jpg */*.png */*.pdf;extractbb -m *.png *.jpg *.pdf */*.jpg */*.png */*.pdf") rescue exec("ebb *.png *.jpg *.pdf */*.jpg */*.png */*.pdf") end } } Process.waitall end end def make_custom_titlepage(coverfile) coverfile_sty = coverfile.to_s.sub(/\.[^.]+$/, ".tex") if File.exist?(coverfile_sty) File.read(coverfile_sty) else nil end end def get_template(values) dclass = values["texdocumentclass"] || [] documentclass = dclass[0] || "jsbook" documentclassoption = dclass[1] || "oneside" okuduke = "" authors = "" if values["aut"] okuduke += "著 者 & #{values["aut"]} \\\\\n" authors = values["aut"]+ "\\vspace*{1zh} 著" end if values["trl"] authors += " \\\\\n"+values["trl"]+ "\\vspace*{1zh} 訳" okuduke += "翻 訳 & #{values["trl"]} \\\\\n" end if values["edt"] okuduke += "編集者 & #{values["edt"]} \\\\\n" end okuduke += <