bin/review-pdfmaker in review-1.0.0 vs bin/review-pdfmaker in review-1.1.0

- old
+ new

@@ -10,11 +10,18 @@ # 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 @@ -30,42 +37,52 @@ end end def build_path(values) if values["debug"].nil? - Dir.mktmpdir+"/#{values["bookname"]}" + Dir.mktmpdir+"/#{values["bookname"]}-pdf" else - "./#{values["bookname"]}" + "./#{values["bookname"]}-pdf" end end def main usage if ARGV.size != 1 yamlfile = ARGV[0] - values = YAML.load_file(yamlfile) + values = ReVIEW::Configure.values.merge(YAML.load_file(yamlfile)) check_book(values) - basedir = Dir.pwd + @basedir = Dir.pwd @path = build_path(values) bookname = values["bookname"] Dir.mkdir(@path) - @chaps_filenames = Hash.new{|h, key| h[key] = ""} + @chaps_fnames = Hash.new{|h, key| h[key] = ""} - if File.exists?("PREDEF") - output_chaps("PREDEF", values) + 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 - if File.exists?("CHAPS") - output_chaps("CHAPS", values) - end - if File.exists?("POSTDEF") - output_chaps("POSTDEF", values) - end - values["pre_str"] = @chaps_filenames["PREDEF"] - values["chap_str"] = @chaps_filenames["CHAPS"] - values["post_str"] = @chaps_filenames["POSTDEF"] + 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 @@ -74,11 +91,11 @@ 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") @@ -95,100 +112,66 @@ fork { exec("dvipdfmx -d 5 book.dvi") } Process.waitall } - FileUtils.cp("#{@path}/book.pdf", "#{basedir}/#{bookname}.pdf") + FileUtils.cp("#{@path}/book.pdf", "#{@basedir}/#{bookname}.pdf") end -def output_chaps(chapsfile, values) - File.open(chapsfile) {|chaps| - chaps.each_line {|l| - next if l =~ /^#/ - file_id = File.basename(l.chomp.strip,".*") - filename = "#{file_id}.tex" - fork { - STDOUT.reopen("#{@path}/#{filename}") - $stderr.puts "compiling #{l}" - exec("review-compile --target=latex --level=#{values["secnolevel"]} #{values["params"]} #{l}") - } - Process.waitall - @chaps_filenames[chapsfile] << %Q|\\input{#{filename}}\n| - } +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) - copyImagesToDir(from, to) + ReVIEW::MakerHelper.copy_images_to_dir(from, to) Dir.chdir(to) { fork { begin - exec("extractbb *.png *.jpg */*.jpg */*.png;extractbb -m *.png *.jpg */*.jpg */*.png") + exec("extractbb *.png *.jpg *.pdf */*.jpg */*.png */*.pdf;extractbb -m *.png *.jpg *.pdf */*.jpg */*.png */*.pdf") rescue - exec("ebb *.png *.jpg */*.jpg */*.png") + exec("ebb *.png *.jpg *.pdf */*.jpg */*.png */*.pdf") end } } Process.waitall end end -def make_titlepage(values, authors) - coverfile = values["coverfile"] - if coverfile - coverfile_sty = coverfile.sub(/\.[^.]+$/, ".tex") - if File.exist?(coverfile_sty) - titlepage = File.read(coverfile_sty) - return titlepage - 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 - - ## use default titlepage template - titlepage = <<EOB -\\begin{titlepage} -EOB - if values["coverimage"] - titlepage += <<EOB - \\begin{center} - %%%\\mbox{}\\vskip5zw% - \\includegraphics[width=\\textwidth,height=\\textheight,keepaspectratio]{./images/#{values["coverimage"]}} - \\end{center} - \\clearpage -EOB - end - titlepage += <<EOB -\\thispagestyle{empty} -\\begin{center}% - \\mbox{} \\vskip5zw - \\reviewtitlefont% - {\\Huge #{values["booktitle"]} \\par}% - \\vskip 15em% - {\\huge - \\lineskip .75em - \\begin{tabular}[t]{c}% - #{authors} - \\end{tabular}\\par}% - \\vfill - {\\large #{values["date"]} 版\\hspace{2zw}#{values["prt"]} 発行\\par}% -\\vskip4zw\\mbox{} - \\end{center}% -\\end{titlepage} -EOB - - return titlepage 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"] @@ -200,222 +183,23 @@ end okuduke += <<EOB 発行所 & #{values["prt"]} \\\\ EOB - titlepage = make_titlepage(values, authors) + custom_titlepage = make_custom_titlepage(values["coverfile"]) - str = <<EOB -\\documentclass[#{documentclassoption}]{#{documentclass}} -\\usepackage[deluxe]{otf} -\\usepackage[dvipdfmx]{color} -\\usepackage[dvipdfmx]{graphicx} -\\usepackage{framed} -\\usepackage{wrapfig} -\\definecolor{shadecolor}{gray}{0.9} -\\definecolor{shadecolorb}{gray}{0.1} + template = File.expand_path(File.dirname(__FILE__) + + '/../lib/review/review.tex.erb') + layout_file = File.join(@basedir, "layouts", "layout.tex.erb") + if File.exists?(layout_file) + template = layout_file + end -\\usepackage[utf8]{inputenc} - -\\usepackage{jumoline} - -\\usepackage{ascmac} - -\\usepackage{float} -\\usepackage{alltt} - -\\newenvironment{shadedb}{% - \\def\\FrameCommand{\\fboxsep=\\FrameSep \\colorbox{shadecolorb}}% - \\MakeFramed {\\FrameRestore}}% - {\\endMakeFramed} - - -\\usepackage[top=10zw,bottom=12zw,left=10zw,right=10zw]{geometry} -%\\usepackage[top=5zw,bottom=5zw,left=1zw,right=1zw]{geometry} - - -\\newcommand{\\parasep}{\\vspace*{3zh}} -\\setlength{\\footskip}{30pt} - -%% Bookmarkの文字化け対策(日本語向け) -\\ifnum 46273=\\euc"B4C1 % 46273 == 0xB4C1 == 漢(EUC-JP) - \\usepackage{atbegshi}% - \\AtBeginShipoutFirst{\\special{pdf:tounicode EUC-UCS2}}% - %%\\AtBeginDvi{\\special{pdf:tounicode EUC-UCS2}}% -\\else - \\usepackage{atbegshi}% - \\AtBeginShipoutFirst{\\special{pdf:tounicode 90ms-RKSJ-UCS2}}% - %%\\AtBeginDvi{\\special{pdf:tounicode 90ms-RKSJ-UCS2}}% -\\fi - -\\usepackage[dvipdfm,bookmarks=true,bookmarksnumbered=true,colorlinks=true,% - pdftitle={#{values["booktitle"]}},% - pdfauthor={#{values["aut"]}}]{hyperref} - -\\newenvironment{reviewimage}{% - \\begin{figure}[H] - \\begin{center}}{% - \\end{center} - \\end{figure}} - -\\newenvironment{reviewdummyimage}{% - \\begin{figure}[H] - \\begin{center}}{% - \\end{center} - \\end{figure}} - -\\newenvironment{reviewemlist}{% - \\medskip\\small\\begin{shaded}\\setlength{\\baselineskip}{1.3zw}}{% - \\end{shaded}} - -\\newenvironment{reviewlist}{% - \\begin{shaded}\\small\\setlength{\\baselineskip}{1.3zw}}{% - \\end{shaded}\\par\\vspace*{0.5zw}} - -\\newenvironment{reviewcmd}{% - \\color{white}\\medskip\\small\\begin{shadedb}\\setlength{\\baselineskip}{1.3zw}}{% - \\end{shadedb}} - -\\newenvironment{reviewtable}[1]{% - \\begin{center}\\small\\setlength{\\baselineskip}{1.2zw} - \\begin{tabular}{#1}}{% - \\end{tabular} - \\end{center}} - -\\newenvironment{reviewcolumn}{% - \\begin{framed} - }{% - \\end{framed} - \\vspace{2zw}} - -\\newcommand{\\reviewcolumnhead}[2]{% -{\\noindent\\large ■コラム: #2}} - -\\newcommand{\\reviewtablecaption}[1]{% - \\caption{#1}} - -\\newcommand{\\reviewbackslash}[0]{% - \\textbackslash{}} - -\\newcommand{\\reviewlistcaption}[1]{% - \\medskip{\\small\\noindent #1}\\vspace*{-1.3zw}} - -\\newcommand{\\reviewemlistcaption}[1]{% - \\medskip{\\small\\noindent #1}\\vspace*{-1.3zw}} - -\\newcommand{\\reviewcmdcaption}[1]{% - \\medskip{\\small\\noindent #1}\\vspace*{-1.3zw}} - -\\newcommand{\\reviewimageref}[1]{% - 図 #1} -\\newcommand{\\reviewtableref}[1]{% - 表 #1} -\\newcommand{\\reviewlistref}[1]{% - リスト #1} - -\\newcommand{\\reviewminicolumntitle}[1]{% - {\\large ■メモ:#1}\\\\} - -\\newenvironment{reviewminicolumn}{% - \\vspace{1.5zw}\\begin{screen}}{% - \\end{screen}\\vspace{2zw}} - -\\newcommand{\\reviewkw}[1]{% - \\textbf{\\textgt{#1}}} - -\\newcommand{\\reviewtitlefont}[0]{% - \\usefont{T1}{phv}{b}{n}\\gtfamily} - -\\newcommand{\\reviewmainfont}[0]{% - } - -\\newcommand{\\reviewcolophon}[0]{% - \\backmatter - \\clearpage} -\\newcommand{\\reviewappendix}[0]{% - \\appendix} - -\\makeatletter -\\def\\maxwidth{% - \\ifdim\\Gin@nat@width>\\linewidth - \\linewidth - \\else - \\Gin@nat@width - \\fi -} -\\makeatother - -#{values["usepackage"]} - -\\usepackage[T1]{fontenc} - -\\begin{document} - -\\reviewmainfont -EOB - - str += titlepage - str += <<EOB - -\\renewcommand{\\chaptermark}[1]{{}} -\\frontmatter - -%% preface -#{values["pre_str"]} - -\\tableofcontents - -\\renewcommand{\\chaptermark}[1]{\\markboth{\\prechaptername\\thechapter\\postchaptername~#1}{}} -\\mainmatter - -#{values["chap_str"]} - -\\renewcommand{\\chaptermark}[1]{\\markboth{\\appendixname\\thechapter~#1}{}} -\\reviewappendix - -#{values["post_str"]} - -%% okuduke -\\reviewcolophon -\\thispagestyle{empty} - -\\vspace*{\\fill} - -{\\noindent\\reviewtitlefont\\Large #{values["booktitle"]}} \\\\ -\\rule[8pt]{14cm}{1pt} \\\\ -{\\noindent -#{values["pubhistory"].to_s.gsub(/\n/){"\n\n\\noindent"} } -} - -\\begin{tabular}{ll} -#{okuduke} -\\end{tabular} - \\\\ -\\rule[0pt]{14cm}{1pt} \\\\ -#{values["rights"]} \\\\ - -\\end{document} - -EOB + erb = ERB.new(File.open(template).read) + erb.result(binding) end -def copyImagesToDir(dirname, copybase) - Dir.open(dirname) {|dir| - dir.each {|fname| - next if fname =~ /^\./ - if FileTest.directory?("#{dirname}/#{fname}") - copyImagesToDir("#{dirname}/#{fname}", "#{copybase}/#{fname}") - else - if fname =~ /\.(png|gif|jpg|jpeg|svg|pdf)$/i - Dir.mkdir(copybase) unless File.exist?(copybase) - FileUtils.cp "#{dirname}/#{fname}", copybase - end - end - } - } -end - def copyStyToDir(dirname, copybase, extname = "sty") unless File.directory?(dirname) $stderr.puts "No such directory - #{dirname}" return end @@ -428,8 +212,7 @@ FileUtils.cp "#{dirname}/#{fname}", copybase end } } end - main