Class: ErbLatex::Template

Inherits:
Object
  • Object
show all
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

Examples:

for a hypothetical Rails controller

tmpl = ErbLatex::Template.new( 'article.tex',
   :layout => 'layout.tex'
   :data   => { :sentence=>"hello, this is doge" }
)
render :pdf => tmpl.to_stringio.read

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view_file, options = {}) ⇒ Template

create a new Template

Parameters:

  • view_file (String)

    path to the latex template

  • options (Hash) (defaults to: {})

    a customizable set of options

  • context (Hash)

    a customizable set of options

Options Hash (options):

  • :layout (String)

    path to a latex template that calls yield

  • :data (Hash)

    an instance variable will be created in the view for each key/value pair



40
41
42
43
44
45
46
47
48
# File 'lib/erb_latex/template.rb', line 40

def initialize( view_file, options={} )
    @data   = options[:data] || {}
    @layout = options[:layout]
    @context = options[:context] || ErbLatex::Context
    @packages_path = options[:packages_path]
    @partials_path = options[:partials_path]
    @view   = Pathname.new( view_file )
    @log  = ''
end

Instance Attribute Details

#layoutString

Returns path to a file to use for layout.

Returns:

  • (String)

    path to a file to use for layout



32
33
34
# File 'lib/erb_latex/template.rb', line 32

def layout
  @layout
end

#logString (readonly)

Returns the log from the last xelatex run.

Returns:

  • (String)

    the log from the last xelatex run



25
26
27
# File 'lib/erb_latex/template.rb', line 25

def log
  @log
end

#packages_pathString

Returns path to a directory to search for packages.

Returns:

  • (String)

    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_pathString

Returns path to a directory to search for partials to include.

Returns:

  • (String)

    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_countObject (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_latexString

Runs the ERB pre-process on the latex file

Returns:

  • (String)

    latex with ERB substitutions performed

Raises:

  • (LatexError)

    if the xelatex process does not complete successfully



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

Parameters:

  • hash (Hash)

    data to set for the template



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

Yields:

  • (Pathname)

    complete path to the PDF file

Raises:

  • (LatexError)

    if the xelatex process does not complete successfully



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_filePathname

Returns layout file.

Returns:

  • (Pathname)

    layout file



110
111
112
# File 'lib/erb_latex/template.rb', line 110

def layout_file
    Pathname.new( layout )
end

#suggested_filenameString

It removes the extension from the name and replaces it with ‘.pdf’

Returns:

  • (String)

    the suggested filename for this template.



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

Parameters:

  • file (String, IO) (defaults to: suggested_filename)

    if file is a String, the PDF is moved to the path indicated (most efficient). Otherwise, file is considered an instance of IO, and write is called on it with the PDF contents

Returns:

  • (String, IO)

    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

#to_stringioStringIO

Returns containing the the PDF.

Returns:



81
82
83
# File 'lib/erb_latex/template.rb', line 81

def to_stringio
    to_file( ::ErbLatex::StringIO.new(suggested_filename) )
end