class RDoc::RDoc
这是生成 RDoc
输出的驱动程序。它处理文件解析和输出生成。
要使用此类通过 API 生成 RDoc
输出,推荐的方法是
rdoc = RDoc::RDoc.new options = RDoc::Options.load_options # returns an RDoc::Options instance # set extra options rdoc.document options
您还可以像 rdoc
可执行文件一样生成输出
rdoc = RDoc::RDoc.new rdoc.document argv
其中 argv
是一个字符串数组,每个字符串对应于您在命令行上给 rdoc 的参数。 有关详细信息,请参阅 rdoc --help
。
常量
- GENERATORS
-
这是支持的输出生成器的列表
- TEST_SUITE_DIRECTORY_NAMES
-
如果应跳过测试套件,则跳过的目录名称列表
- UNCONDITIONALLY_SKIPPED_DIRECTORIES
-
始终跳过的目录名称列表
属性
用于创建输出的生成器实例
文件及其最后修改时间的 Hash
。
RDoc
选项
统计信息的访问器。在每次调用 parse_files
后可用
当前的文档存储
公共类方法
源代码
# File lib/rdoc/rdoc.rb, line 77 def self.add_generator(klass) name = klass.name.sub(/^RDoc::Generator::/, '').downcase GENERATORS[name] = klass end
添加在解析后可以生成输出的 klass
源代码
# File lib/rdoc/rdoc.rb, line 92 def self.current= rdoc @current = rdoc end
设置活动的 RDoc::RDoc
实例
源代码
# File lib/rdoc/rdoc.rb, line 100 def initialize @current = nil @generator = nil @last_modified = {} @old_siginfo = nil @options = nil @stats = nil @store = nil end
创建一个新的 RDoc::RDoc
实例。调用 document
来解析文件并生成文档。
公共实例方法
源代码
# File lib/rdoc/rdoc.rb, line 452 def document options self.store = RDoc::Store.new if RDoc::Options === options then @options = options else @options = RDoc::Options.load_options @options.parse options end @options.finish if @options.pipe then handle_pipe exit end 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
根据 options
中的设置生成文档或覆盖率报告。
options
可以是 RDoc::Options
实例,也可以是等效于命令行上传递的字符串的字符串数组,例如 %w[-q -o doc -t My\ Doc\ Title]
。 如果给出了 options 实例,document
将自动调用 RDoc::Options#finish
。
有关选项列表,请参阅 RDoc::Options
或 rdoc --help
。
默认情况下,输出将存储在当前目录下的一个名为“doc”的目录中,因此请确保您在调用之前处于可写的位置。
源代码
# File lib/rdoc/rdoc.rb, line 113 def error(msg) raise RDoc::Error, msg end
报告错误消息并退出
源代码
# File lib/rdoc/rdoc.rb, line 121 def gather_files files files = [@options.root.to_s] if files.empty? file_list = normalized_file_list files, true, @options.exclude file_list = remove_unparseable(file_list) if file_list.count {|name, mtime| file_list[name] = @last_modified[name] unless mtime mtime } > 0 @last_modified.replace file_list file_list.keys.sort else [] end end
从 files
中列出的文件和目录中收集一组可解析的文件。
源代码
# File lib/rdoc/rdoc.rb, line 517 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}..." $stderr.puts "\nYou can visit the home page at: \e]8;;file://#{Dir.pwd}/index.html\e\\file://#{Dir.pwd}/index.html\e]8;;\e\\" end @generator.generate update_output_dir '.', @start_time, @last_modified end end end
使用 RDoc
选项选择的生成器,为 file_info
(来自 parse_files
)在输出目录中生成文档
源代码
# File lib/rdoc/rdoc.rb, line 142 def handle_pipe @html = RDoc::Markup::ToHtml.new @options parser = RDoc::Text::MARKUP_FORMAT[@options.markup] document = parser.parse $stdin.read out = @html.convert document $stdout.write out end
将来自 stdin 的 RDoc
转换为 HTML
源代码
# File lib/rdoc/rdoc.rb, line 157 def install_siginfo_handler return unless Signal.list.include? 'INFO' @old_siginfo = trap 'INFO' do puts @current if @current end end
安装一个 siginfo 处理程序,该处理程序打印当前文件名。
源代码
# File lib/rdoc/rdoc.rb, line 323 def list_files_in_directory dir files = Dir.glob File.join(dir, "*") normalized_file_list files, false, @options.exclude end
返回要在目录中处理的文件列表。 我们知道此目录没有 .document 文件,因此我们正在寻找真实的文件。 但是,我们可能包含必须测试 .document 文件的子目录。
源代码
# File lib/rdoc/rdoc.rb, line 275 def normalized_file_list(relative_files, force_doc = false, exclude_pattern = nil) file_list = {} relative_files.each do |rel_file_name| rel_file_name = rel_file_name.sub(/^\.\//, '') 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 mtime = (stat.mtime unless (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] = mtime end when "directory" then next if UNCONDITIONALLY_SKIPPED_DIRECTORIES.include?(rel_file_name) basename = File.basename(rel_file_name) next if options.skip_tests && TEST_SUITE_DIRECTORY_NAMES.include?(basename) 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.update(parse_dot_doc_file(rel_file_name, dot_doc)) else file_list.update(list_files_in_directory(rel_file_name)) end else warn "rdoc can't parse the #{type} #{rel_file_name}" end end file_list end
给定文件和目录列表,创建它们包含的所有 Ruby 文件的列表。
如果 force_doc
为 true,我们始终添加给定的文件,如果为 false,则仅添加我们保证可以解析的文件。 在查看命令行上给定的文件时为 true,在递归遍历子目录时为 false。
这样做的效果是,如果您希望解析具有非标准扩展名的文件,则必须显式命名它。
源代码
# File lib/rdoc/rdoc.rb, line 240 def output_flag_file(op_dir) File.join op_dir, "created.rid" end
返回输出目录中标志文件的路径名。
源代码
# File lib/rdoc/rdoc.rb, line 249 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.update normalized_file_list(candidates, false, @options.exclude) end result end
.document 文件包含文件和目录名称模式的列表,表示文档的候选项。 它也可能包含注释(以“#”开头)
源代码
# File lib/rdoc/rdoc.rb, line 332 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).expand_path 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_name: relative_path.to_s parser = RDoc::Parser.for top_level, 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.message} 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.message} EOF $stderr.puts e.backtrace.join("\n\t") if $DEBUG_RDOC raise e end
解析 filename
并返回一个 RDoc::TopLevel
源代码
# File lib/rdoc/rdoc.rb, line 403 def parse_files files file_list = gather_files files @stats = RDoc::Stats.new @store, file_list.length, @options.verbosity return [] if file_list.empty? # This workaround can be removed after the :main: directive is removed original_options = @options.dup @stats.begin_adding file_info = file_list.map do |filename| @current = filename parse_file filename end.compact @store.resolve_c_superclasses @stats.done_adding @options = original_options file_info end
解析命令行上的每个文件,递归进入目录。
源代码
# File lib/rdoc/rdoc.rb, line 537 def remove_siginfo_handler return unless Signal.list.key? 'INFO' handler = @old_siginfo || 'DEFAULT' trap 'INFO', handler end
删除 siginfo 处理程序并替换之前的处理程序
源代码
# File lib/rdoc/rdoc.rb, line 430 def remove_unparseable files files.reject do |file, *| file =~ /\.(?:class|eps|erb|scpt\.txt|svg|ttf|yml)$/i or (file =~ /tags$/i and /\A(\f\n[^,]+,\d+$|!_TAG_)/.match?(File.binread(file, 100))) end end
从 files
和 emacs 和 vim 的 TAGS 文件中删除已知不可解析的文件扩展名。
源代码
# File lib/rdoc/rdoc.rb, line 170 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 File.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
如果输出目录不存在,则创建它。如果它确实存在,但不包含标志文件 created.rid
,那么我们拒绝使用它,因为我们可能会破坏一些手动生成的文档
源代码
# File lib/rdoc/rdoc.rb, line 215 def store= store @store = store @store.rdoc = self end
将当前的文档树设置为 store
,并将 store 的 rdoc 驱动程序设置为此实例。
源代码
# File lib/rdoc/rdoc.rb, line 223 def update_output_dir(op_dir, time, last = {}) return if @options.dry_run or not @options.update_output_dir unless ENV['SOURCE_DATE_EPOCH'].nil? time = Time.at(ENV['SOURCE_DATE_EPOCH'].to_i).gmtime end File.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
更新输出目录中的标志文件。