Class: Madness::Search

Inherits:
Object
  • Object
show all
Includes:
ServerHelper
Defined in:
lib/madness/search.rb

Instance Method Summary collapse

Methods included from ServerHelper

#disallowed_static?, #docroot, #find_static_file, #log, #theme

Constructor Details

#initialize(path = nil) ⇒ Search

Returns a new instance of Search.



8
9
10
# File 'lib/madness/search.rb', line 8

def initialize(path = nil)
  @path = path || docroot
end

Instance Method Details

#indexObject



12
13
14
# File 'lib/madness/search.rb', line 12

def index
  @index ||= index!
end

#search(query) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/madness/search.rb', line 16

def search(query)
  query = query.downcase
  words = Shellwords.split query
  word_count = words.count
  result = {}
  return result if words.empty?

  index.each do |file, content|
    file = file.remove("#{@path}/").sub(/.md$/, '')
    url = file_url file
    label = file_label file
    found = 0
    words.each { |word| found += 1 if content.include? word }
    next unless found == word_count

    result[label] = url
  end

  result
end