Class: ReVIEW::PDFMaker
- Includes:
- FileUtils, LaTeXUtils
- Defined in:
- lib/review/pdfmaker.rb
Instance Attribute Summary collapse
-
#basedir ⇒ Object
Returns the value of attribute basedir.
-
#basehookdir ⇒ Object
Returns the value of attribute basehookdir.
-
#config ⇒ Object
Returns the value of attribute config.
Class Method Summary collapse
Instance Method Summary collapse
- #build_path ⇒ Object
- #build_pdf ⇒ 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).
- #copy_sty(dirname, copybase, extname = 'sty') ⇒ Object
- #date_to_s(date) ⇒ Object
- #error(msg) ⇒ Object
- #execute(*args) ⇒ Object
- #generate_pdf(yamlfile) ⇒ 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
- #make_history_list ⇒ Object
- #make_input_files(book, yamlfile) ⇒ Object
- #output_chaps(filename, _yamlfile) ⇒ Object
- #parse_opts(args) ⇒ Object
- #pdf_filepath ⇒ Object
- #remove_old_file ⇒ Object
- #system_or_raise(*args) ⇒ Object
- #template_content ⇒ Object
- #warn(msg) ⇒ Object
Methods included from LaTeXUtils
#escape_index, #escape_latex, #escape_url, #initialize_metachars, #macro, #unescape_latex
Constructor Details
#initialize ⇒ PDFMaker
Returns a new instance of PDFMaker.
31 32 33 34 35 36 |
# File 'lib/review/pdfmaker.rb', line 31 def initialize @basedir = nil @basehookdir = nil @logger = ReVIEW.logger @input_files = Hash.new { |h, key| h[key] = '' } end |
Instance Attribute Details
#basedir ⇒ Object
Returns the value of attribute basedir.
29 30 31 |
# File 'lib/review/pdfmaker.rb', line 29 def basedir @basedir end |
#basehookdir ⇒ Object
Returns the value of attribute basehookdir.
29 30 31 |
# File 'lib/review/pdfmaker.rb', line 29 def basehookdir @basehookdir end |
#config ⇒ Object
Returns the value of attribute config.
29 30 31 |
# File 'lib/review/pdfmaker.rb', line 29 def config @config end |
Class Method Details
.execute(*args) ⇒ Object
80 81 82 |
# File 'lib/review/pdfmaker.rb', line 80 def self.execute(*args) self.new.execute(*args) end |
Instance Method Details
#build_path ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/review/pdfmaker.rb', line 59 def build_path if @config['debug'] path = "#{@config['bookname']}-pdf" FileUtils.rm_rf(path, secure: true) if File.exist?(path) Dir.mkdir(path) path else Dir.mktmpdir("#{@config['bookname']}-pdf-") end end |
#build_pdf ⇒ Object
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 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/review/pdfmaker.rb', line 151 def build_pdf template = template_content Dir.chdir(@path) do File.open('./book.tex', 'wb') { |f| f.write(template) } call_hook('hook_beforetexcompile') ## do compile if ENV['REVIEW_SAFE_MODE'].to_i & 4 > 0 warn 'command configuration is prohibited in safe mode. ignored.' texcommand = ReVIEW::Configure.values['texcommand'] dvicommand = ReVIEW::Configure.values['dvicommand'] = ReVIEW::Configure.values['dvioptions'] = ReVIEW::Configure.values['texoptions'] makeindex_command = ReVIEW::Configure.values['pdfmaker']['makeindex_command'] = ReVIEW::Configure.values['pdfmaker']['makeindex_options'] makeindex_sty = ReVIEW::Configure.values['pdfmaker']['makeindex_sty'] makeindex_dic = ReVIEW::Configure.values['pdfmaker']['makeindex_dic'] else texcommand = @config['texcommand'] if @config['texcommand'] dvicommand = @config['dvicommand'] if @config['dvicommand'] = @config['dvioptions'] if @config['dvioptions'] = @config['texoptions'] if @config['texoptions'] makeindex_command = @config['pdfmaker']['makeindex_command'] = @config['pdfmaker']['makeindex_options'] makeindex_sty = @config['pdfmaker']['makeindex_sty'] makeindex_dic = @config['pdfmaker']['makeindex_dic'] end if makeindex_sty.present? makeindex_sty = File.absolute_path(makeindex_sty, @basedir) += " -s #{makeindex_sty}" if File.exist?(makeindex_sty) end if makeindex_dic.present? makeindex_dic = File.absolute_path(makeindex_dic, @basedir) += " -d #{makeindex_dic}" if File.exist?(makeindex_dic) end 2.times do system_or_raise("#{texcommand} #{texoptions} book.tex") end call_hook('hook_beforemakeindex') system_or_raise("#{makeindex_command} #{makeindex_options} book") if @config['pdfmaker']['makeindex'] && File.exist?('book.idx') call_hook('hook_aftermakeindex') system_or_raise("#{texcommand} #{texoptions} book.tex") call_hook('hook_aftertexcompile') if File.exist?('book.dvi') system_or_raise("#{dvicommand} #{dvioptions} book.dvi") call_hook('hook_afterdvipdf') end end end |
#call_hook(hookname) ⇒ Object
406 407 408 409 410 411 412 413 414 |
# File 'lib/review/pdfmaker.rb', line 406 def call_hook(hookname) return if !@config['pdfmaker'].is_a?(Hash) || @config['pdfmaker'][hookname].nil? hook = File.absolute_path(@config['pdfmaker'][hookname], @basehookdir) 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} #{@basehookdir}") end end |
#check_compile_status(ignore_errors) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/review/pdfmaker.rb', line 70 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).
251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/review/pdfmaker.rb', line 251 def copy_images(from, to) return unless 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|ai|eps|tif)\z/ } break if images.empty? system('extractbb', *images) system_or_raise('ebb', *images) unless system('extractbb', '-m', *images) end end |
#copy_sty(dirname, copybase, extname = 'sty') ⇒ Object
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/review/pdfmaker.rb', line 390 def copy_sty(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 |
#date_to_s(date) ⇒ Object
333 334 335 336 337 |
# File 'lib/review/pdfmaker.rb', line 333 def date_to_s(date) require 'date' d = Date.parse(date) d.strftime(ReVIEW::I18n.t('date_format')) end |
#error(msg) ⇒ Object
42 43 44 45 |
# File 'lib/review/pdfmaker.rb', line 42 def error(msg) @logger.error "#{File.basename($PROGRAM_NAME, '.*')}: #{msg}" exit 1 end |
#execute(*args) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/review/pdfmaker.rb', line 106 def execute(*args) @config = ReVIEW::Configure.values @config.maker = 'pdfmaker' cmd_config, yamlfile = parse_opts(args) loader = ReVIEW::YAMLLoader.new @config.deep_merge!(loader.load_file(yamlfile)) # YAML configs will be overridden by command line options. @config.merge!(cmd_config) I18n.setup(@config['language']) @basedir = File.dirname(yamlfile) @basehookdir = File.absolute_path(File.dirname(yamlfile)) begin @config.check_version(ReVIEW::VERSION) rescue ReVIEW::ConfigError => e warn e. end generate_pdf(yamlfile) end |
#generate_pdf(yamlfile) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/review/pdfmaker.rb', line 207 def generate_pdf(yamlfile) remove_old_file @path = build_path begin @compile_errors = nil book = ReVIEW::Book.load(@basedir) book.config = @config @converter = ReVIEW::Converter.new(book, ReVIEW::LATEXBuilder.new) @input_files = make_input_files(book, yamlfile) check_compile_status(@config['ignore-errors']) @config['usepackage'] = '' @config['usepackage'] = "\\usepackage{#{@config['texstyle']}}" if @config['texstyle'] copy_images(@config['imagedir'], File.join(@path, @config['imagedir'])) copy_sty(File.join(Dir.pwd, 'sty'), @path) copy_sty(File.join(Dir.pwd, 'sty'), @path, 'fd') copy_sty(File.join(Dir.pwd, 'sty'), @path, 'cls') copy_sty(Dir.pwd, @path, 'tex') build_pdf FileUtils.cp(File.join(@path, 'book.pdf'), pdf_filepath) ensure remove_entry_secure @path unless @config['debug'] end end |
#join_with_separator(value, sep) ⇒ Object
269 270 271 272 273 274 275 |
# File 'lib/review/pdfmaker.rb', line 269 def join_with_separator(value, sep) if value.is_a?(Array) value.join(sep) else value end end |
#make_authors ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/review/pdfmaker.rb', line 292 def = '' if @config['aut'].present? = join_with_separator(@config.names_of('aut').map { |s| escape_latex(s) }, ReVIEW::I18n.t('names_splitter')) = ReVIEW::I18n.t('author_with_label', ) end if @config['csl'].present? csl_names = join_with_separator(@config.names_of('csl').map { |s| escape_latex(s) }, 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.names_of('trl').map { |s| escape_latex(s) }, ReVIEW::I18n.t('names_splitter')) += " \\\\\n" + ReVIEW::I18n.t('translator_with_label', trl_names) end end |
#make_colophon ⇒ Object
286 287 288 289 290 |
# File 'lib/review/pdfmaker.rb', line 286 def make_colophon colophon = '' @config['colophon_order'].each { |role| colophon += make_colophon_role(role) } colophon end |
#make_colophon_role(role) ⇒ Object
277 278 279 280 281 282 283 284 |
# File 'lib/review/pdfmaker.rb', line 277 def make_colophon_role(role) if @config[role].present? (@config['texcommand']) "#{ReVIEW::I18n.t(role)} & #{escape_latex(join_with_separator(@config.names_of(role), ReVIEW::I18n.t('names_splitter')))} \\\\\n" else '' end end |
#make_custom_page(file) ⇒ Object
263 264 265 266 267 |
# File 'lib/review/pdfmaker.rb', line 263 def make_custom_page(file) file_sty = file.to_s.sub(/\.[^.]+\Z/, '.tex') return File.read(file_sty) if File.exist?(file_sty) nil end |
#make_history_list ⇒ Object
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/review/pdfmaker.rb', line 309 def make_history_list buf = [] if @config['history'] @config['history'].each_with_index do |items, edit| items.each_with_index do |item, rev| editstr = edit == 0 ? ReVIEW::I18n.t('first_edition') : ReVIEW::I18n.t('nth_edition', (edit + 1).to_s) revstr = ReVIEW::I18n.t('nth_impression', (rev + 1).to_s) if item =~ /\A\d+\-\d+\-\d+\Z/ buf << ReVIEW::I18n.t('published_by1', [date_to_s(item), editstr + revstr]) elsif item =~ /\A(\d+\-\d+\-\d+)[\s ](.+)/ # custom date with string item.match(/\A(\d+\-\d+\-\d+)[\s ](.+)/) { |m| buf << ReVIEW::I18n.t('published_by3', [date_to_s(m[1]), m[2]]) } else # free format buf << item end end end elsif @config['date'] buf << ReVIEW::I18n.t('published_by2', date_to_s(@config['date'])) end buf end |
#make_input_files(book, yamlfile) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/review/pdfmaker.rb', line 126 def make_input_files(book, yamlfile) input_files = Hash.new { |h, key| h[key] = '' } book.parts.each do |part| if part.name.present? if part.file? output_chaps(part.name, yamlfile) input_files['CHAPS'] << %Q(\\input{#{part.name}.tex}\n) else input_files['CHAPS'] << %Q(\\part{#{part.name}}\n) end end part.chapters.each do |chap| filename = File.basename(chap.path, '.*') output_chaps(filename, yamlfile) input_files['PREDEF'] << "\\input{#{filename}.tex}\n" if chap.on_predef? input_files['CHAPS'] << "\\input{#{filename}.tex}\n" if chap.on_chaps? input_files['APPENDIX'] << "\\input{#{filename}.tex}\n" if chap.on_appendix? input_files['POSTDEF'] << "\\input{#{filename}.tex}\n" if chap.on_postdef? end end input_files end |
#output_chaps(filename, _yamlfile) ⇒ Object
238 239 240 241 242 243 244 245 246 247 |
# File 'lib/review/pdfmaker.rb', line 238 def output_chaps(filename, _yamlfile) $stderr.puts "compiling #{filename}.tex" begin @converter.convert(filename + '.re', File.join(@path, filename + '.tex')) rescue => e @compile_errors = true warn "compile error in #{filename}.tex (#{e.class})" warn e. end end |
#parse_opts(args) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/review/pdfmaker.rb', line 84 def parse_opts(args) cmd_config = {} 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.') { |debug| cmd_config['debug'] = debug } opts.on('--ignore-errors', 'Ignore review-compile errors.') { cmd_config['ignore-errors'] = true } opts.parse!(args) if args.size != 1 puts opts.help exit 0 end [cmd_config, args[0]] end |
#pdf_filepath ⇒ Object
51 52 53 |
# File 'lib/review/pdfmaker.rb', line 51 def pdf_filepath File.join(@basedir, @config['bookname'] + '.pdf') end |
#remove_old_file ⇒ Object
55 56 57 |
# File 'lib/review/pdfmaker.rb', line 55 def remove_old_file FileUtils.rm_f(pdf_filepath) end |
#system_or_raise(*args) ⇒ Object
38 39 40 |
# File 'lib/review/pdfmaker.rb', line 38 def system_or_raise(*args) Kernel.system(*args) or raise("failed to run command: #{args.join(' ')}") end |
#template_content ⇒ Object
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/review/pdfmaker.rb', line 339 def template_content dclass = @config['texdocumentclass'] || [] @documentclass = dclass[0] || 'jsbook' @documentclassoption = dclass[1] || 'uplatex,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']) @custom_colophonpage = make_custom_page(@config['colophon']) if @config['colophon'] && @config['colophon'].is_a?(String) @custom_backcoverpage = make_custom_page(@config['backcover']) if @config['pubhistory'] warn 'pubhistory is oboleted. use history.' else @config['pubhistory'] = make_history_list.join("\n") end @coverimageoption = if @documentclass == 'ubook' || @documentclass == 'utbook' 'width=\\textheight,height=\\textwidth,keepaspectratio,angle=90' else 'width=\\textwidth,height=\\textheight,keepaspectratio' end @locale_latex = {} part_tuple = I18n.get('part').split(/\%[A-Za-z]{1,3}/, 2) chapter_tuple = I18n.get('chapter').split(/\%[A-Za-z]{1,3}/, 2) appendix_tuple = I18n.get('appendix').split(/\%[A-Za-z]{1,3}/, 2) @locale_latex['prepartname'] = part_tuple[0] @locale_latex['postpartname'] = part_tuple[1] @locale_latex['prechaptername'] = chapter_tuple[0] @locale_latex['postchaptername'] = chapter_tuple[1] @locale_latex['preappendixname'] = appendix_tuple[0] @locale_latex['postappendixname'] = appendix_tuple[1] template = File.('./latex/layout.tex.erb', ReVIEW::Template::TEMPLATE_DIR) layout_file = File.join(@basedir, 'layouts', 'layout.tex.erb') template = layout_file if File.exist?(layout_file) @texcompiler = File.basename(@config['texcommand'], '.*') erb = ReVIEW::Template.load(template, '-') erb.result(binding) end |
#warn(msg) ⇒ Object
47 48 49 |
# File 'lib/review/pdfmaker.rb', line 47 def warn(msg) @logger.warn "#{File.basename($PROGRAM_NAME, '.*')}: #{msg}" end |