Class: RDoc::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/rdoc/options.rb

Overview

RDoc::Options handles the parsing and storage of options

Saved Options

You can save some options like the markup format in the .rdoc_options file in your gem. The easiest way to do this is:

rdoc --markup tomdoc --write-options

Which will automatically create the file and fill it with the options you specified.

The following options will not be saved since they interfere with the user's preferences or with the normal operation of RDoc:

  • --coverage-report
  • --dry-run
  • --encoding
  • --force-update
  • --format
  • --pipe
  • --quiet
  • --template
  • --verbose

Custom Options

Generators can hook into RDoc::Options to add generator-specific command line options.

When --format is encountered in ARGV, RDoc calls ::setup_options on the generator class to add extra options to the option parser. Options for custom generators must occur after --format. rdoc --help will list options for all installed generators.

Example:

class RDoc::Generator::Spellcheck
RDoc::RDoc.add_generator self

def self.setup_options rdoc_options
  op = rdoc_options.option_parser

  op.on('--spell-dictionary DICTIONARY',
        RDoc::Options::Path) do |dictionary|
    rdoc_options.spell_dictionary = dictionary
  end
end
end

Of course, RDoc::Options does not respond to spell_dictionary by default so you will need to add it:

class RDoc::Options

##
# The spell dictionary used by the spell-checking plugin.

attr_accessor :spell_dictionary

end

Option Validators

OptionParser validators will validate and cast user input values. In addition to the validators that ship with OptionParser (String, Integer, Float, TrueClass, FalseClass, Array, Regexp, Date, Time, URI, etc.), RDoc::Options adds Path, PathArray and Template.

Constant Summary collapse

SPECIAL =

RDoc options ignored (or handled specially) by --write-options

%w[
  coverage_report
  dry_run
  encoding
  files
  force_output
  force_update
  generator
  generator_name
  generator_options
  generators
  locale
  op_dir
  page_dir
  option_parser
  pipe
  rdoc_include
  root
  server_port
  static_path
  template
  template_dir
  update_output_dir
  verbosity
  write_options
]
Directory =

Option validator for OptionParser that matches a directory that exists on the filesystem.

Object.new
Path =

Option validator for OptionParser that matches a file or directory that exists on the filesystem.

Object.new
PathArray =

Option validator for OptionParser that matches a comma-separated list of files or directories that exist on the filesystem.

Object.new
Template =

Option validator for OptionParser that matches a template directory for an installed generator that lives in "rdoc/generator/template/#{template_name}"

Object.new
DEFAULT_EXCLUDE =
%w[
  ~\z \.orig\z \.rej\z \.bak\z
  \.gemspec\z
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loaded_options = nil) ⇒ Options

:nodoc:



387
388
389
390
# File 'lib/rdoc/options.rb', line 387

def initialize(loaded_options = nil) # :nodoc:
  init_ivars
  override loaded_options if loaded_options
end

Instance Attribute Details

#apply_default_exclude ⇒ Object (readonly)

Exclude the default patterns as well if true.



351
352
353
# File 'lib/rdoc/options.rb', line 351

def apply_default_exclude
  @apply_default_exclude
end

Words to be ignored in autolink cross-references



355
356
357
# File 'lib/rdoc/options.rb', line 355

def autolink_excluded_words
  @autolink_excluded_words
end

#canonical_root ⇒ Object

The preferred root URL for the documentation



370
371
372
# File 'lib/rdoc/options.rb', line 370

def canonical_root
  @canonical_root
end

#charset ⇒ Object

Character-set for HTML output. #encoding is preferred over #charset



136
137
138
# File 'lib/rdoc/options.rb', line 136

def charset
  @charset
end

#class_module_path_prefix ⇒ Object

The prefix to use for class and module page paths



360
361
362
# File 'lib/rdoc/options.rb', line 360

def class_module_path_prefix
  @class_module_path_prefix
end

#coverage_report ⇒ Object

If true, only report on undocumented files



228
229
230
# File 'lib/rdoc/options.rb', line 228

def coverage_report
  @coverage_report
end

#dry_run ⇒ Object

If true, RDoc will not write any files.



141
142
143
# File 'lib/rdoc/options.rb', line 141

def dry_run
  @dry_run
end

#embed_mixins ⇒ Object

Embed mixin methods, attributes, and constants into class documentation. Set via --[no-]embed-mixins (Default is false.)



347
348
349
# File 'lib/rdoc/options.rb', line 347

def embed_mixins
  @embed_mixins
end

#encoding ⇒ Object

The output encoding. All input files will be transcoded to this encoding.

The default encoding is UTF-8. This is set via --encoding.



148
149
150
# File 'lib/rdoc/options.rb', line 148

def encoding
  @encoding
end

#exclude ⇒ Object

Create a regexp for #exclude



614
615
616
617
618
619
620
621
622
623
624
625
# File 'lib/rdoc/options.rb', line 614

def exclude
  if @exclude.nil? or Regexp === @exclude
    # done, #finish is being re-run
    @exclude
  elsif !@apply_default_exclude and @exclude.empty?
    nil
  else
    exclude = @exclude
    exclude |= DEFAULT_EXCLUDE if @apply_default_exclude
    Regexp.new(exclude.join("|"))
  end
end

#file_path_prefix ⇒ Object

The prefix to use for file page paths



365
366
367
# File 'lib/rdoc/options.rb', line 365

def file_path_prefix
  @file_path_prefix
end

#files ⇒ Object

The list of files to be processed



158
159
160
# File 'lib/rdoc/options.rb', line 158

def files
  @files
end

Custom footer content configuration for themes that support it. Currently only supported by the Aliki theme.

A hash where keys are column titles and values are hashes of link text => URL pairs. Each column will be displayed in the upper footer section.

Example:

{
"DOCUMENTATION" => {"Home" => "/index.html", "Guide" => "/guide.html"},
"RESOURCES" => {"RDoc" => "https://ruby.github.io/rdoc/", "GitHub" => "https://github.com/ruby/rdoc"}
}


385
386
387
# File 'lib/rdoc/options.rb', line 385

def footer_content
  @footer_content
end

#force_output ⇒ Object

Create the output even if the output directory does not look like an rdoc output directory



164
165
166
# File 'lib/rdoc/options.rb', line 164

def force_output
  @force_output
end

#force_update ⇒ Object

Scan newer sources than the flag file if true.



169
170
171
# File 'lib/rdoc/options.rb', line 169

def force_update
  @force_update
end

#formatter ⇒ Object

Formatter to mark up text with



174
175
176
# File 'lib/rdoc/options.rb', line 174

def formatter
  @formatter
end

#generator ⇒ Object

Description of the output generator (set with the --format option)



179
180
181
# File 'lib/rdoc/options.rb', line 179

def generator
  @generator
end

#generator_name ⇒ Object (readonly)

For #==



184
185
186
# File 'lib/rdoc/options.rb', line 184

def generator_name
  @generator_name
end

#generator_options ⇒ Object

Loaded generator options. Used to prevent --help from loading the same options multiple times.



190
191
192
# File 'lib/rdoc/options.rb', line 190

def generator_options
  @generator_options
end

Old rdoc behavior: hyperlink all words that match a method name, even if not preceded by '#' or '::'



196
197
198
# File 'lib/rdoc/options.rb', line 196

def hyperlink_all
  @hyperlink_all
end

#line_numbers ⇒ Object

Include line numbers in the source code



201
202
203
# File 'lib/rdoc/options.rb', line 201

def line_numbers
  @line_numbers
end

#locale ⇒ Object

The output locale.



206
207
208
# File 'lib/rdoc/options.rb', line 206

def locale
  @locale
end

#locale_dir ⇒ Object

The directory where locale data live.



211
212
213
# File 'lib/rdoc/options.rb', line 211

def locale_dir
  @locale_dir
end

#main_page ⇒ Object

Name of the file, class or module to display in the initial index page (if not specified the first file we encounter is used)



217
218
219
# File 'lib/rdoc/options.rb', line 217

def main_page
  @main_page
end

#markup ⇒ Object

The markup format. One of: rdoc (the default), markdown, rd, tomdoc. See Markup Formats.



223
224
225
# File 'lib/rdoc/options.rb', line 223

def markup
  @markup
end

#op_dir ⇒ Object

The name of the output directory



233
234
235
# File 'lib/rdoc/options.rb', line 233

def op_dir
  @op_dir
end

#option_parser ⇒ Object

The OptionParser for this instance



238
239
240
# File 'lib/rdoc/options.rb', line 238

def option_parser
  @option_parser
end

#output_decoration ⇒ Object

Output heading decorations?



242
243
244
# File 'lib/rdoc/options.rb', line 242

def output_decoration
  @output_decoration
end

#page_dir ⇒ Object

Directory where guides, FAQ, and other pages not associated with a class live. You may leave this unset if these are at the root of your project.



248
249
250
# File 'lib/rdoc/options.rb', line 248

def page_dir
  @page_dir
end

#pipe ⇒ Object

Is RDoc in pipe mode?



253
254
255
# File 'lib/rdoc/options.rb', line 253

def pipe
  @pipe
end

#rdoc_include ⇒ Object

Array of directories to search for files to satisfy an :include:



258
259
260
# File 'lib/rdoc/options.rb', line 258

def rdoc_include
  @rdoc_include
end

#root ⇒ Object

Root of the source documentation will be generated for. Set this when building documentation outside the source directory. Defaults to the current directory.



265
266
267
# File 'lib/rdoc/options.rb', line 265

def root
  @root
end

#server_port ⇒ Object (readonly)

When set to a port number, starts a live-reloading server instead of writing files. Defaults to false (no server). Set via --server.



338
339
340
# File 'lib/rdoc/options.rb', line 338

def server_port
  @server_port
end

#show_hash ⇒ Object

