Class: RbLatex::Maker
- Inherits:
-
Object
- Object
- RbLatex::Maker
- Extended by:
- Forwardable
- Defined in:
- lib/rb_latex/maker.rb
Instance Attribute Summary collapse
-
#document_class ⇒ Object
Returns the value of attribute document_class.
-
#work_dir ⇒ Object
Returns the value of attribute work_dir.
Instance Method Summary collapse
- #add_item(filename, content) ⇒ Object
- #apply_template(template_file) ⇒ Object
- #copy_files(dir) ⇒ Object
- #default_config ⇒ Object
- #exec_dvipdfmx(dir) ⇒ Object
- #exec_latex(dir) ⇒ 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.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rb_latex/maker.rb', line 26 def initialize(root_dir) @root_dir = root_dir @root_fullpath = File.absolute_path(root_dir) @config = default_config @work_dir = ".rblatex_work" @item_list = RbLatex::ItemList.new @meta_info = RbLatex::MetaInfo.new @latex_cmd = "uplatex" @dvipdfmx_cmd = "dvipdfmx" @document_class = ["jlreq", "book,b5paper,openany"] end |
Instance Attribute Details
#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 |
#work_dir ⇒ Object
Returns the value of attribute work_dir.
24 25 26 |
# File 'lib/rb_latex/maker.rb', line 24 def work_dir @work_dir end |
Instance Method Details
#add_item(filename, content) ⇒ Object
42 43 44 |
# File 'lib/rb_latex/maker.rb', line 42 def add_item(filename, content) @item_list.add_item(filename, content) end |
#apply_template(template_file) ⇒ Object
124 125 126 127 |
# File 'lib/rb_latex/maker.rb', line 124 def apply_template(template_file) template = File.read(File.join(RbLatex::TEMPLATES_DIR, template_file)) return ERB.new(template).result(binding) end |
#copy_files(dir) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/rb_latex/maker.rb', line 57 def copy_files(dir) Dir.entries(@root_dir).each do |path| next if path == "." or path == ".." FileUtils.cp_r(File.join(@root_dir, path), dir, dereference_root: true) end end |
#default_config ⇒ Object
38 39 40 |
# File 'lib/rb_latex/maker.rb', line 38 def default_config {} end |
#exec_dvipdfmx(dir) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/rb_latex/maker.rb', line 85 def exec_dvipdfmx(dir) cmd = "#{@dvipdfmx_cmd} book.dvi" out, status = Open3.capture2e(cmd) if !status.success? @error_log = out raise RbLatex::Error, "fail to exec latex #{i}: #{cmd}" end FileUtils.cp("book.pdf", File.join(@root_fullpath, "book.pdf")) end |
#exec_latex(dir) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/rb_latex/maker.rb', line 74 def exec_latex(dir) cmd = "#{@latex_cmd} book.tex" 3.times do |i| out, status = Open3.capture2e(cmd) if !status.success? @error_log = out raise RbLatex::Error, "fail to exec latex #{i}: #{cmd}" end end end |
#generate_pdf(filename, debug: nil) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rb_latex/maker.rb', line 46 def generate_pdf(filename, debug: nil) in_working_dir(debug) do |dir| copy_files(dir) Dir.chdir(dir) do generate_src(dir) exec_latex(dir) exec_dvipdfmx(dir) end end end |
#generate_src(dir) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/rb_latex/maker.rb', line 64 def generate_src(dir) @item_list.generate(dir) @dclass, @dclass_opt = @document_class if @meta_info.page_progression_direction == "rtl" && !@dclass_opt.split(",").include?("tate") @dclass_opt += ",tate" end book_tex = apply_template("book.tex.erb") File.write(File.join(dir, "book.tex"), book_tex) end |
#in_working_dir(debug) ⇒ Object
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/rb_latex/maker.rb', line 103 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
114 115 116 117 118 119 120 121 122 |
# File 'lib/rb_latex/maker.rb', line 114 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 |