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/README.rb

Download raw file: README.rb

1 fname = ARGV[0] 2 fname_input = "#{fname}.md" 3 myseg = :start 4 segments = {} 5 segments[myseg] = [] 6 File.open(fname_input).each do |line| 7 if(line.start_with?('## ')) 8 myseg = line[3..].chomp 9 segments[myseg] = [] 10 end 11 if(line.start_with?(' ')) 12 segments[myseg].push(line) 13 end 14 end 15 def put_lines(file, segments, sname) 16 segments[sname].each do |line| 17 if(m = /^\s*<<([^>]+)>>\s*$/.match(line)) 18 put_lines(file, segments, m[1]) 19 else 20 file.puts line 21 end 22 end 23 end 24 File.open("#{fname}.rb", 'w') do |out| 25 put_lines(out, segments, :start) 26 end 27 require 'rdoc' 28 data = File.read(fname_input) 29 formatter = RDoc::Markup::ToHtml.new(RDoc::Options.new, nil) 30 html = RDoc::Markdown.parse(data).accept(formatter) 31 File.open("#{fname}.html", 'w') do |out| 32 out.puts("<html><body>") 33 out.print(html) 34 out.puts("</body></html>") 35 end