Include the '#' at the front of hyperlinked instance method names



270
271
272
# File 'lib/rdoc/options.rb', line 270

def show_hash
  @show_hash
end

#skip_tests ⇒ Object

Indicates if files of test suites should be skipped



342
343
344
# File 'lib/rdoc/options.rb', line 342

def skip_tests
  @skip_tests
end

#static_path ⇒ Object

Directory to copy static files from



275
276
277
# File 'lib/rdoc/options.rb', line 275

def static_path
  @static_path
end

#tab_width ⇒ Object

The number of columns in a tab



280
281
282
# File 'lib/rdoc/options.rb', line 280

def tab_width
  @tab_width
end

#template ⇒ Object

Template to be used when generating output



285
286
287
# File 'lib/rdoc/options.rb', line 285

def template
  @template
end

#template_dir ⇒ Object

Directory the template lives in



290
291
292
# File 'lib/rdoc/options.rb', line 290

def template_dir
  @template_dir
end

#template_stylesheets ⇒ Object

Additional template stylesheets



295
296
297
# File 'lib/rdoc/options.rb', line 295

def template_stylesheets
  @template_stylesheets
end

#title ⇒ Object

Documentation title



300
301
302
# File 'lib/rdoc/options.rb', line 300

def title
  @title
end

#update_output_dir ⇒ Object

Should RDoc update the timestamps in the output dir?



305
306
307
# File 'lib/rdoc/options.rb', line 305

def update_output_dir
  @update_output_dir
end

#verbosity ⇒ Object

Verbosity, zero means quiet



310
311
312
# File 'lib/rdoc/options.rb', line 310

def verbosity
  @verbosity
end

#visibility ⇒ Object

Minimum visibility of a documented method. One of :public, :protected, :private or :nodoc.

The :nodoc visibility ignores all directives related to visibility. The other visibilities may be overridden on a per-method basis with the :doc: directive.



331
332
333
# File 'lib/rdoc/options.rb', line 331

def visibility
  @visibility
end

#warn_missing_rdoc_ref ⇒ Object

Warn if rdoc-ref links can't be resolved Default is true



316
317
318
# File 'lib/rdoc/options.rb', line 316

def warn_missing_rdoc_ref
  @warn_missing_rdoc_ref
end

#webcvs ⇒ Object

URL of web cvs frontend



321
322
323
# File 'lib/rdoc/options.rb', line 321

def webcvs
  @webcvs
end

Class Method Details

.load_options ⇒ Object

Loads options from .rdoc_options if the file exists, otherwise creates a new RDoc::Options instance.

Raises:



1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
# File 'lib/rdoc/options.rb', line 1382

def self.load_options
  options_file = File.expand_path '.rdoc_options'
  return Options.new unless File.exist? options_file

  ::RDoc.load_yaml

  content = File.read('.rdoc_options')

  if defined?(Psych)
    begin
      options = Psych.safe_load content, permitted_classes: [Options, Symbol]
    rescue Psych::SyntaxError
      raise Error, "#{options_file} is not a valid rdoc options file"
    end
  else
    options = ::RDoc.yaml_serializer.load(content)
  end

  return Options.new unless options # Allow empty file.

  raise Error, "#{options_file} is not a valid rdoc options file" unless
    Options === options or Hash === options

  if Hash === options
    # Override the default values with the contents of YAML file.
    options = Options.new options
  end

  options
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/rdoc/options.rb', line 521

def ==(other) # :nodoc:
  self.class === other and
    @encoding       == other.encoding       and
    @embed_mixins   == other.embed_mixins   and
    @generator_name == other.generator_name and
    @hyperlink_all  == other.hyperlink_all  and
    @line_numbers   == other.line_numbers   and
    @locale         == other.locale         and
    @locale_dir     == other.locale_dir     and
    @main_page      == other.main_page      and
    @markup         == other.markup         and
    @op_dir         == other.op_dir         and
    @rdoc_include   == other.rdoc_include   and
    @show_hash      == other.show_hash      and
    @static_path    == other.static_path    and
    @tab_width      == other.tab_width      and
    @template       == other.template       and
    @title          == other.title          and
    @visibility     == other.visibility     and
    @webcvs         == other.webcvs         and
    @apply_default_exclude == other.apply_default_exclude and
    @autolink_excluded_words == other.autolink_excluded_words
end

#check_files ⇒ Object

Check that the files on the command line exist



548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# File 'lib/rdoc/options.rb', line 548

def check_files
  @files.delete_if do |file|
    if File.exist? file
      if File.readable? file
        false
      else
        warn "file '#{file}' not readable"

        true
      end
    else
      warn "file '#{file}' not found"

      true
    end
  end
end

#check_generator ⇒ Object

Ensure only one generator is loaded



569
570
571
572
573
574
# File 'lib/rdoc/options.rb', line 569

def check_generator
  if @generator
    raise OptionParser::InvalidOption,
      "generator already set to #{@generator_name}"
  end
