Class: RDoc::RDoc
| Relationships & Source Files | |
| Inherits: | Object | 
| Defined in: | lib/rdoc/rdoc.rb | 
Overview
This is the driver for generating RDoc output.  It handles file parsing and generation of output.
To use this class to generate RDoc output via the API, the recommended way is:
rdoc = RDoc::RDoc.new
 = rdoc. # returns an RDoc::Options instance
# set extra options
rdoc.document You can also generate output like the rdoc executable:
rdoc = RDoc::RDoc.new
rdoc.document argvWhere argv is an array of strings, each corresponding to an argument you'd give rdoc on the command line.  See <tt>rdoc –help<tt> for details.
Constant Summary
- 
    GENERATORS =
    # File 'lib/rdoc/rdoc.rb', line 36This is the list of supported output generators {}
Class Attribute Summary
- 
    
      .current  
    
    rw
    Active RDocinstance.
- 
    
      .current=(rdoc)  
    
    rw
    Sets the active RDocinstance.
Class Method Summary
- 
    
      .add_generator(klass)  
    
    Add klassthat can generate output after parsing.
- 
    
      .new  ⇒ RDoc 
    
    constructor
    Creates a new RDocinstance.
Instance Attribute Summary
- 
    
      #exclude  
    
    rw
    File pattern to exclude. 
- 
    
      #generator  
    
    rw
    Generator instance used for creating output. 
- 
    
      #last_modified  
    
    readonly
    Hash of files and their last modified times. 
- 
    
      #options  
    
    rw
    RDocoptions.
- 
    
      #stats  
    
    readonly
    Accessor for statistics. 
- 
    
      #store  
    
    rw
    The current documentation store. 
- 
    
      #store=(store)  
    
    rw
    Sets the current documentation tree to #store and sets the store's rdoc driver to this instance. 
Instance Method Summary
- 
    
      #document(options)  
    
    Generates documentation or a coverage report depending upon the settings in #options. 
- 
    
      #error(msg)  
    
    Report an error message and exit. 
- 
    
      #gather_files(files)  
    
    Gathers a set of parseable files from the files and directories listed in files.
