forked from slashdotdash/jekyll-lunr-js-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_entry.rb
53 lines (42 loc) · 1.52 KB
/
search_entry.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
require 'nokogiri'
module Jekyll
class SearchEntry
def self.create(page_or_post, renderer)
return create_from_post(page_or_post, renderer) if page_or_post.is_a?(Jekyll::Post)
return create_from_page(page_or_post, renderer) if page_or_post.is_a?(Jekyll::Page)
raise 'Not supported'
end
def self.create_from_page(page, renderer)
title, url = extract_title_and_url(page)
body = renderer.render(page)
date = nil
categories = []
SearchEntry.new(title, url, date, categories, body)
end
def self.create_from_post(post, renderer)
title, url = extract_title_and_url(post)
body = renderer.render(post)
date = post.date
categories = post.categories
SearchEntry.new(title, url, date, categories, body)
end
def self.extract_title_and_url(item)
data = item.to_liquid
[ data['title'], data['url'] ]
end
attr_reader :title, :url, :date, :categories, :body
def initialize(title, url, date, categories, body)
@title, @url, @date, @categories, @body = title, url, date, categories, body
end
def strip_index_suffix_from_url!
@url.gsub!(/index\.html$/, '')
end
# remove anything that is in the stop words list from the text to be indexed
def strip_stopwords!(stopwords, min_length)
@body = @body.split.delete_if() do |x|
t = x.downcase.gsub(/[^a-z]/, '')
t.length < min_length || stopwords.include?(t)
end.join(' ')
end
end
end