1 # We handle the parsing of options, and subsequently as a singleton
2 # object to be queried for option values
4 require "rdoc/ri/paths"
10 # Should the output be placed into a single file
12 attr_reader :all_one_file
25 # Should diagrams be drawn
30 # Files matching this pattern will be excluded
32 attr_accessor :exclude
35 # Additional attr_... style method flags
37 attr_reader :extra_accessor_flags
40 # Pattern for additional attr_... style methods
42 attr_accessor :extra_accessors
45 # Should we draw fileboxes in diagrams
47 attr_reader :fileboxes
50 # The list of files to be processed
55 # Scan newer sources than the flag file if true.
57 attr_reader :force_update
60 # Description of the output generator (set with the <tt>-fmt</tt> option)
62 attr_accessor :generator
65 # Formatter to mark up text with
67 attr_accessor :formatter
70 # image format for diagrams
72 attr_reader :image_format
75 # Include line numbers in the source listings
77 attr_reader :include_line_numbers
80 # Should source code be included inline, or displayed in a popup
82 attr_accessor :inline_source
85 # Name of the file, class or module to display in the initial index page (if
86 # not specified the first file we encounter is used)
88 attr_accessor :main_page
91 # Merge into classes of the same name when generating ri
96 # The name of the output directory
101 # The name to use for the output
103 attr_accessor :op_name
106 # Are we promiscuous about showing module contents across multiple files
108 attr_reader :promiscuous
111 # Array of directories to search for files to satisfy an :include:
113 attr_reader :rdoc_include
116 # Include private and protected methods in the output
118 attr_accessor :show_all
121 # Include the '#' at the front of hyperlinked instance method names
123 attr_reader :show_hash
126 # The number of columns in a tab
128 attr_reader :tab_width
131 # template to be used when generating output
133 attr_reader :template
136 # Template class for file generation
138 # HACK around dependencies in lib/rdoc/generator/html.rb
140 attr_accessor :template_class # :nodoc:
143 # Documentation title
148 # Verbosity, zero means quiet
150 attr_accessor :verbosity
153 # URL of web cvs frontend
157 def initialize(generators = {}) # :nodoc:
164 @generators = generators
165 @generator_name = 'html'
166 @generator = @generators[@generator_name]
170 @template_class = nil
174 @image_format = 'png'
175 @inline_source = false
176 @all_one_file = false
178 @include_line_numbers = false
179 @extra_accessor_flags = {}
181 @force_update = false
187 @charset = 'iso-8859-1'
191 # Parse command line options.
196 opts = OptionParser.new do |opt|
197 opt.program_name = File.basename $0
198 opt.version = RDoc::VERSION
199 opt.summary_indent = ' ' * 4
201 Usage: #{opt.program_name} [options] [names...]
203 Files are parsed, and the information they contain collected, before any
204 output is produced. This allows cross references between all files to be
205 resolved. If a name is a directory, it is traversed. If no names are
206 specified, all Ruby files in the current directory (and subdirectories) are
209 How RDoc generates output depends on the output formatter being used, and on
210 the options you give.
212 - HTML output is normally produced into a number of separate files
213 (one per class, module, and file, along with various indices).
214 These files will appear in the directory given by the --op
215 option (doc/ by default).
217 - XML output by default is written to standard output. If a
218 --opname option is given, the output will instead be written
219 to a file with that name in the output directory.
221 - .chm files (Windows help files) are written in the --op directory.
222 If an --opname parameter is present, that name is used, otherwise
223 the file will be called rdoc.chm.
227 opt.separator "Options:"
230 opt.on("--accessor=ACCESSORS", "-A", Array,
231 "A comma separated list of additional class",
232 "methods that should be treated like",
233 "'attr_reader' and friends.",
235 "Option may be repeated.",
237 "Each accessorname may have '=text'",
238 "appended, in which case that text appears",
239 "where the r/w/rw appears for normal.",
240 "accessors") do |value|
241 value.each do |accessor|
242 if accessor =~ /^(\w+)(=(.*))?$/
244 @extra_accessor_flags[$1] = $3
251 opt.on("--all", "-a",
252 "Include all methods (not just public) in",
253 "the output.") do |value|
259 opt.on("--charset=CHARSET", "-c",
260 "Specifies the HTML character-set.") do |value|
266 opt.on("--debug", "-D",
267 "Displays lots on internal stuff.") do |value|
273 opt.on("--diagram", "-d",
274 "Generate diagrams showing modules and",
275 "classes. You need dot V1.8.6 or later to",
276 "use the --diagram option correctly. Dot is",
277 "available from http://graphviz.org") do |value|
284 opt.on("--exclude=PATTERN", "-x", Regexp,
285 "Do not process files or directories",
286 "matching PATTERN. Files given explicitly",
287 "on the command line will never be",
288 "excluded.") do |value|
294 opt.on("--extension=NEW=OLD", "-E",
295 "Treat files ending with .new as if they",
296 "ended with .old. Using '-E cgi=rb' will",
297 "cause xxx.cgi to be parsed as a Ruby file.") do |value|
298 new, old = value.split(/=/, 2)
300 unless new and old then
301 raise OptionParser::InvalidArgument, "Invalid parameter to '-E'"
304 unless RDoc::ParserFactory.alias_extension old, new then
305 raise OptionParser::InvalidArgument, "Unknown extension .#{old} to -E"
311 opt.on("--fileboxes", "-F",
312 "Classes are put in boxes which represents",
313 "files, where these classes reside. Classes",
314 "shared between more than one file are",
315 "shown with list of files that are sharing",
316 "them. Silently discarded if --diagram is",
317 "not given.") do |value|
323 opt.on("--force-update", "-U",
324 "Forces rdoc to scan all sources even if",
325 "newer than the flag file.") do |value|
326 @force_update = value
331 opt.on("--fmt=FORMAT", "--format=FORMAT", "-f", @generators.keys,
332 "Set the output formatter.") do |value|
333 @generator_name = value.downcase
339 image_formats = %w[gif png jpg jpeg]
340 opt.on("--image-format=FORMAT", "-I", image_formats,
341 "Sets output image format for diagrams. Can",
342 "be #{image_formats.join ', '}. If this option",
343 "is omitted, png is used. Requires",
344 "diagrams.") do |value|
345 @image_format = value
350 opt.on("--include=DIRECTORIES", "-i", Array,
351 "set (or add to) the list of directories to",
352 "be searched when satisfying :include:",
353 "requests. Can be used more than once.") do |value|
354 @rdoc_include.concat value.map { |dir| dir.strip }
359 opt.on("--inline-source", "-S",
360 "Show method source code inline, rather than",
361 "via a popup link.") do |value|
362 @inline_source = value
367 opt.on("--line-numbers", "-N",
368 "Include line numbers in the source code.") do |value|
369 @include_line_numbers = value
374 opt.on("--main=NAME", "-m",
375 "NAME will be the initial page displayed.") do |value|
381 opt.on("--merge", "-M",
382 "When creating ri output, merge previously",
383 "processed classes into previously",
384 "documented classes of the same name.") do |value|
390 opt.on("--one-file", "-1",
391 "Put all the output into a single file.") do |value|
392 @all_one_file = value
393 @inline_source = value if value
394 @template = 'one_page_html'
399 opt.on("--op=DIR", "-o",
400 "Set the output directory.") do |value|
406 opt.on("--opname=NAME", "-n",
407 "Set the NAME of the output. Has no effect",
408 "for HTML.") do |value|
414 opt.on("--promiscuous", "-p",
415 "When documenting a file that contains a",
416 "module or class also defined in other",
417 "files, show all stuff for that module or",
418 "class in each files page. By default, only",
419 "show stuff defined in that particular file.") do |value|
425 opt.on("--quiet", "-q",
426 "Don't show progress as we parse.") do |value|
430 opt.on("--verbose", "-v",
431 "Display extra progress as we parse.") do |value|
439 "Generate output for use by `ri`. The files",
440 "are stored in the '.rdoc' directory under",
441 "your home directory unless overridden by a",
442 "subsequent --op parameter, so no special",
443 "privileges are needed.") do |value|
444 @generator_name = "ri"
445 @op_dir = RDoc::RI::Paths::HOMEDIR
451 opt.on("--ri-site", "-R",
452 "Generate output for use by `ri`. The files",
453 "are stored in a site-wide directory,",
454 "making them accessible to others, so",
455 "special privileges are needed.") do |value|
456 @generator_name = "ri"
457 @op_dir = RDoc::RI::Paths::SITEDIR
463 opt.on("--ri-system", "-Y",
464 "Generate output for use by `ri`. The files",
465 "are stored in a site-wide directory,",
466 "making them accessible to others, so",
467 "special privileges are needed. This",
468 "option is intended to be used during Ruby",
469 "installation.") do |value|
470 @generator_name = "ri"
471 @op_dir = RDoc::RI::Paths::SYSDIR
477 opt.on("--show-hash", "-H",
478 "A name of the form #name in a comment is a",
479 "possible hyperlink to an instance method",
480 "name. When displayed, the '#' is removed",
481 "unless this option is specified.") do |value|
487 opt.on("--style=URL", "-s",
488 "Specifies the URL of a separate stylesheet.") do |value|
494 opt.on("--tab-width=WIDTH", "-w", OptionParser::DecimalInteger,
495 "Set the width of tab characters.") do |value|
501 opt.on("--template=NAME", "-T",
502 "Set the template used when generating",
503 "output.") do |value|
509 opt.on("--title=TITLE", "-t",
510 "Set TITLE as the title for HTML output.") do |value|
516 opt.on("--webcvs=URL", "-W",
517 "Specify a URL for linking to a web frontend",
518 "to CVS. If the URL contains a '\%s', the",
519 "name of the current file will be",
520 "substituted; if the URL doesn't contain a",
521 "'\%s', the filename will be appended to it.") do |value|
526 argv.insert(0, *ENV['RDOCOPT'].split) if ENV['RDOCOPT']
532 @rdoc_include << "." if @rdoc_include.empty?
534 if @exclude.empty? then
537 @exclude = Regexp.new(@exclude.join("|"))
542 # If no template was specified, use the default template for the output
545 @template ||= @generator_name
547 # Generate a regexp from the accessors
548 unless accessors.empty? then
549 re = '^(' + accessors.map { |a| Regexp.quote a }.join('|') + ')$'
550 @extra_accessors = Regexp.new re
553 rescue OptionParser::InvalidArgument, OptionParser::InvalidOption => e
561 # Set the title, but only if not already set. This means that a title set
562 # from the command line trumps one set in a source file
569 # Don't display progress as we process the files
576 @verbosity = bool ? 0 : 1
582 # Set up an output generator for the format in @generator_name
585 @generator = @generators[@generator_name]
587 unless @generator then
588 raise OptionParser::InvalidArgument, "Invalid output formatter"
591 if @generator_name == "xml" then
593 @inline_source = true
597 # Check that the right version of 'dot' is available. Unfortunately this
598 # doesn't work correctly under Windows NT, so we'll bypass the test under
602 return if RUBY_PLATFORM =~ /mswin|cygwin|mingw|bccwin/
607 IO.popen "dot -V 2>&1" do |io|
609 if ver =~ /dot.+version(?:\s+gviz)?\s+(\d+)\.(\d+)/ then
610 ok = ($1.to_i > 1) || ($1.to_i == 1 && $2.to_i >= 8)
615 if ver =~ /^dot.+version/ then
616 $stderr.puts "Warning: You may need dot V1.8.6 or later to use\n",
617 "the --diagram option correctly. You have:\n\n ",
619 "\nDiagrams might have strange background colors.\n\n"
621 $stderr.puts "You need the 'dot' program to produce diagrams.",
622 "(see http://www.research.att.com/sw/tools/graphviz/)\n\n"
629 # Check that the files on the command line exist
634 raise RDoc::Error, "file '#{f}' not readable" unless stat.readable?