Class: Teapot::Substitutions::NestedSubstitution
- Inherits:
-
Object
- Object
- Teapot::Substitutions::NestedSubstitution
- Defined in:
- lib/teapot/substitutions.rb
Class Method Summary collapse
Instance Method Summary collapse
- #apply(text, level = 0) ⇒ Object
-
#initialize(keyword, open, close, indent = "\t") ⇒ NestedSubstitution
constructor
A new instance of NestedSubstitution.
- #line_pattern(prefix = '') ⇒ Object
- #write_close(prefix, postfix, output, indentation) ⇒ Object
- #write_open(prefix, postfix, output, indentation) ⇒ Object
Constructor Details
#initialize(keyword, open, close, indent = "\t") ⇒ NestedSubstitution
Returns a new instance of NestedSubstitution.
125 126 127 128 129 130 131 132 |
# File 'lib/teapot/substitutions.rb', line 125 def initialize(keyword, open, close, indent = "\t") @keyword = keyword @open = open @close = close @indent = indent end |
Class Method Details
.apply(text, group) ⇒ Object
197 198 199 200 201 202 203 |
# File 'lib/teapot/substitutions.rb', line 197 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
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/teapot/substitutions.rb', line 169 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 |
#line_pattern(prefix = '') ⇒ Object
134 135 136 137 138 139 |
# File 'lib/teapot/substitutions.rb', line 134 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
155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/teapot/substitutions.rb', line 155 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
141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/teapot/substitutions.rb', line 141 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 |