end

#default_title=(string) ⇒ Object

Set the title, but only if not already set. Used to set the title from a source file, so that a title set from the command line will have the priority.



581
582
583
# File 'lib/rdoc/options.rb', line 581

def default_title=(string)
  @title ||= string
end

#finish ⇒ Object

Completes any unfinished option setup business such as filtering for existent files, creating a regexp for #exclude and setting a default #template.



632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
# File 'lib/rdoc/options.rb', line 632

def finish
  if @write_options
    write_options
    exit
  end

  @op_dir ||= 'doc'

  root = @root.to_s
  if @rdoc_include.empty? || !@rdoc_include.include?(root)
    @rdoc_include << root
  end

  @exclude = self.exclude

  finish_page_dir

  check_files

  # If no template was specified, use the default template for the output
  # formatter

  unless @template
    @template     = @generator_name
    @template_dir = template_dir_for @template
  end

  if @locale_name
    @locale = I18n::Locale[@locale_name]
    @locale.load(@locale_dir)
  else
    @locale = nil
  end

  self
end

#finish_page_dir ⇒ Object

Fixes the page_dir to be relative to the root_dir and adds the page_dir to the files list.



673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
# File 'lib/rdoc/options.rb', line 673

def finish_page_dir
  return unless @page_dir

  @files << @page_dir

  page_dir = Pathname(@page_dir)
  begin
    page_dir = page_dir.expand_path.relative_path_from @root
  rescue ArgumentError
    # On Windows, sometimes crosses different drive letters.
    page_dir = page_dir.expand_path
  end

  @page_dir = page_dir
end

#generator_descriptions ⇒ Object

Returns a properly-space list of generators and their descriptions.



692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
# File 'lib/rdoc/options.rb', line 692

def generator_descriptions
  lengths = []

  generators = RDoc::GENERATORS.map do |name, generator|
    lengths << name.length

    description = generator::DESCRIPTION if
      generator.const_defined? :DESCRIPTION

    [name, description]
  end

  longest = lengths.max

  generators.sort.map do |name, description|
    if description
      "  %-*s - %s" % [longest, name, description]
    else
      "  #{name}"
    end
  end.join "\n"
end

#init_ivars ⇒ Object

:nodoc:



397
398
399
400
401
402
403
404
405
406
407
408
409
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
# File 'lib/rdoc/options.rb', line 397

def init_ivars # :nodoc:
  @autolink_excluded_words = []
  @dry_run = false
  @embed_mixins = false
  @exclude = []
  @files = nil
  @force_output = false
  @force_update = true
  @generator_name = "aliki"
  @generators = RDoc::GENERATORS
  @generator_options = []
  @hyperlink_all = false
  @line_numbers = false
  @locale = nil
  @locale_name = nil
  @locale_dir = 'locale'
  @main_page = nil
  @markup = 'rdoc'
  @coverage_report = false
  @op_dir = nil
  @page_dir = nil
  @pipe = false
  @output_decoration = true
  @rdoc_include = []
  @root = Pathname(Dir.pwd)
  @server_port = false
  @show_hash = false
  @static_path = []
  @tab_width = 8
  @template = nil
  @template_dir = nil
  @template_stylesheets = []
  @title = nil
  @update_output_dir = true
  @verbosity = 1
  @visibility = :protected
  @warn_missing_rdoc_ref = true
  @webcvs = nil
  @write_options = false
  @encoding = ::Encoding::UTF_8
  @charset = @encoding.name
  @skip_tests = true
  @apply_default_exclude = true
  @class_module_path_prefix = nil
  @file_path_prefix = nil
  @canonical_root = nil
  @footer_content = nil
end

#init_with(map) ⇒ Object

:nodoc:



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/rdoc/options.rb', line 446

def init_with(map) # :nodoc:
  init_ivars

  encoding = map['encoding']
  @encoding = encoding ? ::Encoding.find(encoding) : encoding

  @charset        = map['charset']
  @embed_mixins   = map['embed_mixins']
  @exclude        = map['exclude']
  @generator_name = map['generator_name']
  @hyperlink_all  = map['hyperlink_all']
  @line_numbers   = map['line_numbers']
  @locale_name    = map['locale_name']
  @locale_dir     = map['locale_dir']
  @main_page      = map['main_page']
  @markup         = map['markup']
  @op_dir         = map['op_dir']
  @show_hash      = map['show_hash']
  @tab_width      = map['tab_width']
  @template_dir   = map['template_dir']
  @title          = map['title']
  @visibility     = map['visibility']
  @webcvs         = map['webcvs']

  @apply_default_exclude   = map['apply_default_exclude']
  @autolink_excluded_words = map['autolink_excluded_words']
  @footer_content          = map['footer_content']

  @rdoc_include = sanitize_path map['rdoc_include']
  @static_path  = sanitize_path map['static_path']
end

#override(map) ⇒ Object

:nodoc:



482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'lib/rdoc/options.rb', line 482

