Skip to content

Instantly share code, notes, and snippets.

@NakedMoleRatScientist
Last active December 28, 2015 22:39
Show Gist options
  • Save NakedMoleRatScientist/7573509 to your computer and use it in GitHub Desktop.
Save NakedMoleRatScientist/7573509 to your computer and use it in GitHub Desktop.
msg = ["hello","world","replace","me","with","this\n","now!"]
filename = "test_me"
def load_lines filename
lines = []
file = File.open(filename,"r")
file.each_line do |f|
lines.push(f)
end
lines
end
lines = load_lines(filename)
def build_sectors blocks, lines
sectors = []
lines.each_with_index do |l,i|
if l.match("file: ") && (blocks[0] - 1) == i
section = {
:file => load_lines(l.split(":").last.strip()),
:begin => blocks.shift(),
:end => blocks.shift()
}
sectors.push(section)
end
end
return sectors
end
def find_blocks(lines)
blocks = []
lines.each_with_index do |l,i|
if l.match("```")
blocks.push(i)
end
end
blocks
end
def insert_msg lines, sector, offset
start = sector[:begin] + 1 + offset
finish = sector[:end] + offset
sector[:file].each.with_index(start) do |m,i|
if i >= finish
lines.insert(i,"\n")
offset += 1
end
lines[i] = m
end
return lines, offset
end
blocks = find_blocks(lines)
sectors = build_sectors(blocks,lines)
offset = 0
sectors.each do |s|
lines, offset = insert_msg(lines,s,offset)
end
result = File.open("result","w")
lines.each do |l|
result.write(l)
end
result.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment