ruby-changes:25839
From: drbrain <ko1@a...>
Date: Tue, 27 Nov 2012 17:54:18 +0900 (JST)
Subject: [ruby-changes:25839] drbrain:r37896 (trunk): * lib/rdoc/*: Added --root option for building documentation outside
drbrain 2012-11-27 17:54:03 +0900 (Tue, 27 Nov 2012) New Revision: 37896 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=37896 Log: * lib/rdoc/*: Added --root option for building documentation outside the source directory. * test/rdoc/*: ditto * common.mk (rdoc): Added --root to rdoc rule Modified files: trunk/ChangeLog trunk/common.mk trunk/lib/rdoc/any_method.rb trunk/lib/rdoc/attr.rb trunk/lib/rdoc/class_module.rb trunk/lib/rdoc/comment.rb trunk/lib/rdoc/constant.rb trunk/lib/rdoc/generator/markup.rb trunk/lib/rdoc/generator/template/darkfish/_sidebar_in_files.rhtml trunk/lib/rdoc/markup/document.rb trunk/lib/rdoc/options.rb trunk/lib/rdoc/parser/ruby.rb trunk/lib/rdoc/rdoc.rb trunk/lib/rdoc/store.rb trunk/lib/rdoc/top_level.rb trunk/test/rdoc/test_rdoc_options.rb trunk/test/rdoc/test_rdoc_parser_ruby.rb trunk/test/rdoc/test_rdoc_rdoc.rb trunk/test/rdoc/test_rdoc_store.rb trunk/test/rdoc/test_rdoc_top_level.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 37895) +++ ChangeLog (revision 37896) @@ -1,3 +1,10 @@ +Tue Nov 27 17:43:46 2012 Eric Hodel <drbrain@s...> + + * lib/rdoc/*: Added --root option for building documentation outside + the source directory. + * test/rdoc/*: ditto + * common.mk (rdoc): Added --root to rdoc rule + Tue Nov 27 16:24:45 2012 Eric Hodel <drbrain@s...> * test/rdoc/test_rdoc_ri_paths.rb: Fixed duplicate path bug which Index: lib/rdoc/generator/markup.rb =================================================================== --- lib/rdoc/generator/markup.rb (revision 37895) +++ lib/rdoc/generator/markup.rb (revision 37896) @@ -159,9 +159,9 @@ url = @store.rdoc.options.webcvs if /%s/ =~ url then - url % @absolute_name + url % @relative_name else - url + @absolute_name + url + @relative_name end end Index: lib/rdoc/generator/template/darkfish/_sidebar_in_files.rhtml =================================================================== --- lib/rdoc/generator/template/darkfish/_sidebar_in_files.rhtml (revision 37895) +++ lib/rdoc/generator/template/darkfish/_sidebar_in_files.rhtml (revision 37896) @@ -2,7 +2,7 @@ <h3 class="section-header">Defined In</h3> <ul> <% klass.in_files.each do |tl| %> - <li><%= h tl.absolute_name %> + <li><%= h tl.relative_name %> <% end %> </ul> </nav> Index: lib/rdoc/rdoc.rb =================================================================== --- lib/rdoc/rdoc.rb (revision 37895) +++ lib/rdoc/rdoc.rb (revision 37896) @@ -2,6 +2,7 @@ require 'find' require 'fileutils' +require 'pathname' require 'time' ## @@ -345,8 +346,11 @@ return unless content - top_level = @store.add_file filename + filename_path = Pathname(filename).expand_path + relative_path = filename_path.relative_path_from @options.root + top_level = @store.add_file filename, relative_path.to_s + parser = RDoc::Parser.for top_level, filename, content, @options, @stats return unless parser Index: lib/rdoc/constant.rb =================================================================== --- lib/rdoc/constant.rb (revision 37895) +++ lib/rdoc/constant.rb (revision 37896) @@ -109,7 +109,7 @@ @visibility, alias_name, parse(@comment), - @file.absolute_name, + @file.relative_name, parent.name, parent.class, section.title, Index: lib/rdoc/top_level.rb =================================================================== --- lib/rdoc/top_level.rb (revision 37895) +++ lib/rdoc/top_level.rb (revision 37896) @@ -35,14 +35,16 @@ attr_accessor :parser ## - # Creates a new TopLevel for +file_name+ + # Creates a new TopLevel for the file at +absolute_name+. If documentation + # is being generated outside the source dir +relative_name+ is relative to + # the source directory. - def initialize file_name + def initialize absolute_name, relative_name = absolute_name super() @name = nil - @relative_name = file_name - @absolute_name = file_name - @file_stat = File.stat(file_name) rescue nil # HACK for testing + @absolute_name = absolute_name + @relative_name = relative_name + @file_stat = File.stat(absolute_name) rescue nil # HACK for testing @diagram = nil @parser = nil @@ -50,10 +52,10 @@ end ## - # An RDoc::TopLevel is equal to another with the same absolute_name + # An RDoc::TopLevel is equal to another with the same relative_name def == other - self.class === other and @absolute_name == other.absolute_name + self.class === other and @relative_name == other.relative_name end alias eql? == @@ -106,7 +108,7 @@ # Base name of this file def base_name - File.basename @absolute_name + File.basename @relative_name end alias name base_name @@ -152,10 +154,10 @@ ## # An RDoc::TopLevel has the same hash as another with the same - # absolute_name + # relative_name def hash - @absolute_name.hash + @relative_name.hash end ## @@ -188,7 +190,7 @@ def marshal_dump [ MARSHAL_VERSION, - @absolute_name, + @relative_name, @parser, parse(@comment), ] @@ -223,7 +225,7 @@ # Base name of this file without the extension def page_name - basename = File.basename @absolute_name + basename = File.basename @relative_name basename =~ /\.[^.]*$/ $` || basename Index: lib/rdoc/store.rb =================================================================== --- lib/rdoc/store.rb (revision 37895) +++ lib/rdoc/store.rb (revision 37896) @@ -139,11 +139,11 @@ # Adds the file with +name+ as an RDoc::TopLevel to the store. Returns the # created RDoc::TopLevel. - def add_file name - unless top_level = @files_hash[name] then - top_level = RDoc::TopLevel.new name + def add_file absolute_name, relative_name = absolute_name + unless top_level = @files_hash[relative_name] then + top_level = RDoc::TopLevel.new absolute_name, relative_name top_level.store = self - @files_hash[name] = top_level + @files_hash[relative_name] = top_level end top_level Index: lib/rdoc/markup/document.rb =================================================================== --- lib/rdoc/markup/document.rb (revision 37895) +++ lib/rdoc/markup/document.rb (revision 37896) @@ -96,7 +96,7 @@ def file= location @file = case location when RDoc::TopLevel then - location.absolute_name + location.relative_name else location end Index: lib/rdoc/comment.rb =================================================================== --- lib/rdoc/comment.rb (revision 37895) +++ lib/rdoc/comment.rb (revision 37896) @@ -148,7 +148,7 @@ end def inspect # :nodoc: - location = @location ? @location.absolute_name : '(unknown)' + location = @location ? @location.relative_name : '(unknown)' "#<%s:%x %s %p>" % [self.class, object_id, location, @text] end Index: lib/rdoc/class_module.rb =================================================================== --- lib/rdoc/class_module.rb (revision 37895) +++ lib/rdoc/class_module.rb (revision 37896) @@ -304,7 +304,7 @@ end, @sections.values, @in_files.map do |tl| - tl.absolute_name + tl.relative_name end, parent.full_name, parent.class, Index: lib/rdoc/any_method.rb =================================================================== --- lib/rdoc/any_method.rb (revision 37895) +++ lib/rdoc/any_method.rb (revision 37896) @@ -107,7 +107,7 @@ @block_params, aliases, @params, - @file.absolute_name, + @file.relative_name, @calls_super, @parent.name, @parent.class, Index: lib/rdoc/parser/ruby.rb =================================================================== --- lib/rdoc/parser/ruby.rb (revision 37895) +++ lib/rdoc/parser/ruby.rb (revision 37896) @@ -785,7 +785,7 @@ indent.set_text " " * column position_comment = TkCOMMENT.new 0, line_no, 1 - position_comment.set_text "# File #{@top_level.absolute_name}, line #{line_no}" + position_comment.set_text "# File #{@top_level.relative_name}, line #{line_no}" meth.add_tokens [position_comment, NEWLINE_TOKEN, indent] meth.params = '' @@ -843,7 +843,7 @@ indent.set_text " " * offset position_comment = TkCOMMENT.new 0, line_no, 1 - position_comment.set_text "# File #{@top_level.absolute_name}, line #{line_no}" + position_comment.set_text "# File #{@top_level.relative_name}, line #{line_no}" meth.add_tokens [position_comment, NEWLINE_TOKEN, indent] meth.call_seq = signature @@ -1015,7 +1015,7 @@ indent.set_text " " * column position_comment = TkCOMMENT.new 0, line_no, 1 - position_comment.value = "# File #{@top_level.absolute_name}, line #{line_no}" + position_comment.value = "# File #{@top_level.relative_name}, line #{line_no}" meth.add_tokens [position_comment, NEWLINE_TOKEN, indent] meth.add_tokens @token_stream @@ -1171,7 +1171,7 @@ indent.set_text " " * column token = TkCOMMENT.new 0, line_no, 1 - token.set_text "# File #{@top_level.absolute_name}, line #{line_no}" + token.set_text "# File #{@top_level.relative_name}, line #{line_no}" meth.add_tokens [token, NEWLINE_TOKEN, indent] meth.add_tokens @token_stream Index: lib/rdoc/options.rb =================================================================== --- lib/rdoc/options.rb (revision 37895) +++ lib/rdoc/options.rb (revision 37896) @@ -1,4 +1,5 @@ require 'optparse' +require 'pathname' ## # RDoc::Options handles the parsing and storage of options @@ -95,6 +96,7 @@ option_parser pipe rdoc_include + root static_path stylesheet_url template @@ -105,6 +107,12 @@ ] ## + # Option validator for OptionParser that matches a directory that exists on + # the filesystem. + + Directory = Object.new + + ## # Option validator for OptionParser that matches a file or directory that # exists on the filesystem. @@ -231,6 +239,13 @@ attr_accessor :rdoc_include ## + # Root of the source documentation will be generated for. Set this when + # building documentation outside the source directory. Defaults to the + # current directory. + + attr_accessor :root + + ## # Include the '#' at the front of hyperlinked instance method names attr_accessor :show_hash @@ -304,6 +319,7 @@ @op_dir = nil @pipe = false @rdoc_include = [] + @root = Pathname(Dir.pwd) @show_hash = false @static_path = [] @stylesheet_url = nil # TODO remove in RDoc 4 @@ -562,6 +578,14 @@ 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 @@ -674,6 +698,17 @@ 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.separator "Common generator options:" opt.separator nil Index: lib/rdoc/attr.rb =================================================================== --- lib/rdoc/attr.rb (revision 37895) +++ lib/rdoc/attr.rb (revision 37896) @@ -106,7 +106,7 @@ @visibility, parse(@comment), singleton, - @file.absolute_name, + @file.relative_name, @parent.full_name, @parent.class, @section.title Index: common.mk =================================================================== --- common.mk (revision 37895) +++ common.mk (revision 37896) @@ -408,11 +408,11 @@ rdoc: PHONY main @echo Generating RDoc documentation - $(Q) $(XRUBY) "$(srcdir)/bin/rdoc" --encoding=UTF-8 --no-force-update --all --ri --op "$(RDOCOUT)" --debug $(RDOCFLAGS) "$(srcdir)" + $(Q) $(XRUBY) "$(srcdir)/bin/rdoc" --root "$(srcdir)" --encoding=UTF-8 --no-force-update --all --ri --op "$(RDOCOUT)" --debug $(RDOCFLAGS) "$(srcdir)" rdoc-coverage: PHONY main @echo Generating RDoc coverage report - $(Q) $(XRUBY) "$(srcdir)/bin/rdoc" --encoding=UTF-8 --all --quiet -C $(RDOCFLAGS) "$(srcdir)" + $(Q) $(XRUBY) "$(srcdir)/bin/rdoc" --root "$(srcdir)" --encoding=UTF-8 --all --quiet -C $(RDOCFLAGS) "$(srcdir)" nodoc: PHONY Index: test/rdoc/test_rdoc_rdoc.rb =================================================================== --- test/rdoc/test_rdoc_rdoc.rb (revision 37895) +++ test/rdoc/test_rdoc_rdoc.rb (revision 37896) @@ -141,6 +141,50 @@ assert_match %r%/dev/stdin$%, err end + def test_parse_file + pwd = Dir.pwd + + @rdoc.store = RDoc::Store.new + + temp_dir do |dir| + @rdoc.options.root = Pathname(Dir.pwd) + + open 'test.txt', 'w' do |io| + io.puts 'hi' + end + + test_txt = File.join dir, 'test.txt' + + top_level = @rdoc.parse_file 'test.txt' + + assert_equal 'test.txt', top_level.absolute_name + assert_equal 'test.txt', top_level.relative_name + end + end + + def test_parse_file_relative + pwd = Dir.pwd + + @rdoc.store = RDoc::Store.new + + temp_dir do |dir| + @rdoc.options.root = Pathname(dir) + + open 'test.txt', 'w' do |io| + io.puts 'hi' + end + + test_txt = File.join dir, 'test.txt' + + Dir.chdir pwd do + top_level = @rdoc.parse_file test_txt + + assert_equal test_txt, top_level.absolute_name + assert_equal 'test.txt', top_level.relative_name + end + end + end + def test_parse_file_encoding skip "Encoding not implemented" unless Object.const_defined? :Encoding @rdoc.options.encoding = Encoding::ISO_8859_1 Index: test/rdoc/test_rdoc_top_level.rb =================================================================== --- test/rdoc/test_rdoc_top_level.rb (revision 37895) +++ test/rdoc/test_rdoc_top_level.rb (revision 37896) @@ -9,6 +9,20 @@ @top_level.parser = RDoc::Parser::Ruby end + def test_initialize + t = RDoc::TopLevel.new 'path/file.rb' + + assert_equal 'path/file.rb', t.absolute_name + assert_equal 'path/file.rb', t.relative_name + end + + def test_initialize_relative + t = RDoc::TopLevel.new 'path/file.rb', 'file.rb' + + assert_equal 'path/file.rb', t.absolute_name + assert_equal 'file.rb', t.relative_name + end + def test_add_alias a = RDoc::Alias.new nil, 'old', 'new', nil @top_level.add_alias a Index: test/rdoc/test_rdoc_store.rb =================================================================== --- test/rdoc/test_rdoc_store.rb (revision 37895) +++ test/rdoc/test_rdoc_store.rb (revision 37896) @@ -127,6 +127,21 @@ refute_same top_level, @store.add_file('other.rb') end + def test_add_file_relative + top_level = @store.add_file 'path/file.rb', 'file.rb' + + assert_kind_of RDoc::TopLevel, top_level + assert_equal @store, top_level.store + + assert_equal 'path/file.rb', top_level.absolute_name + assert_equal 'file.rb', top_level.relative_name + + assert_includes @store.all_files, top_level + + assert_same top_level, @store.add_file('file.rb') + refute_same top_level, @store.add_file('other.rb') + end + def test_all_classes_and_modules expected = %w[ C1 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1 Index: test/rdoc/test_rdoc_options.rb =================================================================== --- test/rdoc/test_rdoc_options.rb (revision 37895) +++ test/rdoc/test_rdoc_options.rb (revision 37896) @@ -430,6 +430,19 @@ assert_equal 'tomdoc', @options.markup end + def test_parse_root + assert_equal Pathname(Dir.pwd), @options.root + + out, err = capture_io do + @options.parse %W[--root #{Dir.tmpdir}] + end + + assert_empty out + assert_empty err + + assert_equal Pathname(Dir.tmpdir), @options.root + end + def test_parse_template out, err = capture_io do @options.parse %w[--template darkfish] Index: test/rdoc/test_rdoc_parser_ruby.rb =================================================================== --- test/rdoc/test_rdoc_parser_ruby.rb (revision 37895) +++ test/rdoc/test_rdoc_parser_ruby.rb (revision 37896) @@ -1026,7 +1026,7 @@ stream = [ tk(:COMMENT, 0, 1, 1, nil, - "# File #{@top_level.absolute_name}, line 1"), + "# File #{@top_level.relative_name}, line 1"), RDoc::Parser::Ruby::NEWLINE_TOKEN, tk(:SPACE, 0, 1, 1, nil, ''), ] @@ -1199,7 +1199,7 @@ stream = [ tk(:COMMENT, 0, 1, 1, nil, - "# File #{@top_level.absolute_name}, line 1"), + "# File #{@top_level.relative_name}, line 1"), RDoc::Parser::Ruby::NEWLINE_TOKEN, tk(:SPACE, 0, 1, 1, nil, ''), tk(:IDENTIFIER, 0, 1, 0, 'add_my_method', 'add_my_method'), @@ -1397,7 +1397,7 @@ stream = [ tk(:COMMENT, 0, 1, 1, nil, - "# File #{@top_level.absolute_name}, line 1"), + "# File #{@top_level.relative_name}, line 1"), RDoc::Parser::Ruby::NEWLINE_TOKEN, tk(:SPACE, 0, 1, 1, nil, ''), tk(:DEF, 0, 1, 0, 'def', 'def'), -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/