class Build::Text::Substitutions::NestedSubstitution
Public Class Methods
apply(text, group)
click to toggle source
# 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
new(keyword, open, close, indent = "\t")
click to toggle source
# File lib/build/text/substitutions.rb, line 109 def initialize(keyword, open, close, indent = "\t") @keyword = keyword @open = open @close = close @indent = indent end
Public Instance Methods
apply(text, level = 0)
click to toggle source
# 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
freeze()
click to toggle source
Calls superclass method
# File lib/build/text/substitutions.rb, line 118 def freeze @keyword.freeze @open.freeze @close.freeze @indent.freeze super end
line_pattern(prefix = '')
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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