Class: Utopia::Trenni

Inherits:
Object show all
Defined in:
lib/utopia/trenni.rb

Defined Under Namespace

Classes: Buffer, Scanner

Constant Summary collapse

OUT =

The output variable that will be used in templates:

'_out'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, filename = '<Trenni>') ⇒ Trenni

Returns a new instance of Trenni.



130
131
132
133
134
# File 'lib/utopia/trenni.rb', line 130

def initialize(template, filename = '<Trenni>')
	@template = template
	@filename = filename
	compile!
end

Class Method Details

.buffer(binding) ⇒ Object

Returns the buffer used for capturing output.



23
24
25
# File 'lib/utopia/trenni.rb', line 23

def self.buffer(binding)
	eval(OUT, binding)
end

.capture(*args, &block) ⇒ Object

Returns the output produced by calling the given block.



13
14
15
16
17
18
19
20
# File 'lib/utopia/trenni.rb', line 13

def self.capture(*args, &block)
	out = eval(OUT, block.binding)
	top = out.size
	
	block.call *args
	
	return out.pop(out.size - top).join
end

.load(path) ⇒ Object



126
127
128
# File 'lib/utopia/trenni.rb', line 126

def self.load(path)
	return self.new(File.read(path), path)
end

Instance Method Details

#compile!(filename = @filename) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/utopia/trenni.rb', line 136

def compile!(filename = @filename)
	buffer = Buffer.new
	scanner = Scanner.new(buffer, @template)
	
	scanner.parse
	
	@code = buffer.code
end

#result(binding) ⇒ Object



145
146
147
# File 'lib/utopia/trenni.rb', line 145

def result(binding)
	eval(@code, binding, @filename)
end