Class: RbLatex::Maker
- Inherits:
-
Object
- Object
- RbLatex::Maker
- Extended by:
- Forwardable
- Defined in:
- lib/rb_latex/maker.rb
Instance Attribute Summary collapse
-
#book_name ⇒ Object
Returns the value of attribute book_name.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#document_class ⇒ Object
Returns the value of attribute document_class.
-
#dvipdf_command ⇒ Object
Returns the value of attribute dvipdf_command.
-
#latex_command ⇒ Object
Returns the value of attribute latex_command.
-
#work_dir ⇒ Object
Returns the value of attribute work_dir.
Instance Method Summary collapse
- #add_item(filename, content) ⇒ Object
- #apply_template(template_file) ⇒ Object
- #book_filename(ext = ".pdf") ⇒ Object
- #compile_latex(dir) ⇒ Object
- #copy_files(dir) ⇒ Object
- #exec_dvipdf(dvifile) ⇒ Object
- #generate_pdf(filename, debug: nil) ⇒ Object
- #generate_src(dir) ⇒ Object
- #in_working_dir(debug) ⇒ Object
-
#initialize(root_dir) ⇒ Maker
constructor
A new instance of Maker.
- #prepare_working_dir(debug) ⇒ Object
Constructor Details
#initialize(root_dir) ⇒ Maker
Returns a new instance of Maker.
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rb_latex/maker.rb', line 25 def initialize(root_dir) @root_dir = root_dir @root_fullpath = File.absolute_path(root_dir) @work_dir = ".rblatex_work" @item_list = RbLatex::ItemList.new = RbLatex::MetaInfo.new @latex_command = "uplatex" @dvipdf_command = "dvipdfmx" @document_class = ["jlreq", "book,b5paper,openany"] @debug = nil @book_name = "book" @default_option = nil end |
Instance Attribute Details
#book_name ⇒ Object
Returns the value of attribute book_name.
14 15 16 |
# File 'lib/rb_latex/maker.rb', line 14 def book_name @book_name end |
#debug ⇒ Object
Returns the value of attribute debug.
13 14 15 |
# File 'lib/rb_latex/maker.rb', line 13 def debug @debug end |
#document_class ⇒ Object
Returns the value of attribute document_class.
10 11 12 |
# File 'lib/rb_latex/maker.rb', line 10 def document_class @document_class end |
#dvipdf_command ⇒ Object
Returns the value of attribute dvipdf_command.
12 13 14 |
# File 'lib/rb_latex/maker.rb', line 12 def dvipdf_command @dvipdf_command end |
#latex_command ⇒ Object
Returns the value of attribute latex_command.
11 12 13 |
# File 'lib/rb_latex/maker.rb', line 11 def latex_command @latex_command end |
#work_dir ⇒ Object
Returns the value of attribute work_dir.
15 16 17 |
# File 'lib/rb_latex/maker.rb', line 15 def work_dir @work_dir end |
Instance Method Details
#add_item(filename, content) ⇒ Object
39 40 41 |
# File 'lib/rb_latex/maker.rb', line 39 def add_item(filename, content) @item_list.add_item(filename, content) end |
#apply_template(template_file) ⇒ Object
137 138 139 140 |
# File 'lib/rb_latex/maker.rb', line 137 def apply_template(template_file) template = File.read(File.join(RbLatex::TEMPLATES_DIR, template_file)) return ERB.new(template).result(binding) end |
#book_filename(ext = ".pdf") ⇒ Object
43 44 45 |
# File 'lib/rb_latex/maker.rb', line 43 def book_filename(ext = ".pdf") "#{@book_name}#{ext}" end |
#compile_latex(dir) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rb_latex/maker.rb', line 80 def compile_latex(dir) texfile = book_filename(".tex") cmd = "#{@latex_command} #{texfile}" 3.times do |i| out, status = Open3.capture2e(cmd) if !status.success? @error_log = out if @debug print STDERR, @error_log, "\n" end raise RbLatex::Error, "fail to exec latex (#{i}): #{cmd}" end end dvifile = book_filename(".dvi") if File.exist?(dvifile) exec_dvipdf(dvifile) end end |
#copy_files(dir) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/rb_latex/maker.rb', line 58 def copy_files(dir) Dir.entries(@root_dir).each do |path| next if path == "." or path == ".." or path == @work_dir FileUtils.cp_r(File.join(@root_dir, path), dir, dereference_root: true) end end |
#exec_dvipdf(dvifile) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/rb_latex/maker.rb', line 99 def exec_dvipdf(dvifile) cmd = "#{@dvipdf_command} #{dvifile}" out, status = Open3.capture2e(cmd) if !status.success? @error_log = out raise RbLatex::Error, "fail to exec dvipdf: #{cmd}" end end |
#generate_pdf(filename, debug: nil) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rb_latex/maker.rb', line 47 def generate_pdf(filename, debug: nil) in_working_dir(debug) do |dir| copy_files(dir) Dir.chdir(dir) do generate_src(dir) compile_latex(dir) end FileUtils.cp(File.join(dir, book_filename), filename) end end |
#generate_src(dir) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rb_latex/maker.rb', line 65 def generate_src(dir) @item_list.generate(dir) @dclass, @dclass_option = @document_class if .page_progression_direction == "rtl" && !@dclass_option.split(",").include?("tate") @dclass_option += ",tate" end if @latex_command =~ /lualatex/ @default_option = "lualatex" end book_tex = apply_template("book.tex.erb") File.write(File.join(dir, book_filename(".tex")), book_tex) rblatexdefault_sty = apply_template("rblatexdefault.sty") File.write(File.join(dir, "rblatexdefault.sty"), rblatexdefault_sty) end |
#in_working_dir(debug) ⇒ Object
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/rb_latex/maker.rb', line 116 def in_working_dir(debug) work_dir = prepare_working_dir(debug) begin yield work_dir ensure if !debug FileUtils.remove_entry_secure(work_dir) end end end |
#prepare_working_dir(debug) ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'lib/rb_latex/maker.rb', line 127 def prepare_working_dir(debug) if !debug Dir.mktmpdir('rblatex') else FileUtils.rm_rf(@work_dir) Dir.mkdir(@work_dir) File.absolute_path(@work_dir) end end |