class Madness::Search
Public Class Methods
new(path=nil)
click to toggle source
# File lib/madness/search.rb, line 8 def initialize(path=nil) @path = path || docroot end
Public Instance Methods
index()
click to toggle source
# File lib/madness/search.rb, line 12 def index @index ||= index! end
search(query)
click to toggle source
# 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
Private Instance Methods
config()
click to toggle source
# File lib/madness/search.rb, line 71 def config @config ||= Settings.instance end
file_label(filename)
click to toggle source
# File lib/madness/search.rb, line 63 def file_label(filename) filename .remove(/\/(index|README)$/) .split('/') .map { |i| i.to_label } .join(' / ') end
file_url(filename)
click to toggle source
# File lib/madness/search.rb, line 75 def file_url(filename) filename.remove(/\/(index|README)$/) end
index!()
click to toggle source
# File lib/madness/search.rb, line 38 def index! results = {} Dir["#{@path}/**/#{config.dir_glob}"].sort.each do |file| next if skip_index? file filename = file_url(file.sub("#{@path}/", '')).downcase index_content = File.extname(file) == '.md' content = index_content ? File.read(file).downcase : '' results[file] = "#{filename} #{content}" end results end
skip_index?(file)
click to toggle source
We are going to avoid indexing of README.md when there is also an index.md in the same directory, to keep behavior consistent with the display logic
# File lib/madness/search.rb, line 54 def skip_index?(file) if file.end_with? 'README.md' dir = File.dirname file File.exist? "#{dir}/index.md" else false end end