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

Private Instance Methods

config() click to toggle source
# File lib/madness/search.rb, line 73
def config
  @config ||= Settings.instance
end
file_label(filename) click to toggle source
# File lib/madness/search.rb, line 65
def file_label(filename)
  filename
    .remove(%r{/(index|README)$})
    .split('/')
    .map(&:to_label)
    .join(' / ')
end
file_url(filename) click to toggle source
# File lib/madness/search.rb, line 77
def file_url(filename)
  filename.remove(%r{/(index|README)$})
end
index!() click to toggle source
# File lib/madness/search.rb, line 39
def index!
  results = {}

  Dir["#{@path}/**/#{config.dir_glob}"].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 56
def skip_index?(file)
  if file.end_with? 'README.md'
    dir = File.dirname file
    File.exist? "#{dir}/index.md"
  else
    false
  end
end