3
|
1 |
/**
|
|
2 |
* helper function to return a node containing the
|
|
3 |
* search summary for a given text. keywords is a list
|
|
4 |
* of stemmed words, hlwords is the list of normal, unstemmed
|
|
5 |
* words. the first one is used to find the occurance, the
|
|
6 |
* latter for highlighting it.
|
|
7 |
*/
|
|
8 |
|
|
9 |
jQuery.makeSearchSummary = function(text, keywords, hlwords) {
|
|
10 |
var textLower = text.toLowerCase();
|
|
11 |
var start = 0;
|
|
12 |
$.each(keywords, function() {
|
|
13 |
var i = textLower.indexOf(this.toLowerCase());
|
|
14 |
if (i > -1)
|
|
15 |
start = i;
|
|
16 |
});
|
|
17 |
start = Math.max(start - 120, 0);
|
|
18 |
var excerpt = ((start > 0) ? '...' : '') +
|
|
19 |
$.trim(text.substr(start, 240)) +
|
|
20 |
((start + 240 - text.length) ? '...' : '');
|
|
21 |
var rv = $('<div class="context"></div>').text(excerpt);
|
|
22 |
$.each(hlwords, function() {
|
|
23 |
rv = rv.highlightText(this, 'highlight');
|
|
24 |
});
|
|
25 |
return rv;
|
|
26 |
}
|
|
27 |
|
|
28 |
/**
|
|
29 |
* Porter Stemmer
|
|
30 |
*/
|
|
31 |
var PorterStemmer = function() {
|
|
32 |
|
|
33 |
var step2list = {
|
|
34 |
ational: 'ate',
|
|
35 |
tional: 'tion',
|
|
36 |
enci: 'ence',
|
|
37 |
anci: 'ance',
|
|
38 |
izer: 'ize',
|
|
39 |
bli: 'ble',
|
|
40 |
alli: 'al',
|
|
41 |
entli: 'ent',
|
|
42 |
eli: 'e',
|
|
43 |
ousli: 'ous',
|
|
44 |
ization: 'ize',
|
|
45 |
ation: 'ate',
|
|
46 |
ator: 'ate',
|
|
47 |
alism: 'al',
|
|
48 |
iveness: 'ive',
|
|
49 |
fulness: 'ful',
|
|
50 |
ousness: 'ous',
|
|
51 |
aliti: 'al',
|
|
52 |
iviti: 'ive',
|
|
53 |
biliti: 'ble',
|
|
54 |
logi: 'log'
|
|
55 |
};
|
|
56 |
|
|
57 |
var step3list = {
|
|
58 |
icate: 'ic',
|
|
59 |
ative: '',
|
|
60 |
alize: 'al',
|
|
61 |
iciti: 'ic',
|
|
62 |
ical: 'ic',
|
|
63 |
ful: '',
|
|
64 |
ness: ''
|
|
65 |
};
|
|
66 |
|
|
67 |
var c = "[^aeiou]"; // consonant
|
|
68 |
var v = "[aeiouy]"; // vowel
|
|
69 |
var C = c + "[^aeiouy]*"; // consonant sequence
|
|
70 |
var V = v + "[aeiou]*"; // vowel sequence
|
|
71 |
|
|
72 |
var mgr0 = "^(" + C + ")?" + V + C; // [C]VC... is m>0
|
|
73 |
var meq1 = "^(" + C + ")?" + V + C + "(" + V + ")?$"; // [C]VC[V] is m=1
|
|
74 |
var mgr1 = "^(" + C + ")?" + V + C + V + C; // [C]VCVC... is m>1
|
|
75 |
var s_v = "^(" + C + ")?" + v; // vowel in stem
|
|
76 |
|
|
77 |
this.stemWord = function (w) {
|
|
78 |
var stem;
|
|
79 |
var suffix;
|
|
80 |
var firstch;
|
|
81 |
var origword = w;
|
|
82 |
|
|
83 |
if (w.length < 3)
|
|
84 |
return w;
|
|
85 |
|
|
86 |
var re;
|
|
87 |
var re2;
|
|
88 |
var re3;
|
|
89 |
var re4;
|
|
90 |
|
|
91 |
firstch = w.substr(0,1);
|
|
92 |
if (firstch == "y")
|
|
93 |
w = firstch.toUpperCase() + w.substr(1);
|
|
94 |
|
|
95 |
// Step 1a
|
|
96 |
re = /^(.+?)(ss|i)es$/;
|
|
97 |
re2 = /^(.+?)([^s])s$/;
|
|
98 |
|
|
99 |
if (re.test(w))
|
|
100 |
w = w.replace(re,"$1$2");
|
|
101 |
else if (re2.test(w))
|
|
102 |
w = w.replace(re2,"$1$2");
|
|
103 |
|
|
104 |
// Step 1b
|
|
105 |
re = /^(.+?)eed$/;
|
|
106 |
re2 = /^(.+?)(ed|ing)$/;
|
|
107 |
if (re.test(w)) {
|
|
108 |
var fp = re.exec(w);
|
|
109 |
re = new RegExp(mgr0);
|
|
110 |
if (re.test(fp[1])) {
|
|
111 |
re = /.$/;
|
|
112 |
w = w.replace(re,"");
|
|
113 |
}
|
|
114 |
}
|
|
115 |
else if (re2.test(w)) {
|
|
116 |
var fp = re2.exec(w);
|
|
117 |
stem = fp[1];
|
|
118 |
re2 = new RegExp(s_v);
|
|
119 |
if (re2.test(stem)) {
|
|
120 |
w = stem;
|
|
121 |
re2 = /(at|bl|iz)$/;
|
|
122 |
re3 = new RegExp("([^aeiouylsz])\\1$");
|
|
123 |
re4 = new RegExp("^" + C + v + "[^aeiouwxy]$");
|
|
124 |
if (re2.test(w))
|
|
125 |
w = w + "e";
|
|
126 |
else if (re3.test(w)) {
|
|
127 |
re = /.$/;
|
|
128 |
w = w.replace(re,"");
|
|
129 |
}
|
|
130 |
else if (re4.test(w))
|
|
131 |
w = w + "e";
|
|
132 |
}
|
|
133 |
}
|
|
134 |
|
|
135 |
// Step 1c
|
|
136 |
re = /^(.+?)y$/;
|
|
137 |
if (re.test(w)) {
|
|
138 |
var fp = re.exec(w);
|
|
139 |
stem = fp[1];
|
|
140 |
re = new RegExp(s_v);
|
|
141 |
if (re.test(stem))
|
|
142 |
w = stem + "i";
|
|
143 |
}
|
|
144 |
|
|
145 |
// Step 2
|
|
146 |
re = /^(.+?)(ational|tional|enci|anci|izer|bli|alli|entli|eli|ousli|ization|ation|ator|alism|iveness|fulness|ousness|aliti|iviti|biliti|logi)$/;
|
|
147 |
if (re.test(w)) {
|
|
148 |
var fp = re.exec(w);
|
|
149 |
stem = fp[1];
|
|
150 |
suffix = fp[2];
|
|
151 |
re = new RegExp(mgr0);
|
|
152 |
if (re.test(stem))
|
|
153 |
w = stem + step2list[suffix];
|
|
154 |
}
|
|
155 |
|
|
156 |
// Step 3
|
|
157 |
re = /^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/;
|
|
158 |
if (re.test(w)) {
|
|
159 |
var fp = re.exec(w);
|
|
160 |
stem = fp[1];
|
|
161 |
suffix = fp[2];
|
|
162 |
re = new RegExp(mgr0);
|
|
163 |
if (re.test(stem))
|
|
164 |
w = stem + step3list[suffix];
|
|
165 |
}
|
|
166 |
|
|
167 |
// Step 4
|
|
168 |
re = /^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$/;
|
|
169 |
re2 = /^(.+?)(s|t)(ion)$/;
|
|
170 |
if (re.test(w)) {
|
|
171 |
var fp = re.exec(w);
|
|
172 |
stem = fp[1];
|
|
173 |
re = new RegExp(mgr1);
|
|
174 |
if (re.test(stem))
|
|
175 |
w = stem;
|
|
176 |
}
|
|
177 |
else if (re2.test(w)) {
|
|
178 |
var fp = re2.exec(w);
|
|
179 |
stem = fp[1] + fp[2];
|
|
180 |
re2 = new RegExp(mgr1);
|
|
181 |
if (re2.test(stem))
|
|
182 |
w = stem;
|
|
183 |
}
|
|
184 |
|
|
185 |
// Step 5
|
|
186 |
re = /^(.+?)e$/;
|
|
187 |
if (re.test(w)) {
|
|
188 |
var fp = re.exec(w);
|
|
189 |
stem = fp[1];
|
|
190 |
re = new RegExp(mgr1);
|
|
191 |
re2 = new RegExp(meq1);
|
|
192 |
re3 = new RegExp("^" + C + v + "[^aeiouwxy]$");
|
|
193 |
if (re.test(stem) || (re2.test(stem) && !(re3.test(stem))))
|
|
194 |
w = stem;
|
|
195 |
}
|
|
196 |
re = /ll$/;
|
|
197 |
re2 = new RegExp(mgr1);
|
|
198 |
if (re.test(w) && re2.test(w)) {
|
|
199 |
re = /.$/;
|
|
200 |
w = w.replace(re,"");
|
|
201 |
}
|
|
202 |
|
|
203 |
// and turn initial Y back to y
|
|
204 |
if (firstch == "y")
|
|
205 |
w = firstch.toLowerCase() + w.substr(1);
|
|
206 |
return w;
|
|
207 |
}
|
|
208 |
}
|
|
209 |
|
|
210 |
|
|
211 |
/**
|
|
212 |
* Search Module
|
|
213 |
*/
|
|
214 |
var Search = {
|
|
215 |
|
|
216 |
_index : null,
|
|
217 |
_queued_query : null,
|
|
218 |
_pulse_status : -1,
|
|
219 |
|
|
220 |
init : function() {
|
|
221 |
var params = $.getQueryParameters();
|
|
222 |
if (params.q) {
|
|
223 |
var query = params.q[0];
|
|
224 |
$('input[name="q"]')[0].value = query;
|
|
225 |
this.performSearch(query);
|
|
226 |
}
|
|
227 |
},
|
|
228 |
|
|
229 |
/**
|
|
230 |
* Sets the index
|
|
231 |
*/
|
|
232 |
setIndex : function(index) {
|
|
233 |
var q;
|
|
234 |
this._index = index;
|
|
235 |
if ((q = this._queued_query) !== null) {
|
|
236 |
this._queued_query = null;
|
|
237 |
Search.query(q);
|
|
238 |
}
|
|
239 |
},
|
|
240 |
|
|
241 |
hasIndex : function() {
|
|
242 |
return this._index !== null;
|
|
243 |
},
|
|
244 |
|
|
245 |
deferQuery : function(query) {
|
|
246 |
this._queued_query = query;
|
|
247 |
},
|
|
248 |
|
|
249 |
stopPulse : function() {
|
|
250 |
this._pulse_status = 0;
|
|
251 |
},
|
|
252 |
|
|
253 |
startPulse : function() {
|
|
254 |
if (this._pulse_status >= 0)
|
|
255 |
return;
|
|
256 |
function pulse() {
|
|
257 |
Search._pulse_status = (Search._pulse_status + 1) % 4;
|
|
258 |
var dotString = '';
|
|
259 |
for (var i = 0; i < Search._pulse_status; i++)
|
|
260 |
dotString += '.';
|
|
261 |
Search.dots.text(dotString);
|
|
262 |
if (Search._pulse_status > -1)
|
|
263 |
window.setTimeout(pulse, 500);
|
|
264 |
};
|
|
265 |
pulse();
|
|
266 |
},
|
|
267 |
|
|
268 |
/**
|
|
269 |
* perform a search for something
|
|
270 |
*/
|
|
271 |
performSearch : function(query) {
|
|
272 |
// create the required interface elements
|
|
273 |
this.out = $('#search-results');
|
|
274 |
this.title = $('<h2>' + _('Searching') + '</h2>').appendTo(this.out);
|
|
275 |
this.dots = $('<span></span>').appendTo(this.title);
|
|
276 |
this.status = $('<p style="display: none"></p>').appendTo(this.out);
|
|
277 |
this.output = $('<ul class="search"/>').appendTo(this.out);
|
|
278 |
|
|
279 |
$('#search-progress').text(_('Preparing search...'));
|
|
280 |
this.startPulse();
|
|
281 |
|
|
282 |
// index already loaded, the browser was quick!
|
|
283 |
if (this.hasIndex())
|
|
284 |
this.query(query);
|
|
285 |
else
|
|
286 |
this.deferQuery(query);
|
|
287 |
},
|
|
288 |
|
|
289 |
query : function(query) {
|
|
290 |
var stopwords = ['and', 'then', 'into', 'it', 'as', 'are', 'in',
|
|
291 |
'if', 'for', 'no', 'there', 'their', 'was', 'is',
|
|
292 |
'be', 'to', 'that', 'but', 'they', 'not', 'such',
|
|
293 |
'with', 'by', 'a', 'on', 'these', 'of', 'will',
|
|
294 |
'this', 'near', 'the', 'or', 'at'];
|
|
295 |
|
|
296 |
// stem the searchterms and add them to the correct list
|
|
297 |
var stemmer = new PorterStemmer();
|
|
298 |
var searchterms = [];
|
|
299 |
var excluded = [];
|
|
300 |
var hlterms = [];
|
|
301 |
var tmp = query.split(/\s+/);
|
|
302 |
var object = (tmp.length == 1) ? tmp[0].toLowerCase() : null;
|
|
303 |
for (var i = 0; i < tmp.length; i++) {
|
|
304 |
if (stopwords.indexOf(tmp[i]) != -1 || tmp[i].match(/^\d+$/)) {
|
|
305 |
// skip this word
|
|
306 |
continue;
|
|
307 |
}
|
|
308 |
// stem the word
|
|
309 |
var word = stemmer.stemWord(tmp[i]).toLowerCase();
|
|
310 |
// select the correct list
|
|
311 |
if (word[0] == '-') {
|
|
312 |
var toAppend = excluded;
|
|
313 |
word = word.substr(1);
|
|
314 |
}
|
|
315 |
else {
|
|
316 |
var toAppend = searchterms;
|
|
317 |
hlterms.push(tmp[i].toLowerCase());
|
|
318 |
}
|
|
319 |
// only add if not already in the list
|
|
320 |
if (!$.contains(toAppend, word))
|
|
321 |
toAppend.push(word);
|
|
322 |
};
|
|
323 |
var highlightstring = '?highlight=' + $.urlencode(hlterms.join(" "));
|
|
324 |
|
|
325 |
console.debug('SEARCH: searching for:');
|
|
326 |
console.info('required: ', searchterms);
|
|
327 |
console.info('excluded: ', excluded);
|
|
328 |
|
|
329 |
// prepare search
|
|
330 |
var filenames = this._index.filenames;
|
|
331 |
var titles = this._index.titles;
|
|
332 |
var terms = this._index.terms;
|
|
333 |
var descrefs = this._index.descrefs;
|
|
334 |
var modules = this._index.modules;
|
|
335 |
var desctypes = this._index.desctypes;
|
|
336 |
var fileMap = {};
|
|
337 |
var files = null;
|
|
338 |
var objectResults = [];
|
|
339 |
var regularResults = [];
|
|
340 |
$('#search-progress').empty();
|
|
341 |
|
|
342 |
// lookup as object
|
|
343 |
if (object != null) {
|
|
344 |
for (var module in modules) {
|
|
345 |
if (module.indexOf(object) > -1) {
|
|
346 |
fn = modules[module];
|
|
347 |
descr = _('module, in ') + titles[fn];
|
|
348 |
objectResults.push([filenames[fn], module, '#module-'+module, descr]);
|
|
349 |
}
|
|
350 |
}
|
|
351 |
for (var prefix in descrefs) {
|
|
352 |
for (var name in descrefs[prefix]) {
|
|
353 |
var fullname = (prefix ? prefix + '.' : '') + name;
|
|
354 |
if (fullname.toLowerCase().indexOf(object) > -1) {
|
|
355 |
match = descrefs[prefix][name];
|
|
356 |
descr = desctypes[match[1]] + _(', in ') + titles[match[0]];
|
|
357 |
objectResults.push([filenames[match[0]], fullname, '#'+fullname, descr]);
|
|
358 |
}
|
|
359 |
}
|
|
360 |
}
|
|
361 |
}
|
|
362 |
|
|
363 |
// sort results descending
|
|
364 |
objectResults.sort(function(a, b) {
|
|
365 |
return (a[1] > b[1]) ? -1 : ((a[1] < b[1]) ? 1 : 0);
|
|
366 |
});
|
|
367 |
|
|
368 |
|
|
369 |
// perform the search on the required terms
|
|
370 |
for (var i = 0; i < searchterms.length; i++) {
|
|
371 |
var word = searchterms[i];
|
|
372 |
// no match but word was a required one
|
|
373 |
if ((files = terms[word]) == null)
|
|
374 |
break;
|
|
375 |
if (files.length == undefined) {
|
|
376 |
files = [files];
|
|
377 |
}
|
|
378 |
// create the mapping
|
|
379 |
for (var j = 0; j < files.length; j++) {
|
|
380 |
var file = files[j];
|
|
381 |
if (file in fileMap)
|
|
382 |
fileMap[file].push(word);
|
|
383 |
else
|
|
384 |
fileMap[file] = [word];
|
|
385 |
}
|
|
386 |
}
|
|
387 |
|
|
388 |
// now check if the files don't contain excluded terms
|
|
389 |
for (var file in fileMap) {
|
|
390 |
var valid = true;
|
|
391 |
|
|
392 |
// check if all requirements are matched
|
|
393 |
if (fileMap[file].length != searchterms.length)
|
|
394 |
continue;
|
|
395 |
|
|
396 |
// ensure that none of the excluded terms is in the
|
|
397 |
// search result.
|
|
398 |
for (var i = 0; i < excluded.length; i++) {
|
|
399 |
if (terms[excluded[i]] == file ||
|
|
400 |
$.contains(terms[excluded[i]] || [], file)) {
|
|
401 |
valid = false;
|
|
402 |
break;
|
|
403 |
}
|
|
404 |
}
|
|
405 |
|
|
406 |
// if we have still a valid result we can add it
|
|
407 |
// to the result list
|
|
408 |
if (valid)
|
|
409 |
regularResults.push([filenames[file], titles[file], '', null]);
|
|
410 |
}
|
|
411 |
|
|
412 |
// delete unused variables in order to not waste
|
|
413 |
// memory until list is retrieved completely
|
|
414 |
delete filenames, titles, terms;
|
|
415 |
|
|
416 |
// now sort the regular results descending by title
|
|
417 |
regularResults.sort(function(a, b) {
|
|
418 |
var left = a[1].toLowerCase();
|
|
419 |
var right = b[1].toLowerCase();
|
|
420 |
return (left > right) ? -1 : ((left < right) ? 1 : 0);
|
|
421 |
});
|
|
422 |
|
|
423 |
// combine both
|
|
424 |
var results = regularResults.concat(objectResults);
|
|
425 |
|
|
426 |
// print the results
|
|
427 |
var resultCount = results.length;
|
|
428 |
function displayNextItem() {
|
|
429 |
// results left, load the summary and display it
|
|
430 |
if (results.length) {
|
|
431 |
var item = results.pop();
|
|
432 |
var listItem = $('<li style="display:none"></li>');
|
|
433 |
listItem.append($('<a/>').attr(
|
|
434 |
'href',
|
|
435 |
item[0] + DOCUMENTATION_OPTIONS.FILE_SUFFIX +
|
|
436 |
highlightstring + item[2]).html(item[1]));
|
|
437 |
if (item[3]) {
|
|
438 |
listItem.append($('<span> (' + item[3] + ')</span>'));
|
|
439 |
Search.output.append(listItem);
|
|
440 |
listItem.slideDown(5, function() {
|
|
441 |
displayNextItem();
|
|
442 |
});
|
|
443 |
} else if (DOCUMENTATION_OPTIONS.HAS_SOURCE) {
|
|
444 |
$.get('_sources/' + item[0] + '.txt', function(data) {
|
|
445 |
listItem.append($.makeSearchSummary(data, searchterms, hlterms));
|
|
446 |
Search.output.append(listItem);
|
|
447 |
listItem.slideDown(5, function() {
|
|
448 |
displayNextItem();
|
|
449 |
});
|
|
450 |
});
|
|
451 |
} else {
|
|
452 |
// no source available, just display title
|
|
453 |
Search.output.append(listItem);
|
|
454 |
listItem.slideDown(5, function() {
|
|
455 |
displayNextItem();
|
|
456 |
});
|
|
457 |
}
|
|
458 |
}
|
|
459 |
// search finished, update title and status message
|
|
460 |
else {
|
|
461 |
Search.stopPulse();
|
|
462 |
Search.title.text(_('Search Results'));
|
|
463 |
if (!resultCount)
|
|
464 |
Search.status.text(_('Your search did not match any documents. Please make sure that all words are spelled correctly and that you\'ve selected enough categories.'));
|
|
465 |
else
|
|
466 |
Search.status.text(_('Search finished, found %s page(s) matching the search query.').replace('%s', resultCount));
|
|
467 |
Search.status.fadeIn(500);
|
|
468 |
}
|
|
469 |
}
|
|
470 |
displayNextItem();
|
|
471 |
}
|
|
472 |
}
|
|
473 |
|
|
474 |
$(document).ready(function() {
|
|
475 |
Search.init();
|
|
476 |
});
|