bri 0.5.0 → 1.0.0.pre.beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/Changelog +0 -2
  3. data/TODO +2 -3
  4. data/bin/bri +21 -8
  5. data/lib/bri/mall.rb +12 -17
  6. data/lib/bri/match/base.rb +2 -7
  7. data/lib/bri/match/class.rb +6 -3
  8. data/lib/bri/match/method.rb +3 -3
  9. data/lib/bri/renderer/blank_line.rb +9 -0
  10. data/lib/bri/renderer/default.rb +27 -0
  11. data/lib/bri/renderer/document.rb +11 -0
  12. data/lib/bri/renderer/heading.rb +9 -0
  13. data/lib/bri/renderer/list/base.rb +18 -0
  14. data/lib/bri/renderer/list/bullet.rb +15 -0
  15. data/lib/bri/renderer/list/labeled.rb +15 -0
  16. data/lib/bri/renderer/list/lower_lettered.rb +18 -0
  17. data/lib/bri/renderer/list/note.rb +15 -0
  18. data/lib/bri/renderer/list/numbered.rb +18 -0
  19. data/lib/bri/renderer/list/upper_lettered.rb +18 -0
  20. data/lib/bri/renderer/list.rb +24 -0
  21. data/lib/bri/renderer/list_item.rb +24 -0
  22. data/lib/bri/renderer/paragraph.rb +11 -0
  23. data/lib/bri/renderer/result.rb +73 -0
  24. data/lib/bri/renderer/rule.rb +9 -0
  25. data/lib/bri/renderer/verbatim.rb +14 -0
  26. data/lib/bri/renderer.rb +28 -188
  27. data/lib/bri/search/class.rb +3 -1
  28. data/lib/bri/search/method.rb +3 -3
  29. data/lib/bri/templates.rb +7 -38
  30. data/lib/bri/text_formatting_utils.rb +92 -0
  31. data/lib/bri.rb +18 -15
  32. metadata +27 -17
  33. data/spec/bri_dummy_spec_class.rb +0 -132
  34. data/spec/lib/bri/mall_spec.rb +0 -38
  35. data/spec/lib/bri/match/class_spec.rb +0 -125
  36. data/spec/lib/bri/match/method_spec.rb +0 -116
  37. data/spec/lib/bri/matcher_spec.rb +0 -70
  38. data/spec/lib/bri/renderer_spec.rb +0 -338
  39. data/spec/lib/bri/search/class_method_spec.rb +0 -173
  40. data/spec/lib/bri/search/class_spec.rb +0 -66
  41. data/spec/lib/bri/search/instance_method_spec.rb +0 -174
  42. data/spec/lib/bri/search/method_spec.rb +0 -41
  43. data/spec/spec_helper.rb +0 -22
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a88edaf5dc38f84306db04f904d1fb32859536587b7fa5ce84f3be687c5d1638
4
- data.tar.gz: 1d8ace29c2676219311960e6729bc8b0e453ae00fd5a88294f809be65955024f
3
+ metadata.gz: 2a26dfbcd940347c2bf68246c1223702a30ae82cc6ff032fd9160f4f6cd2587f
4
+ data.tar.gz: ad88f5eb0b894300eb1f8073da1baf4444ebebd07aa992d772465044f1a6d444
5
5
  SHA512:
6
- metadata.gz: 3e7eb14da6bd17fdff9e5db187fe086f9592dcb682c18e082248ab61ef37685fd81e16cbe49198fbf9446cf3eccb1ac5f163ba7da906ef1b67a38329cfdb3eb2
7
- data.tar.gz: 91fef7322fd718245385929888a5dcf48616c07c247621d4e22f8369b625e77185969af70fff295bc1538c0bcb5b2651a716143d15b54ee8f830f71439bbe317
6
+ metadata.gz: c258a7980e40af727d7d543f70d14398df03019be7496116180d6c6812e5becbad29bde410d26983b49ec6617cbb477c73ed357ef18cf60e72ab362fce914ec1
7
+ data.tar.gz: 9cbe6dc1a11b9cc745ce75b8f602416dbba701bcb0a22f0c973d15375875cf23d67faa1b03836d63595752a189cbccacb4c6d943a0464a2ef616cf4254b2b6a0
data/Changelog CHANGED
@@ -1,5 +1,3 @@
1
- 0.5.0
2
- - Update rdoc requirement to 6.13
3
1
  0.4.4
4
2
  - No longer display all found class/module documentation found. If core
5
3
  documentation is available, output only that, otherwise falls back to gem
