diff --git a/lib/review/pdfmaker.rb b/lib/review/pdfmaker.rb index d7d72c052..63aae1242 100644 --- a/lib/review/pdfmaker.rb +++ b/lib/review/pdfmaker.rb @@ -52,7 +52,16 @@ def remove_old_file end def build_path - "./#{@config["bookname"]}-pdf" + if @config["debug"] + path = "#{@config["bookname"]}-pdf" + if File.exist?(path) + FileUtils.rm_rf(path, :secure => true) + end + Dir.mkdir(path) + return path + else + return Dir.mktmpdir("#{@config["bookname"]}-pdf-") + end end def check_compile_status(ignore_errors) @@ -117,88 +126,89 @@ def execute(*args) def generate_pdf(yamlfile) remove_old_file @path = build_path() - Dir.mkdir(@path) - - @chaps_fnames = Hash.new{|h, key| h[key] = ""} - @compile_errors = nil - - book = ReVIEW::Book.load(@basedir) - book.config = @config - @converter = ReVIEW::Converter.new(book, ReVIEW::LATEXBuilder.new) - book.parts.each do |part| - if part.name.present? - if part.file? - output_chaps(part.name, yamlfile) - @chaps_fnames["CHAPS"] << %Q|\\input{#{part.name}.tex}\n| - else - @chaps_fnames["CHAPS"] << %Q|\\part{#{part.name}}\n| + begin + @chaps_fnames = Hash.new{|h, key| h[key] = ""} + @compile_errors = nil + + book = ReVIEW::Book.load(@basedir) + book.config = @config + @converter = ReVIEW::Converter.new(book, ReVIEW::LATEXBuilder.new) + book.parts.each do |part| + if part.name.present? + if part.file? + output_chaps(part.name, yamlfile) + @chaps_fnames["CHAPS"] << %Q|\\input{#{part.name}.tex}\n| + else + @chaps_fnames["CHAPS"] << %Q|\\part{#{part.name}}\n| + end end - end - part.chapters.each do |chap| - filename = File.basename(chap.path, ".*") - output_chaps(filename, yamlfile) - @chaps_fnames["PREDEF"] << "\\input{#{filename}.tex}\n" if chap.on_PREDEF? - @chaps_fnames["CHAPS"] << "\\input{#{filename}.tex}\n" if chap.on_CHAPS? - @chaps_fnames["APPENDIX"] << "\\input{#{filename}.tex}\n" if chap.on_APPENDIX? - @chaps_fnames["POSTDEF"] << "\\input{#{filename}.tex}\n" if chap.on_POSTDEF? + part.chapters.each do |chap| + filename = File.basename(chap.path, ".*") + output_chaps(filename, yamlfile) + @chaps_fnames["PREDEF"] << "\\input{#{filename}.tex}\n" if chap.on_PREDEF? + @chaps_fnames["CHAPS"] << "\\input{#{filename}.tex}\n" if chap.on_CHAPS? + @chaps_fnames["APPENDIX"] << "\\input{#{filename}.tex}\n" if chap.on_APPENDIX? + @chaps_fnames["POSTDEF"] << "\\input{#{filename}.tex}\n" if chap.on_POSTDEF? + end end - end - check_compile_status(@config["ignore-errors"]) + check_compile_status(@config["ignore-errors"]) - @config["pre_str"] = @chaps_fnames["PREDEF"] - @config["chap_str"] = @chaps_fnames["CHAPS"] - @config["appendix_str"] = @chaps_fnames["APPENDIX"] - @config["post_str"] = @chaps_fnames["POSTDEF"] + @config["pre_str"] = @chaps_fnames["PREDEF"] + @config["chap_str"] = @chaps_fnames["CHAPS"] + @config["appendix_str"] = @chaps_fnames["APPENDIX"] + @config["post_str"] = @chaps_fnames["POSTDEF"] - @config["usepackage"] = "" - if @config["texstyle"] - @config["usepackage"] = "\\usepackage{#{@config['texstyle']}}" - end + @config["usepackage"] = "" + if @config["texstyle"] + @config["usepackage"] = "\\usepackage{#{@config['texstyle']}}" + end - copy_images("./images", File.join(@path, "images")) - copyStyToDir(File.join(Dir.pwd, "sty"), @path) - copyStyToDir(File.join(Dir.pwd, "sty"), @path, "fd") - copyStyToDir(File.join(Dir.pwd, "sty"), @path, "cls") - copyStyToDir(Dir.pwd, @path, "tex") + copy_images("./images", File.join(@path, "images")) + copyStyToDir(File.join(Dir.pwd, "sty"), @path) + copyStyToDir(File.join(Dir.pwd, "sty"), @path, "fd") + copyStyToDir(File.join(Dir.pwd, "sty"), @path, "cls") + copyStyToDir(Dir.pwd, @path, "tex") - template = get_template - Dir.chdir(@path) do - File.open("./book.tex", "wb"){|f| f.write(template)} + template = get_template + Dir.chdir(@path) do + File.open("./book.tex", "wb"){|f| f.write(template)} - call_hook("hook_beforetexcompile") + call_hook("hook_beforetexcompile") - ## do compile - kanji = 'utf8' - texcommand = "platex" - texoptions = "-kanji=#{kanji}" - dvicommand = "dvipdfmx" - dvioptions = "-d 5" + ## do compile + kanji = 'utf8' + texcommand = "platex" + texoptions = "-kanji=#{kanji}" + dvicommand = "dvipdfmx" + dvioptions = "-d 5" - if ENV["REVIEW_SAFE_MODE"].to_i & 4 > 0 - warn "command configuration is prohibited in safe mode. ignored." - else - texcommand = @config["texcommand"] if @config["texcommand"] - dvicommand = @config["dvicommand"] if @config["dvicommand"] - dvioptions = @config["dvioptions"] if @config["dvioptions"] - texoptions = @config["texoptions"] if @config["texoptions"] - end - 3.times do - system_or_raise("#{texcommand} #{texoptions} book.tex") - end - call_hook("hook_aftertexcompile") + if ENV["REVIEW_SAFE_MODE"].to_i & 4 > 0 + warn "command configuration is prohibited in safe mode. ignored." + else + texcommand = @config["texcommand"] if @config["texcommand"] + dvicommand = @config["dvicommand"] if @config["dvicommand"] + dvioptions = @config["dvioptions"] if @config["dvioptions"] + texoptions = @config["texoptions"] if @config["texoptions"] + end + 3.times do + system_or_raise("#{texcommand} #{texoptions} book.tex") + end + call_hook("hook_aftertexcompile") - if File.exist?("book.dvi") - system_or_raise("#{dvicommand} #{dvioptions} book.dvi") + if File.exist?("book.dvi") + system_or_raise("#{dvicommand} #{dvioptions} book.dvi") + end end - end - call_hook("hook_afterdvipdf") + call_hook("hook_afterdvipdf") - FileUtils.cp(File.join(@path, "book.pdf"), pdf_filepath) + FileUtils.cp(File.join(@path, "book.pdf"), pdf_filepath) - unless @config["debug"] - remove_entry_secure @path + ensure + unless @config["debug"] + remove_entry_secure @path + end end end diff --git a/test/test_pdfmaker.rb b/test/test_pdfmaker.rb index 164ce2f4e..80eabcc25 100644 --- a/test/test_pdfmaker.rb +++ b/test/test_pdfmaker.rb @@ -43,8 +43,14 @@ def test_check_book_none end end - def test_buildpath - assert_equal(@maker.build_path, "./sample-pdf") + def test_buildpath_debug + @maker.config["debug"] = true + path = @maker.build_path + begin + assert_equal(path, "sample-pdf") + ensure + FileUtils.remove_entry_secure path + end end def test_parse_opts_help