Class: Trenni::Builder
- Inherits:
-
Object
show all
- Includes:
- Markup
- Defined in:
- lib/trenni/builder.rb,
lib/trenni/template.rb
Overview
Build markup quickly and efficiently.
Defined Under Namespace
Classes: Fragment
Constant Summary
collapse
- INDENT =
"\t"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Markup
append, escape_string, raw
Constructor Details
#initialize(output = nil, indent: true, encoding: Encoding::UTF_8) ⇒ Builder
Returns a new instance of Builder.
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/trenni/builder.rb', line 86
def initialize(output = nil, indent: true, encoding: Encoding::UTF_8)
@indent = indent
@output = output || MarkupString.new.force_encoding(encoding)
@level = [0]
@children = [0]
end
|
Instance Attribute Details
#output ⇒ Object
Returns the value of attribute output.
97
98
99
|
# File 'lib/trenni/builder.rb', line 97
def output
@output
end
|
Class Method Details
.fragment(output = nil, &block) ⇒ Object
A helper to generate fragments of markup.
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/trenni/builder.rb', line 60
def self.fragment(output = nil, &block)
if output.is_a?(Binding)
output = Template.buffer(output)
end
if output.nil?
return Fragment.new(block)
end
if output.is_a?(Builder)
block.call(output)
else
block.call(Builder.new(output))
end
return nil
end
|
.tag(name, content, **attributes) ⇒ Object
78
79
80
81
82
83
84
|
# File 'lib/trenni/builder.rb', line 78
def self.tag(name, content, **attributes)
self.fragment do |builder|
builder.inline(name, attributes) do
builder.text(content)
end
end
end
|
Instance Method Details
#<<(content) ⇒ Object
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/trenni/builder.rb', line 173
def <<(content)
return unless content
if content.is_a?(Fragment)
inline! do
content.call(self)
end
else
Markup.append(@output, content)
end
end
|
#==(other) ⇒ Object
110
111
112
|
# File 'lib/trenni/builder.rb', line 110
def == other
@output == String(other)
end
|
#append(value) ⇒ Object
Append pre-existing markup:
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
# File 'lib/trenni/builder.rb', line 186
def append(value)
return unless value
@level[-1] += 1
if @indent
value.each_line.with_index do |line, i|
@output << indentation << line
end
else
@output << value
end
end
|
#capture(*arguments, &block) ⇒ Object
33
34
35
|
# File 'lib/trenni/template.rb', line 33
def capture(*arguments, &block)
Template.capture(*arguments, output: self, &block)
end
|
#doctype(attributes = 'html') ⇒ Object
122
123
124
|
# File 'lib/trenni/builder.rb', line 122
def doctype(attributes = 'html')
@output << "<!DOCTYPE #{attributes}>\n"
end
|
#encoding ⇒ Object
99
100
101
|
# File 'lib/trenni/builder.rb', line 99
def encoding
@output.encoding
end
|
#indentation ⇒ Object
114
115
116
117
118
119
120
|
# File 'lib/trenni/builder.rb', line 114
def indentation
if @indent
INDENT * (@level.size - 1)
else
''
end
end
|
#inline! ⇒ Object
145
146
147
148
149
150
151
152
|
# File 'lib/trenni/builder.rb', line 145
def inline!
original_indent = @indent
@indent = false
yield
ensure
@indent = original_indent
end
|
#inline_tag(name, attributes = {}, &block) ⇒ Object
Also known as:
inline
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/trenni/builder.rb', line 132
def inline_tag(name, attributes = {}, &block)
original_indent = @indent
full_tag(name, attributes, @indent, false) do
@indent = false
yield if block_given?
end
ensure
@indent = original_indent
end
|
#raw(content) ⇒ Object
169
170
171
|
# File 'lib/trenni/builder.rb', line 169
def raw(content)
@output << content
end
|
#tag(name, attributes = {}, &block) ⇒ Object
127
128
129
|
# File 'lib/trenni/builder.rb', line 127
def tag(name, attributes = {}, &block)
full_tag(name, attributes, @indent, @indent, &block)
end
|
#text(content) ⇒ Object
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# File 'lib/trenni/builder.rb', line 154
def text(content)
return unless content
if @indent
@output << "\n" if @level.last > 0
@output << indentation
end
Markup.append(@output, content)
if @indent
@output << "\n"
end
end
|
#to_str ⇒ Object
Also known as:
to_s
Required for output to buffer.
104
105
106
|
# File 'lib/trenni/builder.rb', line 104
def to_str
@output
end
|