colorful rat Ratfactor.com > Dave's Repos

rubylit

A literate programming system in 35 lines of Ruby
git clone http://ratfactor.com/repos/rubylit/rubylit.git

rubylit/stage1.rb

Download raw file: stage1.rb

1 fname = ARGV[0] 2 3 # Tangle 4 myseg = :start 5 segments = {} 6 segments[myseg] = [] 7 File.open("#{fname}.md").each do |line| 8 if(line.start_with?('## ')) 9 myseg = line[3..].chomp 10 puts "Found segment '#{myseg}'" 11 segments[myseg] = [] 12 end 13 14 if(line.start_with?(' ')) 15 segments[myseg].push(line) 16 end 17 end 18 def put_lines(file, segments, sname) 19 puts "Fetching segment '#{sname}'" 20 segments[sname].each do |line| 21 if(m = /^\s*<<([^>]+)>>\s*$/.match(line)) 22 put_lines(file, segments, m[1]) 23 else 24 file.puts line 25 end 26 end 27 end 28 File.open("#{fname}.rb", 'w') do |out| 29 put_lines(out, segments, :start) 30 end