Class: ErbLatex::Template
- Inherits:
-
Object
- Object
- ErbLatex::Template
- Defined in:
- lib/erb_latex/template.rb
Overview
A template is an latex file that contains embedded ERB code It can optionally have a layout be rendered to either a file or StringIO instance
Instance Attribute Summary collapse
-
#layout ⇒ String
Path to a file to use for layout.
-
#log ⇒ String
readonly
The log from the last xelatex run.
-
#packages_path ⇒ String
Path to a directory to search for packages.
-
#partials_path ⇒ String
Path to a directory to search for partials to include.
-
#pass_count ⇒ Object
readonly
Returns the value of attribute pass_count.
Instance Method Summary collapse
-
#compile_latex ⇒ String
Runs the ERB pre-process on the latex file.
-
#data=(hash) ⇒ Object
Sets the data to be used for the template An instance variable will be created for the template for each key in the hash with it’s value set accordingly.
-
#execute {|Pathname| ... } ⇒ Object
Compile the Latex template into a PDF file.
-
#initialize(view_file, options = {}) ⇒ Template
constructor
create a new Template.
-
#layout_file ⇒ Pathname
Layout file.
-
#suggested_filename ⇒ String
It removes the extension from the name and replaces it with ‘.pdf’.
-
#to_file(file = suggested_filename) ⇒ String, IO
Save the PDF to the file.
-
#to_stringio ⇒ StringIO
Containing the the PDF.
Constructor Details
#initialize(view_file, options = {}) ⇒ Template
create a new Template
40 41 42 43 44 45 46 47 48 |
# File 'lib/erb_latex/template.rb', line 40 def initialize( view_file, ={} ) @data = [:data] || {} @layout = [:layout] @context = [:context] || ErbLatex::Context @packages_path = [:packages_path] @partials_path = [:partials_path] @view = Pathname.new( view_file ) @log = '' end |
Instance Attribute Details
#layout ⇒ String
Returns path to a file to use for layout.
32 33 34 |
# File 'lib/erb_latex/template.rb', line 32 def layout @layout end |
#log ⇒ String (readonly)
Returns the log from the last xelatex run.
25 26 27 |
# File 'lib/erb_latex/template.rb', line 25 def log @log end |
#packages_path ⇒ String
Returns path to a directory to search for packages.
32 |
# File 'lib/erb_latex/template.rb', line 32 attr_accessor :layout, :packages_path, :partials_path |
#partials_path ⇒ String
Returns path to a directory to search for partials to include.
32 |
# File 'lib/erb_latex/template.rb', line 32 attr_accessor :layout, :packages_path, :partials_path |
#pass_count ⇒ Object (readonly)
Returns the value of attribute pass_count.
25 |
# File 'lib/erb_latex/template.rb', line 25 attr_reader :log, :pass_count |
Instance Method Details
#compile_latex ⇒ String
Runs the ERB pre-process on the latex file
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/erb_latex/template.rb', line 117 def compile_latex begin context = @context.new( @partials_path || @view.dirname, @data ) content = ErbLatex::File.evaluate(@view, context.getBinding) if layout ErbLatex::File.evaluate(layout_file, context.getBinding{ content }) else content end rescue LocalJumpError=>e raise LatexError.new( "ERB compile raised #{e.class} on #{@view}", e.backtrace ) end end |
#data=(hash) ⇒ Object
Sets the data to be used for the template An instance variable will be created for the template for each key in the hash with it’s value set accordingly
54 55 56 |
# File 'lib/erb_latex/template.rb', line 54 def data=( hash ) @data = hash end |
#execute {|Pathname| ... } ⇒ Object
Compile the Latex template into a PDF file
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/erb_latex/template.rb', line 88 def execute latex = compile_latex Dir.mktmpdir do | dir | @pass_count = 0 @log = '' success = false while log_suggests_rerunning? && @pass_count < 5 @pass_count += 1 success = execute_xelatex(latex,dir) end pdf_file = Pathname.new(dir).join( "output.pdf" ) if success && pdf_file.exist? yield pdf_file else errors = @log.scan(/\*\!\s(.*?)\n\s*\n/m).map{|e| e.first.gsub(/\n/,'') }.join("; ") STDERR.puts @log, errors if ErbLatex.config.verbose_logs raise LatexError.new( errors.empty? ? "xelatex compile error" : errors, @log ) end end end |
#layout_file ⇒ Pathname
Returns layout file.
110 111 112 |
# File 'lib/erb_latex/template.rb', line 110 def layout_file Pathname.new( layout ) end |
#suggested_filename ⇒ String
It removes the extension from the name and replaces it with ‘.pdf’
60 61 62 |
# File 'lib/erb_latex/template.rb', line 60 def suggested_filename @view.basename.to_s.gsub(/\..*$/, '.pdf') end |
#to_file(file = suggested_filename) ⇒ String, IO
Save the PDF to the file
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/erb_latex/template.rb', line 68 def to_file( file = suggested_filename ) execute do | contents | if file.is_a?(String) FileUtils.mv contents, file else file.write contents.read file.rewind end end file end |