def override(map) # :nodoc:
  if map.has_key?('encoding')
    encoding = map['encoding']
    @encoding = encoding ? ::Encoding.find(encoding) : encoding
  end

  @charset        = map['charset']        if map.has_key?('charset')
  @embed_mixins   = map['embed_mixins']   if map.has_key?('embed_mixins')
  @exclude        = map['exclude']        if map.has_key?('exclude')
  @generator_name = map['generator_name'] if map.has_key?('generator_name')
  @hyperlink_all  = map['hyperlink_all']  if map.has_key?('hyperlink_all')
  @line_numbers   = map['line_numbers']   if map.has_key?('line_numbers')
  @locale_name    = map['locale_name']    if map.has_key?('locale_name')
  @locale_dir     = map['locale_dir']     if map.has_key?('locale_dir')
  @main_page      = map['main_page']      if map.has_key?('main_page')
  @markup         = map['markup']         if map.has_key?('markup')
  @op_dir         = map['op_dir']         if map.has_key?('op_dir')
  @page_dir       = map['page_dir']       if map.has_key?('page_dir')
  @show_hash      = map['show_hash']      if map.has_key?('show_hash')
  @tab_width      = map['tab_width']      if map.has_key?('tab_width')
  @template_dir   = map['template_dir']   if map.has_key?('template_dir')
  @title          = map['title']          if map.has_key?('title')
  @visibility     = map['visibility']     if map.has_key?('visibility')
  @webcvs         = map['webcvs']         if map.has_key?('webcvs')
  @autolink_excluded_words = map['autolink_excluded_words'] if map.has_key?('autolink_excluded_words')
  @apply_default_exclude = map['apply_default_exclude'] if map.has_key?('apply_default_exclude')
  @canonical_root = map['canonical_root'] if map.has_key?('canonical_root')
  @footer_content = map['footer_content'] if map.has_key?('footer_content')

  @warn_missing_rdoc_ref = map['warn_missing_rdoc_ref'] if map.has_key?('warn_missing_rdoc_ref')

  if map.has_key?('rdoc_include')
    @rdoc_include = sanitize_path map['rdoc_include']
  end
  if map.has_key?('static_path')
    @static_path  = sanitize_path map['static_path']
  end
end

#parse(argv) ⇒ Object

Parses command line options.



718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
# File 'lib/rdoc/options.rb', line 718

def parse(argv)
  ignore_invalid = true

  argv.insert(0, *ENV['RDOCOPT'].split) if ENV['RDOCOPT']

  opts = OptionParser.new do |opt|
    @option_parser = opt
    opt.program_name = File.basename $0
    opt.version = VERSION
    opt.release = nil
    opt.summary_indent = ' ' * 4
    opt.banner = <<-EOF
Usage: #{opt.program_name} [options] [names...]

  Files are parsed, and the information they contain collected, before any
  output is produced. This allows cross references between all files to be
  resolved. If a name is a directory, it is traversed. If no names are
  specified, all Ruby files in the current directory (and subdirectories) are
  processed.

  How RDoc generates output depends on the output formatter being used, and on
  the options you give.

  Options can be specified via the RDOCOPT environment variable, which
  functions similar to the RUBYOPT environment variable for ruby.

$ export RDOCOPT="--show-hash"

  will make rdoc show hashes in method links by default.  Command-line options
  always will override those in RDOCOPT.

  Available formatters:

