Class: ReVIEW::PDFMaker
- Includes:
- FileUtils, LaTeXUtils
- Defined in:
- lib/review/pdfmaker.rb
Constant Summary
Constants included from LaTeXUtils
LaTeXUtils::METACHARS, LaTeXUtils::METACHARS_INVERT, LaTeXUtils::METACHARS_RE
Instance Attribute Summary collapse
-
#basedir ⇒ Object
Returns the value of attribute basedir.
-
#config ⇒ Object
Returns the value of attribute config.
Class Method Summary collapse
Instance Method Summary collapse
- #build_path ⇒ Object
- #call_hook(hookname) ⇒ Object
- #check_compile_status(ignore_errors) ⇒ Object
-
#copy_images(from, to) ⇒ Object
PDFMaker#copy_images should copy image files AND execute extractbb (or ebb).
- #copyStyToDir(dirname, copybase, extname = "sty") ⇒ Object
- #error(msg) ⇒ Object
- #execute(*args) ⇒ Object
- #generate_pdf(yamlfile) ⇒ Object
- #get_template ⇒ Object
-
#initialize ⇒ PDFMaker
constructor
A new instance of PDFMaker.
- #join_with_separator(value, sep) ⇒ Object
- #make_authors ⇒ Object
- #make_colophon ⇒ Object
- #make_colophon_role(role) ⇒ Object
- #make_custom_page(file) ⇒ Object
- #output_chaps(filename, yamlfile) ⇒ Object
- #parse_opts(args) ⇒ Object
- #pdf_filepath ⇒ Object
- #remove_old_file ⇒ Object
- #system_or_raise(*args) ⇒ Object
- #warn(msg) ⇒ Object
Methods included from LaTeXUtils
#escape_index, #escape_latex, #escape_url, #macro, #unescape_latex
Constructor Details
#initialize ⇒ PDFMaker
Returns a new instance of PDFMaker.
27 28 29 |
# File 'lib/review/pdfmaker.rb', line 27 def initialize @basedir = Dir.pwd end |
Instance Attribute Details
#basedir ⇒ Object
Returns the value of attribute basedir.
25 26 27 |
# File 'lib/review/pdfmaker.rb', line 25 def basedir @basedir end |
#config ⇒ Object
Returns the value of attribute config.
25 26 27 |
# File 'lib/review/pdfmaker.rb', line 25 def config @config end |
Class Method Details
.execute(*args) ⇒ Object
66 67 68 |
# File 'lib/review/pdfmaker.rb', line 66 def self.execute(*args) self.new.execute(*args) end |
Instance Method Details
#build_path ⇒ Object
52 53 54 |
# File 'lib/review/pdfmaker.rb', line 52 def build_path "./#{@config["bookname"]}-pdf" end |
#call_hook(hookname) ⇒ Object
320 321 322 323 324 325 326 327 328 329 |
# File 'lib/review/pdfmaker.rb', line 320 def call_hook(hookname) if @config["pdfmaker"].instance_of?(Hash) && @config["pdfmaker"][hookname] hook = File.absolute_path(@config["pdfmaker"][hookname], @basedir) if ENV["REVIEW_SAFE_MODE"].to_i & 1 > 0 warn "hook configuration is prohibited in safe mode. ignored." else system_or_raise("#{hook} #{Dir.pwd} #{@basedir}") end end end |
#check_compile_status(ignore_errors) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/review/pdfmaker.rb', line 56 def check_compile_status(ignore_errors) return unless @compile_errors if ignore_errors $stderr.puts "compile error, but try to generate PDF file" else error "compile error, No PDF file output." end end |
#copy_images(from, to) ⇒ Object
PDFMaker#copy_images should copy image files AND execute extractbb (or ebb).
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/review/pdfmaker.rb', line 207 def copy_images(from, to) if File.exist?(from) Dir.mkdir(to) ReVIEW::MakerHelper.copy_images_to_dir(from, to) Dir.chdir(to) do images = Dir.glob("**/*").find_all{|f| File.file?(f) and f =~ /\.(jpg|jpeg|png|pdf)\z/ } break if images.empty? system("extractbb", *images) unless system("extractbb", "-m", *images) system_or_raise("ebb", *images) end end end end |
#copyStyToDir(dirname, copybase, extname = "sty") ⇒ Object
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/review/pdfmaker.rb', line 304 def copyStyToDir(dirname, copybase, extname = "sty") unless File.directory?(dirname) warn "No such directory - #{dirname}" return end Dir.open(dirname) do |dir| dir.each do |fname| if File.extname(fname).downcase == "."+extname FileUtils.mkdir_p(copybase) FileUtils.cp File.join(dirname, fname), copybase end end end end |
#error(msg) ⇒ Object
35 36 37 38 |
# File 'lib/review/pdfmaker.rb', line 35 def error(msg) $stderr.puts "#{File.basename($0, '.*')}: error: #{msg}" exit 1 end |
#execute(*args) ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/review/pdfmaker.rb', line 96 def execute(*args) @config = ReVIEW::Configure.values cmd_config, yamlfile = parse_opts(args) @config.merge!(YAML.load_file(yamlfile)) # YAML configs will be overridden by command line options. @config.merge!(cmd_config) I18n.setup(@config["language"]) generate_pdf(yamlfile) end |
#generate_pdf(yamlfile) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/review/pdfmaker.rb', line 107 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 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 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 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["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") Dir.chdir(@path) do template = get_template File.open("./book.tex", "wb"){|f| f.write(template)} call_hook("hook_beforetexcompile") ## do compile kanji = 'utf8' texcommand = "platex" = "-kanji=#{kanji}" dvicommand = "dvipdfmx" = "-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"] = @config["dvioptions"] if @config["dvioptions"] = @config["texoptions"] if @config["texoptions"] end 3.times do system_or_raise("#{texcommand} #{} book.tex") end call_hook("hook_aftertexcompile") if File.exist?("book.dvi") system_or_raise("#{dvicommand} #{} book.dvi") end end call_hook("hook_afterdvipdf") FileUtils.cp(File.join(@path, "book.pdf"), pdf_filepath) unless @config["debug"] remove_entry_secure @path end end |
#get_template ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/review/pdfmaker.rb', line 274 def get_template dclass = @config["texdocumentclass"] || [] documentclass = dclass[0] || "jsbook" documentclassoption = dclass[1] || "oneside" okuduke = make_colophon = custom_titlepage = make_custom_page(@config["cover"]) || make_custom_page(@config["coverfile"]) custom_originaltitlepage = make_custom_page(@config["originaltitlefile"]) custom_creditpage = make_custom_page(@config["creditfile"]) custom_profilepage = make_custom_page(@config["profile"]) custom_advfilepage = make_custom_page(@config["advfile"]) if @config["colophon"] && @config["colophon"].kind_of?(String) custom_colophonpage = make_custom_page(@config["colophon"]) end custom_backcoverpage = make_custom_page(@config["backcover"]) template = File.('layout.tex.erb', File.dirname(__FILE__)) 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 |
#join_with_separator(value, sep) ⇒ Object
233 234 235 236 237 238 239 |
# File 'lib/review/pdfmaker.rb', line 233 def join_with_separator(value, sep) if value.kind_of? Array value.join(sep) else value end end |
#make_authors ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/review/pdfmaker.rb', line 257 def = "" if @config["aut"].present? = join_with_separator(@config["aut"], ReVIEW::I18n.t("names_splitter")) = ReVIEW::I18n.t("author_with_label", ) end if @config["csl"].present? csl_names = join_with_separator(@config["csl"], ReVIEW::I18n.t("names_splitter")) += " \\\\\n"+ ReVIEW::I18n.t("supervisor_with_label", csl_names) end if @config["trl"].present? trl_names = join_with_separator(@config["trl"], ReVIEW::I18n.t("names_splitter")) += " \\\\\n"+ ReVIEW::I18n.t("translator_with_label", trl_names) end end |
#make_colophon ⇒ Object
249 250 251 252 253 254 255 |
# File 'lib/review/pdfmaker.rb', line 249 def make_colophon colophon = "" @config["colophon_order"].each do |role| colophon += make_colophon_role(role) end colophon end |
#make_colophon_role(role) ⇒ Object
241 242 243 244 245 246 247 |
# File 'lib/review/pdfmaker.rb', line 241 def make_colophon_role(role) if @config[role].present? return "#{ReVIEW::I18n.t(role)} & #{escape_latex(join_with_separator(@config[role], ReVIEW::I18n.t("names_splitter")))} \\\\\n" else "" end end |
#make_custom_page(file) ⇒ Object
224 225 226 227 228 229 230 231 |
# File 'lib/review/pdfmaker.rb', line 224 def make_custom_page(file) file_sty = file.to_s.sub(/\.[^.]+$/, ".tex") if File.exist?(file_sty) File.read(file_sty) else nil end end |
#output_chaps(filename, yamlfile) ⇒ Object
194 195 196 197 198 199 200 201 202 203 |
# File 'lib/review/pdfmaker.rb', line 194 def output_chaps(filename, yamlfile) $stderr.puts "compiling #{filename}.tex" cmd = "#{ReVIEW::MakerHelper.bindir}/review-compile-peg --yaml=#{yamlfile} --target=latex --level=#{@config["secnolevel"]} --toclevel=#{@config["toclevel"]} #{@config["params"]} #{filename}.re > #{@path}/#{filename}.tex" if system cmd # OK else @compile_errors = true warn cmd end end |
#parse_opts(args) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/review/pdfmaker.rb', line 70 def parse_opts(args) cmd_config = Hash.new opts = OptionParser.new opts. = "Usage: review-pdfmaker 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!(args) if args.size != 1 puts opts.help exit 0 end return cmd_config, args[0] end |
#pdf_filepath ⇒ Object
44 45 46 |
# File 'lib/review/pdfmaker.rb', line 44 def pdf_filepath File.join(@basedir, @config["bookname"]+".pdf") end |
#remove_old_file ⇒ Object
48 49 50 |
# File 'lib/review/pdfmaker.rb', line 48 def remove_old_file FileUtils.rm_f(pdf_filepath) end |
#system_or_raise(*args) ⇒ Object
31 32 33 |
# File 'lib/review/pdfmaker.rb', line 31 def system_or_raise(*args) Kernel.system(*args) or raise("failed to run command: #{args.join(' ')}") end |
#warn(msg) ⇒ Object
40 41 42 |
# File 'lib/review/pdfmaker.rb', line 40 def warn(msg) $stderr.puts "#{File.basename($0, '.*')}: warning: #{msg}" end |