Class: Trenni::Template::Assembler

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

Direct Known Subclasses

MarkupTemplate::Assembler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(encoding: Encoding::UTF_8) ⇒ Assembler

Returns a new instance of Assembler.



66
67
68
# File 'lib/trenni/template.rb', line 66

def initialize(encoding: Encoding::UTF_8)
	@code = String.new.force_encoding(encoding)
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



70
71
72
# File 'lib/trenni/template.rb', line 70

def code
  @code
end

Instance Method Details

#expression(text) ⇒ Object

Output a string interpolation.



87
88
89
90
# File 'lib/trenni/template.rb', line 87

def expression(text)
	# Double brackets are required here to handle expressions like #{foo rescue "bar"}.
	@code << "#{OUT}<<String(#{text});"
end

#instruction(text, postfix = nil) ⇒ Object

Output a ruby expression (or part of).



82
83
84
# File 'lib/trenni/template.rb', line 82

def instruction(text, postfix = nil)
	@code << text << (postfix || ';')
end

#text(text) ⇒ Object

Output raw text to the template.



73
74
75
76
77
78
79
# File 'lib/trenni/template.rb', line 73

def text(text)
	text = text.gsub("'", "\\\\'")
	@code << "#{OUT}<<'#{text}';"
	
	# This is an interesting approach, but it doens't preserve newlines or tabs as raw characters, so template line numbers don't match up.
	# @parts << "#{OUT}<<#{text.dump};"
end