rocco/bin/rocco

90 lines
2.6 KiB
Plaintext
Raw Normal View History

2010-03-08 17:56:18 +00:00
#!/usr/bin/env ruby
2010-03-16 09:27:49 +00:00
#/ Usage: rocco [-l <lang>] [-c <chars>] [-o <dir>] <file>...
#/ Generate literate-programming-style documentation for Ruby source <file>s.
#/
#/ Options:
2010-03-16 09:27:49 +00:00
#/ -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
#/
2010-03-16 09:27:49 +00:00
#/ --help Show this help message
require 'optparse'
2010-03-08 23:05:01 +00:00
# Write usage message to stdout and exit.
def usage(stream=$stderr, status=1)
stream.puts File.read(__FILE__).
grep(/^#\//).
map { |line| line.sub(/^#. ?/, '') }.
join
exit status
end
# 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."
end
# Parse command line options, aborting if anything goes wrong.
output_dir = '.'
2010-03-11 16:01:46 +00:00
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_tail("-h", "--help") { usage($stdout, 0) }
o.parse!
} or abort_with_note
2010-03-11 16:01:46 +00:00
# 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"
end
# What a fucking mess. Most of this is duplicated in rocco.rb too.
libdir = File.expand_path('../../lib', __FILE__).sub(/^#{Dir.pwd}\//, '')
2010-03-08 23:05:01 +00:00
begin
require 'rdiscount'
require 'rocco'
rescue LoadError
case $!.to_s
when /rdiscount/
if !defined?(Gem)
warn "warn: #$!. trying again with rubygems"
require 'rubygems'
retry
else
require 'bluecloth'
Markdown = BlueCloth
$LOADED_FEATURES << 'rdiscount.rb'
retry
2010-03-08 23:05:01 +00:00
end
when /rocco/
if !$:.include?(libdir)
warn "warn: #$!. trying again with #{libdir} on load path"
$:.unshift(libdir)
retry
end
end
raise
end
2010-03-08 17:56:18 +00:00
# Create the output directory if it doesn't already exist.
Dir.mkdir output_dir if !File.directory?(output_dir)
2010-03-11 16:01:46 +00:00
# Run each file through Rocco and write output.
sources.each do |filename|
rocco = Rocco.new(filename, sources, options)
dest = filename.split('.')[0..-2].join('.') + '.html'
2010-03-11 16:01:46 +00:00
puts "rocco: #{filename} -> #{dest}"
2010-03-09 01:30:40 +00:00
File.open(dest, 'wb') { |fd| fd.write(rocco.to_html) }
end