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.
-
#colophon ⇒ Object
Returns the value of attribute colophon.
-
#colophon_after ⇒ Object
Returns the value of attribute colophon_after.
-
#colophon_before ⇒ Object
Returns the value of attribute colophon_before.
-
#colophon_template ⇒ Object
Returns the value of attribute colophon_template.
-
#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.
-
#titlepage ⇒ Object
Returns the value of attribute titlepage.
-
#work_dir ⇒ Object
Returns the value of attribute work_dir.
Instance Method Summary collapse
- #add_item(filename, content) ⇒ Object
- #apply_template(template_file, template_dir: nil) ⇒ 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.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rb_latex/maker.rb', line 32 def initialize(root_dir) @root_dir = root_dir @root_fullpath = File.absolute_path(root_dir) @work_dir = ".rblatex_work" @item_list = RbLatex::ItemList.new @meta_info = RbLatex::MetaInfo.new @latex_command = "uplatex" @dvipdf_command = "dvipdfmx" @document_class = ["jlreq", "book,b5paper,openany"] @debug = nil @book_name = "book" @default_option = nil @colophon_template = "colophon.tex.erb" @colophon = nil @colophon_before = nil @colophon_after = nil @titlepage = 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 |
#colophon ⇒ Object
Returns the value of attribute colophon.
16 17 18 |
# File 'lib/rb_latex/maker.rb', line 16 def colophon @colophon end |
#colophon_after ⇒ Object
Returns the value of attribute colophon_after.
18 19 20 |
# File 'lib/rb_latex/maker.rb', line 18 def colophon_after @colophon_after end |
#colophon_before ⇒ Object
Returns the value of attribute colophon_before.
17 18 19 |
# File 'lib/rb_latex/maker.rb', line 17 def colophon_before @colophon_before end |
#colophon_template ⇒ Object
Returns the value of attribute colophon_template.
19 20 21 |
# File 'lib/rb_latex/maker.rb', line 19 def colophon_template @colophon_template 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 |
#titlepage ⇒ Object
Returns the value of attribute titlepage.
15 16 17 |
# File 'lib/rb_latex/maker.rb', line 15 def titlepage @titlepage end |
#work_dir ⇒ Object
Returns the value of attribute work_dir.
20 21 22 |
# File 'lib/rb_latex/maker.rb', line 20 def work_dir @work_dir end |
Instance Method Details
#add_item(filename, content) ⇒ Object
51 52 53 |
# File 'lib/rb_latex/maker.rb', line 51 def add_item(filename, content) @item_list.add_item(filename, content) end |
#apply_template(template_file, template_dir: nil) ⇒ Object
153 154 155 156 157 |
# File 'lib/rb_latex/maker.rb', line 153 def apply_template(template_file, template_dir: nil) template_dir ||= RbLatex::TEMPLATES_DIR template = File.read(File.join(template_dir, template_file)) return ERB.new(template, trim_mode: '-').result(binding) end |
#book_filename(ext = ".pdf") ⇒ Object
55 56 57 |
# File 'lib/rb_latex/maker.rb', line 55 def book_filename(ext = ".pdf") "#{@book_name}#{ext}" end |
#compile_latex(dir) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rb_latex/maker.rb', line 96 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
70 71 72 73 74 75 |
# File 'lib/rb_latex/maker.rb', line 70 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
115 116 117 118 119 120 121 122 |
# File 'lib/rb_latex/maker.rb', line 115 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
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rb_latex/maker.rb', line 59 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
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rb_latex/maker.rb', line 77 def generate_src(dir) @item_list.generate(dir) @dclass, @dclass_option = @document_class if @meta_info.page_progression_direction == "rtl" && !@dclass_option.split(",").include?("tate") @dclass_option += ",tate" end if @latex_command =~ /lualatex/ @default_option = "luatex" 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) if @colophon colophon_tex = apply_template(@colophon_template, template_dir: @colophon_dir) File.write(File.join(dir, "colophon.tex"), colophon_tex) end end |
#in_working_dir(debug) ⇒ Object
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/rb_latex/maker.rb', line 132 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
143 144 145 146 147 148 149 150 151 |
# File 'lib/rb_latex/maker.rb', line 143 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 |