Class: Trenni::Builder

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

Constant Summary collapse

DEFAULT_INDENTATION =
"\t".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strict: false, indent: true, indentation: DEFAULT_INDENTATION, escape: false, output: String.new) ⇒ Builder

Returns a new instance of Builder.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/trenni/builder.rb', line 47

def initialize(strict: false, indent: true, indentation: DEFAULT_INDENTATION, escape: false, output: String.new)
	@strict = strict
	
	@indent = indent
	
	@indentation = indentation
	# This field gets togged in #inline so we keep track of it separately from @indentation.
	
	@escape = escape
	
	@output = output
	
	@level = [0]
	@children = [0]
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



63
64
65
# File 'lib/trenni/builder.rb', line 63

def output
  @output
end

Class Method Details

.fragment(builder = nil, &block) ⇒ Object

A helper to generate fragments of markup.



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/trenni/builder.rb', line 33

def self.fragment(builder = nil, &block)
	if builder
		yield builder
		
		return nil
	else
		builder = Builder.new
		
		yield builder
		
		return builder.output
	end
end

Instance Method Details

#append(data) ⇒ Object

Append pre-existing markup:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/trenni/builder.rb', line 104

def append(data)
	return unless data
	
	# The parent has one more child:
	@level[-1] += 1
	
	if @indent
		data.each_line.with_index do |line, i|
			@output << indentation << line
		end
	else
		@output << data
	end
end

#doctype(attributes = 'html') ⇒ Object



79
80
81
# File 'lib/trenni/builder.rb', line 79

def doctype(attributes = 'html')
	@output << "<!DOCTYPE #{attributes}>\n"
end

#indentationObject



65
66
67
68
69
70
71
# File 'lib/trenni/builder.rb', line 65

def indentation
	if @indent
		@indentation * (@level.size - 1)
	else
		''
	end
end

#inline(name, attributes = {}, &block) ⇒ Object

Begin an inline tag.



89
90
91
92
93
94
95
96
97
# File 'lib/trenni/builder.rb', line 89

def inline(name, attributes = {}, &block)
	indent = @indent
	
	full_tag(name, attributes, @indent, false) do
		@indent = false
		yield if block_given?
		@indent = indent
	end
end

#instruct(attributes = nil) ⇒ Object



73
74
75
76
77
# File 'lib/trenni/builder.rb', line 73

def instruct(attributes = nil)
	attributes ||= INSTRUCT_ATTRIBUTES
	
	@output << "<?xml#{tag_attributes(attributes)}?>\n"
end

#tag(name, attributes = {}, &block) ⇒ Object

Begin a block tag.



84
85
86
# File 'lib/trenni/builder.rb', line 84

def tag(name, attributes = {}, &block)
	full_tag(name, attributes, @indent, @indent, &block)
end

#tag_attributes(attributes, buffer = [], prefix = nil) ⇒ Object

Convert a set of attributes into a string suitable for use within a <tag>.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/trenni/builder.rb', line 120

def tag_attributes(attributes, buffer = [], prefix = nil)
	attributes.each do |key, value|
		attribute_key = prefix ? "#{prefix}-#{key}" : key
		
		if value == true
			buffer << Strings::to_simple_attribute(attribute_key, @strict)
		elsif value.is_a? Hash
			tag_attributes(value, buffer, attribute_key)
		elsif value
			buffer << Strings::to_attribute(attribute_key, to_html(value))
		end
	end
	
	if buffer.size > 0
		return ' ' + buffer.join(' ')
	else
		return ''
	end
end

#text(data) ⇒ Object



99
100
101
# File 'lib/trenni/builder.rb', line 99

def text(data)
	append to_html(data)
end