This repository has been archived by the owner. It is now read-only.
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 75 lines (66 sloc) 2.65 KB
#!/usr/bin/env ruby
#/ Usage: rocco [-l <lang>] [-c <chars>] [-o <dir>] <file>...
#/ Generate literate-programming-style documentation for Ruby source <file>s.
#/ Options:
#/ -l, --language=<lang> The Pygments lexer to use to highlight code
#/ -c, --comment-chars=<chars>
#/ The string to recognize as a comment marker
#/ -o, --output=<dir> Directory where generated HTML files are written
#/ -t, --template=<path> The file to use as template when rendering HTML
#/ -d, --docblocks Parse Docblock @annotations in comments
#/ --help Show this help message
require 'optparse'
require 'fileutils'
require 'rocco'
# Write usage message to stdout and exit.
def usage(stream=$stderr, status=1)
stream.puts File.readlines(__FILE__).
map { |line| line.sub(/^#. ?/, '') }.
exit status
# Like `Kernel#abort` but writes a note encouraging the user to consult
# `rocco --help` for more information.
def abort_with_note(message=nil)
$stderr.puts message if message
abort "See `rocco --help' for usage information."
# Parse command line options, aborting if anything goes wrong.
output_dir = '.'
sources = []
options = {}
ARGV.options { |o|
o.program_name = File.basename($0)
o.on("-o", "--output=DIR") { |dir| output_dir = dir }
o.on("-l", "--language=LANG") { |lang| options[:language] = lang }
o.on("-c", "--comment-chars=CHARS") { |chars| options[:comment_chars] = Regexp.escape(chars) }
o.on("-t", "--template=TEMPLATE") { |template| options[:template_file] = template }
o.on("-d", "--docblocks") { options[:docblocks] = true }
o.on("-s", "--stylesheet=STYLESHEET") { |stylesheet| options[:stylesheet] = stylesheet }
o.on_tail("-h", "--help") { usage($stdout, 0) }
} or abort_with_note
# Use in case `pygmentize(1)` isn't available.
unless ENV['PATH'].split(':').any? { |dir| File.exist?("#{dir}/pygmentize") }
unless options[:webservice]
$stderr.puts "pygmentize not in PATH; using instead"
options[:webservice] = true
# Eat sources from ARGV.
sources << ARGV.shift while ARGV.any?
# Make sure we have some files to work with.
if sources.empty?
abort_with_note "#{File.basename($0)}: no input <file>s given"
# Run each file through Rocco and write output.
sources.each do |filename|
rocco =, sources, options)
dest = filename.sub("#{File.extname(filename)}$"),".html")
dest = File.join(output_dir, dest) if output_dir != '.'
puts "rocco: #{filename} -> #{dest}"
FileUtils.mkdir_p File.dirname(dest), 'wb') { |fd| fd.write(rocco.to_html) }