diff options
author | drbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-04-26 16:14:19 +0000 |
---|---|---|
committer | drbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-04-26 16:14:19 +0000 |
commit | c42a631063e79fab2369c1983a6f2a075b584f62 (patch) | |
tree | 2ea8c0381d5d491a5f46e210239e1494df57d240 /lib/rdoc/ri | |
parent | 2142a5af51ce882641a060e69bc58c7322ff5188 (diff) |
Import RDoc 2.0.0 r56.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16212 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rdoc/ri')
-rw-r--r-- | lib/rdoc/ri/descriptions.rb | 3 | ||||
-rw-r--r-- | lib/rdoc/ri/display.rb | 238 | ||||
-rw-r--r-- | lib/rdoc/ri/driver.rb | 15 | ||||
-rw-r--r-- | lib/rdoc/ri/formatter.rb | 19 |
4 files changed, 165 insertions, 110 deletions
diff --git a/lib/rdoc/ri/descriptions.rb b/lib/rdoc/ri/descriptions.rb index 9887862e0a..adc3e7774b 100644 --- a/lib/rdoc/ri/descriptions.rb +++ b/lib/rdoc/ri/descriptions.rb @@ -13,6 +13,7 @@ class RDoc::RI::NamedThing def initialize(name) @name = name end + def <=>(other) @name <=> other.name end @@ -30,6 +31,7 @@ class RDoc::RI::AliasName < RDoc::RI::NamedThing; end class RDoc::RI::Attribute < RDoc::RI::NamedThing attr_reader :rw, :comment + def initialize(name, rw, comment) super(name) @rw = rw @@ -39,6 +41,7 @@ end class RDoc::RI::Constant < RDoc::RI::NamedThing attr_reader :value, :comment + def initialize(name, value, comment) super(name) @value = value diff --git a/lib/rdoc/ri/display.rb b/lib/rdoc/ri/display.rb index fa331c46c2..379cef11b3 100644 --- a/lib/rdoc/ri/display.rb +++ b/lib/rdoc/ri/display.rb @@ -2,9 +2,9 @@ require 'rdoc/ri' ## # This is a kind of 'flag' module. If you want to write your own 'ri' display -# module (perhaps because you'r writing an IDE or somesuch beast), you simply -# write a class which implements the various 'display' methods in -# 'DefaultDisplay', and include the 'RiDisplay' module in that class. +# module (perhaps because you're writing an IDE), you write a class which +# implements the various 'display' methods in RDoc::RI::DefaultDisplay, and +# include the RDoc::RI::Display module in that class. # # To access your class from the command line, you can do # @@ -32,26 +32,14 @@ class RDoc::RI::DefaultDisplay include RDoc::RI::Display - def initialize(formatter, width, use_stdout) + def initialize(formatter, width, use_stdout, output = $stdout) @use_stdout = use_stdout - @formatter = formatter.new $stdout, width, " " + @formatter = formatter.new output, width, " " end - def display_method_info(method) - page do - @formatter.draw_line(method.full_name) - display_params(method) - @formatter.draw_line - display_flow(method.comment) - if method.aliases && !method.aliases.empty? - @formatter.blankline - aka = "(also known as " - aka << method.aliases.map {|a| a.name }.join(", ") - aka << ")" - @formatter.wrap(aka) - end - end - end + ## + # Display information about +klass+. Fetches additional information from + # +ri_reader+ as necessary. def display_class_info(klass, ri_reader) page do @@ -90,89 +78,150 @@ class RDoc::RI::DefaultDisplay unless klass.constants.empty? @formatter.blankline @formatter.display_heading("Constants:", 2, "") - len = 0 - klass.constants.each { |c| len = c.name.length if c.name.length > len } - len += 2 - klass.constants.each do |c| - @formatter.wrap(c.value, - @formatter.indent+((c.name+":").ljust(len))) + + constants = klass.constants.sort_by { |constant| constant.name } + + constants.each do |constant| + if constant.comment then + @formatter.wrap "#{constant.name}:" + + @formatter.indent do + @formatter.display_flow constant.comment + end + else + @formatter.wrap constant.name + end end end - unless klass.class_methods.empty? - @formatter.blankline - @formatter.display_heading("Class methods:", 2, "") - @formatter.wrap(klass.class_methods.map{|m| m.name}.sort.join(', ')) - end + class_data = [ + :class_methods, + :class_method_extensions, + :instance_methods, + :instance_method_extensions, + ] - unless klass.class_method_extensions.empty? - @formatter.blankline - @formatter.display_heading("Class Method Extensions:", 2, "") - @formatter.wrap(klass.class_method_extensions.map{|m| m.name}.sort.join(', ')) - end + class_data.each do |data_type| + data = klass.send data_type - unless klass.instance_methods.empty? - @formatter.blankline - @formatter.display_heading("Instance methods:", 2, "") - @formatter.wrap(klass.instance_methods.map{|m| m.name}.sort.join(', ')) + unless data.empty? then + @formatter.blankline + + heading = data_type.to_s.split('_').join(' ').capitalize << ':' + @formatter.display_heading heading, 2, '' + + data = data.map { |item| item.name }.sort.join ', ' + @formatter.wrap data + end end - unless klass.instance_method_extensions.empty? + unless klass.attributes.empty? then @formatter.blankline - @formatter.display_heading("Instance Method Extensions:", 2, "") - @formatter.wrap(klass.instance_method_extensions.map{|m| m.name}.sort.join(', ')) + + @formatter.display_heading 'Attributes:', 2, '' + + attributes = klass.attributes.sort_by { |attribute| attribute.name } + + attributes.each do |attribute| + if attribute.comment then + @formatter.wrap "#{attribute.name} (#{attribute.rw}):" + @formatter.indent do + @formatter.display_flow attribute.comment + end + else + @formatter.wrap "#{attribute.name} (#{attribute.rw})" + end + end end + end + end + + ## + # Display an Array of RDoc::Markup::Flow objects, +flow+. + + def display_flow(flow) + if flow and not flow.empty? then + @formatter.display_flow flow + else + @formatter.wrap '[no description]' + end + end + + ## + # Display information about +method+. - unless klass.attributes.empty? + def display_method_info(method) + page do + @formatter.draw_line(method.full_name) + display_params(method) + + @formatter.draw_line + display_flow(method.comment) + + if method.aliases and not method.aliases.empty? then @formatter.blankline - @formatter.wrap("Attributes:", "") - @formatter.wrap(klass.attributes.map{|a| a.name}.sort.join(', ')) + aka = "(also known as #{method.aliases.map { |a| a.name }.join(', ')})" + @formatter.wrap aka end end end ## - # Display a list of method names + # Display the list of +methods+. def display_method_list(methods) page do - @formatter.raw_print_line("More than one method matched your request. You can refine") - @formatter.raw_print_line("your search by asking for information on one of:\n\n") - @formatter.wrap(methods.map {|m| m.full_name} .join(", ")) + @formatter.wrap "More than one method matched your request. You can refine your search by asking for information on one of:" + + @formatter.blankline + + @formatter.wrap methods.map { |m| m.full_name }.join(", ") end end - def display_class_list(namespaces) - page do - @formatter.raw_print_line("More than one class or module matched your request. You can refine") - @formatter.raw_print_line("your search by asking for information on one of:\n\n") - @formatter.wrap(namespaces.map {|m| m.full_name}.join(", ")) + ## + # Display the params for +method+. + + def display_params(method) + params = method.params + + if params[0,1] == "(" then + if method.is_singleton + params = method.full_name + params + else + params = method.name + params + end + end + + params.split(/\n/).each do |param| + @formatter.wrap param + @formatter.break_to_newline + end + + if method.source_path then + @formatter.blankline + @formatter.wrap("Extension from #{method.source_path}") end end + ## + # List the classes in +classes+. + def list_known_classes(classes) if classes.empty? warn_no_database else page do - @formatter.draw_line("Known classes and modules") + @formatter.draw_line "Known classes and modules" @formatter.blankline - @formatter.wrap(classes.sort.join(", ")) - end - end - end - def list_known_names(names) - if names.empty? - warn_no_database - else - page do - names.each {|n| @formatter.raw_print_line(n)} + @formatter.wrap classes.sort.join(', ') end end end - private + ## + # Paginates output through a pager program. def page if pager = setup_pager then @@ -190,6 +239,9 @@ class RDoc::RI::DefaultDisplay rescue Errno::EPIPE end + ## + # Sets up a pager program to pass output through. + def setup_pager unless @use_stdout then for pager in [ ENV['PAGER'], "less", "more", 'pager' ].compact.uniq @@ -200,45 +252,23 @@ class RDoc::RI::DefaultDisplay end end - def display_params(method) - params = method.params - - if params[0,1] == "(" - if method.is_singleton - params = method.full_name + params - else - params = method.name + params - end - end - params.split(/\n/).each do |p| - @formatter.wrap(p) - @formatter.break_to_newline - end - if method.source_path then - @formatter.blankline - @formatter.wrap("Extension from #{method.source_path}") - end - end - - def display_flow(flow) - if !flow || flow.empty? - @formatter.wrap("(no description...)") - else - @formatter.display_flow(flow) - end - end + ## + # Displays a message that describes how to build RI data. def warn_no_database - puts "No ri data found" - puts - puts "If you've installed Ruby yourself, you need to generate documentation using:" - puts - puts " make install-doc" - puts - puts "from the same place you ran `make` to build ruby." - puts - puts "If you installed Ruby from a packaging system, then you may need to" - puts "install an additional package, or ask the packager to enable ri generation." + output = @formatter.output + + output.puts "No ri data found" + output.puts + output.puts "If you've installed Ruby yourself, you need to generate documentation using:" + output.puts + output.puts " make install-doc" + output.puts + output.puts "from the same place you ran `make` to build ruby." + output.puts + output.puts "If you installed Ruby from a packaging system, then you may need to" + output.puts "install an additional package, or ask the packager to enable ri generation." end + end diff --git a/lib/rdoc/ri/driver.rb b/lib/rdoc/ri/driver.rb index b254d3574f..7ac2e0f15c 100644 --- a/lib/rdoc/ri/driver.rb +++ b/lib/rdoc/ri/driver.rb @@ -344,7 +344,11 @@ Options may also be set in the 'RI' environment variable. end def read_yaml(path) - YAML.load File.read(path).gsub(/ \!ruby\/(object|struct):(RDoc::RI|RI|SM).*/, '') + data = File.read path + data = data.gsub(/ \!ruby\/(object|struct):(RDoc::RI|RI).*/, '') + data = data.gsub(/ \!ruby\/(object|struct):SM::(\S+)/, + ' !ruby/\1:RDoc::Markup::\2') + YAML.load data end def get_info_for(arg) @@ -418,7 +422,7 @@ Options may also be set in the 'RI' environment variable. end -class Hash +class Hash # HACK don't add stuff to Hash. def method_missing method, *args self[method.to_s] end @@ -428,7 +432,12 @@ class Hash if self[k] then case v when Array then - self[k] += v + # HACK dunno + if String === self[k] and self[k].empty? then + self[k] = v + else + self[k] += v + end when Hash then self[k].merge! v else diff --git a/lib/rdoc/ri/formatter.rb b/lib/rdoc/ri/formatter.rb index df73bf5eb3..0a0c3f7380 100644 --- a/lib/rdoc/ri/formatter.rb +++ b/lib/rdoc/ri/formatter.rb @@ -3,7 +3,7 @@ require 'rdoc/markup' class RDoc::RI::Formatter - attr_reader :indent + attr_writer :indent attr_accessor :output FORMATTERS = { } @@ -20,6 +20,7 @@ class RDoc::RI::Formatter @output = output @width = width @indent = indent + @original_indent = indent.dup end def draw_line(label=nil) @@ -42,6 +43,18 @@ class RDoc::RI::Formatter end end + def indent + return @indent unless block_given? + + begin + indent = @indent.dup + @indent += @original_indent + yield + ensure + @indent = indent + end + end + def wrap(txt, prefix=@indent, linelen=@width) return unless txt && !txt.empty? @@ -481,13 +494,13 @@ class RDoc::RI::HtmlFormatter < RDoc::RI::AttributeFormatter when :LABELED then list_type = "dl" prefixer = proc do |li| - "<dt><b>" + escape(li.label) + "</b><dd>" + "<dt><b>" + escape(li.label) + "</b><dd>" end when :NOTE then list_type = "table" prefixer = proc do |li| - %{<tr valign="top"><td>#{li.label.gsub(/ /, ' ')}</td><td>} + %{<tr valign="top"><td>#{li.label.gsub(/ /, ' ')}</td><td>} end else fail "unknown list type" |