nanoc-algolia 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +47 -0
  3. data/lib/nanoc-algolia.rb +72 -0
  4. metadata +73 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 870dce1c8736b04a2d8ab13d2510a73cb49a5e07
4
+ data.tar.gz: d4bad8dea5215ac2f73f7e73126dbde9b8e72b9b
5
+ SHA512:
6
+ metadata.gz: 7756a20f41a2de1130d71233d8ec45f34841d52b00220c0026ca39d22402c49212ffde1bc26f85710b1bb4e919768e6a77ac9e9a84143b23e71deb2a1556d9c2
7
+ data.tar.gz: 12e5e04a09accd688e37bad77e5b98cdb7b5b31c6b4220e9f2b36964a14747e86fbcb9e16150b895b0144c95016832b98da5f1fd9741b2ce3fb4f98e38ca6347
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # Nanoc algolia
2
+
3
+ Index an item in algolia
4
+
5
+ It use [nokogiri](http://nokogiri.org/) and algolia.
6
+
7
+ Note: It doesn't allow quering the index, it cannot works with nanoc. You should probably use the javascript integration.
8
+
9
+ ## Install
10
+
11
+ gem install nanoc-algolia
12
+
13
+ If you use bundler, add it to your Gemfile:
14
+
15
+ gem "nanoc-algolia", "~> 0.0.1"
16
+
17
+ ## Usage
18
+
19
+ Add to *lib/default.rb*:
20
+
21
+ ```ruby
22
+ require 'nanoc-algolia'
23
+ ```
24
+
25
+ Add to *config.yaml*:
26
+
27
+ algolia:
28
+ application_id: your_application_id
29
+ api_key: your_api_key
30
+ index: your_index_name
31
+
32
+ Add a filter at the compile time to use it:
33
+
34
+ ```ruby
35
+ compile '*' do
36
+ filter :algolia
37
+ filter :erb
38
+ layout 'default'
39
+ end
40
+ ```
41
+
42
+ ## License
43
+
44
+ (c) 2011 Pascal Widdershoven (https://github.com/PascalW/jekyll_indextank)
45
+ (c) 2015 Stormz
46
+
47
+ This code is free to use under the terms of the MIT license
@@ -0,0 +1,72 @@
1
+ # Derived from https://github.com/PascalW/jekyll_indextank
2
+ require 'algoliasearch'
3
+ require 'nokogiri'
4
+
5
+ class SearchFilter < Nanoc::Filter
6
+ identifier :search
7
+ type :text
8
+
9
+ def initialize(hash = {})
10
+ super
11
+
12
+ raise ArgumentError.new 'Missing algolia:application_id' unless @config[:algolia][:application_id]
13
+ raise ArgumentError.new 'Missing algolia:api_key' unless @config[:algolia][:api_key]
14
+ raise ArgumentError.new 'Missing algolia:index' unless @config[:algolia][:index]
15
+
16
+ @last_indexed_file = '.nanoc_algolia'
17
+
18
+ load_last_timestamp
19
+
20
+ Algolia.init application_id: @config[:algolia][:application_id],
21
+ api_key: @config[:algolia][:api_key]
22
+
23
+ @index = Algolia::Index.new(@config[:algolia][:index])
24
+ end
25
+
26
+ # Index all pages
27
+ # The main content from each page is extracted and indexed at algolia
28
+ # The id of each document will be the absolute url to the resource without domain name
29
+ 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
+ puts "Indexing page #{@item.identifier}"
36
+
37
+ page_text = extract_text(content)
38
+
39
+ @index.save_object(
40
+ {
41
+ text: => page_text,
42
+ title: => @item[:title] || item.identifier
43
+ }, item.identifer)
44
+ puts 'Indexed ' << item.identifier
45
+
46
+ @last_indexed = Time.now
47
+ write_last_indexed
48
+
49
+ content
50
+ end
51
+
52
+ def extract_text(content)
53
+ doc = Nokogiri::HTML(content)
54
+ doc.xpath('//*/text()').to_a.join(" ").gsub("\r"," ").gsub("\n"," ")
55
+ 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
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nanoc-algolia
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - François de Metz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: algoliasearch
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ description:
42
+ email: francois@2metz.fr
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files:
46
+ - README.md
47
+ files:
48
+ - README.md
49
+ - lib/nanoc-algolia.rb
50
+ homepage: https://github.com/stormz/nanoc-algolia
51
+ licenses: []
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 2.4.6
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Index items from nanoc site to algolia
73
+ test_files: []