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>...
|
2010-03-11 15:58:25 +00:00
|
|
|
#/ 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-10-17 10:25:35 +00:00
|
|
|
#/ -t, --template=<path> The file to use as template when rendering HTML
|
2011-03-20 20:23:11 +00:00
|
|
|
#/ -d, --docblocks Parse Docblock @annotations in comments
|
2010-03-16 09:27:49 +00:00
|
|
|
#/ --help Show this help message
|
2010-03-11 15:58:25 +00:00
|
|
|
|
|
|
|
require 'optparse'
|
2010-04-02 22:46:12 +00:00
|
|
|
require 'fileutils'
|
2010-03-08 23:05:01 +00:00
|
|
|
|
2010-03-11 15:58:25 +00:00
|
|
|
# Write usage message to stdout and exit.
|
|
|
|
def usage(stream=$stderr, status=1)
|
2010-03-29 12:21:58 +00:00
|
|
|
stream.puts File.readlines(__FILE__).
|
2010-03-11 15:58:25 +00:00
|
|
|
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 = []
|
2010-03-16 09:17:17 +00:00
|
|
|
options = {}
|
2010-03-11 15:58:25 +00:00
|
|
|
ARGV.options { |o|
|
|
|
|
o.program_name = File.basename($0)
|
|
|
|
o.on("-o", "--output=DIR") { |dir| output_dir = dir }
|
2010-03-29 12:22:42 +00:00
|
|
|
o.on("-l", "--language=LANG") { |lang| options[:language] = lang }
|
|
|
|
o.on("-c", "--comment-chars=CHARS") { |chars| options[:comment_chars] = Regexp.escape(chars) }
|
2010-10-17 10:25:35 +00:00
|
|
|
o.on("-t", "--template=TEMPLATE") { |template| options[:template_file] = template }
|
2011-03-20 20:23:11 +00:00
|
|
|
o.on("-d", "--docblocks") { options[:docblocks] = true }
|
2010-03-11 15:58:25 +00:00
|
|
|
o.on_tail("-h", "--help") { usage($stdout, 0) }
|
|
|
|
o.parse!
|
|
|
|
} or abort_with_note
|
|
|
|
|
2010-03-19 22:53:51 +00:00
|
|
|
# Use http://pygments.appspot.com in case `pygmentize(1)` isn't available.
|
|
|
|
if ! ENV['PATH'].split(':').any? { |dir| File.exist?("#{dir}/pygmentize") }
|
|
|
|
unless options[:webservice]
|
|
|
|
$stderr.puts "pygmentize not in PATH; using pygments.appspot.com instead"
|
|
|
|
options[:webservice] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
2010-03-11 15:58:25 +00:00
|
|
|
# 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
|
2010-03-09 16:56:01 +00:00
|
|
|
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
|
|
|
|
2010-03-11 16:01:46 +00:00
|
|
|
# Run each file through Rocco and write output.
|
|
|
|
sources.each do |filename|
|
2010-03-16 09:17:17 +00:00
|
|
|
rocco = Rocco.new(filename, sources, options)
|
2011-03-05 11:37:05 +00:00
|
|
|
dest = filename.sub(Regexp.new("#{File.extname(filename)}$"),".html")
|
|
|
|
dest = File.join(output_dir, dest) if output_dir != '.'
|
2010-03-11 16:01:46 +00:00
|
|
|
puts "rocco: #{filename} -> #{dest}"
|
2010-04-02 22:46:12 +00:00
|
|
|
FileUtils.mkdir_p File.dirname(dest)
|
2010-03-09 01:30:40 +00:00
|
|
|
File.open(dest, 'wb') { |fd| fd.write(rocco.to_html) }
|
|
|
|
end
|