nesta-plugin-search 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +16 -0
- data/README.md +80 -0
- data/Rakefile +2 -0
- data/lib/nesta-plugin-search.rb +3 -0
- data/lib/nesta-plugin-search/init.rb +83 -0
- data/lib/nesta-plugin-search/version.rb +7 -0
- data/nesta-plugin-search.gemspec +21 -0
- metadata +71 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/README.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
README
|
2
|
+
======
|
3
|
+
|
4
|
+
nesta-plugin-search is a plugin for the Nesta CMS use
|
5
|
+
[Ferret](https://github.com/dbalmain/ferret) to index and search your site.
|
6
|
+
|
7
|
+
Installation
|
8
|
+
------------
|
9
|
+
|
10
|
+
To install it as a gem add nesta-search to the `Gemfile` in your Nesta
|
11
|
+
project, and then re-run `bundle`:
|
12
|
+
|
13
|
+
$ echo "gem 'nesta-plugin-search'" >> Gemfile
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Text Search Usage
|
17
|
+
-----------------
|
18
|
+
|
19
|
+
Create a `/search` page -- `content/pages/search.haml` -- here's an example:
|
20
|
+
|
21
|
+
%h1 Search Results
|
22
|
+
- if (results = search_results( search_index, params[:q] )) && !results.empty?
|
23
|
+
%section.articles= article_summaries(results)
|
24
|
+
- else
|
25
|
+
%h3 No articles found!
|
26
|
+
|
27
|
+
Add a searchbox to your layout, something like:
|
28
|
+
|
29
|
+
%form{:action => "/search"}
|
30
|
+
%input{:type => "text", :name => "q", :value => "#{params[:q]||''}"}
|
31
|
+
|
32
|
+
|
33
|
+
Text Search Configuration
|
34
|
+
-------------------------
|
35
|
+
|
36
|
+
Set `search_index_timeout` to the number of seconds you want your default indexes to
|
37
|
+
be stored for. By default, this is `false`, which never expires the index. It should
|
38
|
+
be noted that reindexing is slow.
|
39
|
+
|
40
|
+
Set `search_ignore_list` to the abspath of the pages you want to be excluded from the
|
41
|
+
search index. "/search" is forcefully excluded always and "/" is the default.
|
42
|
+
|
43
|
+
Example:
|
44
|
+
|
45
|
+
search_index_timeout: 3600
|
46
|
+
search_ignore_list:
|
47
|
+
- /
|
48
|
+
- /foo
|
49
|
+
- /foo/bar
|
50
|
+
|
51
|
+
> IMPORTANT NOTE: If you're not using "/search" as your search page, please add it to
|
52
|
+
> this list or bad things may happen.
|
53
|
+
|
54
|
+
Advanced Usage Notes
|
55
|
+
--------------------
|
56
|
+
|
57
|
+
Provided Index Helpers:
|
58
|
+
|
59
|
+
- `search_index` -- an index of all pages and articles.
|
60
|
+
- `article_index` -- an index of articles only.
|
61
|
+
|
62
|
+
Creating your own index:
|
63
|
+
|
64
|
+
- my_index = Nesta::Plugin::Search::Index.new( :my_find_method ).index
|
65
|
+
%h1 Search Results
|
66
|
+
- if (results = search_results( my_index, params[:q] )) && !results.empty?
|
67
|
+
%section.articles= article_summaries(results)
|
68
|
+
- else
|
69
|
+
%h3 No articles found!
|
70
|
+
|
71
|
+
If you want to pregenerate your cache to disk and load it that way, it would be something
|
72
|
+
like this:
|
73
|
+
|
74
|
+
- existing_index = Ferret::Index::Index.new(:path => "/path/to/existing/index")
|
75
|
+
%h1 Search Results
|
76
|
+
- if (results = search_results( existing_index, params[:q] )) && !results.empty?
|
77
|
+
%section.articles= article_summaries(results)
|
78
|
+
- else
|
79
|
+
%h3 No articles found!
|
80
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
module Nesta
|
2
|
+
class App
|
3
|
+
helpers do
|
4
|
+
def search_index
|
5
|
+
$search_index ||= Nesta::Plugin::Search::Index.new(:find_all, Nesta::Config.search_index_timeout).index
|
6
|
+
end
|
7
|
+
|
8
|
+
def article_index
|
9
|
+
$article_index ||= Nesta::Plugin::Search::Index.new(:find_articles, Nesta::Config.search_index_timeout).index
|
10
|
+
end
|
11
|
+
|
12
|
+
def search_results index, term
|
13
|
+
Nesta::Plugin::Search.results( index, term ) || []
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class Config
|
19
|
+
@settings += %w[ search_index_timeout search_ignore_list ]
|
20
|
+
|
21
|
+
def self.search_index_timeout
|
22
|
+
from_yaml("search_index_timeout")||false
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.search_ignore_list
|
26
|
+
(from_yaml("search_ignore_list")||["/"]) + %w[ /search ]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
module Plugin
|
31
|
+
module Search
|
32
|
+
|
33
|
+
def self.results index, term
|
34
|
+
results = []
|
35
|
+
index.search_each(term) do |key, score|
|
36
|
+
results.push Nesta::Page.find_by_path(index[key][:href])# rescue nil
|
37
|
+
end
|
38
|
+
results
|
39
|
+
end
|
40
|
+
|
41
|
+
class Index
|
42
|
+
attr_accessor :index_method
|
43
|
+
|
44
|
+
# Currently imethod is expected to be:
|
45
|
+
# - :find_all
|
46
|
+
# - :find_articles
|
47
|
+
#
|
48
|
+
# Initializing with a timeout = false will
|
49
|
+
# cause index to not expire without a server
|
50
|
+
# restart.
|
51
|
+
def initialize(imethod, timeout=false) # 12 hours default
|
52
|
+
@index_method = imethod.to_sym
|
53
|
+
if timeout
|
54
|
+
@timeout = timeout
|
55
|
+
@expire_at = Time.now+timeout
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def index
|
60
|
+
# index and return if index doesn't exist or is empty
|
61
|
+
return @index = reindex unless @index && @index.size > 0
|
62
|
+
|
63
|
+
# return existing index if current index isn't expired or expired_at isn't set
|
64
|
+
return @index if @expire_at && @expire_at > Time.now
|
65
|
+
|
66
|
+
# if all else fails, index and return
|
67
|
+
return @index = reindex
|
68
|
+
end
|
69
|
+
|
70
|
+
def reindex
|
71
|
+
@index = Ferret::Index::Index.new
|
72
|
+
|
73
|
+
Nesta::Page.send(@index_method).each do |item|
|
74
|
+
unless Nesta::Config.search_ignore_list.include?(item.abspath)
|
75
|
+
@index << {:heading => item.heading, :href => item.abspath, :summary => item.summary, :body => item.body}
|
76
|
+
end
|
77
|
+
end
|
78
|
+
@index
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "nesta-plugin-search/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "nesta-plugin-search"
|
7
|
+
s.version = Nesta::Plugin::Search::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Joshua Mervine"]
|
10
|
+
s.email = ["joshua@mervine.net"]
|
11
|
+
s.homepage = "http://github.com/jmervine/search-plugin-nesta"
|
12
|
+
s.summary = %q{Search plugin for Nesta CMS}
|
13
|
+
s.description = %q{Uses Ferret to search your Nesta site. Based on https://github.com/gma/nesta-search.}
|
14
|
+
|
15
|
+
s.add_dependency 'ferret', '=0.11.8.5'
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nesta-plugin-search
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Joshua Mervine
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: ferret
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - '='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.11.8.5
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - '='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.11.8.5
|
30
|
+
description: Uses Ferret to search your Nesta site. Based on https://github.com/gma/nesta-search.
|
31
|
+
email:
|
32
|
+
- joshua@mervine.net
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- Gemfile.lock
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- lib/nesta-plugin-search.rb
|
43
|
+
- lib/nesta-plugin-search/init.rb
|
44
|
+
- lib/nesta-plugin-search/version.rb
|
45
|
+
- nesta-plugin-search.gemspec
|
46
|
+
homepage: http://github.com/jmervine/search-plugin-nesta
|
47
|
+
licenses: []
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.8.24
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Search plugin for Nesta CMS
|
70
|
+
test_files: []
|
71
|
+
has_rdoc:
|