nanoc-algolia 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/nanoc-algolia.rb +5 -32
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53f5e8a82ea8ef18ea785ae83047d3b3a2077105
|
4
|
+
data.tar.gz: 7723efa1b08bcd3aec24512f5698f0a4bf2446fa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 69d5db3145174ab4194a994d3bfd5eeac74a24d342d7cc4f9dff20ddab70dbaa9572a5558cb9ea89bec026c520d4b17636c221451d4ee236a2563799387e9596
|
7
|
+
data.tar.gz: 2b884a9555983a6cb454cb83762ac74840673838ded0220854ecae5fb07f88257b50ff87699974b738680f5a705018e5f2bbeae51c2c63ce7e5ea0b0528551d5
|
data/lib/nanoc-algolia.rb
CHANGED
@@ -3,20 +3,17 @@ require 'algoliasearch'
|
|
3
3
|
require 'nokogiri'
|
4
4
|
|
5
5
|
class SearchFilter < Nanoc::Filter
|
6
|
-
identifier :
|
6
|
+
identifier :algolia
|
7
7
|
type :text
|
8
8
|
|
9
9
|
def initialize(hash = {})
|
10
10
|
super
|
11
11
|
|
12
|
+
raise ArgumentError.new 'Missing algolia config' unless @config[:algolia]
|
12
13
|
raise ArgumentError.new 'Missing algolia:application_id' unless @config[:algolia][:application_id]
|
13
14
|
raise ArgumentError.new 'Missing algolia:api_key' unless @config[:algolia][:api_key]
|
14
15
|
raise ArgumentError.new 'Missing algolia:index' unless @config[:algolia][:index]
|
15
16
|
|
16
|
-
@last_indexed_file = '.nanoc_algolia'
|
17
|
-
|
18
|
-
load_last_timestamp
|
19
|
-
|
20
17
|
Algolia.init application_id: @config[:algolia][:application_id],
|
21
18
|
api_key: @config[:algolia][:api_key]
|
22
19
|
|
@@ -27,25 +24,17 @@ class SearchFilter < Nanoc::Filter
|
|
27
24
|
# The main content from each page is extracted and indexed at algolia
|
28
25
|
# The id of each document will be the absolute url to the resource without domain name
|
29
26
|
def run(content, params={})
|
30
|
-
# only process item that are changed since last regeneration
|
31
|
-
if (!@last_indexed.nil? && @last_indexed > item.mtime)
|
32
|
-
return content
|
33
|
-
end
|
34
|
-
|
35
27
|
puts "Indexing page #{@item.identifier}"
|
36
28
|
|
37
29
|
page_text = extract_text(content)
|
38
30
|
|
39
31
|
@index.save_object(
|
40
32
|
{
|
41
|
-
text:
|
42
|
-
title:
|
43
|
-
}, item.
|
33
|
+
text: page_text,
|
34
|
+
title: @item[:title] || item.identifier
|
35
|
+
}, item.identifier)
|
44
36
|
puts 'Indexed ' << item.identifier
|
45
37
|
|
46
|
-
@last_indexed = Time.now
|
47
|
-
write_last_indexed
|
48
|
-
|
49
38
|
content
|
50
39
|
end
|
51
40
|
|
@@ -53,20 +42,4 @@ class SearchFilter < Nanoc::Filter
|
|
53
42
|
doc = Nokogiri::HTML(content)
|
54
43
|
doc.xpath('//*/text()').to_a.join(" ").gsub("\r"," ").gsub("\n"," ")
|
55
44
|
end
|
56
|
-
|
57
|
-
def write_last_indexed
|
58
|
-
begin
|
59
|
-
File.open(@last_indexed_file, 'w') {|f| Marshal.dump(@last_indexed, f)}
|
60
|
-
rescue
|
61
|
-
puts 'WARNING: cannot write indexed timestamps file.'
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
def load_last_timestamp
|
66
|
-
begin
|
67
|
-
@last_indexed = File.open(@last_indexed_file, "rb") {|f| Marshal.load(f)}
|
68
|
-
rescue
|
69
|
-
@last_indexed = nil
|
70
|
-
end
|
71
|
-
end
|
72
45
|
end
|