Skip to content

Instantly share code, notes, and snippets.

@metade
Created June 29, 2011 07:39
Show Gist options
  • Save metade/1053338 to your computer and use it in GitHub Desktop.
Save metade/1053338 to your computer and use it in GitHub Desktop.
Takes a source code repository and makes it into a PDF
require 'prawn'
dir = '../music-example'
files = Dir.glob("#{dir}/*") + Dir.glob("#{dir}/**/*")
pdf = Prawn::Document.new(:page_size => 'A4')
pdf.font "Courier"
stats = { :files => 0, :lines => 0, :chars => 0 }
files.each do |file|
next if File.directory?(file)
next if file =~ /\.(log|jpg|png|sqlite3?|ico|pid)$/
next if file =~ %r[#{dir}/(spec|test|features|cucumber)]
next if file =~ /(jquery.js|jquery.min.js|jquery_ujs.js|cucumber)/
puts file
pdf.start_new_page
pdf.text file.sub("#{dir}/", ''), :align => :center, :size => 8
pdf.stroke_horizontal_rule
pdf.move_down 5
contents = File.read(file)
stats[:files] += 1
stats[:lines] += contents.split.size
stats[:chars] += contents.size
formatted = contents.gsub(/^([ \t]+)/m) { |s| Prawn::Text::NBSP * s.size }
pdf.text formatted, :size => 8
end
p stats
pdf.render_file('test.pdf')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment