def indent(s, n) s.split(/\n/).map{|x| " " * n + x}.join("\n") end rope_code = File.read("rope.ml") vect_template = File.read("vect.ml.in") chunks = Hash.new{|h,k| h[k] = []} rope_code.scan(/\(\*\s*=begin\s+(\w+)\s*\*\)(.*?)\(\*\s*=end\s*\*\)/m).each do |name, code| puts "chunk #{name}" chunks[name] << code end txt = vect_template.gsub(/( *)__CHUNK__\((.*?)\)/) do name, nindent = $2, $1.size raise "Cannot find chunk #{name}" unless chunks.has_key?(name) indent(chunks[name].join(""), nindent) end File.open("vect.ml", "w"){|f| f.print txt }