bin/review-pdfmaker in review-1.2.0 vs bin/review-pdfmaker in review-1.3.0
- old
+ new
@@ -11,156 +11,190 @@
require 'tmpdir'
require 'yaml'
require 'fileutils'
require 'erb'
+require 'optparse'
require 'pathname'
bindir = Pathname.new(__FILE__).realpath.dirname
$LOAD_PATH.unshift((bindir + '../lib').realpath)
require 'review'
require 'review/i18n'
+include ReVIEW::LaTeXUtils
+
+include FileUtils
+
def error(msg)
$stderr.puts "#{File.basename($0, '.*')}: error: #{msg}"
exit 1
end
-def usage
- $stderr.puts "Usage: #{$0} configfile"
- exit 0
+def warn(msg)
+ $stderr.puts "#{File.basename($0, '.*')}: warning: #{msg}"
end
-def check_book(values)
- pdf_file = values["bookname"]+".pdf"
+def check_book(config)
+ pdf_file = config["bookname"]+".pdf"
if File.exist? pdf_file
error "file already exists:#{pdf_file}"
end
end
-def build_path(values)
- if values["debug"].nil?
- Dir.mktmpdir+"/#{values["bookname"]}-pdf"
+def build_path(config)
+ "./#{config["bookname"]}-pdf"
+end
+
+def check_compile_status(ignore_errors)
+ return unless @compile_errors
+
+ if ignore_errors
+ $stderr.puts "compile error, but try to generate PDF file"
else
- "./#{values["bookname"]}-pdf"
+ error "compile error, No PDF file output."
end
end
+
def main
- usage if ARGV.size != 1
+ config = ReVIEW::Configure.values
+ cmd_config = Hash.new
+ opts = OptionParser.new
+ opts.banner = "Usage: #{File.basename($0)} configfile"
+ opts.version = ReVIEW::VERSION
+ opts.on('--help', 'Prints this message and quit.') do
+ puts opts.help
+ exit 0
+ end
+ opts.on('--[no-]debug', 'Keep temporary files.') do |debug|
+ cmd_config["debug"] = debug
+ end
+ opts.on('--ignore-errors', 'Ignore review-compile errors.') do
+ cmd_config["ignore-errors"] = true
+ end
+
+ opts.parse!(ARGV)
+ if ARGV.size != 1
+ puts opts.help
+ exit 0
+ end
yamlfile = ARGV[0]
- values = ReVIEW::Configure.values.merge(YAML.load_file(yamlfile))
- check_book(values)
+ config.merge!(YAML.load_file(yamlfile))
+ # YAML configs will be overridden by command line options.
+ config.merge!(cmd_config)
+
+ check_book(config)
@basedir = Dir.pwd
- @path = build_path(values)
- bookname = values["bookname"]
+ @path = build_path(config)
+ bookname = config["bookname"]
Dir.mkdir(@path)
@chaps_fnames = Hash.new{|h, key| h[key] = ""}
+ @compile_errors = nil
ReVIEW::Book.load(@basedir).parts.each do |part|
if part.name.present?
if part.file?
- output_parts(part.name, values)
+ output_parts(part.name, config)
@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)
+ output_chaps(filename, config)
@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"]
+ check_compile_status(config["ignore-errors"])
- values["usepackage"] = ""
- if values["texstyle"]
- values["usepackage"] = "\\usepackage{#{values['texstyle']}}"
+ config["pre_str"] = @chaps_fnames["PREDEF"]
+ config["chap_str"] = @chaps_fnames["CHAPS"]
+ config["post_str"] = @chaps_fnames["POSTDEF"]
+
+ config["usepackage"] = ""
+ if config["texstyle"]
+ config["usepackage"] = "\\usepackage{#{config['texstyle']}}"
end
%w[aut csl trl dsr ill cov edt].each do |item|
- values[item] = [values[item]] if !values[item].nil? && values[item].instance_of?(String)
+ config[item] = [config[item]] if !config[item].nil? && config[item].instance_of?(String)
end
copy_images("./images", "#{@path}/images")
copyStyToDir(Dir.pwd + "/sty", @path)
copyStyToDir(Dir.pwd, @path, "tex")
Dir.chdir(@path) {
- template = get_template(values)
+ template = get_template(config)
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'
- texcommand = values["texcommand"] || "platex"
- fork {
- exec("#{texcommand} -kanji=#{kanji} book.tex")
- }
- Process.waitall
- fork {
- exec("#{texcommand} -kanji=#{kanji} book.tex")
- }
- Process.waitall
- fork {
- exec("#{texcommand} -kanji=#{kanji} book.tex")
- }
- if texcommand != "lualatex"
- Process.waitall
- fork {
- exec("dvipdfmx -d 5 book.dvi")
- }
- Process.waitall
+ enc = config["params"].to_s.split(/\s+/).find{|i| i =~ /\A--outencoding=/ }
+ kanji = 'utf8'
+ if enc
+ kanji = enc.split(/\=/).last.gsub(/-/, '').downcase
end
+ texcommand = config["texcommand"] || "platex"
+ 3.times do
+ system("#{texcommand} -kanji=#{kanji} book.tex")
+ end
+ if File.exist?("book.dvi")
+ system("dvipdfmx -d 5 book.dvi")
+ end
}
FileUtils.cp("#{@path}/book.pdf", "#{@basedir}/#{bookname}.pdf")
+
+ unless config["debug"]
+ remove_entry_secure @path
+ end
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
+def output_chaps(filename, config)
+ $stderr.puts "compiling #{filename}"
+ cmd = "#{ReVIEW::MakerHelper.bindir}/review-compile --target=latex --level=#{config["secnolevel"]} --toclevel=#{config["toclevel"]} #{config["params"]} #{filename} > #{@path}/#{filename}"
+ if system cmd
+ # OK
+ else
+ @compile_errors = true
+ warn cmd
+ end
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
+def output_parts(filename, config)
+ $stderr.puts "compiling #{filename}.tex"
+ cmd = "review-compile --target=latex --level=#{config["secnolevel"]} --toclevel=#{config["toclevel"]} #{config["params"]} #{filename}.re | sed -e s/\\chapter{/\\part{/ > #{@path}/#{filename}.tex"
+ if system cmd
+ # OK
+ else
+ @compile_errors = true
+ warn cmd
+ end
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
+ Dir.chdir(to) do
+ images = Dir.glob("**/*").find_all{|f|
+ File.file?(f) and f =~ /\.(jpg|jpeg|png|pdf)\z/
}
- }
- Process.waitall
+ system("extractbb", *images)
+ unless system("extractbb", "-m", *images)
+ system("ebb", *images)
+ end
+ end
end
end
def make_custom_titlepage(coverfile)
coverfile_sty = coverfile.to_s.sub(/\.[^.]+$/, ".tex")
@@ -169,54 +203,55 @@
else
nil
end
end
-def get_template(values)
- dclass = values["texdocumentclass"] || []
+def get_template(config)
+ dclass = config["texdocumentclass"] || []
documentclass = dclass[0] || "jsbook"
documentclassoption = dclass[1] || "oneside"
okuduke = ""
authors = ""
- if !values["aut"].nil? && !values["aut"].empty?
- okuduke += "著 者 & #{values["aut"].join(ReVIEW::I18n.t("names_splitter"))} \\\\\n"
- authors = values["aut"].join(ReVIEW::I18n.t("names_splitter")) + ReVIEW::I18n.t("author_postfix")
+ if !config["aut"].nil? && !config["aut"].empty?
+ okuduke += "著 者 & #{escape_latex(config["aut"].join(ReVIEW::I18n.t("names_splitter")))} \\\\\n"
+ authors = config["aut"].join(ReVIEW::I18n.t("names_splitter")) + ReVIEW::I18n.t("author_postfix")
end
- if !values["csl"].nil? && !values["csl"].empty?
- okuduke += "監 修 & #{values["csl"].join(ReVIEW::I18n.t("names_splitter"))} \\\\\n"
- authors += " \\\\\n"+values["csl"].join(ReVIEW::I18n.t("names_splitter")) + ReVIEW::I18n.t("supervisor_postfix")
+ if !config["csl"].nil? && !config["csl"].empty?
+ okuduke += "監 修 & #{escape_latex(config["csl"].join(ReVIEW::I18n.t("names_splitter")))} \\\\\n"
+ authors += " \\\\\n"+config["csl"].join(ReVIEW::I18n.t("names_splitter")) + ReVIEW::I18n.t("supervisor_postfix")
end
- if !values["trl"].nil? && !values["trl"].empty?
- okuduke += "翻 訳 & #{values["trl"].join(ReVIEW::I18n.t("names_splitter"))} \\\\\n"
- authors += " \\\\\n"+values["trl"].join(ReVIEW::I18n.t("names_splitter")) + ReVIEW::I18n.t("translator_postfix")
+ if !config["trl"].nil? && !config["trl"].empty?
+ okuduke += "翻 訳 & #{escape_latex(config["trl"].join(ReVIEW::I18n.t("names_splitter")))} \\\\\n"
+ authors += " \\\\\n"+config["trl"].join(ReVIEW::I18n.t("names_splitter")) + ReVIEW::I18n.t("translator_postfix")
end
- if !values["dsr"].nil? && !values["dsr"].empty?
- okuduke += "デザイン & #{values["dsr"].join(ReVIEW::I18n.t("names_splitter"))} \\\\\n"
+ if !config["dsr"].nil? && !config["dsr"].empty?
+ okuduke += "デザイン & #{escape_latex(config["dsr"].join(ReVIEW::I18n.t("names_splitter")))} \\\\\n"
end
- if !values["ill"].nil? && !values["ill"].empty?
- okuduke += "イラスト & #{values["ill"].join(ReVIEW::I18n.t("names_splitter"))} \\\\\n"
+ if !config["ill"].nil? && !config["ill"].empty?
+ okuduke += "イラスト & #{escape_latex(config["ill"].join(ReVIEW::I18n.t("names_splitter")))} \\\\\n"
end
- if !values["cov"].nil? && !values["cov"].empty?
- okuduke += "表 紙 & #{values["cov"].join(ReVIEW::I18n.t("names_splitter"))} \\\\\n"
+ if !config["cov"].nil? && !config["cov"].empty?
+ okuduke += "表 紙 & #{escape_latex(config["cov"].join(ReVIEW::I18n.t("names_splitter")))} \\\\\n"
end
- if !values["edt"].nil? && !values["edt"].empty?
- okuduke += "編集者 & #{values["edt"].join(ReVIEW::I18n.t("names_splitter"))} \\\\\n"
+ if !config["edt"].nil? && !config["edt"].empty?
+ okuduke += "編集者 & #{escape_latex(config["edt"].join(ReVIEW::I18n.t("names_splitter")))} \\\\\n"
end
okuduke += <<EOB
-発行所 & #{values["prt"]} \\\\
+発行所 & #{config["prt"]} \\\\
EOB
- custom_titlepage = make_custom_titlepage(values["coverfile"])
+ custom_titlepage = make_custom_titlepage(config["coverfile"])
template = File.expand_path(File.dirname(__FILE__) +
'/../lib/review/review.tex.erb')
layout_file = File.join(@basedir, "layouts", "layout.tex.erb")
if File.exist?(layout_file)
template = layout_file
end
erb = ERB.new(File.open(template).read)
+ values = config # must be 'values' for legacy files
erb.result(binding)
end
def copyStyToDir(dirname, copybase, extname = "sty")
unless File.directory?(dirname)