data/TODO CHANGED
@@ -1,7 +1,4 @@
1
1
  - Extend unqualified search to include class methods and case insensitive searches
2
- - {multi word label}[links] won't display correctly if the label is right where
3
- a line wrap occurs
4
- - Refactor the convoluted rendering of lists and list items
5
2
  - Detect ansi capabilities and use what is possible with graceful degradation?
6
3
  - Potentially try to detect method references in documentation prose and
7
4
  highlight these (e.g. Object#send and Object.new)
@@ -9,3 +6,5 @@
9
6
  - Add display of module constant aliases
10
7
  - Rewrite the search code into something understandable and maintainable
11
8
  (sleep deprivation is one hell of a drug)
9
+ - Add UTF8 rendering mode
10
+ - Add --stats switch to see timing data
data/bin/bri CHANGED
@@ -20,13 +20,26 @@ def parse_options
20
20
  opts.separator " * #instance_method_name"
21
21
  opts.separator " * .class_method_name"
22
22
  opts.separator ""
23
- opts.on( nil, "--classes", "List known classes" ) { |v| @options[:list_classes] = true }
24
- opts.on( nil, "--methods", "List known methods" ) { |v| @options[:list_methods] = true }
25
- opts.on( "-l", "--list-names", "List known namespaces/methods" ) { |v| @options[:list_names] = true }
26
- opts.on( "-w", "--width [COLUMNS]", "Set the output width", Integer ) { |v| Bri.width = v.to_i - 8 }
27
- opts.on( "-a", "--all", "Output all documentation for the term. Prefers core documents otherwise") { |v| @options[:show_all] = true }
28
- opts.on( "-v", "--version", "0.5.0.beta1")
29
- opts.on_tail( "-h", "--help", "This help text" ) { puts opts; exit }
23
+ opts.on( nil, "--classes", "List known classes" ) do |v|
24
+ @options[:list_classes] = true
25
+ end
26
+ opts.on( nil, "--methods", "List known methods" ) do |v|
27
+ @options[:list_methods] = true
28
+ end
29
+ opts.on( "-l", "--list-names", "List known namespaces/methods" ) do |v|
30
+ @options[:list_names] = true
31
+ end
32
+ opts.on( "-w", "--width [COLUMNS]", "Set the output width", Integer ) do |v|
33
+ Bri.width = v.to_i - 8
34
+ end
35
+ opts.on( "-a", "--all",
36
+ "Output gem documentation in addition to core documents" ) do |v|
37
+ @options[:show_all] = true
38
+ end
39
+ opts.on_tail( "-h", "--help", "This help text" ) do
40
+ puts opts
41
+ exit
42
+ end
30
43
  end
31
44
  parser.parse!( ARGV )
32
45
 
@@ -46,5 +59,5 @@ elsif @options[:list_methods]
46
59
  elsif @options[:list_names]
47
60
  puts Bri.list_names
48
61
  else
49
- puts Bri.ri( ARGV[0], show_all: @options[:show_all] )
62
+ puts Bri.ri( ARGV[0], @options )
50
63
  end
data/lib/bri/mall.rb CHANGED
@@ -1,18 +1,24 @@
1
1
  require 'rdoc/ri/paths'
2
2
  require 'rdoc/ri/store'
3
- require 'singleton'
4
3
 
5
4
  module Bri
6
5
  class Mall
7
- include Singleton
8
6
 
9
- attr_reader :stores
7
+ def self.ri_paths( system: true, site: true, home: true, gems: true )
8
+ @ri_paths ||= RDoc::RI::Paths.each( system, site, home, gems )
9
+ end
10
+
11
+ def self.stores
12
+ @stores ||= ri_paths.each_with_object( [] ) do |(path, type), stores|
13
+ stores << RDoc::Store.new( path, type ).tap { |store| store.load_cache }
14
+ end
15
+ end
10
16
 
11
- def classes
17
+ def self.classes
12
18
  stores.flat_map(&:module_names).uniq.sort
13
19
  end
14
20
 
15
- def class_methods
21
+ def self.class_methods
16
22
  stores.each_with_object( [] ) do |store, result|
17
23
  store.class_methods.each do |klass, methods|
18
24
  methods.each { |method| result << "#{klass}.#{method}" }
@@ -20,23 +26,12 @@ module Bri
20
26
  end.uniq
21
27
  end
22
28
 
23
- def instance_methods
29
+ def self.instance_methods
24
30
  stores.each_with_object( [] ) do |store, result|
25
31
  store.instance_methods.each do |klass, methods|
26
32
  methods.each { |method| result << "#{klass}##{method}" }
27
33
  end
28
34
  end.uniq
29
35
  end
30
-
31
- private
32
- def initialize
33
- # We want: system, site, home and gem documentation
34
- options = RDoc::Options.new
35
- @stores = RDoc::RI::Paths.each( true, true, true, true ).
36
- each_with_object( [] ) do |(path, type), stores|
37
- stores << RDoc::Store.new( options, path: path, type: type ).
38
- tap { |store| store.load_cache }
39
- end
40
- end
41
36
  end
42
37
  end
@@ -2,18 +2,13 @@ module Bri
2
2
  module Match
3
3
  class Base
4
4
  def to_s
5
- ERB.new( self.class.const_get( :TEMPLATE ), trim_mode: '<>' ).
5
+ ERB.new( self.class.const_get( :TEMPLATE ), nil, '<>' ).
6
6
  result( binding )
7
7
  end
8
8
 
9
9
  private
10
- def build_description_from_comment( comment )
11
- document = comment.instance_variable_get( :@document )
12
- build_description( document.parts )
13
- end
14
-
15
10
  def build_description( source )
16
- source.map { |element| Bri::Renderer.render( element ) }.compact
11
+ source.map { |element| Bri::Renderer.new( element ).render }.compact.map(&:output)
17
12
  end
18
13
  end
19
14
  end
@@ -4,7 +4,11 @@ require 'rdoc/markup/paragraph'
4
4
  module Bri
5
5
  module Match
6
6
  class Class < Base
7
- include Bri::Templates::Helpers
7
+ # FIXME This should not be part of this class; the template may need
8
+ # a modiified binding.
9
+ #
10
+ include Bri::TextFormattingUtils
11
+
8
12
  TEMPLATE = Bri::Templates::CLASS_DESCRIPTION
9
13
 
10
14
  attr_reader :type, :name, :description_paragraphs,
@@ -18,8 +22,7 @@ module Bri
18
22
  @type = rdoc_result.type
19
23
  @name = "#{rdoc_result.full_name}"
20
24
  @name << " < #{rdoc_result.superclass}" if @type == "class" && rdoc_result.superclass
21
-
22
- @description_paragraphs = build_description_from_comment( rdoc_result.comment )
25
+ @description_paragraphs = build_description( rdoc_result.comment.parts )
23
26
  @includes = rdoc_result.includes.map(&:full_name)
24
27
  @extends = rdoc_result.extends.map(&:full_name)
25
28
  @attributes = rdoc_result.attributes.map { |a| "#{a.name} (#{a.rw})" }
@@ -1,7 +1,8 @@
1
1
  module Bri
2
2
  module Match
3
3
  class Method < Base
4
- include Bri::Templates::Helpers
4
+ include Bri::TextFormattingUtils
5
+
5
6
  TEMPLATE = Bri::Templates::METHOD_DESCRIPTION
6
7
 
7
8
  attr_accessor :full_name, :call_syntaxes, :description_paragraphs, :origin
@@ -11,8 +12,7 @@ module Bri
11
12
  @call_syntaxes = rdoc_method.arglists.lines( chomp: true ).
12
13
  map { |e| e.prepend( " " ) }.
13
14
  join( "\n" ) + "\n" rescue ''
14
-
15
- @description_paragraphs = build_description_from_comment( rdoc_method.comment )
15
+ @description_paragraphs = build_description( rdoc_method.comment.parts )
16
16
  @origin = store&.friendly_path
17
17
  end
18
18
  end
@@ -0,0 +1,9 @@
1
+ module Bri
2
+ module Renderer
3
+ class BlankLine < Default
4
+ def extract_text( width, conserve_newlines = false )
5
+ "\n"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ module Bri
2
+ module Renderer
3
+ class Default
4
+ include ::Bri::TextFormattingUtils
5
+
6
+ attr_reader :element
7
+
8
+ def initialize( element )
9
+ @element = element
10
+ end
11
+
12
+ def render( width = Bri.width )
13
+ text = extract_text( width )
14
+
15
+ if text.empty?
16
+ nil
17
+ else
18
+ ::Bri::Renderer::Result.new( text, width )
19
+ end
20
+ end
21
+
22
+ def extract_text( width, conserve_newslines = false )
23
+ raise "Don't know how to handle type #{element.class}: #{element.inspect}"
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,11 @@
1
+ module Bri
2
+ module Renderer
3
+ class Document < Default
4
+ def extract_text( width, conserve_newlines = false )
5
+ element.parts.
6
+ map { |part| ::Bri::Renderer.new( part ).extract_text( width, conserve_newlines ) }.
7
+ join
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module Bri
2
+ module Renderer
3
+ class Heading < Default
4
+ def extract_text( width, conserve_newlines = false )
5
+ "<h>#{element.text.chomp}</h>"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ module Bri
2
+ module Renderer
3
+ class List
4
+ class Base < ::Bri::Renderer::Default
5
+ def extract_text( width, conserve_newlines = false )
6
+ item_width = width - max_bullet_width
7
+ rendered_items = element.items.each_with_index.
8
+ map do |item, index|
9
+ bullet = next_bullet( item )
10
+ ::Bri::Renderer.new( item ).text( item_width, bullet )
11
+ end
12
+
13
+ rendered_items.join
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ module Bri
2
+ module Renderer
3
+ class List
4
+ class Bullet < Base
5
+ def max_bullet_width
6
+ 3
7
+ end
8
+
9
+ def next_bullet( item )
10
+ '* '
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Bri
2
+ module Renderer
3
+ class List
4
+ class Labeled < Base
5
+ def max_bullet_width
6
+ @max_bullet_width ||= element.items.map(&:label).map(&:size).max
7
+ end
8
+
9
+ def next_bullet( item )
10
+ "#{item.label.first}: "
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ module Bri
2
+ module Renderer
3
+ class List
4
+ class LowerLettered < Base
5
+ def max_bullet_width
6
+ @max_bullet_width ||= ( Math.log10( element.items.size ) / Math.log10( 26 ) ).ceil + 3
7
+ end
8
+
9
+ def next_bullet( item )
10
+ @current_bullet ||= 'a'
11
+ result = "#{@current_bullet}. "
12
+ @current_bullet.succ!
13
+ result
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ module Bri
2
+ module Renderer
3
+ class List
4
+ class Note < Base
5
+ def max_bullet_width
6
+ @max_bullet_width ||= element.items.flat_map(&:label).map(&:size).max
7
+ end
8
+
9
+ def next_bullet( item )
10
+ "#{item.label.first}:" + " " * (max_bullet_width - item.label.first.size + 1)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ module Bri
2
+ module Renderer
3
+ class List
4
+ class Numbered < Base
5
+ def next_bullet( item )
6
+ @current_bullet ||= '1'
7
+ result = "#{@current_bullet}. "
8
+ @current_bullet.succ!
9
+ result
10
+ end
11
+
12
+ def max_bullet_width
13
+ @max_bullet_width ||= Math.log10( element.items.size ).ceil + 3
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module Bri
2
+ module Renderer
3
+ class List
4
+ class UpperLettered < Base
5
+ def max_bullet_width
6
+ @max_bullet_width ||= ( Math.log10( element.items.size ) / Math.log10( 26 ) ).ceil + 3
7
+ end
8
+
9
+ def next_bullet( item )
10
+ @current_bullet ||= 'A'
11
+ result = "#{@current_bullet}. "
12
+ @current_bullet.succ!
13
+ result
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,24 @@
1
+ module Bri
2
+ module Renderer
3
+ class List < Default
4
+ def self.new( element )
5
+ case element.type
6
+ when :BULLET then ::Bri::Renderer::List::Bullet.new( element )
7
+ when :NUMBER then ::Bri::Renderer::List::Numbered.new( element )
8
+ when :LALPHA then ::Bri::Renderer::List::LowerLettered.new( element )
9
+ when :UALPHA then ::Bri::Renderer::List::UpperLettered.new( element )
10
+ when :LABEL then ::Bri::Renderer::List::Labeled.new( element )
11
+ when :NOTE then ::Bri::Renderer::List::Note.new( element )
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ require_relative 'list/base'
19
+ require_relative 'list/bullet'
20
+ require_relative 'list/numbered'
21
+ require_relative 'list/lower_lettered'
22
+ require_relative 'list/upper_lettered'
23
+ require_relative 'list/labeled'
24
+ require_relative 'list/note'
@@ -0,0 +1,24 @@
1
+ module Bri
2
+ module Renderer
3
+ class ListItem < Default
4
+ def text( total_width, bullet )
5
+ element.parts.map.with_index do |part, index|
6
+ part_is_list = part.is_a?(RDoc::Markup::List)
7
+ first_line_indent = ( part_is_list || index > 0 ) ? bullet.size : 0
8
+ following_line_indent = bullet.size
9
+
10
+ text = ::Bri::Renderer.new( part ).extract_text( total_width, true )
11
+ text.prepend( bullet ) if index == 0 && !part_is_list
12
+ lines = text.lines
13
+
14
+ result = indent( lines.first, indent_depth: first_line_indent )
15
+ lines.drop( 1 ).each do |line|
16
+ result << indent( line, indent_depth: following_line_indent )
17
+ end
18
+
19
+ result
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,11 @@
1
+ module Bri
2
+ module Renderer
3
+ class Paragraph < Default
4
+ def extract_text( width, conserve_newlines = false )
5
+ join_char = conserve_newlines ? "\n" : " "
6
+ intermediate = element.parts.map(&:strip).join( join_char )
7
+ wrap_to_width( intermediate, width )
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,73 @@
1
+ module Bri
2
+ module Renderer
3
+ class Result
4
+ include ::Bri::TextFormattingUtils
5
+
6
+ Color = ::Term::ANSIColor
7
+
8
+ attr_reader :input, :width
9
+
10
+ def initialize(input, width )
11
+ @input = input
12
+ @width = width
13
+ end
14
+
15
+ def output
16
+ return @output if instance_variable_defined?( :@output )
17
+
18
+ @output = replace_markup( input ).
19
+ then { |styled_text| width ? wrap_to_width( styled_text, width ) : styled_text }.
20
+ then { |wrapped_text| indent( wrapped_text ) }
21
+ end
22
+
23
+ def concat( other )
24
+ output.concat( other.output )
25
+ end
26
+
27
+ private
28
+
29
+ def replace_markup( text )
30
+ text.gsub!( /(?<!\\)<(?:tt|code)>/, Color.cyan )
31
+ text.gsub!( /(?<!\\)<\/(?:tt|code)>/, Color.reset )
32
+
33
+ text.gsub!( /(?<!\\)<b>/, Color.bold )
34
+ text.gsub!( /(?<!\\)<\/b>/, Color.reset )
35
+
36
+ text.gsub!( /(?<!\\)<(?:em|i)>/, Color.yellow )
37
+ text.gsub!( /(?<!\\)<\/(?:em|i)>/, Color.reset )
38
+
39
+ text.gsub!( "<h>", Color.green )
40
+ text.gsub!( "</h>", Color.reset )
41
+
42
+ text.gsub!( "\\<", "<" )
43
+
44
+ text.gsub!( /(#\s*=>)(.*)/,
45
+ "#{Color.dark}\\1#{Color.reset}#{Color.bold}\\2#{Color.reset}" )
46
+
47
+ text.gsub!( /(^|\s)\*(.*?[a-zA-Z0-9]+.*?)\*/,
48
+ "\\1#{Color.bold}\\2#{Color.reset}" )
49
+ text.gsub!( /(^|\s)\+(.*?[a-zA-Z0-9]+.*?)\+/,
50
+ "\\1#{Color.cyan}\\2#{Color.reset}" )
51
+ text.gsub!( /(^|\s)_(.*?[a-zA-Z0-9]+.*?)_/,
52
+ "\\1#{Color.yellow}\\2#{Color.reset}" )
53
+
54
+ text.gsub!( %r{\b((?:https?|ftp)://[-\w.?%&=/]+)\b},
55
+ "#{Color.underline}\\1#{Color.reset}" )
56
+
57
+ text.gsub!( %r{\b(mailto:[-\w.%]+@[-\w.]+)\b},
58
+ "#{Color.underline}\\1#{Color.reset}" )
59
+
60
+ text.gsub!( %r{\b((?<!:\/\/)www.[-\w.?%&=]+)\b},
61
+ "#{Color.underline}\\1#{Color.reset}" )
62
+
63
+ text.gsub!( %r{\blink:(.*?)(\s|$)},
64
+ "#{Color.underline}\\1#{Color.reset}\\2" )
65
+
66
+ text.gsub!( %r{\{(.*?)\}\[(.*?)\]}m, "\\1 (\\2)" )
67
+ text.gsub!( %r{\[(#{Regexp.escape( Color.underline )}.*?#{Regexp.escape( Color.reset )})\]},
68
+ " (\\1)" )
69
+ text
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,9 @@
1
+ module Bri
2
+ module Renderer
3
+ class Rule < Default
4
+ def extract_text( width, conserve_newlines = false )
5
+ "#{"-" * width}\n"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ module Bri
2
+ module Renderer
3
+ class Verbatim < Default
4
+ def render( width = Bri.width )
5
+ text = ::Bri::Renderer.new( element ).extract_text( width )
6
+ ::Bri::Renderer::Result.new( "#{text}\n", nil )
7
+ end
8
+
9
+ def extract_text( width, conserve_newlines = false )
10
+ element.parts.map { |part| indent(part) }.join + "\n"
11
+ end
12
+ end
13
+ end
14
+ end