2008-01-08 04:07:31 -05:00
|
|
|
require 'rdoc'
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
require 'rdoc/parser'
|
|
|
|
|
|
|
|
# Simple must come first
|
|
|
|
require 'rdoc/parser/simple'
|
|
|
|
require 'rdoc/parser/ruby'
|
|
|
|
require 'rdoc/parser/c'
|
|
|
|
require 'rdoc/parser/f95'
|
2008-09-24 22:43:03 -04:00
|
|
|
require 'rdoc/parser/perl'
|
2008-01-07 01:56:46 -05:00
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
require 'rdoc/stats'
|
2003-12-01 02:12:49 -05:00
|
|
|
require 'rdoc/options'
|
|
|
|
|
|
|
|
require 'rdoc/diagram'
|
|
|
|
|
|
|
|
require 'find'
|
2007-12-24 09:09:57 -05:00
|
|
|
require 'fileutils'
|
2006-09-17 10:46:13 -04:00
|
|
|
require 'time'
|
2003-12-01 02:12:49 -05:00
|
|
|
|
|
|
|
module RDoc
|
|
|
|
|
2008-01-07 01:56:46 -05:00
|
|
|
##
|
2008-07-21 14:35:14 -04:00
|
|
|
# Encapsulate the production of rdoc documentation. Basically you can use
|
|
|
|
# this as you would invoke rdoc from the command line:
|
2003-12-01 02:12:49 -05:00
|
|
|
#
|
2008-07-21 14:35:14 -04:00
|
|
|
# rdoc = RDoc::RDoc.new
|
|
|
|
# rdoc.document(args)
|
2003-12-01 02:12:49 -05:00
|
|
|
#
|
2008-07-21 14:35:14 -04:00
|
|
|
# Where +args+ is an array of strings, each corresponding to an argument
|
|
|
|
# you'd give rdoc on the command line. See rdoc/rdoc.rb for details.
|
2008-01-07 01:56:46 -05:00
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
class RDoc
|
|
|
|
|
2008-01-07 01:56:46 -05:00
|
|
|
Generator = Struct.new(:file_name, :class_name, :key)
|
|
|
|
|
2008-07-21 14:35:14 -04:00
|
|
|
##
|
|
|
|
# Accessor for statistics. Available after each call to parse_files
|
|
|
|
|
|
|
|
attr_reader :stats
|
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
##
|
2008-01-12 22:13:37 -05:00
|
|
|
# This is the list of output generator that we support
|
2008-01-07 01:56:46 -05:00
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
GENERATORS = {}
|
2008-01-07 01:56:46 -05:00
|
|
|
|
|
|
|
$LOAD_PATH.collect do |d|
|
|
|
|
File.expand_path d
|
|
|
|
end.find_all do |d|
|
2008-01-12 22:13:37 -05:00
|
|
|
File.directory? "#{d}/rdoc/generator"
|
2008-01-07 01:56:46 -05:00
|
|
|
end.each do |dir|
|
2008-01-12 22:13:37 -05:00
|
|
|
Dir.entries("#{dir}/rdoc/generator").each do |gen|
|
2008-01-12 22:02:49 -05:00
|
|
|
next unless /(\w+)\.rb$/ =~ gen
|
2003-12-01 02:12:49 -05:00
|
|
|
type = $1
|
|
|
|
unless GENERATORS.has_key? type
|
2008-01-12 22:13:37 -05:00
|
|
|
GENERATORS[type] = Generator.new("rdoc/generator/#{gen}",
|
2008-01-12 22:02:49 -05:00
|
|
|
"#{type.upcase}".intern,
|
2003-12-01 02:12:49 -05:00
|
|
|
type)
|
|
|
|
end
|
2008-01-07 01:56:46 -05:00
|
|
|
end
|
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-02-09 22:59:08 -05:00
|
|
|
def initialize
|
2008-07-21 14:35:14 -04:00
|
|
|
@stats = nil
|
2008-02-09 22:59:08 -05:00
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# Report an error message and exit
|
2008-01-07 01:56:46 -05:00
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
def error(msg)
|
2008-02-09 22:59:08 -05:00
|
|
|
raise ::RDoc::Error, msg
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
2008-01-07 01:56:46 -05:00
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
##
|
2008-01-07 01:56:46 -05:00
|
|
|
# Create an output dir if it doesn't exist. If it does exist, but doesn't
|
|
|
|
# contain the flag file <tt>created.rid</tt> then we refuse to use it, as
|
|
|
|
# we may clobber some manually generated documentation
|
|
|
|
|
2006-09-17 10:46:13 -04:00
|
|
|
def setup_output_dir(op_dir, force)
|
|
|
|
flag_file = output_flag_file(op_dir)
|
2003-12-01 02:12:49 -05:00
|
|
|
if File.exist?(op_dir)
|
|
|
|
unless File.directory?(op_dir)
|
2008-01-07 01:56:46 -05:00
|
|
|
error "'#{op_dir}' exists, and is not a directory"
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
2006-09-17 10:46:13 -04:00
|
|
|
begin
|
|
|
|
created = File.read(flag_file)
|
|
|
|
rescue SystemCallError
|
2003-12-01 02:12:49 -05:00
|
|
|
error "\nDirectory #{op_dir} already exists, but it looks like it\n" +
|
|
|
|
"isn't an RDoc directory. Because RDoc doesn't want to risk\n" +
|
|
|
|
"destroying any of your existing files, you'll need to\n" +
|
|
|
|
"specify a different output directory name (using the\n" +
|
|
|
|
"--op <dir> option).\n\n"
|
2006-09-17 10:46:13 -04:00
|
|
|
else
|
|
|
|
last = (Time.parse(created) unless force rescue nil)
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
|
|
|
else
|
2007-12-24 09:09:57 -05:00
|
|
|
FileUtils.mkdir_p(op_dir)
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
2006-09-17 10:46:13 -04:00
|
|
|
last
|
|
|
|
end
|
|
|
|
|
2008-01-07 01:56:46 -05:00
|
|
|
##
|
2006-09-17 10:46:13 -04:00
|
|
|
# Update the flag file in an output directory.
|
2008-01-07 01:56:46 -05:00
|
|
|
|
2006-09-17 10:46:13 -04:00
|
|
|
def update_output_dir(op_dir, time)
|
|
|
|
File.open(output_flag_file(op_dir), "w") {|f| f.puts time.rfc2822 }
|
|
|
|
end
|
|
|
|
|
2008-01-07 01:56:46 -05:00
|
|
|
##
|
2006-09-17 10:46:13 -04:00
|
|
|
# Return the path name of the flag file in an output directory.
|
2008-01-07 01:56:46 -05:00
|
|
|
|
2006-09-17 10:46:13 -04:00
|
|
|
def output_flag_file(op_dir)
|
|
|
|
File.join(op_dir, "created.rid")
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
|
|
|
|
2008-01-07 01:56:46 -05:00
|
|
|
##
|
|
|
|
# The .document file contains a list of file and directory name patterns,
|
|
|
|
# representing candidates for documentation. It may also contain comments
|
|
|
|
# (starting with '#')
|
|
|
|
|
2004-01-02 01:01:12 -05:00
|
|
|
def parse_dot_doc_file(in_dir, filename, options)
|
|
|
|
# read and strip comments
|
|
|
|
patterns = File.read(filename).gsub(/#.*/, '')
|
|
|
|
|
|
|
|
result = []
|
|
|
|
|
|
|
|
patterns.split.each do |patt|
|
|
|
|
candidates = Dir.glob(File.join(in_dir, patt))
|
|
|
|
result.concat(normalized_file_list(options, candidates))
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2008-01-07 01:56:46 -05:00
|
|
|
##
|
|
|
|
# Given a list of files and directories, create a list of all the Ruby
|
|
|
|
# files they contain.
|
2004-02-23 23:24:02 -05:00
|
|
|
#
|
2008-01-07 01:56:46 -05:00
|
|
|
# If +force_doc+ is true we always add the given files, if false, only
|
|
|
|
# add files that we guarantee we can parse. It is true when looking at
|
|
|
|
# files given on the command line, false when recursing through
|
|
|
|
# subdirectories.
|
2004-12-11 20:16:19 -05:00
|
|
|
#
|
2008-01-07 01:56:46 -05:00
|
|
|
# The effect of this is that if you want a file with a non-standard
|
2008-06-04 05:37:38 -04:00
|
|
|
# extension parsed, you must name it explicitly.
|
2004-02-23 23:24:02 -05:00
|
|
|
|
2008-01-07 01:56:46 -05:00
|
|
|
def normalized_file_list(options, relative_files, force_doc = false,
|
|
|
|
exclude_pattern = nil)
|
2003-12-01 02:12:49 -05:00
|
|
|
file_list = []
|
|
|
|
|
|
|
|
relative_files.each do |rel_file_name|
|
2004-12-11 20:16:19 -05:00
|
|
|
next if exclude_pattern && exclude_pattern =~ rel_file_name
|
2006-09-17 10:46:13 -04:00
|
|
|
stat = File.stat(rel_file_name)
|
|
|
|
case type = stat.ftype
|
2003-12-01 02:12:49 -05:00
|
|
|
when "file"
|
2006-09-17 10:46:13 -04:00
|
|
|
next if @last_created and stat.mtime < @last_created
|
2008-07-17 20:46:16 -04:00
|
|
|
|
|
|
|
if force_doc or ::RDoc::Parser.can_parse(rel_file_name) then
|
|
|
|
file_list << rel_file_name.sub(/^\.\//, '')
|
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
when "directory"
|
2004-01-12 11:06:35 -05:00
|
|
|
next if rel_file_name == "CVS" || rel_file_name == ".svn"
|
2004-01-02 01:01:12 -05:00
|
|
|
dot_doc = File.join(rel_file_name, DOT_DOC_FILENAME)
|
|
|
|
if File.file?(dot_doc)
|
|
|
|
file_list.concat(parse_dot_doc_file(rel_file_name, dot_doc, options))
|
|
|
|
else
|
|
|
|
file_list.concat(list_files_in_directory(rel_file_name, options))
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
|
|
|
else
|
2008-01-07 01:56:46 -05:00
|
|
|
raise RDoc::Error, "I can't deal with a #{type} #{rel_file_name}"
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
|
|
|
end
|
2008-01-07 01:56:46 -05:00
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
file_list
|
|
|
|
end
|
|
|
|
|
2008-01-07 01:56:46 -05:00
|
|
|
##
|
|
|
|
# Return a list of the files to be processed in a directory. We know that
|
|
|
|
# this directory doesn't have a .document file, so we're looking for real
|
|
|
|
# files. However we may well contain subdirectories which must be tested
|
|
|
|
# for .document files.
|
|
|
|
|
2004-01-02 01:01:12 -05:00
|
|
|
def list_files_in_directory(dir, options)
|
2008-01-13 22:34:05 -05:00
|
|
|
files = Dir.glob File.join(dir, "*")
|
|
|
|
|
|
|
|
normalized_file_list options, files, false, options.exclude
|
2004-01-02 01:01:12 -05:00
|
|
|
end
|
|
|
|
|
2008-01-07 01:56:46 -05:00
|
|
|
##
|
|
|
|
# Parse each file on the command line, recursively entering directories.
|
2003-12-01 02:12:49 -05:00
|
|
|
|
|
|
|
def parse_files(options)
|
2008-07-21 14:35:14 -04:00
|
|
|
@stats = Stats.new options.verbosity
|
2008-09-24 22:43:03 -04:00
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
files = options.files
|
|
|
|
files = ["."] if files.empty?
|
|
|
|
|
2008-09-24 22:43:03 -04:00
|
|
|
file_list = normalized_file_list(options, files, true, options.exclude)
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
return [] if file_list.empty?
|
|
|
|
|
|
|
|
file_info = []
|
|
|
|
|
2008-07-21 14:35:14 -04:00
|
|
|
file_list.each do |filename|
|
|
|
|
@stats.add_file filename
|
2008-01-07 01:56:46 -05:00
|
|
|
|
2008-02-11 20:27:52 -05:00
|
|
|
content = if RUBY_VERSION >= '1.9' then
|
2008-07-21 14:35:14 -04:00
|
|
|
File.open(filename, "r:ascii-8bit") { |f| f.read }
|
2008-02-11 20:27:52 -05:00
|
|
|
else
|
2008-07-21 14:35:14 -04:00
|
|
|
File.read filename
|
2008-02-11 20:27:52 -05:00
|
|
|
end
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
if defined? Encoding then
|
|
|
|
if /coding:\s*(\S+)/ =~ content[/\A(?:.*\n){0,2}/]
|
|
|
|
if enc = ::Encoding.find($1)
|
|
|
|
content.force_encoding(enc)
|
|
|
|
end
|
2007-12-24 11:44:39 -05:00
|
|
|
end
|
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-07-21 14:35:14 -04:00
|
|
|
top_level = ::RDoc::TopLevel.new filename
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2008-07-21 14:35:14 -04:00
|
|
|
parser = ::RDoc::Parser.for top_level, filename, content, options,
|
|
|
|
@stats
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
file_info << parser.scan
|
|
|
|
end
|
|
|
|
|
|
|
|
file_info
|
|
|
|
end
|
|
|
|
|
2008-01-07 01:56:46 -05:00
|
|
|
##
|
2003-12-01 02:12:49 -05:00
|
|
|
# Format up one or more files according to the given arguments.
|
|
|
|
#
|
2008-01-07 01:56:46 -05:00
|
|
|
# For simplicity, _argv_ is an array of strings, equivalent to the strings
|
|
|
|
# that would be passed on the command line. (This isn't a coincidence, as
|
|
|
|
# we _do_ pass in ARGV when running interactively). For a list of options,
|
|
|
|
# see rdoc/rdoc.rb. By default, output will be stored in a directory
|
|
|
|
# called +doc+ below the current directory, so make sure you're somewhere
|
|
|
|
# writable before invoking.
|
|
|
|
#
|
|
|
|
# Throws: RDoc::Error on error
|
2003-12-01 02:12:49 -05:00
|
|
|
|
|
|
|
def document(argv)
|
|
|
|
TopLevel::reset
|
|
|
|
|
2008-04-26 12:14:19 -04:00
|
|
|
@options = Options.new GENERATORS
|
|
|
|
@options.parse argv
|
2006-09-17 10:46:13 -04:00
|
|
|
|
|
|
|
@last_created = nil
|
2008-01-07 01:56:46 -05:00
|
|
|
|
2008-04-26 12:14:19 -04:00
|
|
|
unless @options.all_one_file then
|
|
|
|
@last_created = setup_output_dir @options.op_dir, @options.force_update
|
2004-02-23 16:17:26 -05:00
|
|
|
end
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2006-09-17 10:46:13 -04:00
|
|
|
start_time = Time.now
|
2004-02-23 16:17:26 -05:00
|
|
|
|
2008-04-26 12:14:19 -04:00
|
|
|
file_info = parse_files @options
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
@options.title = "RDoc Documentation"
|
|
|
|
|
2006-09-17 10:46:13 -04:00
|
|
|
if file_info.empty?
|
2008-04-26 12:14:19 -04:00
|
|
|
$stderr.puts "\nNo newer files." unless @options.quiet
|
2006-09-17 10:46:13 -04:00
|
|
|
else
|
2008-07-17 20:46:16 -04:00
|
|
|
@gen = @options.generator
|
2006-09-17 10:46:13 -04:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
$stderr.puts "\nGenerating #{@gen.key.upcase}..." unless @options.quiet
|
2006-09-17 10:46:13 -04:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
require @gen.file_name
|
2006-09-17 10:46:13 -04:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
gen_class = ::RDoc::Generator.const_get @gen.class_name
|
|
|
|
@gen = gen_class.for @options
|
2003-12-01 02:12:49 -05:00
|
|
|
|
|
|
|
pwd = Dir.pwd
|
|
|
|
|
2008-04-26 12:14:19 -04:00
|
|
|
Dir.chdir @options.op_dir unless @options.all_one_file
|
2003-12-01 02:12:49 -05:00
|
|
|
|
|
|
|
begin
|
2008-04-26 12:14:19 -04:00
|
|
|
Diagram.new(file_info, @options).draw if @options.diagram
|
2008-07-17 20:46:16 -04:00
|
|
|
@gen.generate(file_info)
|
2006-09-17 10:46:13 -04:00
|
|
|
update_output_dir(".", start_time)
|
2003-12-01 02:12:49 -05:00
|
|
|
ensure
|
|
|
|
Dir.chdir(pwd)
|
|
|
|
end
|
2004-01-02 01:01:12 -05:00
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-04-26 12:14:19 -04:00
|
|
|
unless @options.quiet
|
2004-01-02 01:01:12 -05:00
|
|
|
puts
|
|
|
|
@stats.print
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|