Class: ErbLatex::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/erb_latex/context.rb

Overview

Is the execution context for ERB evaluation

Constant Summary collapse

ESCAPE =

List of characters that need to be escaped and their escaped values

{
    "#"=>"\\#",
    "$"=>"\\$",
    "%"=>"\\%",
    "&"=>"\\&",
    "~"=>"\\~{}",
    "_"=>"\\_",
    "^"=>"\\^{}",
    "\\"=>"\\textbackslash{}",
    "{"=>"\\{",
    "}"=>"\\}"
}

Instance Method Summary collapse

Constructor Details

#initialize(directory, data) ⇒ Context

create new Context

Parameters:

  • directory (String)

    directory to use as a base for finding partials

  • data (Hash)


25
26
27
28
29
# File 'lib/erb_latex/context.rb', line 25

def initialize( directory, data )
    @directory = directory
    @data      = data
    data.each{ |k,v| instance_variable_set( '@'+k.to_s, v ) }
end

Instance Method Details

#break_lines(txt) ⇒ Object

convert newline characters into latex ‘\linebreak’



38
39
40
# File 'lib/erb_latex/context.rb', line 38

def break_lines( txt )
    q(txt.to_s).gsub("\n",'\\linebreak ')
end

#getBindingObject

return a reference to the instance’s scope or ‘binding’



43
44
45
# File 'lib/erb_latex/context.rb', line 43

def getBinding
    return binding()
end

#partial(template, data = {}) ⇒ Object

include another latex file into the current template



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

def partial( template, data={} )
    context = self.class.new( @directory, data )
    ErbLatex::File.evaluate(Pathname.new(template), context.getBinding, @directory)
end

#q(text) ⇒ String

escape using latex escaping rules

Parameters:

  • text

    string to escape

Returns:

  • (String)

    text after ESCAPE characters are replaced



50
51
52
# File 'lib/erb_latex/context.rb', line 50

def q(text)
    text.to_s.gsub(  /([\^\%~\\\\#\$%&_\{\}])/ ) { |s| ESCAPE[s] }
end