Class | RDoc::RDoc |
In: |
rdoc.rb
|
Parent: | Object |
Encapsulate the production of rdoc documentation. Basically you can use this as you would invoke rdoc from the command line:
rdoc = RDoc::RDoc.new rdoc.document(args)
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.
Generator | = | Struct.new(:file_name, :class_name, :key) | This is the list of output generators that we support | |
GENERATORS | = | {} |
Format up one or more files according to the given arguments. 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.
# File rdoc.rb, line 231 231: def document(argv) 232: 233: TopLevel::reset 234: 235: @stats = Stats.new 236: 237: options = Options.instance 238: options.parse(argv, GENERATORS) 239: 240: unless options.all_one_file 241: setup_output_dir(options.op_dir) 242: end 243: 244: file_info = parse_files(options) 245: 246: gen = options.generator 247: 248: $stderr.puts "\nGenerating #{gen.key.upcase}..." unless options.quiet 249: 250: require gen.file_name 251: 252: gen_class = Generators.const_get(gen.class_name) 253: 254: unless file_info.empty? 255: gen = gen_class.for(options) 256: 257: pwd = Dir.pwd 258: 259: Dir.chdir(options.op_dir) unless options.all_one_file 260: 261: begin 262: Diagram.new(file_info, options).draw if options.diagram 263: gen.generate(file_info) 264: ensure 265: Dir.chdir(pwd) 266: end 267: end 268: 269: unless options.quiet 270: puts 271: @stats.print 272: end 273: end
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
# File rdoc.rb, line 184 184: def list_files_in_directory(dir, options) 185: normalized_file_list(options, Dir.glob(File.join(dir, "*")), false, options.exclude) 186: end
Given a list of files and directories, create a list of all the Ruby files they contain.
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.
The effect of this is that if you want a file with a non- standard extension parsed, you must name it explicity.
# File rdoc.rb, line 156 156: def normalized_file_list(options, relative_files, force_doc = false, exclude_pattern=nil) 157: file_list = [] 158: 159: relative_files.each do |rel_file_name| 160: next if exclude_pattern && exclude_pattern =~ rel_file_name 161: case type = File.stat(rel_file_name).ftype 162: when "file" 163: file_list << rel_file_name.sub(/^\.\//, '') if force_doc || ParserFactory.can_parse(rel_file_name) 164: when "directory" 165: next if rel_file_name == "CVS" || rel_file_name == ".svn" 166: dot_doc = File.join(rel_file_name, DOT_DOC_FILENAME) 167: if File.file?(dot_doc) 168: file_list.concat(parse_dot_doc_file(rel_file_name, dot_doc, options)) 169: else 170: file_list.concat(list_files_in_directory(rel_file_name, options)) 171: end 172: else 173: raise RDocError.new("I can't deal with a #{type} #{rel_file_name}") 174: end 175: end 176: file_list 177: end
The .document file contains a list of file and directory name patterns, representing candidates for documentation. It may also contain comments (starting with ’#’)
# File rdoc.rb, line 130 130: def parse_dot_doc_file(in_dir, filename, options) 131: # read and strip comments 132: patterns = File.read(filename).gsub(/#.*/, '') 133: 134: result = [] 135: 136: patterns.split.each do |patt| 137: candidates = Dir.glob(File.join(in_dir, patt)) 138: result.concat(normalized_file_list(options, candidates)) 139: end 140: result 141: end
Parse each file on the command line, recursively entering directories
# File rdoc.rb, line 192 192: def parse_files(options) 193: 194: file_info = [] 195: 196: files = options.files 197: files = ["."] if files.empty? 198: 199: file_list = normalized_file_list(options, files, true) 200: 201: file_list.each do |fn| 202: $stderr.printf("\n%35s: ", File.basename(fn)) unless options.quiet 203: 204: content = File.open(fn, "r") {|f| f.read} 205: 206: top_level = TopLevel.new(fn) 207: parser = ParserFactory.parser_for(top_level, fn, content, options, @stats) 208: file_info << parser.scan 209: @stats.num_files += 1 210: end 211: 212: file_info 213: end
Create an output dir if it doesn‘t exist. If it does exist, but doesn‘t contain the flag file created.rid then we refuse to use it, as we may clobber some manually generated documentation
# File rdoc.rb, line 107 107: def setup_output_dir(op_dir) 108: flag_file = File.join(op_dir, "created.rid") 109: if File.exist?(op_dir) 110: unless File.directory?(op_dir) 111: error "'#{op_dir}' exists, and is not a directory" 112: end 113: unless File.file?(flag_file) 114: error "\nDirectory #{op_dir} already exists, but it looks like it\n" + 115: "isn't an RDoc directory. Because RDoc doesn't want to risk\n" + 116: "destroying any of your existing files, you'll need to\n" + 117: "specify a different output directory name (using the\n" + 118: "--op <dir> option).\n\n" 119: end 120: else 121: File.makedirs(op_dir) 122: end 123: File.open(flag_file, "w") {|f| f.puts Time.now } 124: end