class Trenni::Template

def self.buffer(binding)

Returns the buffer used for capturing output.
def self.buffer(binding)
	binding.local_variable_get(OUT)
end

def self.capture(*arguments, output: nil, &block)

Returns the output produced by calling the given block.
def self.capture(*arguments, output: nil, &block)
	scope = block.binding
	previous_output = scope.local_variable_get(OUT)
	
	output ||= previous_output.class.new(encoding: previous_output.encoding)
	scope.local_variable_set(OUT, output)
	
	begin
		block.call(*arguments)
	ensure
		scope.local_variable_set(OUT, previous_output)
	end
	
	return output
end

def self.load(string, *arguments, **options)

def self.load(string, *arguments, **options)
	self.new(Buffer.new(string), **options).freeze
end

def self.load_file(path, **options)

def self.load_file(path, **options)
	self.new(FileBuffer.new(path), **options).freeze
end

def code

def code
	@code ||= compile!
end

def compile!

def compile!
	assembler = make_assembler
	
	Parsers.parse_template(@buffer, assembler)
	
	assembler.code
end

def freeze

def freeze
	return self if frozen?
	
	to_proc
	
	super
end

def initialize(buffer, binding: BINDING)

Parameters:
  • binding (Binding) -- The binding in which the template is compiled. e.g. `TOPLEVEL_BINDING`.
def initialize(buffer, binding: BINDING)
	@buffer = buffer
	@binding = binding
end

def make_assembler

def make_assembler
	Assembler.new
end

def output_buffer

def output_buffer
	String.new.force_encoding(code.encoding)
end

def to_buffer(scope)

def to_buffer(scope)
	Buffer.new(to_string(scope), path: @buffer.path)
end

def to_proc(scope = @binding.dup)

def to_proc(scope = @binding.dup)
	@compiled_proc ||= eval("\# frozen_string_literal: true\nproc{|#{OUT}|;#{code}}", scope, @buffer.path, 0).freeze
end

def to_string(scope = Object.new, output = nil)

def to_string(scope = Object.new, output = nil)
	output ||= output_buffer
	
	scope.instance_exec(output, &to_proc)
	
	return output
end