Class: RDoc::RDoc
- Inherits:
-
Object
- Object
- RDoc::RDoc
- Defined in:
- lib/rdoc/rdoc.rb
Overview
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 <tt>rdoc --help<tt> for details.
Plugins
When you require 'rdoc/rdoc'
RDoc looks for 'rdoc/discover' files in your installed gems. This can be used to load alternate generators or add additional preprocessor directives.
You will want to wrap your plugin loading in an RDoc version check. Something like:
begin
gem 'rdoc', '~> 3'
require 'path/to/my/awesome/rdoc/plugin'
rescue Gem::LoadError
end
The most obvious plugin type is a new output generator. See RDoc::Generator for details.
You can also hook into RDoc::Markup to add new directives (:nodoc: is a directive). See RDoc::Markup::PreProcess::register for details.
Constant Summary collapse
- GENERATORS =
This is the list of supported output generators
{}
Instance Attribute Summary collapse
-
#exclude ⇒ Object
File pattern to exclude.
-
#generator ⇒ Object
Generator instance used for creating output.
-
#last_modified ⇒ Object
readonly
Hash of files and their last modified times.
-
#options ⇒ Object
RDoc options.
-
#stats ⇒ Object
readonly
Accessor for statistics.
Class Method Summary collapse
-
.add_generator(klass) ⇒ Object
Add
klass
that can generate output after parsing. -
.current ⇒ Object
Active RDoc::RDoc instance.
-
.current=(rdoc) ⇒ Object
Sets the active RDoc::RDoc instance.
-
.reset ⇒ Object
Resets all internal state.
Instance Method Summary collapse
-
#document(options) ⇒ Object
Generates documentation or a coverage report depending upon the settings in
options
. -
#error(msg) ⇒ Object
Report an error message and exit.
-
#gather_files(files) ⇒ Object
Gathers a set of parseable files from the files and directories listed in
files
. -
#generate(file_info) ⇒ Object
Generates documentation for
file_info
(from #parse_files) into the output dir using the generator selected by the RDoc options. -
#handle_pipe ⇒ Object
Turns RDoc from stdin into HTML.
-
#initialize ⇒ RDoc
constructor
Creates a new RDoc::RDoc instance.
-
#install_siginfo_handler ⇒ Object
Installs a siginfo handler that prints the current filename.
-
#list_files_in_directory(dir) ⇒ Object
Return a list of the files to be processed in a directory.
-
#normalized_file_list(relative_files, force_doc = false, exclude_pattern = nil) ⇒ Object
Given a list of files and directories, create a list of all the Ruby files they contain.
-
#output_flag_file(op_dir) ⇒ Object
Return the path name of the flag file in an output directory.
-
#parse_dot_doc_file(in_dir, filename) ⇒ Object
The .document file contains a list of file and directory name patterns, representing candidates for documentation.
-
#parse_file(filename) ⇒ Object
Parses
filename
and returns an RDoc::TopLevel. -
#parse_files(files) ⇒ Object
Parse each file on the command line, recursively entering directories.
-
#remove_siginfo_handler ⇒ Object
Removes a siginfo handler and replaces the previous.
-
#remove_unparseable(files) ⇒ Object
Removes file extensions known to be unparseable from
files
. -
#setup_output_dir(dir, force) ⇒ Object
Create an output dir if it doesn't exist.
-
#update_output_dir(op_dir, time, last = {}) ⇒ Object
Update the flag file in an output directory.
Constructor Details
#initialize ⇒ RDoc
Creates a new RDoc::RDoc instance. Call #document to parse files and generate documentation.
115 116 117 118 119 120 121 122 123 |
# File 'lib/rdoc/rdoc.rb', line 115 def initialize @current = nil @exclude = nil @generator = nil @last_modified = {} @old_siginfo = nil @options = nil @stats = nil end |
Instance Attribute Details
#exclude ⇒ Object
File pattern to exclude
54 55 56 |
# File 'lib/rdoc/rdoc.rb', line 54 def exclude @exclude end |
#generator ⇒ Object
Generator instance used for creating output
59 60 61 |
# File 'lib/rdoc/rdoc.rb', line 59 def generator @generator end |
#last_modified ⇒ Object (readonly)
Hash of files and their last modified times.
64 65 66 |
# File 'lib/rdoc/rdoc.rb', line 64 def last_modified @last_modified end |
#options ⇒ Object
RDoc options
69 70 71 |
# File 'lib/rdoc/rdoc.rb', line 69 def @options end |
#stats ⇒ Object (readonly)
Accessor for statistics. Available after each call to parse_files
74 75 76 |
# File 'lib/rdoc/rdoc.rb', line 74 def stats @stats end |
Class Method Details
.add_generator(klass) ⇒ Object
Add klass
that can generate output after parsing
84 85 86 87 |
# File 'lib/rdoc/rdoc.rb', line 84 def self.add_generator(klass) name = klass.name.sub(/^RDoc::Generator::/, '').downcase GENERATORS[name] = klass end |
.current ⇒ Object
Active RDoc::RDoc instance
92 93 94 |
# File 'lib/rdoc/rdoc.rb', line 92 def self.current @current end |
.current=(rdoc) ⇒ Object
Sets the active RDoc::RDoc instance
99 100 101 |
# File 'lib/rdoc/rdoc.rb', line 99 def self.current=(rdoc) @current = rdoc end |
Instance Method Details
#document(options) ⇒ Object
Generates documentation or a coverage report depending upon the settings in options
.
options
can be either an RDoc::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 RDoc::Options#finish if an options instance was given.
For a list of options, see either RDoc::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.
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 |
# File 'lib/rdoc/rdoc.rb', line 410 def document RDoc::RDoc.reset if RDoc::Options === then @options = @options.finish else @options = RDoc::Options.new @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 @start_time = Time.now file_info = parse_files @options.files @options.default_title = "RDoc Documentation" RDoc::TopLevel.complete @options.visibility @stats.coverage_level = @options.coverage_report if @options.coverage_report then puts puts @stats.report elsif file_info.empty? then $stderr.puts "\nNo newer files." unless @options.quiet else gen_klass = @options.generator @generator = gen_klass.new @options generate file_info end if @stats and (@options.coverage_report or not @options.quiet) then puts puts @stats.summary end exit @stats.fully_documented? if @options.coverage_report end |
#error(msg) ⇒ Object
Report an error message and exit
128 129 130 |
# File 'lib/rdoc/rdoc.rb', line 128 def error(msg) raise RDoc::Error, msg end |
#gather_files(files) ⇒ Object
Gathers a set of parseable files from the files and directories listed in files
.
136 137 138 139 140 141 142 143 144 |
# File 'lib/rdoc/rdoc.rb', line 136 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 end |
#generate(file_info) ⇒ Object
Generates documentation for file_info
(from #parse_files) into the output dir using the generator selected by the RDoc options
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
# File 'lib/rdoc/rdoc.rb', line 469 def generate file_info Dir.chdir @options.op_dir do begin self.class.current = self unless @options.quiet then $stderr.puts "\nGenerating #{@generator.class.name.sub(/^.*::/, '')} format into #{Dir.pwd}..." end @generator.generate file_info update_output_dir '.', @start_time, @last_modified ensure self.class.current = nil end end end |
#handle_pipe ⇒ Object
Turns RDoc from stdin into HTML
149 150 151 152 153 154 155 |
# File 'lib/rdoc/rdoc.rb', line 149 def handle_pipe @html = RDoc::Markup::ToHtml.new out = @html.convert $stdin.read $stdout.write out end |
#install_siginfo_handler ⇒ Object
Installs a siginfo handler that prints the current filename.
160 161 162 163 164 165 166 |
# File 'lib/rdoc/rdoc.rb', line 160 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) ⇒ Object
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.
307 308 309 310 311 |
# File 'lib/rdoc/rdoc.rb', line 307 def list_files_in_directory dir files = Dir.glob File.join(dir, "*") normalized_file_list files, false, @options.exclude end |
#normalized_file_list(relative_files, force_doc = false, exclude_pattern = nil) ⇒ Object
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.
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/rdoc/rdoc.rb', line 266 def normalized_file_list(relative_files, force_doc = false, exclude_pattern = nil) file_list = [] relative_files.each do |rel_file_name| 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" 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 raise RDoc::Error, "I can't deal with a #{type} #{rel_file_name}" end end file_list.flatten end |
#output_flag_file(op_dir) ⇒ Object
Return the path name of the flag file in an output directory.
231 232 233 |
# File 'lib/rdoc/rdoc.rb', line 231 def output_flag_file(op_dir) File.join op_dir, "created.rid" end |
#parse_dot_doc_file(in_dir, filename) ⇒ Object
The .document file contains a list of file and directory name patterns, representing candidates for documentation. It may also contain comments (starting with '#')
240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/rdoc/rdoc.rb', line 240 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) ⇒ Object
Parses filename
and returns an RDoc::TopLevel
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/rdoc/rdoc.rb', line 316 def parse_file filename if defined?(Encoding) then encoding = @options.encoding filename = filename.encode encoding end @stats.add_file filename content = RDoc::Encoding.read_file filename, encoding return unless content top_level = RDoc::TopLevel.new filename 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 => 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) ⇒ Object
Parse each file on the command line, recursively entering directories.
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/rdoc/rdoc.rb', line 367 def parse_files files file_list = gather_files files @stats = RDoc::Stats.new file_list.size, @options.verbosity return [] if file_list.empty? file_info = [] @stats.begin_adding file_info = file_list.map do |filename| @current = filename parse_file filename end.compact @stats.done_adding file_info end |
#remove_siginfo_handler ⇒ Object
Removes a siginfo handler and replaces the previous
489 490 491 492 493 494 495 |
# File 'lib/rdoc/rdoc.rb', line 489 def remove_siginfo_handler return unless Signal.list.key? 'INFO' handler = @old_siginfo || 'DEFAULT' trap 'INFO', handler end |
#remove_unparseable(files) ⇒ Object
Removes file extensions known to be unparseable from files
390 391 392 393 394 |
# File 'lib/rdoc/rdoc.rb', line 390 def remove_unparseable files files.reject do |file| file =~ /\.(?:class|eps|erb|scpt\.txt|ttf|yml)$/i end end |
#setup_output_dir(dir, force) ⇒ Object
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
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/rdoc/rdoc.rb', line 173 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 output_flag_file dir end last end |
#update_output_dir(op_dir, time, last = {}) ⇒ Object
Update the flag file in an output directory.
217 218 219 220 221 222 223 224 225 226 |
# File 'lib/rdoc/rdoc.rb', line 217 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 |