Skip to content

Instantly share code, notes, and snippets.

@ayosec

ayosec/code2html.rb

Created Jul 12, 2020
Embed
What would you like to do?
#!/usr/bin/env ruby
require "erb"
require "etc"
if ARGV.empty?
puts "Usage: #$0 files... > output.html"
exit 1
end
Job = Struct.new(:file, :child)
MAX_JOBS = Etc.nprocessors
JOBS = []
puts <<~HEAD
<!DOCTYPE html>
<meta charset="utf-8">
HEAD
def wait_job(job)
puts "<h1><tt>#{ERB::Util.h job.file}</tt></h1>"
while data = job.child.read(1024)
print data
end
Process.waitpid(job.child.pid)
end
while file = ARGV.shift
while JOBS.size >= MAX_JOBS
wait_job(JOBS.shift)
end
child = IO.popen(%W(
pygmentize
-f html
-O nobackground,noclasses,linenos=inline
#{file}
))
JOBS << Job.new(file, child)
end
while job = JOBS.shift
wait_job(job)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment