bri 0.1.2 → 0.1.3

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.
data/lib/bri.rb CHANGED
@@ -3,6 +3,7 @@ require 'erb'
3
3
  require 'term/ansicolor'
4
4
 
5
5
 
6
+ require 'bri/renderer'
6
7
  require 'bri/mall'
7
8
  require 'bri/matcher'
8
9
  require 'bri/templates'
@@ -48,7 +49,11 @@ module Bri
48
49
  end
49
50
 
50
51
  def self.width
51
- @@width ||= [ ( ENV['COLUMNS'] || 80 ) - 8, 1 ].max
52
+ return @@width if defined?( @@width )
53
+ base_width = ENV['COLUMNS'].to_i
54
+ base_width = 80 if base_width == 0
55
+
56
+ @@width ||= [ base_width - 8, 1 ].max
52
57
  end
53
58
 
54
59
  def self.width=( width )
@@ -1,6 +1,7 @@
1
1
  module Bri
2
2
  module Match
3
3
  class Base
4
+ @@renderer = Bri::Renderer.new
4
5
  def to_s
5
6
  ERB.new( self.class.const_get( :TEMPLATE ), nil, '<>' ).
6
7
  result( binding )
@@ -8,45 +9,7 @@ module Bri
8
9
 
9
10
  private
10
11
  def build_description( source )
11
- result = []
12
- source.each do |element|
13
- case element
14
- when RDoc::Markup::Paragraph
15
- result << reflow( element.text )
16
- when RDoc::Markup::BlankLine
17
- result << ""
18
- when RDoc::Markup::Rule
19
- result << "-" * Bri.width
20
- when RDoc::Markup::Verbatim
21
- result << element.text
22
- when RDoc::Markup::Heading
23
- result << ' ' * element.level + element.text
24
- when RDoc::Markup::ListItem
25
- result << "#{element.label} #{build_description( element.parts ).join}"
26
- when RDoc::Markup::List
27
- case element.type
28
- when :NOTE, :LABEL
29
- result << "Note:"
30
- result << build_description( element.items ).join( "\n" )
31
- when :NUMBER
32
- result << "Numbered List:"
33
- result << build_description( element.items ).join( "\n" )
34
- when :BULLET
35
- #FIXME: Add bullet symbol to each item
36
- result << "Bulletet List:"
37
- result << build_description( element.items ).join( "\n" )
38
- else
39
- raise "Don't know how to handle list type #{element.type}: #{element.inspect}"
40
- end
41
- else
42
- raise "Don't know how to handle type #{element.class}: #{element.inspect}"
43
- end
44
- end
45
- result
46
- end
47
-
48
- def reflow( text, width = Bri.width )
49
- array_to_width( text.split( /\s+/ ), width, " ", 1 )
12
+ source.collect { |element| @@renderer.render element }
50
13
  end
51
14
  end
52
15
  end
@@ -8,7 +8,7 @@ module Bri
8
8
 
9
9
  def initialize( rdoc_method )
10
10
  @full_name = rdoc_method.full_name
11
- @call_syntaxes = rdoc_method.call_seq.split( "\n" ).
11
+ @call_syntaxes = rdoc_method.arglists.split( "\n" ).
12
12
  map { |e| " " + e }.
13
13
  join( "\n" ) + "\n" rescue ''
14
14
  @description_paragraphs = build_description( rdoc_method.comment.parts )
