middleman-search 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e7801c0f17626f09057c99a551a7cd9c1e462dc1
4
- data.tar.gz: b7b1e6fb3098d533c073d25923916aa3d42181d9
3
+ metadata.gz: 4fc0d3fb638aed4295e5f062bee990b4dd5b17fa
4
+ data.tar.gz: b9bce741409fcfba231c0de4db86c85f4bad9077
5
5
  SHA512:
6
- metadata.gz: 254aeb5de6fb9687de60ebb47e7a01f6d49b6f01dbea6cc5b8fe4b191d52ee86bf83d07cc3cc105b2ea95b7ad4a12eb3e48c7aa6199a2422adf51aed8a11c7de
7
- data.tar.gz: 6c33807e8c13e227b71f4a4bf76c7330800b3676af08812f2eb241a52136a96089a5dbbf1a75bc481cea969df4b98fd24bc0400c45274a238b9834c0c3418833
6
+ metadata.gz: 6e07b8c657510273d73c1a815e1bd34cb50398ed173cfe9b1be48edac9b31c48ab6985241a9df50eb781d0bc0c5788f4af94eadfcede20eeeecff926c70e68e1
7
+ data.tar.gz: 39511db8a4ad2151f58277796e0c545c40e5baf211b53aec91378ddac34d71ab55e3e5390cced788f471d745b43c62c5baf223be70b67572e894ed28c44e0cb9
data/README.md CHANGED
@@ -26,6 +26,10 @@ activate :search do |search|
26
26
  search.resources = ['blog/', 'index.html', 'contactus/index.html']
27
27
 
28
28
  search.index_path = 'search/lunr-index.json' # defaults to `search.json`
29
+
30
+ search.lunr_dirs = ['source/vendor/lunr-custom/'] # optional alternate paths where to look for lunr js files
31
+
32
+ search.language = 'es' # defaults to 'en'
29
33
 
