Class: Build::Text::Substitutions::NestedSubstitution

Inherits:
Object
  • Object
show all
Defined in:
lib/build/text/substitutions.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keyword, open, close, indent = "\t") ⇒ NestedSubstitution

Returns a new instance of NestedSubstitution.



109
110
111
112
113
114
115
116
# File 'lib/build/text/substitutions.rb', line 109

def initialize(keyword, open, close, indent = "\t")
	@keyword = keyword

	@open = open
	@close = close

	@indent = indent
end

Class Method Details

.apply(text, group) ⇒ Object



190
191
192
193
194
195
196
# File 'lib/build/text/substitutions.rb', line 190

def self.apply(text, group)
	group.each do |substitution|
		text = substitution.apply(text)
	end
	
	return text
end

Instance Method Details

#apply(text, level = 0) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/build/text/substitutions.rb', line 162

def apply(text, level = 0)
	open_pattern = line_pattern
	close_pattern = line_pattern('/')

	lines = text.each_line
	output = StringIO.new

	indent = lambda do |level, indentation|
		while line = lines.next rescue nil
			if line =~ open_pattern
				write_open($1, $2, output, indentation)

				indent[level + @open.count, indentation.by(@open.count)]

				write_close($1, $2, output, indentation)
			elsif line =~ close_pattern
				break
			else
				output.write(indentation + line)
			end
		end
	end

	indent[0, Indentation.none]

	return output.string
end

#freezeObject



118
119
120
121
122
123
124
125
# File 'lib/build/text/substitutions.rb', line 118

def freeze
	@keyword.freeze
	@open.freeze
	@close.freeze
	@indent.freeze
	
	super
end

#line_pattern(prefix = '') ⇒ Object



127
128
129
130
131
132
# File 'lib/build/text/substitutions.rb', line 127

def line_pattern(prefix = '')
	tag_pattern = Regexp.escape('<' + prefix + @keyword + '>')
	
	# Line matching pattern:
	Regexp.new('^(.*?)' + tag_pattern + '(.*)$', Regexp::MULTILINE | Regexp::EXTENDED)
end

#write_close(prefix, postfix, output, indentation) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/build/text/substitutions.rb', line 148

def write_close(prefix, postfix, output, indentation)
	depth = @close.size
	indentation = indentation.with_prefix(prefix)

	#output.write(prefix)
	(0...depth).reverse_each do |i|
		chunk = @close[-1 - i]
		chunk.chomp! if i == 0
		
		output.write(indentation.by(i) << chunk)
	end
	output.write(postfix)
end

#write_open(prefix, postfix, output, indentation) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/build/text/substitutions.rb', line 134

def write_open(prefix, postfix, output, indentation)
	depth = @open.size
	indentation = indentation.with_prefix(prefix)

	#output.write(prefix)
	(0...depth).each do |i|
		chunk = @open[i]
		chunk.chomp! if i == depth-1
		
		output.write(indentation.by(i) << chunk)
	end
	output.write(postfix)
end