-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
search.rb
77 lines (61 loc) · 1.79 KB
/
search.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
require 'ferret'
module Madness
class Search
include ServerHelper
include Ferret
include Ferret::Index
using StringRefinements
def initialize(path=nil)
@path = path || docroot
end
def has_index?
Dir.exist? index_dir
end
def build_index
Dir.mkdir index_dir unless Dir.exist? index_dir
index = Index.new path: index_dir, create: true
Dir["#{@path}/**/*.md"].each do |file|
index << { file: file, content: searchable_content(file) }
end
index.optimize()
index.close()
end
def search(query)
index = Index.new path: index_dir
results = []
index.search_each(query, limit: 20) do |doc_id, score|
filename = index[doc_id][:file].sub("#{@path}/", '')[0...-3]
highlights = index.highlight "content:(#{query.tr(' ',' OR ')}) ", doc_id, field: :content,
pre_tag: "<strong>", post_tag: "</strong>",
excerpt_length: 100
results << {
score: score,
file: filename,
label: file_label(filename),
highlights: highlights
}
end
index.close()
results
end
def remove_index_dir
return unless Dir.exist? index_dir
FileUtils.rm_r index_dir
end
def index_dir
"#{@path}/_index"
end
private
# This is poor-mans markdown strip.
# Convert to HTML, strip tags and return plain text suitable to act as
# the content for the search index.
def searchable_content(file)
content = File.read file
content = CommonMarker.render_html content
content.gsub(/<\/?[^>]*>/, "").gsub("\n", " ")
end
def file_label(filename)
filename.split('/').map { |i| i.to_label }.join(' / ')
end
end
end