30
34
  search.fields = {
31
35
  title: {boost: 100, store: true, required: true},
@@ -49,6 +53,12 @@ All fields values are retrieved from the resource `data` (i.e. its frontmatter),
49
53
  - `url` which is the actual resource url
50
54
  - `content` the text extracted from the rendered resource, without including its layout
51
55
 
56
+ ### i18n
57
+
58
+ This gem includes assets for alternate languages as provided by [MihaiValentin/lunr-languages](https://github.com/MihaiValentin/lunr-languages). Please refer to that repository for a list of the languages available.
59
+
60
+ If you want to work with a language that is not included, set up a `lunr.yourlang.js` file in a folder in your project, and add that folder to `lunr_dirs` so the gem knows where to look for it.
61
+
52
62
  ### Manual index manipulation
53
63
 
54
64
  You can fully customise the content to be indexed and stored per resource by defining a `before_index` callback:
@@ -146,4 +156,5 @@ A big thank you to:
146
156
  - [Octo-Labs](https://github.com/Octo-Labs)'s [jagthedrummer](https://github.com/jagthedrummer) for his [`middleman-alias`](https://github.com/Octo-Labs/middleman-alias) extension, in which we based for developing this one.
147
157
  - [jnovos](https://github.com/jnovos) and [256dpi](https://github.com/256dpi), for their [`middleman-lunrjs`](https://github.com/jnovos/middleman-lunrjs) and [`middleman-lunr`](https://github.com/256dpi/middleman-lunr) extensions, which served as inspirations for making this one.
148
158
  - [olivernn](https://github.com/olivernn) and all [`lunr.js`](http://lunrjs.com/) [contributors](https://github.com/olivernn/lunr.js/graphs/contributors)
159
+ - [MihaiValentin](https://github.com/MihaiValentin) for the support for 10+ languages in [lunr-languages](https://github.com/MihaiValentin/lunr-languages).
149
160
  - [The Middleman](https://middlemanapp.com/) [team](https://github.com/orgs/middleman/people) and [contributors](https://github.com/middleman/middleman/graphs/contributors)
@@ -9,6 +9,8 @@ module Middleman
9
9
  option :index_path, 'search.json', 'Index file path'
10
10
  option :pipeline, {}, 'Javascript pipeline functions to use in lunr index'
11
11
  option :cache, false, 'Avoid the cache to be rebuilt on every request in development mode'
12
+ option :language, 'en', 'Language code ("es", "fr") to use when indexing site\'s content'
13
+ option :lunr_dirs, [], 'Directories in which to look for custom lunr.js files'
12
14
 
13
15
  def manipulate_resource_list(resources)
14
16
  resources.push Middleman::Sitemap::SearchIndexResource.new(@app.sitemap, @options[:index_path], @options)
@@ -9,6 +9,8 @@ module Middleman
9
9
  @callback = options[:before_index]
10
10
  @pipeline = options[:pipeline]
11
11
  @cache_index = options[:cache]
12
+ @language = options[:language]
13
+ @lunr_dirs = options[:lunr_dirs] + [File.expand_path("../../../vendor/assets/javascripts/", __FILE__)]
12
14
  super(store, path)
13
15
  end
14
16
 
@@ -31,7 +33,14 @@ module Middleman
31
33
  def build_index
32
34
  # Build js context
33
35
  context = V8::Context.new
34
- context.load(File.expand_path('../../../vendor/assets/javascripts/lunr.min.js', __FILE__))
36
+ context.load(lunr_resource('lunr.js'))
37
+
38
+ if @language != 'en' # English is the default
39
+ context.load(lunr_resource("lunr.stemmer.support.js"))
40
+ context.load(lunr_resource("lunr.#{@language}.js"))
41
+ lunr_lang = context.eval("lunr.#{@language}")
42
+ end
43
+
35
44
  context.eval('lunr.Index.prototype.indexJson = function () {return JSON.stringify(this.toJSON());}')
36
45
 
37
46
  # Register pipeline functions
@@ -54,6 +63,7 @@ module Middleman
54
63
  end
55
64
 
56
65
  # Define fields with boost
66
+ this.use(lunr_lang) if @language
57
67
  @fields.each do |field, opts|
58
68
  next if opts[:index] == false
59
69
  this.field(field, {:boost => opts[:boost]})
@@ -118,6 +128,20 @@ module Middleman
118
128
  value ? Array(value).compact.join(" ") : nil
119
129
  end
120
130
  end
131
+
132
+ private
133
+
134
+ def minified_path(resource_name)
135
+ return resource_name if resource_name.end_with? '.min.js'
136
+ return resource_name unless resource_name.end_with? '.js'
137
+ resource_name.sub(/(.*)\.js$/,'\1.min.js')
138
+ end
139
+
140
+ def lunr_resource(resource_name)
141
+ @lunr_dirs.flat_map do |dir|
142
+ [File.join(dir, minified_path(resource_name)), File.join(dir, resource_name)]
143
+ end.detect { |file| File.exists? file } or raise "Couldn't find #{resource_name} nor #{minified_path(resource_name)} in #{@lunr_dirs.map {|dir| File.absolute_path dir }.join File::PATH_SEPARATOR}"
144
+ end
121
145
  end
122
146
  end
123
147
  end
@@ -1,3 +1,3 @@
1
1
  module MiddlemanSearch
2
- VERSION = "0.8.0"
2
+ VERSION = "0.9.0"
3
3
  end
@@ -0,0 +1,273 @@
1
+ /*!
2
+ * Lunr languages, `Danish` language
3
+ * https://github.com/MihaiValentin/lunr-languages
4
+ *
5
+ * Copyright 2014, Mihai Valentin
6
+ * http://www.mozilla.org/MPL/
7
+ */
8
+ /*!
9
+ * based on
10
+ * Snowball JavaScript Library v0.3
11
+ * http://code.google.com/p/urim/
12
+ * http://snowball.tartarus.org/
13
+ *
14
+ * Copyright 2010, Oleg Mazko
15
+ * http://www.mozilla.org/MPL/
16
+ */
17
+
18
+ /**
19
+ * export the module via AMD, CommonJS or as a browser global
20
+ * Export code from https://github.com/umdjs/umd/blob/master/returnExports.js
21
+ */
22
+ ;
23
+ (function(root, factory) {
24
+ if (typeof define === 'function' && define.amd) {
25
+ // AMD. Register as an anonymous module.
26
+ define(factory)
27
+ } else if (typeof exports === 'object') {
28
+ /**
29
+ * Node. Does not work with strict CommonJS, but
30
+ * only CommonJS-like environments that support module.exports,
31
+ * like Node.
32
+ */
33
+ module.exports = factory()
34
+ } else {
35
+ // Browser globals (root is window)
36
+ factory()(root.lunr);
37
+ }
38
+ }(this, function() {
39
+ /**
40
+ * Just return a value to define the module export.
41
+ * This example returns an object, but the module
42
+ * can return a function as the exported value.
43
+ */
44
+ return function(lunr) {
45
+ /* throw error if lunr is not yet included */
46
+ if ('undefined' === typeof lunr) {
47
+ throw new Error('Lunr is not present. Please include / require Lunr before this script.');
48
+ }
49
+
50
+ /* throw error if lunr stemmer support is not yet included */
51
+ if ('undefined' === typeof lunr.stemmerSupport) {
52
+ throw new Error('Lunr stemmer support is not present. Please include / require Lunr stemmer support before this script.');
53
+ }
54
+
55
+ /* register specific locale function */
56
+ lunr.da = function() {
57
+ this.pipeline.reset();
58
+ this.pipeline.add(
59
+ lunr.da.stopWordFilter,
60
+ lunr.da.stemmer
61
+ );
62
+ };
63
+
64
+ /* lunr stemmer function */
65
+ lunr.da.stemmer = (function() {
66
+ /* create the wrapped stemmer object */
67
+ var Among = lunr.stemmerSupport.Among,
68
+ SnowballProgram = lunr.stemmerSupport.SnowballProgram,
69
+ st = new function DanishStemmer() {
70
+ var a_0 = [new Among("hed", -1, 1), new Among("ethed", 0, 1),
71
+ new Among("ered", -1, 1), new Among("e", -1, 1),
72
+ new Among("erede", 3, 1), new Among("ende", 3, 1),
73
+ new Among("erende", 5, 1), new Among("ene", 3, 1),
74
+ new Among("erne", 3, 1), new Among("ere", 3, 1),
75
+ new Among("en", -1, 1), new Among("heden", 10, 1),
76
+ new Among("eren", 10, 1), new Among("er", -1, 1),
77
+ new Among("heder", 13, 1), new Among("erer", 13, 1),
78
+ new Among("s", -1, 2), new Among("heds", 16, 1),
79
+ new Among("es", 16, 1), new Among("endes", 18, 1),
80
+ new Among("erendes", 19, 1), new Among("enes", 18, 1),
81
+ new Among("ernes", 18, 1), new Among("eres", 18, 1),
82
+ new Among("ens", 16, 1), new Among("hedens", 24, 1),
83
+ new Among("erens", 24, 1), new Among("ers", 16, 1),
84
+ new Among("ets", 16, 1), new Among("erets", 28, 1),
85
+ new Among("et", -1, 1), new Among("eret", 30, 1)
86
+ ],
87
+ a_1 = [
88
+ new Among("gd", -1, -1), new Among("dt", -1, -1),
89
+ new Among("gt", -1, -1), new Among("kt", -1, -1)
90
+ ],
91
+ a_2 = [
92
+ new Among("ig", -1, 1), new Among("lig", 0, 1),
93
+ new Among("elig", 1, 1), new Among("els", -1, 1),
94
+ new Among("l\u00F8st", -1, 2)
95
+ ],
96
+ g_v = [17, 65, 16, 1, 0, 0, 0, 0,
97
+ 0, 0, 0, 0, 0, 0, 0, 0, 48, 0, 128
98
+ ],
99
+ g_s_ending = [239, 254, 42, 3,
100
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16
101
+ ],
102
+ I_x, I_p1, S_ch, sbp = new SnowballProgram();
103
+ this.setCurrent = function(word) {
104
+ sbp.setCurrent(word);
105
+ };
106
+ this.getCurrent = function() {
107
+ return sbp.getCurrent();
108
+ };
109
+
110
+ function r_mark_regions() {
111
+ var v_1, c = sbp.cursor + 3;
112
+ I_p1 = sbp.limit;
113
+ if (0 <= c && c <= sbp.limit) {
114
+ I_x = c;
115
+ while (true) {
116
+ v_1 = sbp.cursor;
117
+ if (sbp.in_grouping(g_v, 97, 248)) {
118
+ sbp.cursor = v_1;
119
+ break;
120
+ }
121
+ sbp.cursor = v_1;
122
+ if (v_1 >= sbp.limit)
123
+ return;
124
+ sbp.cursor++;
125
+ }
126
+ while (!sbp.out_grouping(g_v, 97, 248)) {
127
+ if (sbp.cursor >= sbp.limit)
128
+ return;
129
+ sbp.cursor++;
130
+ }
131
+ I_p1 = sbp.cursor;
132
+ if (I_p1 < I_x)
133
+ I_p1 = I_x;
134
+ }
135
+ }
136
+
137
+ function r_main_suffix() {
138
+ var among_var, v_1;
139
+ if (sbp.cursor >= I_p1) {
140
+ v_1 = sbp.limit_backward;
141
+ sbp.limit_backward = I_p1;
142
+ sbp.ket = sbp.cursor;
143
+ among_var = sbp.find_among_b(a_0, 32);
144
+ sbp.limit_backward = v_1;
145
+ if (among_var) {
146
+ sbp.bra = sbp.cursor;
147
+ switch (among_var) {
148
+ case 1:
149
+ sbp.slice_del();
150
+ break;
151
+ case 2:
152
+ if (sbp.in_grouping_b(g_s_ending, 97, 229))
153
+ sbp.slice_del();
154
+ break;
155
+ }
156
+ }
157
+ }
158
+ }
159
+
160
+ function r_consonant_pair() {
161
+ var v_1 = sbp.limit - sbp.cursor,
162
+ v_2;
163
+ if (sbp.cursor >= I_p1) {
164
+ v_2 = sbp.limit_backward;
165
+ sbp.limit_backward = I_p1;
166
+ sbp.ket = sbp.cursor;
167
+ if (sbp.find_among_b(a_1, 4)) {
168
+ sbp.bra = sbp.cursor;
169
+ sbp.limit_backward = v_2;
170
+ sbp.cursor = sbp.limit - v_1;
171
+ if (sbp.cursor > sbp.limit_backward) {
172
+ sbp.cursor--;
173
+ sbp.bra = sbp.cursor;
174
+ sbp.slice_del();
175
+ }
176
+ } else
177
+ sbp.limit_backward = v_2;
178
+ }
179
+ }
180
+
181
+ function r_other_suffix() {
182
+ var among_var, v_1 = sbp.limit - sbp.cursor,
183
+ v_2, v_3;
184
+ sbp.ket = sbp.cursor;
185
+ if (sbp.eq_s_b(2, "st")) {
186
+ sbp.bra = sbp.cursor;
187
+ if (sbp.eq_s_b(2, "ig"))
188
+ sbp.slice_del();
189
+ }
190
+ sbp.cursor = sbp.limit - v_1;
191
+ if (sbp.cursor >= I_p1) {
192
+ v_2 = sbp.limit_backward;
193
+ sbp.limit_backward = I_p1;
194
+ sbp.ket = sbp.cursor;
195
+ among_var = sbp.find_among_b(a_2, 5);
196
+ sbp.limit_backward = v_2;
197
+ if (among_var) {
198
+ sbp.bra = sbp.cursor;
199
+ switch (among_var) {
200
+ case 1:
201
+ sbp.slice_del();
202
+ v_3 = sbp.limit - sbp.cursor;
203
+ r_consonant_pair();
204
+ sbp.cursor = sbp.limit - v_3;
205
+ break;
206
+ case 2:
207
+ sbp.slice_from("l\u00F8s");
208
+ break;
209
+ }
210
+ }
211
+ }
212
+ }
213
+
214
+ function r_undouble() {
215
+ var v_1;
216
+ if (sbp.cursor >= I_p1) {
217
+ v_1 = sbp.limit_backward;
218
+ sbp.limit_backward = I_p1;
219
+ sbp.ket = sbp.cursor;
220
+ if (sbp.out_grouping_b(g_v, 97, 248)) {
221
+ sbp.bra = sbp.cursor;
222
+ S_ch = sbp.slice_to(S_ch);
223
+ sbp.limit_backward = v_1;
224
+ if (sbp.eq_v_b(S_ch))
225
+ sbp.slice_del();
226
+ } else
227
+ sbp.limit_backward = v_1;
228
+ }
229
+ }
230
+ this.stem = function() {
231
+ var v_1 = sbp.cursor;
232
+ r_mark_regions();
233
+ sbp.limit_backward = v_1;
234
+ sbp.cursor = sbp.limit;
235
+ r_main_suffix();
236
+ sbp.cursor = sbp.limit;
237
+ r_consonant_pair();
238
+ sbp.cursor = sbp.limit;
239
+ r_other_suffix();
240
+ sbp.cursor = sbp.limit;
241
+ r_undouble();
242
+ return true;
243
+ }
244
+ };
245
+
246
+ /* and return a function that stems a word for the current locale */
247
+ return function(word) {
248
+ st.setCurrent(word);
249
+ st.stem();
250
+ return st.getCurrent();
251
+ }
252
+ })();
253
+
254
+ lunr.Pipeline.registerFunction(lunr.da.stemmer, 'stemmer-da');
255
+
256
+ /* stop word filter function */
257
+ lunr.da.stopWordFilter = function(token) {
258
+ if (lunr.da.stopWordFilter.stopWords.indexOf(token) === -1) {
259
+ return token;
260
+ }
261
+ };
262
+
263
+ lunr.da.stopWordFilter.stopWords = new lunr.SortedSet();
264
+ lunr.da.stopWordFilter.stopWords.length = 95;
265
+
266
+ // The space at the beginning is crucial: It marks the empty string
267
+ // as a stop word. lunr.js crashes during search when documents
268
+ // processed by the pipeline still contain the empty string.
269
+ lunr.da.stopWordFilter.stopWords.elements = ' ad af alle alt anden at blev blive bliver da de dem den denne der deres det dette dig din disse dog du efter eller en end er et for fra ham han hans har havde have hende hendes her hos hun hvad hvis hvor i ikke ind jeg jer jo kunne man mange med meget men mig min mine mit mod ned noget nogle nu når og også om op os over på selv sig sin sine sit skal skulle som sådan thi til ud under var vi vil ville vor være været'.split(' ');
270
+
271
+ lunr.Pipeline.registerFunction(lunr.da.stopWordFilter, 'stopWordFilter-da');
272
+ };
273
+ }))