Created
June 27, 2012 09:38
-
-
Save ttscoff/3002935 to your computer and use it in GitHub Desktop.
Turns templated input into repeating blocks based on start and end numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# Turns templated input into repeating blocks based on start and end numbers | |
# Basic syntax: `##0,5##` where 0 is start and 5 is end | |
# Modifiers may be included: `##+4##` will be replaced by the current counter plus four | |
# One start,end placeholder allowed, any number of modifers | |
# By Brett Terpstra, 2012 <http://brettterpstra.com> | |
# WTFPL License <http://en.wikipedia.org/wiki/WTFPL> | |
input = STDIN.read | |
template = input.match(/(\?\:)?##(\d+),(\d+)##/) | |
if template.nil? | |
puts input | |
exit | |
end | |
display = template[1].nil? ? true : false | |
modifier = input.scan(/(##([+\-])?(\d+)##)/) | |
modified = [] | |
unless modifier.empty? | |
modifier.each {|x| | |
inc = 0 | |
if x[1] =~ /\+/ | |
inc = x[2].to_i | |
else | |
inc = x[2].to_i * -1 | |
end | |
modified.push([Regexp.escape(x[0]),inc]) | |
} | |
end | |
count_start = template[2].to_i | |
count_end = template[3].to_i | |
duration = (count_end - count_start) + 1 | |
duration.times do | |
sub = display ? count_start.to_s : '' | |
out = input.sub(/#{Regexp.escape(template[0])}/,sub) | |
modified.each {|mod| | |
out.sub!(/#{mod[0]}/,(count_start + mod[1]).to_s) | |
} | |
puts out | |
count_start += 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment