Sha256: 45ce7e769e43d3d6a661b0b22813929ed9cd49ae7c90e108a4611a597d734d71

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

module ErbLatex

    # Is the execution context for ERB evaluation
    #
    class Context

        # List of characters that need to be escaped
        # and their escaped values
        ESCAPE = {
            "#"=>"\\#",
            "$"=>"\\$",
            "%"=>"\\%",
            "&"=>"\\&",
            "~"=>"\\~{}",
            "_"=>"\\_",
            "^"=>"\\^{}",
            "\\"=>"\\textbackslash{}",
            "{"=>"\\{",
            "}"=>"\\}"
        }

        # create new Context
        # @param directory [String] directory to use as a base for finding partials
        # @param data [Hash]
        def initialize( directory, data )
            @directory = directory
            @data      = data
            data.each{ |k,v| instance_variable_set( '@'+k.to_s, v ) }
        end

        # include another latex file into the current template
        def partial( template, data={} )
            context = self.class.new( @directory, data )
            ErbLatex::File.evaluate(Pathname.new(template), context.getBinding, @directory)
        end

        # convert newline characters into latex '\\linebreak'
        def break_lines( txt )
            q(txt.to_s).gsub("\n",'\\linebreak ')
        end

        # return a reference to the instance's scope or 'binding'
        def getBinding
            return binding()
        end

        # escape using latex escaping rules
        # @param text string to escape
        # @return [String] text after {ESCAPE} characters are replaced
        def q(text)
            text.to_s.gsub(  /([\^\%~\\\\#\$%&_\{\}])/ ) { |s| ESCAPE[s] }
        end

    end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
erb_latex-1.0.0 lib/erb_latex/context.rb