#{generator_descriptions}

  RDoc understands the following file formats:

  EOF

    parsers = Hash.new { |h, parser| h[parser] = [] }

    Parser.parsers.each do |regexp, parser|
      parsers[parser.name.sub('RDoc::Parser::', '')] << regexp.source
    end

    parsers.sort.each do |parser, regexp|
      opt.banner += "  - #{parser}: #{regexp.join ', '}\n"
    end
    opt.banner += "  - TomDoc:  Only in ruby files\n"

    opt.accept Template do |template|
      template_dir = template_dir_for template

      unless template_dir
        $stderr.puts "could not find template #{template}"
        nil
      else
        [template, template_dir]
      end
    end

    opt.accept Directory do |directory|
      directory = File.expand_path directory

      raise OptionParser::InvalidArgument unless File.directory? directory

      directory
    end

    opt.accept Path do |path|
      path = File.expand_path path

      raise OptionParser::InvalidArgument unless File.exist? path

      path
    end

    opt.accept PathArray do |paths,|
      paths = if paths
                paths.split(',').map { |d| d unless d.empty? }
              end

      paths.map do |path|
        path = File.expand_path path

        raise OptionParser::InvalidArgument unless File.exist? path

        path
      end
    end

    opt.separator nil
    opt.separator "Parsing options:"
    opt.separator nil

    opt.on("--encoding=ENCODING", "-e", ::Encoding.list.map { |e| e.name },
           "Specifies the output encoding.  All files",
           "read will be converted to this encoding.",
           "The default encoding is UTF-8.",
           "--encoding is preferred over --charset") do |value|
             @encoding = ::Encoding.find value
             @charset = @encoding.name # may not be valid value
           end

    opt.separator nil

    opt.on("--locale=NAME",
           "Specifies the output locale.") do |value|
      @locale_name = value
    end

    opt.on("--locale-data-dir=DIR",
           "Specifies the directory where locale data live.") do |value|
      @locale_dir = value
    end

    opt.separator nil

    opt.on("--all", "-a",
           "Synonym for --visibility=private.") do |value|
      @visibility = :private
    end

    opt.separator nil

    opt.on("--exclude=PATTERN", "-x", Regexp,
           "Do not process files or directories",
           "matching PATTERN.") do |value|
      @exclude << value
    end

    opt.on("--[no-]apply-default-exclude",
           "Use default PATTERN to exclude.") do |value|
      @apply_default_exclude = value
    end

    opt.separator nil

    opt.on("--no-skipping-tests", nil,
           "Don't skip generating documentation for test and spec files") do |value|
      @skip_tests = false
    end

    opt.separator nil

    opt.on("--extension=NEW=OLD", "-E",
           "Treat files ending with .new as if they",
           "ended with .old. Using '-E cgi=rb' will",
           "cause xxx.cgi to be parsed as a Ruby file.") do |value|
      new, old = value.split(/=/, 2)

      unless new and old
        raise OptionParser::InvalidArgument, "Invalid parameter to '-E'"
      end

      unless Parser.alias_extension old, new
        raise OptionParser::InvalidArgument, "Unknown extension .#{old} to -E"
      end
    end

    opt.separator nil

    opt.on("--[no-]force-update", "-U",
           "Forces rdoc to scan all sources even if",
           "no files are newer than the flag file.") do |value|
      @force_update = value
    end

    opt.separator nil

    opt.on("--pipe", "-p",
           "Convert RDoc on stdin to HTML") do
      @pipe = true
    end

    opt.separator nil

    opt.on("--tab-width=WIDTH", "-w", Integer,
           "Set the width of tab characters.") do |value|
      raise OptionParser::InvalidArgument,
            "#{value} is an invalid tab width" if value <= 0
      @tab_width = value
    end

    opt.separator nil

    opt.on("--visibility=VISIBILITY", VISIBILITIES + [:nodoc],
           "Minimum visibility to document a method.",
           "One of 'public', 'protected' (the default),",
           "'private' or 'nodoc' (show everything)") do |value|
      @visibility = value
    end

    opt.separator nil

    opt.on("--[no-]embed-mixins",
           "Embed mixin methods, attributes, and constants",
           "into class documentation. (default false)") do |value|
      @embed_mixins = value
    end

    opt.separator nil

    markup_formats = Text::MARKUP_FORMAT.keys.sort

    opt.on("--markup=MARKUP", markup_formats,
           "The markup format for the named files.",
           "The default is rdoc.  Valid values are:",
           markup_formats.join(', ')) do |value|
      @markup = value
    end

    opt.separator nil

    opt.on("--root=ROOT", Directory,
           "Root of the source tree documentation",
           "will be generated for.  Set this when",
           "building documentation outside the",
           "source directory.  Default is the",
           "current directory.") do |root|
      @root = Pathname(root)
    end

    opt.separator nil

    opt.on("--page-dir=DIR", Directory,
           "Directory where guides, your FAQ or",
           "other pages not associated with a class",
           "live.  Set this when you don't store",
           "such files at your project root.",
           "NOTE: Do not use the same file name in",
           "the page dir and the root of your project") do |page_dir|
      @page_dir = page_dir
    end

    opt.separator nil
    opt.separator "Common generator options:"
    opt.separator nil

    opt.on("--force-output", "-O",
           "Forces rdoc to write the output files,",
           "even if the output directory exists",
           "and does not seem to have been created",
           "by rdoc.") do |value|
      @force_output = value
    end

    opt.separator nil

    generator_text = @generators.keys.map { |name| "  #{name}" }.sort

    opt.on("-f", "--fmt=FORMAT", "--format=FORMAT", @generators.keys,
           "Set the output formatter.  One of:", *generator_text) do |value|
      check_generator

      @generator_name = value.downcase
      setup_generator
    end

    opt.separator nil

    opt.on("--include=DIRECTORIES", "-i", PathArray,
           "Set (or add to) the list of directories to",
           "be searched when satisfying :include:",
           "requests. Can be used more than once.") do |value|
      @rdoc_include.concat value.map { |dir| dir.strip }
    end

    opt.separator nil

    opt.on("--[no-]coverage-report=[LEVEL]", "--[no-]dcov", "-C", Integer,
           "Prints a report on undocumented items.",
           "Does not generate files.") do |value|
      value = 0 if value.nil? # Integer converts -C to nil

      @coverage_report = value
      @force_update = true if value
    end

    opt.separator nil

    opt.on("--output=DIR", "--op", "-o",
           "Set the output directory.") do |value|
      @op_dir = value
    end

    opt.separator nil
    opt.separator 'HTML generator options:'
    opt.separator nil

    opt.on("--charset=CHARSET", "-c",
           "Specifies the output HTML character-set.",
           "Use --encoding instead of --charset if",
           "available.") do |value|
      @charset = value
    end

    opt.separator nil

    opt.on("--autolink-excluded-words=WORDS", Array,
           "Words to be ignored in autolink cross-references") do |value|
      @autolink_excluded_words.concat value
    end

    opt.separator nil

    opt.on("--hyperlink-all", "-A",
           "Generate hyperlinks for all words that",
           "correspond to known methods, even if they",
           "do not start with '#' or '::' (legacy",
           "behavior).") do |value|
      @hyperlink_all = value
    end

    opt.separator nil

    opt.on("--main=NAME", "-m",
           "NAME will be the initial page displayed.") do |value|
      @main_page = value
    end

    opt.separator nil

    opt.on("--[no-]line-numbers", "-N",
           "Include line numbers in the source code.",
           "By default, only the number of the first",
           "line is displayed, in a leading comment.") do |value|
      @line_numbers = value
    end

    opt.separator nil

    opt.on("--show-hash", "-H",
           "A name of the form #name in a comment is a",
           "possible hyperlink to an instance method",
           "name. When displayed, the '#' is removed",
           "unless this option is specified.") do |value|
      @show_hash = value
    end

    opt.separator nil

    opt.on("--template=NAME", "-T", Template,
           "Set the template used when generating",
           "output. The default depends on the",
           "formatter used.") do |(template, template_dir)|
      @template     = template
      @template_dir = template_dir
    end

    opt.separator nil

    opt.on("--template-stylesheets=FILES", PathArray,
           "Set (or add to) the list of files to",
           "include with the html template.") do |value|
      @template_stylesheets.concat value
    end

    opt.separator nil

    opt.on("--title=TITLE", "-t",
           "Set TITLE as the title for HTML output.") do |value|
      @title = value
    end

    opt.separator nil

    opt.on("--copy-files=PATH", Path,
           "Specify a file or directory to copy static",
           "files from.",
           "If a file is given it will be copied into",
           "the output dir.  If a directory is given the",
           "entire directory will be copied.",
           "You can use this multiple times") do |value|
      @static_path << value
    end

    opt.separator nil

    opt.on("--webcvs=URL", "-W",
           "Specify a URL for linking to a web frontend",
           "to CVS. If the URL contains a '\%s', the",
           "name of the current file will be",
           "substituted; if the URL doesn't contain a",
           "'\%s', the filename will be appended to it.") do |value|
      @webcvs = value
    end

    opt.separator nil
    opt.separator "ri generator options:"
    opt.separator nil

    opt.on("--ri", "-r",
           "Generate output for use by `ri`. The files",
           "are stored in the '.rdoc' directory under",
           "your home directory unless overridden by a",
           "subsequent --op parameter, so no special",
           "privileges are needed.") do |value|
      check_generator

      @generator_name = "ri"
      @op_dir ||= RI::Paths::HOMEDIR
      setup_generator
    end

    opt.separator nil

    opt.on("--ri-site", "-R",
           "Generate output for use by `ri`. The files",
           "are stored in a site-wide directory,",
           "making them accessible to others, so",
           "special privileges are needed.") do |value|
      check_generator

      @generator_name = "ri"
      @op_dir = RI::Paths.site_dir
      setup_generator
    end

    opt.separator nil
    opt.separator "Generic options:"
    opt.separator nil

    opt.on("--server[=PORT]", Integer,
           "Start a web server to preview",
           "documentation with live reload.",
           "Defaults to port 4000.") do |port|
      @server_port = port || 4000
    end

    opt.separator nil

    opt.on("--write-options",
           "Write .rdoc_options to the current",
           "directory with the given options.  Not all",
           "options will be used.  See RDoc::Options",
           "for details.") do |value|
      @write_options = true
    end

    opt.separator nil

    opt.on("--[no-]dry-run",
           "Don't write any files") do |value|
      @dry_run = value
    end

    opt.separator nil

    opt.on("-D", "--[no-]debug",
           "Displays lots on internal stuff.") do |value|
      $DEBUG_RDOC = value
    end

    opt.separator nil

    opt.on("--warn-missing-rdoc-ref",
           "Warn if rdoc-ref links can't be resolved") do |value|
      @warn_missing_rdoc_ref = value
    end

    opt.separator nil

    opt.on("--[no-]ignore-invalid",
           "Ignore invalid options and continue",
           "(default true).") do |value|
      ignore_invalid = value
    end

    opt.separator nil

    opt.on("--quiet", "-q",
           "Don't show progress as we parse.") do |value|
      @verbosity = 0
    end

    opt.separator nil

    opt.on("--verbose", "-V",
           "Display extra progress as RDoc parses") do |value|
      @verbosity = 2
    end

    opt.separator nil

    opt.on("--version", "-v", "print the version") do
      puts opt.version
      exit
    end

    opt.separator nil

    opt.on("--help", "-h", "Display this help") do
      RDoc::GENERATORS.each_key do |generator|
        setup_generator generator
      end

      puts opt.help
      exit
    end

    opt.separator nil
  end

  invalid = []

  begin
    opts.parse! argv
  rescue OptionParser::ParseError => e
    if %w[--format --ri -r --ri-site -R].include? e.args.first
      raise
    else
      invalid << e.args.join(' ')
    end

    retry
  end

  setup_generator unless @generator

  if @pipe and not argv.empty?
    @pipe = false
    invalid << '-p (with files)'
  end

  unless invalid.empty?
    invalid = "invalid options: #{invalid.join ', '}"

    if ignore_invalid
      unless quiet
        $stderr.puts invalid
        $stderr.puts '(invalid options are ignored)'
      end
    else
      unless quiet
        $stderr.puts opts
      end
      $stderr.puts invalid
      exit 1
    end
  end

  @files = argv.dup

  self