@@ -0,0 +1,108 @@
1
+ require 'strscan'
2
+
3
+ module Bri
4
+ class Renderer
5
+ INDENT = ' ' * 2
6
+
7
+ def initialize
8
+ end
9
+
10
+ def render( element, width = Bri.width )
11
+ text = extract_text( element, width )
12
+ styled_text = replace_markup( text )
13
+ element.kind_of?( RDoc::Markup::Verbatim ) ?
14
+ wrapped_text = styled_text :
15
+ wrapped_text = wrap_to_width( styled_text, width )
16
+ indent( wrapped_text )
17
+ end
18
+
19
+ def extract_text( element, width )
20
+ text = case element
21
+ when RDoc::Markup::Paragraph
22
+ element.text
23
+ when RDoc::Markup::BlankLine
24
+ ""
25
+ when RDoc::Markup::Rule
26
+ "-" * width
27
+ when RDoc::Markup::Verbatim
28
+ element.text
29
+ when RDoc::Markup::Heading
30
+ "<h>#{element.text}</h>"
31
+ when RDoc::Markup::ListItem
32
+ element.label.to_s + element.parts.collect { |part| extract_text part, width }.join
33
+ when RDoc::Markup::List
34
+ prefix = case element.type
35
+ when :NOTE, :LABEL then "Note:\n"
36
+ when :NUMBER then "Numbered List:\n"
37
+ when :BULLET then "Bulletet List:\n"
38
+ else
39
+ raise "Don't know how to handle list type #{element.type}: #{element.inspect}"
40
+ end
41
+ prefix + element.items.collect { |item| extract_text item, width }.join
42
+ else
43
+ raise "Don't know how to handle type #{element.class}: #{element.inspect}"
44
+ end
45
+ text + "\n"
46
+ end
47
+
48
+ def replace_markup( text )
49
+ text.gsub!( "<tt>", Term::ANSIColor::cyan )
50
+ text.gsub!( "</tt>", Term::ANSIColor::reset )
51
+
52
+ text.gsub!( "<code>", Term::ANSIColor::cyan )
53
+ text.gsub!( "</code>", Term::ANSIColor::reset )
54
+
55
+ text.gsub!( "<h>", Term::ANSIColor::green )
56
+ text.gsub!( "</h>", Term::ANSIColor::reset )
57
+
58
+ text.gsub!( /(^|\s)\+(.*?)\+/,
59
+ "\\1#{Term::ANSIColor::yellow}\\2#{Term::ANSIColor::reset}" )
60
+ text.gsub!( /(^|\s)_(.*?)_/,
61
+ "\\1#{Term::ANSIColor::yellow}\\2#{Term::ANSIColor::reset}" )
62
+ text
63
+ end
64
+
65
+ def printable_length( text )
66
+ embedded_ansi_codes = text.scan( Term::ANSIColor::COLORED_REGEXP )
67
+ if embedded_ansi_codes.empty?
68
+ text.length
69
+ else
70
+ text.length - embedded_ansi_codes.reduce( 0 ) { |total, code| code.length + total }
71
+ end
72
+ end
73
+
74
+ def wrap_to_width( styled_text, width )
75
+ styled_text.split( "\n" ).collect { |row| wrap_row row, width }.join
76
+ end
77
+
78
+ def wrap_row( physical_row, width )
79
+ output_text = ''
80
+ logical_row = ''
81
+ printable_row_length = 0
82
+
83
+ scanner = StringScanner.new( physical_row )
84
+
85
+ while( !scanner.eos? )
86
+ token = scanner.scan( /\S+/ ).to_s
87
+ printable_token_length = printable_length( token )
88
+
89
+ if printable_token_length + printable_row_length > width
90
+ output_text << logical_row << "\n"
91
+ logical_row = ''
92
+ printable_row_length = 0
93
+ end
94
+ logical_row << token
95
+ printable_row_length += printable_token_length
96
+
97
+ token = scanner.scan( /\s*/ ).to_s
98
+ logical_row << token
99
+ printable_row_length += token.length
100
+ end
101
+ output_text << logical_row << "\n"
102
+ end
103
+
104
+ def indent( text )
105
+ text.split( "\n" ).collect { |row| "#{INDENT}#{row}" }.join("\n" )
106
+ end
107
+ end
108
+ end
data/lib/bri/templates.rb CHANGED
@@ -13,9 +13,7 @@ module Bri
13
13
  <% if description_paragraphs.empty? %>
14
14
  (no description...)
15
15
  <% else %>
16
- <% description_paragraphs.each do |paragraph| %>
17
- <%= array_to_width( paragraph.split( /\s+/ ), Bri.width, " ") %>
18
- <% end %>
16
+ <%= description_paragraphs.join("\n" ) %>
19
17
  <% end %>
20
18
 
21
19
  <%= hrule %>
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 2
9
- version: 0.1.2
8
+ - 3
9
+ version: 0.1.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Sven Riedel
@@ -73,6 +73,7 @@ files:
73
73
  - lib/bri/search/class_method.rb
74
74
  - lib/bri/search/instance_method.rb
75
75
  - lib/bri/templates.rb
76
+ - lib/bri/renderer.rb
76
77
  - spec/spec_helper.rb
77
78
  - spec/lib/bri/mall_spec.rb
78
79
  - spec/lib/bri/matcher_spec.rb