/
search_index_resource.rb
58 lines (51 loc) · 1.48 KB
/
search_index_resource.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
require "sanitize"
module Middleman
module Sitemap
class SearchIndexResource < ::Middleman::Sitemap::Resource
def initialize(store, path)
super(store, path)
end
def template?
false
end
def get_source_file
path
end
def render(opts = {}, locs = {})
page_index = build_page_index
page_index.to_json
end
def build_page_index
index = []
pages = @app.sitemap.resources.find_all { |p| p.data.sort_order }
pages.each_with_index do |resource, id|
next if resource.data["index"] == false
item = {
:id => id,
:title => resource.data.title.to_s,
:url => resource.url,
:cat => resource.data.cat.to_s,
:inv => "",
:material => "",
:location => "",
:culture => "",
:contents => Sanitize.fragment(resource.render(:layout => false))
}
if resource.data.cat.is_a? Integer
entry = lookup_entry(resource.data.cat)
item[:title] = entry.title
item[:inv] = entry.inv
item[:material] = entry.material
item[:location] = entry.location
item[:culture] = entry.culture
end
index.push(item)
end
index
end
def lookup_entry(cat)
@app.data.catalogue.find { |entry| entry.cat == cat }
end
end
end
end