Sha256: 572422a09d69e6052018e16f24f5f100b1cdb30c26aa5e40b260a16268fe71cb

Contents?: true

Size: 1.74 KB

Versions: 2

Compression:

Stored size: 1.74 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={} )
            view_file = @directory.join( template )
            if view_file.exist?
                context = Context.new( @directory, data )
                ERB.new( view_file.read, 0, '-' ).result(  context.getBinding )
            else
                "missing partial: #{template}"
            end
        end

        # convert newline characters into latex '\\newline'
        def break_lines( txt )
            q(txt.to_s).gsub("\n",'\\newline ')
        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

2 entries across 2 versions & 1 rubygems

Version Path
erb_latex-0.2.0 lib/erb_latex/context.rb
erb_latex-0.0.1 lib/erb_latex/context.rb