Class: Giblish::TextSearcher
- Inherits:
-
Object
- Object
- Giblish::TextSearcher
- Defined in:
- lib/giblish/search/textsearcher.rb
Overview
Provides text search capability for the given source repository.
Instance Method Summary collapse
-
#grep_tree(repo, search_params) ⇒ Object
result = { filepath => [{ line_no: nil, line: “” }] }.
-
#initialize(repo_cache) ⇒ TextSearcher
constructor
A new instance of TextSearcher.
-
#search(search_params) ⇒ Object
transform the output from grep_tree to an Array of hashes according to:.
-
#search_result(repo, grep_result, search_params) ⇒ Object
- returns
-
a hash described in the ‘search’ method doc.
Constructor Details
#initialize(repo_cache) ⇒ TextSearcher
Returns a new instance of TextSearcher.
258 259 260 |
# File 'lib/giblish/search/textsearcher.rb', line 258 def initialize(repo_cache) @repo_cache = repo_cache end |
Instance Method Details
#grep_tree(repo, search_params) ⇒ Object
result = {
filepath => [{
line_no: nil,
line: ""
}]
}
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/giblish/search/textsearcher.rb', line 290 def grep_tree(repo, search_params) result = Hash.new { |h, k| h[k] = [] } # handle case-sensitivity and input as regex pattern or string r_flags = search_params.consider_case? ? 0 : Regexp::IGNORECASE r_str = search_params.as_regexp? ? search_params.search_phrase : Regexp.escape(search_params.search_phrase) r = Regexp.new(r_str, r_flags) # find all matching lines in the src tree repo.src_tree.traverse_preorder do |level, node| next unless node.leaf? relative_path = node.relative_path_from(repo.src_tree) line_no = 0 node.data.src_lines.each do |line| line_no += 1 next if line.empty? || !r.match?(line) # replace match with an embedded rule that can # be styled result[relative_path] << { line_no: line_no, line: line.gsub(r, '[.red]##*_\0_*##') } end end result end |
#search(search_params) ⇒ Object
transform the output from grep_tree to an Array of hashes according to:
{Pathname(“subdir/file1.adoc”) => {
doc_title: "My doc!!",
sections: [{
url: URI("http://my.site.com/docs/repo1/file1.html#section_id_1"),
title: "Purpose",
lines: [
"this is the line with matching text",
"this is another line with matching text"
]
}]
}}
- search_params
-
a SearchParameters instance
276 277 278 279 280 281 282 |
# File 'lib/giblish/search/textsearcher.rb', line 276 def search(search_params) repo = @repo_cache.repo(search_params) grep_results = grep_tree(repo, search_params) search_result(repo, grep_results, search_params) end |
#search_result(repo, grep_result, search_params) ⇒ Object
- returns
-
a hash described in the ‘search’ method doc
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/giblish/search/textsearcher.rb', line 320 def search_result(repo, grep_result, search_params) result = Hash.new { |h, k| h[k] = [] } grep_result.each do |filepath, matches| db = repo.info(filepath) next if db.nil? sect_to_match = Hash.new { |h, k| h[k] = [] } matches.each do |match| s = repo.in_section(filepath, match[:line_no]) sect_to_match[s] << match end sections = sect_to_match.collect do |section, matches| { url: search_params.url(filepath, section[:id]), title: section[:title], lines: matches.collect { |match| match[:line].chomp } } end result[filepath] = { doc_title: db[:title], sections: sections } end result end |