-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmiddleman-swiftype-helper.rb
127 lines (103 loc) · 3.39 KB
/
middleman-swiftype-helper.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
require 'swiftype'
require 'nokogiri'
require 'digest'
class MiddlemanSwiftypeHelper
def initialize(plugin_options)
@options = plugin_options
@mm_instance = Middleman::Application.server.inst
end
def swiftype_document_type
"page"
end
def generate_swiftype_records
records = []
m_pages = @mm_instance.sitemap.resources.find_all{|p| @options.pages_selector.call(p) }
m_pages.each do |p|
external_id = Digest::MD5.hexdigest(p.url)
# optional selector for retrieving the page title
if @options.title_selector
title = @options.title_selector.call(@mm_instance, p)
else
title = p.metadata[:page]['title']
end
url = p.url
sections = []
body = ''
info = ''
image = ''
f = Nokogiri::HTML.fragment(p.render(:layout => false))
# optionally edit html
if @options.process_html
@options.process_html.call(f)
end
body = f.text
if @options.generate_sections
sections = @options.generate_sections.call(p)
end
# optionally generate extra info
if @options.generate_info
info = @options.generate_info.call(f)
end
# optional image
if @options.generate_image
image = @options.generate_image.call(p)
end
if @options.should_index
should_index = @options.should_index.call(p, title)
next unless should_index
end
fields = [
{:name => 'title', :value => title, :type => 'string'},
{:name => 'url', :value => url, :type => 'enum'},
{:name => 'body', :value => body, :type => 'string'},
{:name => 'info', :value => info, :type => 'string'}
]
if sections.length > 0
{:name => 'sections', :value => sections, :type => 'string'}
end
if image
fields << {:name => 'image', :value => image, :type => 'enum'}
end
records << {
:external_id => external_id,
:fields => fields
}
end
records
end
def push_to_swiftype(records)
# https://github.com/swiftype/swiftype-rb
::Swiftype.configure do |config|
config.api_key = @options.api_key
end
swiftype_client = ::Swiftype::Client.new
records.each do |record|
# https://swiftype.com/documentation/crawler#schema
# https://swiftype.com/documentation/meta_tags
url_field = record[:fields].find { |fields| fields[:name] == "url" }
@mm_instance.logger.info("Pushing contents of #{url_field[:value]} to swiftype")
#next
begin
swiftype_client.create_or_update_document(@options.engine_slug, swiftype_document_type, {
:external_id => record[:external_id],
:fields => record[:fields]
})
rescue ::Swiftype::NonExistentRecord
swiftype_client.create_document_type(@options.engine_slug, swiftype_document_type)
swiftype_client.create_or_update_document(@options.engine_slug, 'page', {
:external_id => record[:external_id],
:fields => record[:fields]
})
end
end
end
def generate_search_json
@mm_instance.logger.info("Generating search.json...")
File.open("./#{Middleman::Application.build_dir}/search.json", "w") do |f|
f.write("{\"documents\": ")
f.write(self.generate_swiftype_records.to_json)
f.write("}")
end
@mm_instance.logger.info("Finished generating search.json.")
end
end