end

#quiet ⇒ Object

Don't display progress as we process the files



1268
1269
1270
# File 'lib/rdoc/options.rb', line 1268

def quiet
  @verbosity.zero?
end

#quiet=(bool) ⇒ Object

Set quietness to bool



1275
1276
1277
# File 'lib/rdoc/options.rb', line 1275

def quiet=(bool)
  @verbosity = bool ? 0 : 1
end

#sanitize_path(path) ⇒ Object

Removes directories from path that are outside the current directory



1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
# File 'lib/rdoc/options.rb', line 1282

def sanitize_path(path)
  require 'pathname'
  dot = Pathname.new('.').expand_path

  path.reject do |item|
    path = Pathname.new(item).expand_path
    is_reject = nil
    relative = nil
    begin
      relative = path.relative_path_from(dot).to_s
    rescue ArgumentError
      # On Windows, sometimes crosses different drive letters.
      is_reject = true
    else
      is_reject = relative.start_with? '..'
    end
    is_reject
  end
end

#setup_generator(generator_name = @generator_name) ⇒ Object

Set up an output generator for the named generator_name.

If the found generator responds to :setup_options it will be called with the options instance. This allows generators to add custom options or set default options.



1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
# File 'lib/rdoc/options.rb', line 1309

def setup_generator(generator_name = @generator_name)
  @generator = @generators[generator_name]

  unless @generator
    raise OptionParser::InvalidArgument,
          "Invalid output formatter #{generator_name}"
  end

  return if @generator_options.include? @generator

  @generator_name = generator_name
  @generator_options << @generator

  if @generator.respond_to? :setup_options
    @option_parser ||= OptionParser.new
    @generator.setup_options self
  end
