Top Level Namespace

Includes:
FastRI::Util::MagicHelp, WEBrick

Defined Under Namespace

Modules: WRI

Constant Summary collapse

MAX_CONTEXT_LINES =
20

Instance Method Summary collapse

Instance Method Details

#context_wrap(text, width) ⇒ Object



177
178
179
180
# File 'lib/wri/fastri_service.rb', line 177

def context_wrap(text, width)
  "... " + 
  text.gsub(/(.{1,#{width-4}})( +|$\n?)|(.{1,#{width-4}})/, "\\1\\3\n").chomp
end

#display_fulltext_search_results(results, gem_dir_info = FastRI::Util.gem_directories_unique, width = 78) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/wri/fastri_service.rb', line 182

def display_fulltext_search_results(results, gem_dir_info = FastRI::Util.gem_directories_unique,
                                    width = 78)
  return if results.empty?
  path = File.expand_path(results[0].path)
  gem_name, version, gem_path = FastRI::Util.gem_info_for_path(path, gem_dir_info)
  if gem_name
    rel_path = path[/#{Regexp.escape(gem_path)}\/(.*)/, 1]
    if rel_path
      entry_name = FastRI::Util.gem_relpath_to_full_name(rel_path)
    end
    puts "Found in #{gem_name} #{version}  #{entry_name}"
  else
    rdoc_system_path = File.expand_path(RI::Paths::SYSDIR)
    if path.index(rdoc_system_path)
      rel_path = path[/#{Regexp.escape(rdoc_system_path)}\/(.*)/, 1]
      puts "Found in system  #{FastRI::Util.gem_relpath_to_full_name(rel_path)}"
    else
      puts "Found in #{path}:"
    end
  end
  text = results.map do |result|
    context = result.context(120)
    from = (context.rindex("\n", context.index(result.query)) || -1) + 1
    to   = (context.index("\n", context.index(result.query)) || 0) - 1
    context_wrap(context[from..to], width)
  end
  puts
  puts text.uniq[0...MAX_CONTEXT_LINES]
  puts
end

#perform_fulltext_search(options) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/wri/fastri_service.rb', line 213

def perform_fulltext_search(options)
  fulltext = File.join(options[:full_text_dir], "full_text.dat")
  suffixes = File.join(options[:full_text_dir], "suffixes.dat")
  begin
    index = FastRI::FullTextIndex.new_from_filenames(fulltext, suffixes)
  rescue Exception
    puts "Couldn't open the full-text index:\n\#{fulltext}\n\#{suffixes}\n\nThe index needs to be rebuilt with\n   fastri-server -B\n"
    exit(-1)
  end
  gem_dir_info = FastRI::Util.gem_directories_unique
  match_sets = ARGV.map do |query|
    result = index.lookup(query)
    if result
      index.next_matches(result) + [result]
    else
      []
    end
  end
  path_map = Hash.new{|h,k| h[k] = []}
  match_sets.each{|matches| matches.each{|m| path_map[m.path] << m} }
  paths = match_sets[1..-1].inject(match_sets[0].map{|x| x.path}.uniq) do |s,x|
    s & x.map{|y| y.path}.uniq
  end
  if paths.empty?
    puts "nil"
  else
    puts "#{paths.size} hits"
    paths.sort_by{|path| 1.0 * -path_map[path].size / path_map[path].first.[:size] ** 0.5}.map do |path|
      puts "=" * options[:width]
      puts 1.0 * path_map[path].size / path_map[path].first.[:size] ** 0.5
      display_fulltext_search_results(path_map[path], gem_dir_info, options[:width])
    end
  end
  
  exit 0
end