#!/usr/bin/env ruby require 'pp' unless ARGV.size >= 1 puts "Usage: ./demoer.rb demofile.demo [INDEX]" exit 1 end demofile = ARGV[0] index = ARGV[1] Section = Struct.new(:title, :contents) sections = [] header_re = /^=== (.*) ===$/ file = File.open(demofile) file.each_line do |line| if header_re.match(line) title = $1 sections.push Section.new(title, []) else sections.last.contents.push(line) end end file.close if index index = index.to_i section = sections[index] puts section.title targetfile = demofile.sub(/\.demo$/, ".rb") target = File.new(targetfile, "w") section.contents.each do |line| target.write(line) end target.close else sections.each_with_index do |section, index| puts "#{index}.) #{section.title}" end end