rocco/bin/rocco

101 lines
3.2 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
#/ -t, --template=<path> The file to use as template when rendering HTML
#/ -d, --docblocks Parse Docblock @annotations in comments
2010-03-16 09:27:49 +00:00
#/ --help Show this help message
require 'optparse'
require 'fileutils'
2010-03-08 23:05:01 +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__).
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 }
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) }
o.on("-t", "--template=TEMPLATE") { |template| options[:template_file] = template }
o.on("-d", "--docblocks") { options[:docblocks] = true }
o.on_tail("-h", "--help") { usage($stdout, 0) }
o.parse!
} or abort_with_note
# 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
# 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
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)
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}"
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