end

#template_dir_for(template) ⇒ Object

Finds the template dir for template



1331
1332
1333
1334
1335
1336
1337
1338
1339
# File 'lib/rdoc/options.rb', line 1331

def template_dir_for(template)
  template_path = File.join 'rdoc', 'generator', 'template', template

  $LOAD_PATH.map do |path|
    File.join File.expand_path(path), template_path
  end.find do |dir|
    File.directory? dir
  end
end

#to_yaml(*options) ⇒ Object

For dumping YAML



588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
# File 'lib/rdoc/options.rb', line 588

def to_yaml(*options) # :nodoc:
  encoding = @encoding ? @encoding.name : nil

  yaml = {}
  yaml['encoding'] = encoding
  yaml['static_path'] = sanitize_path(@static_path)
  yaml['rdoc_include'] = sanitize_path(@rdoc_include)
  yaml['page_dir'] = (sanitize_path([@page_dir]).first if @page_dir)

  ivars = instance_variables.map { |ivar| ivar.to_s[1..-1] }
  ivars -= SPECIAL

  ivars.sort.each do |ivar|
    yaml[ivar] = instance_variable_get("@#{ivar}")
  end

  if yaml.respond_to?(:to_yaml)
    yaml.to_yaml
  else
    ::RDoc.yaml_serializer.dump(yaml)
  end
end

#warn(message) ⇒ Object

Displays a warning using Kernel#warn if we're being verbose



1360
1361
1362
# File 'lib/rdoc/options.rb', line 1360

def warn(message)
  super message if @verbosity > 1
end

#write_options ⇒ Object

Writes the YAML file .rdoc_options to the current directory containing the parsed options.



1368
1369
1370
1371
1372
1373
1374
1375
1376
# File 'lib/rdoc/options.rb', line 1368

def write_options
  ::RDoc.load_yaml

  File.open '.rdoc_options', 'w' do |io|
    io.set_encoding ::Encoding::UTF_8

    io.print to_yaml
  end
end

#yaml_initialize(tag, map) ⇒ Object

:nodoc:



478
479
480
# File 'lib/rdoc/options.rb', line 478

def yaml_initialize(tag, map) # :nodoc:
  init_with map
end