- 
    
      #generate  
    
    Generates documentation for file_info(from #parse_files) into the output dir using the generator selected by theRDocoptions.
- 
    
      #handle_pipe  
    
    Turns RDoc from stdin into HTML. 
- 
    
      #install_siginfo_handler  
    
    Installs a siginfo handler that prints the current filename. 
- 
    
      #list_files_in_directory(dir)  
    
    Return a list of the files to be processed in a directory. 
- 
    
      #load_options  
    
    Loads options from .rdoc_optionsif the file exists, otherwise creates a new Options instance.
- 
    
      #normalized_file_list(relative_files, force_doc = false, exclude_pattern = nil)  
    
    Given a list of files and directories, create a list of all the Ruby files they contain. 
- 
    
      #output_flag_file(op_dir)  
    
    Return the path name of the flag file in an output directory. 
- 
    
      #parse_dot_doc_file(in_dir, filename)  
    
    The #document file contains a list of file and directory name patterns, representing candidates for documentation. 
- 
    
      #parse_file(filename)  
    
    Parses filenameand returns an TopLevel
- 
    
      #parse_files(files)  
    
    Parse each file on the command line, recursively entering directories. 
- 
    
      #remove_siginfo_handler  
    
    Removes a siginfo handler and replaces the previous. 
- 
    
      #remove_unparseable(files)  
    
    Removes file extensions known to be unparseable from filesand TAGS files for emacs and vim.
- 
    
      #setup_output_dir(dir, force)  
    
    Create an output dir if it doesn't exist. 
- 
    
      #update_output_dir(op_dir, time, last = {})  
    
    Update the flag file in an output directory. 
Constructor Details
    .new  ⇒ RDoc 
  
Creates a new RDoc instance.  Call #document to parse files and generate documentation.
# File 'lib/rdoc/rdoc.rb', line 94
def initialize @current = nil @exclude = nil @generator = nil @last_modified = {} @old_siginfo = nil @options = nil @stats = nil @store = nil end
Class Attribute Details
.current (rw)
Active RDoc instance
# File 'lib/rdoc/rdoc.rb', line 79
def self.current @current end
.current=(rdoc) (rw)
Sets the active RDoc instance
# File 'lib/rdoc/rdoc.rb', line 86
def self.current= rdoc @current = rdoc end
Class Method Details
.add_generator(klass)
Add klass that can generate output after parsing
# File 'lib/rdoc/rdoc.rb', line 71
def self.add_generator(klass) name = klass.name.sub(/^RDoc::Generator::/, '').downcase GENERATORS[name] = klass end
Instance Attribute Details
#exclude (rw)
File pattern to exclude
# File 'lib/rdoc/rdoc.rb', line 41
attr_accessor :exclude
#generator (rw)
Generator instance used for creating output
# File 'lib/rdoc/rdoc.rb', line 46
attr_accessor :generator
#last_modified (readonly)
Hash of files and their last modified times.
# File 'lib/rdoc/rdoc.rb', line 51
attr_reader :last_modified
#options (rw)
RDoc options
# File 'lib/rdoc/rdoc.rb', line 56
attr_accessor :
#stats (readonly)
Accessor for statistics. Available after each call to parse_files
# File 'lib/rdoc/rdoc.rb', line 61
attr_reader :stats
#store (rw)
The current documentation store
# File 'lib/rdoc/rdoc.rb', line 66
attr_reader :store
#store=(store) (rw)
Sets the current documentation tree to #store and sets the store's rdoc driver to this instance.
Instance Method Details
#document(options)
Generates documentation or a coverage report depending upon the settings in #options.
#options can be either an Options instance or an array of strings equivalent to the strings that would be passed on the command line like %w[-q -o doc -t My\ Doc\ Title].  #document will automatically call Options#finish if an options instance was given.
For a list of options, see either Options or rdoc --help.
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 'lib/rdoc/rdoc.rb', line 456
def document self.store = RDoc::Store.new if RDoc::Options === then @options = @options.finish else @options = @options.parse end if @options.pipe then handle_pipe exit end @exclude = @options.exclude unless @options.coverage_report then @last_modified = setup_output_dir @options.op_dir, @options.force_update end @store.encoding = @options.encoding @store.dry_run = @options.dry_run @store.main = @options.main_page @store.title = @options.title @store.path = @options.op_dir @start_time = Time.now @store.load_cache file_info = parse_files @options.files @options.default_title = "RDoc Documentation" @store.complete @options.visibility @stats.coverage_level = @options.coverage_report if @options.coverage_report then puts puts @stats.report.accept RDoc::Markup::ToRdoc.new elsif file_info.empty? then $stderr.puts "\nNo newer files." unless @options.quiet else gen_klass = @options.generator @generator = gen_klass.new @store, @options generate end if @stats and (@options.coverage_report or not @options.quiet) then puts puts @stats.summary.accept RDoc::Markup::ToRdoc.new end exit @stats.fully_documented? if @options.coverage_report end
#error(msg)
Report an error message and exit
# File 'lib/rdoc/rdoc.rb', line 108
def error(msg) raise RDoc::Error, msg end
#gather_files(files)
Gathers a set of parseable files from the files and directories listed in files.
# File 'lib/rdoc/rdoc.rb', line 116
def gather_files files files = ["."] if files.empty? file_list = normalized_file_list files, true, @exclude file_list = file_list.uniq file_list = remove_unparseable file_list file_list.sort end
#generate
Generates documentation for file_info (from #parse_files) into the output dir using the generator selected by the RDoc options
# File 'lib/rdoc/rdoc.rb', line 523
def generate if @options.dry_run then # do nothing @generator.generate else Dir.chdir @options.op_dir do unless @options.quiet then $stderr.puts "\nGenerating #{@generator.class.name.sub(/^.*::/, '')} format into #{Dir.pwd}..." end @generator.generate update_output_dir '.', @start_time, @last_modified end end end
#handle_pipe
Turns RDoc from stdin into HTML
#install_siginfo_handler
Installs a siginfo handler that prints the current filename.
# File 'lib/rdoc/rdoc.rb', line 146
def install_siginfo_handler return unless Signal.list.include? 'INFO' @old_siginfo = trap 'INFO' do puts @current if @current end end
#list_files_in_directory(dir)
# File 'lib/rdoc/rdoc.rb', line 327
def list_files_in_directory dir files = Dir.glob File.join(dir, "*") normalized_file_list files, false, @options.exclude end
#load_options
Loads options from .rdoc_options if the file exists, otherwise creates a new Options instance.
#normalized_file_list(relative_files, force_doc = false, exclude_pattern = nil)
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 explicitly.
# File 'lib/rdoc/rdoc.rb', line 282
def normalized_file_list(relative_files, force_doc = false, exclude_pattern = nil) file_list = [] relative_files.each do |rel_file_name| next if rel_file_name.end_with? 'created.rid' next if exclude_pattern && exclude_pattern =~ rel_file_name stat = File.stat rel_file_name rescue next case type = stat.ftype when "file" then next if last_modified = @last_modified[rel_file_name] and stat.mtime.to_i <= last_modified.to_i if force_doc or RDoc::Parser.can_parse(rel_file_name) then file_list << rel_file_name.sub(/^\.\//, '') @last_modified[rel_file_name] = stat.mtime end when "directory" then next if rel_file_name == "CVS" || rel_file_name == ".svn" created_rid = File.join rel_file_name, "created.rid" next if File.file? created_rid dot_doc = File.join rel_file_name, RDoc::DOT_DOC_FILENAME if File.file? dot_doc then file_list << parse_dot_doc_file(rel_file_name, dot_doc) else file_list << list_files_in_directory(rel_file_name) end else warn "rdoc can't parse the #{type} #{rel_file_name}" end end file_list.flatten end
#output_flag_file(op_dir)
Return the path name of the flag file in an output directory.
# File 'lib/rdoc/rdoc.rb', line 247
def output_flag_file(op_dir) File.join op_dir, "created.rid" end
#parse_dot_doc_file(in_dir, filename)
The #document file contains a list of file and directory name patterns, representing candidates for documentation. It may also contain comments (starting with '#')
# File 'lib/rdoc/rdoc.rb', line 256
def parse_dot_doc_file in_dir, filename # 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(candidates) end result end
#parse_file(filename)
Parses filename and returns an TopLevel
# File 'lib/rdoc/rdoc.rb', line 336
def parse_file filename encoding = @options.encoding filename = filename.encode encoding @stats.add_file filename return if RDoc::Parser.binary? filename content = RDoc::Encoding.read_file filename, encoding return unless content filename_path = Pathname(filename). begin relative_path = filename_path.relative_path_from @options.root rescue ArgumentError relative_path = filename_path end if @options.page_dir and relative_path.to_s.start_with? @options.page_dir.to_s then relative_path = relative_path.relative_path_from @options.page_dir end top_level = @store.add_file filename, relative_path.to_s parser = RDoc::Parser.for top_level, filename, content, @options, @stats return unless parser parser.scan # restart documentation for the classes & modules found top_level.classes_or_modules.each do |cm| cm.done_documenting = false end top_level rescue Errno::EACCES => e $stderr.puts <<-EOF Unable to read #{filename}, #{e.} Please check the permissions for this file. Perhaps you do not have access to it or perhaps the original author's permissions are to restrictive. If the this is not your library please report a bug to the author. EOF rescue => e $stderr.puts <<-EOF Before reporting this, could you check that the file you're documenting has proper syntax: #{Gem.ruby} -c #{filename} RDoc is not a full Ruby parser and will fail when fed invalid ruby programs. The internal error was: \t(#{e.class}) #{e.} EOF $stderr.puts e.backtrace.join("\n\t") if $DEBUG_RDOC raise e nil end
#parse_files(files)
Parse each file on the command line, recursively entering directories.
# File 'lib/rdoc/rdoc.rb', line 408
def parse_files files file_list = gather_files files @stats = RDoc::Stats.new @store, file_list.length, @options.verbosity return [] if file_list.empty? = @options.dup @stats.begin_adding file_info = file_list.map do |filename| @current = filename parse_file filename end.compact @stats.done_adding @options = file_info end
#remove_siginfo_handler
Removes a siginfo handler and replaces the previous
# File 'lib/rdoc/rdoc.rb', line 542
def remove_siginfo_handler return unless Signal.list.key? 'INFO' handler = @old_siginfo || 'DEFAULT' trap 'INFO', handler end
#remove_unparseable(files)
Removes file extensions known to be unparseable from files and TAGS files for emacs and vim.
# File 'lib/rdoc/rdoc.rb', line 432
def remove_unparseable files files.reject do |file| file =~ /\.(?:class|eps|erb|scpt\.txt|svg|ttf|yml)$/i or (file =~ /tags$/i and open(file, 'rb') { |io| io.read(100) =~ /\A(\f\n[^,],\d$|!_TAG_)/ }) end end
#setup_output_dir(dir, force)
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 'lib/rdoc/rdoc.rb', line 180
def setup_output_dir(dir, force) flag_file = output_flag_file dir last = {} if @options.dry_run then # do nothing elsif File.exist? dir then error "#{dir} exists and is not a directory" unless File.directory? dir begin open flag_file do |io| unless force then Time.parse io.gets io.each do |line| file, time = line.split "\t", 2 time = Time.parse(time) rescue next last[file] = time end end end rescue SystemCallError, TypeError error <<-ERROR Directory #{dir} already exists, but it looks like it isn't an RDoc directory. Because RDoc doesn't want to risk destroying any of your existing files, you'll need to specify a different output directory name (using the --op <dir> option) ERROR end unless @options.force_output else FileUtils.mkdir_p dir FileUtils.touch flag_file end last end
#update_output_dir(op_dir, time, last = {})
Update the flag file in an output directory.
# File 'lib/rdoc/rdoc.rb', line 233
def update_output_dir(op_dir, time, last = {}) return if @options.dry_run or not @options.update_output_dir open output_flag_file(op_dir), "w" do |f| f.puts time.rfc2822 last.each do |n, t| f.puts "#{n}\t#{t.rfc2822}" end end end