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.
8
jQuery.makeSearchSummary = function(text, keywords, hlwords) {
9
var textLower = text.toLowerCase();
11
$.each(keywords, function() {
12
var i = textLower.indexOf(this.toLowerCase());
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');
31
var PorterStemmer = function() {
67
var c = "[^aeiou]"; // consonant
68
var v = "[aeiouy]"; // vowel
69
var C = c + "[^aeiouy]*"; // consonant sequence
70
var V = v + "[aeiou]*"; // vowel sequence
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
77
this.stemWord = function (w) {
92
firstch = w.substr(0,1);
94
w = firstch.toUpperCase() + w.substr(1);
98
re = /^(.+?)(ss|i)es$/;
99
re2 = /^(.+?)([^s])s$/;
102
w = w.replace(re,"$1$2");
104
else if (re2.test(w)) {
105
w = w.replace(re2,"$1$2");
110
re2 = /^(.+?)(ed|ing)$/;
113
re = new RegExp(mgr0);
114
if (re.test(fp[1])) {
116
w = w.replace(re,"");
119
else if (re2.test(w)) {
120
var fp = re2.exec(w);
122
re2 = new RegExp(s_v);
123
if (re2.test(stem)) {
126
re3 = new RegExp("([^aeiouylsz])\\1$");
127
re4 = new RegExp("^" + C + v + "[^aeiouwxy]$");
131
else if (re3.test(w)) {
132
re = /.$/; w = w.replace(re,"");
134
else if (re4.test(w)) {
145
re = new RegExp(s_v);
146
if (re.test(stem)) { w = stem + "i"; }
150
re = /^(.+?)(ational|tional|enci|anci|izer|bli|alli|entli|eli|ousli|ization|ation|ator|alism|iveness|fulness|ousness|aliti|iviti|biliti|logi)$/;
155
re = new RegExp(mgr0);
157
w = stem + step2list[suffix];
162
re = /^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/;
167
re = new RegExp(mgr0);
169
w = stem + step3list[suffix];
174
re = /^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$/;
175
re2 = /^(.+?)(s|t)(ion)$/;
179
re = new RegExp(mgr1);
184
else if (re2.test(w)) {
185
var fp = re2.exec(w);
186
stem = fp[1] + fp[2];
187
re2 = new RegExp(mgr1);
188
if (re2.test(stem)) {
198
re = new RegExp(mgr1);
199
re2 = new RegExp(meq1);
200
re3 = new RegExp("^" + C + v + "[^aeiouwxy]$");
201
if (re.test(stem) || (re2.test(stem) && !(re3.test(stem)))) {
206
re2 = new RegExp(mgr1);
207
if (re.test(w) && re2.test(w)) {
209
w = w.replace(re,"");
212
// and turn initial Y back to y
213
if (firstch == "y") {
214
w = firstch.toLowerCase() + w.substr(1);
228
var params = $.getQueryParameters();
230
var query = params.q[0];
231
$('input[@name="q"]')[0].value = query;
232
this.performSearch(query);
237
* perform a search for something
239
performSearch : function(query) {
240
// create the required interface elements
241
var out = $('#search-results');
242
var title = $('<h2>Searching</h2>').appendTo(out);
243
var dots = $('<span></span>').appendTo(title);
244
var status = $('<p style="display: none"></p>').appendTo(out);
245
var output = $('<ul class="search"/>').appendTo(out);
247
// spawn a background runner for updating the dots
248
// until the search has finished
251
pulseStatus = (pulseStatus + 1) % 4;
253
for (var i = 0; i < pulseStatus; i++) {
256
dots.text(dotString);
257
if (pulseStatus > -1) {
258
window.setTimeout(pulse, 500);
263
// stem the searchwords and add them to the
265
var stemmer = new PorterStemmer();
266
var searchwords = [];
269
var tmp = query.split(/\s+/);
270
for (var i = 0; i < tmp.length; i++) {
272
var word = stemmer.stemWord(tmp[i]).toLowerCase();
273
// select the correct list
274
if (word[0] == '-') {
275
var toAppend = excluded;
276
word = word.substr(1);
279
var toAppend = searchwords;
280
hlwords.push(tmp[i].toLowerCase());
282
// only add if not already in the list
283
if (!$.contains(toAppend, word)) {
287
var highlightstring = '?highlight=' + $.urlencode(hlwords.join(" "));
289
console.debug('SEARCH: searching for:');
290
console.info('required: ', searchwords);
291
console.info('excluded: ', excluded);
293
// fetch searchindex and perform search
294
$.getJSON('searchindex.json', function(data) {
297
var filenames = data[0];
303
// perform the search on the required words
304
for (var i = 0; i < searchwords.length; i++) {
305
var word = searchwords[i];
306
// no match but word was a required one
307
if ((files = words[word]) == null) {
310
// create the mapping
311
for (var j = 0; j < files.length; j++) {
313
if (file in fileMap) {
314
fileMap[file].push(word);
317
fileMap[file] = [word];
322
// now check if the files are in the correct
323
// areas and if the don't contain excluded words
325
for (var file in fileMap) {
328
// check if all requirements are matched
329
if (fileMap[file].length != searchwords.length) {
332
// ensure that none of the excluded words is in the
334
for (var i = 0; i < excluded.length; i++) {
335
if ($.contains(words[excluded[i]] || [], file)) {
341
// if we have still a valid result we can add it
342
// to the result list
344
results.push([filenames[file], titles[file]]);
348
// delete unused variables in order to not waste
349
// memory until list is retrieved completely
350
delete filenames, titles, words, data;
352
// now sort the results by title
353
results.sort(function(a, b) {
354
var left = a[1].toLowerCase();
355
var right = b[1].toLowerCase();
356
return (left > right) ? -1 : ((left < right) ? 1 : 0);
360
var resultCount = results.length;
361
function displayNextItem() {
362
// results left, load the summary and display it
363
if (results.length) {
364
var item = results.pop();
365
var listItem = $('<li style="display:none"></li>');
366
listItem.append($('<a/>').attr(
368
item[0] + DOCUMENTATION_OPTIONS.FILE_SUFFIX +
369
highlightstring).html(item[1]));
370
$.get('_sources/' + item[0] + '.txt', function(data) {
371
listItem.append($.makeSearchSummary(data, searchwords, hlwords));
372
output.append(listItem);
373
listItem.slideDown(10, function() {
378
// search finished, update title and status message
381
title.text('Search Results');
383
status.text('Your search did not match any documents. ' +
384
'Please make sure that all words are spelled ' +
385
'correctly and that you\'ve selected enough ' +
389
status.text('Search finished, found ' + resultCount +
390
' page' + (resultCount != 1 ? 's' : '') +
391
' matching the search query.');
402
$(document).ready(function() {