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.
9 jQuery.makeSearchSummary = function(text, keywords, hlwords) {
10 var textLower = text.toLowerCase();
12 $.each(keywords, function() {
13 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) {
91 firstch = w.substr(0,1);
93 w = firstch.toUpperCase() + w.substr(1);
96 re = /^(.+?)(ss|i)es$/;
97 re2 = /^(.+?)([^s])s$/;
100 w = w.replace(re,"$1$2");
101 else if (re2.test(w))
102 w = w.replace(re2,"$1$2");
106 re2 = /^(.+?)(ed|ing)$/;
109 re = new RegExp(mgr0);
110 if (re.test(fp[1])) {
112 w = w.replace(re,"");
115 else if (re2.test(w)) {
116 var fp = re2.exec(w);
118 re2 = new RegExp(s_v);
119 if (re2.test(stem)) {
122 re3 = new RegExp("([^aeiouylsz])\\1$");
123 re4 = new RegExp("^" + C + v + "[^aeiouwxy]$");
126 else if (re3.test(w)) {
128 w = w.replace(re,"");
130 else if (re4.test(w))
140 re = new RegExp(s_v);
146 re = /^(.+?)(ational|tional|enci|anci|izer|bli|alli|entli|eli|ousli|ization|ation|ator|alism|iveness|fulness|ousness|aliti|iviti|biliti|logi)$/;
151 re = new RegExp(mgr0);
153 w = stem + step2list[suffix];
157 re = /^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/;
162 re = new RegExp(mgr0);
164 w = stem + step3list[suffix];
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)$/;
173 re = new RegExp(mgr1);
177 else if (re2.test(w)) {
178 var fp = re2.exec(w);
179 stem = fp[1] + fp[2];
180 re2 = new RegExp(mgr1);
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))))
197 re2 = new RegExp(mgr1);
198 if (re.test(w) && re2.test(w)) {
200 w = w.replace(re,"");
203 // and turn initial Y back to y
205 w = firstch.toLowerCase() + w.substr(1);
217 _queued_query : null,
221 var params = $.getQueryParameters();
223 var query = params.q[0];
224 $('input[name="q"]')[0].value = query;
225 this.performSearch(query);
232 setIndex : function(index) {
235 if ((q = this._queued_query) !== null) {
236 this._queued_query = null;
241 hasIndex : function() {
242 return this._index !== null;
245 deferQuery : function(query) {
246 this._queued_query = query;
249 stopPulse : function() {
250 this._pulse_status = 0;
253 startPulse : function() {
254 if (this._pulse_status >= 0)
257 Search._pulse_status = (Search._pulse_status + 1) % 4;
259 for (var i = 0; i < Search._pulse_status; i++)
261 Search.dots.text(dotString);
262 if (Search._pulse_status > -1)
263 window.setTimeout(pulse, 500);
269 * perform a search for something
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);
279 $('#search-progress').text(_('Preparing search...'));
282 // index already loaded, the browser was quick!
286 this.deferQuery(query);
289 query : function(query) {
290 // stem the searchterms and add them to the
292 var stemmer = new PorterStemmer();
293 var searchterms = [];
296 var tmp = query.split(/\s+/);
297 var object = (tmp.length == 1) ? tmp[0].toLowerCase() : null;
298 for (var i = 0; i < tmp.length; i++) {
300 var word = stemmer.stemWord(tmp[i]).toLowerCase();
301 // select the correct list
302 if (word[0] == '-') {
303 var toAppend = excluded;
304 word = word.substr(1);
307 var toAppend = searchterms;
308 hlterms.push(tmp[i].toLowerCase());
310 // only add if not already in the list
311 if (!$.contains(toAppend, word))
314 var highlightstring = '?highlight=' + $.urlencode(hlterms.join(" "));
316 console.debug('SEARCH: searching for:');
317 console.info('required: ', searchterms);
318 console.info('excluded: ', excluded);
321 var filenames = this._index.filenames;
322 var titles = this._index.titles;
323 var terms = this._index.terms;
324 var descrefs = this._index.descrefs;
325 var modules = this._index.modules;
326 var desctypes = this._index.desctypes;
329 var objectResults = [];
330 var regularResults = [];
331 $('#search-progress').empty();
334 if (object != null) {
335 for (var module in modules) {
336 if (module.indexOf(object) > -1) {
337 fn = modules[module];
338 descr = _('module, in ') + titles[fn];
339 objectResults.push([filenames[fn], module, '#module-'+module, descr]);
342 for (var prefix in descrefs) {
343 for (var name in descrefs[prefix]) {
344 var fullname = (prefix ? prefix + '.' : '') + name;
345 if (fullname.toLowerCase().indexOf(object) > -1) {
346 match = descrefs[prefix][name];
347 descr = desctypes[match[1]] + _(', in ') + titles[match[0]];
348 objectResults.push([filenames[match[0]], fullname, '#'+fullname, descr]);
354 // sort results descending
355 objectResults.sort(function(a, b) {
356 return (a[1] > b[1]) ? -1 : ((a[1] < b[1]) ? 1 : 0);
360 // perform the search on the required terms
361 for (var i = 0; i < searchterms.length; i++) {
362 var word = searchterms[i];
363 // no match but word was a required one
364 if ((files = terms[word]) == null)
366 if (files.length == undefined) {
369 // create the mapping
370 for (var j = 0; j < files.length; j++) {
373 fileMap[file].push(word);
375 fileMap[file] = [word];
379 // now check if the files don't contain excluded terms
380 for (var file in fileMap) {
383 // check if all requirements are matched
384 if (fileMap[file].length != searchterms.length)
387 // ensure that none of the excluded terms is in the
389 for (var i = 0; i < excluded.length; i++) {
390 if (terms[excluded[i]] == file ||
391 $.contains(terms[excluded[i]] || [], file)) {
397 // if we have still a valid result we can add it
398 // to the result list
400 regularResults.push([filenames[file], titles[file], '', null]);
403 // delete unused variables in order to not waste
404 // memory until list is retrieved completely
405 delete filenames, titles, terms;
407 // now sort the regular results descending by title
408 regularResults.sort(function(a, b) {
409 var left = a[1].toLowerCase();
410 var right = b[1].toLowerCase();
411 return (left > right) ? -1 : ((left < right) ? 1 : 0);
415 var results = regularResults.concat(objectResults);
418 var resultCount = results.length;
419 function displayNextItem() {
420 // results left, load the summary and display it
421 if (results.length) {
422 var item = results.pop();
423 var listItem = $('<li style="display:none"></li>');
424 listItem.append($('<a/>').attr(
426 item[0] + DOCUMENTATION_OPTIONS.FILE_SUFFIX +
427 highlightstring + item[2]).html(item[1]));
429 listItem.append($('<span> (' + item[3] + ')</span>'));
430 Search.output.append(listItem);
431 listItem.slideDown(5, function() {
434 } else if (DOCUMENTATION_OPTIONS.HAS_SOURCE) {
435 $.get('_sources/' + item[0] + '.txt', function(data) {
436 listItem.append($.makeSearchSummary(data, searchterms, hlterms));
437 Search.output.append(listItem);
438 listItem.slideDown(5, function() {
443 // no source available, just display title
444 Search.output.append(listItem);
445 listItem.slideDown(5, function() {
450 // search finished, update title and status message
453 Search.title.text(_('Search Results'));
455 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.'));
457 Search.status.text(_('Search finished, found %s page(s) matching the search query.').replace('%s', resultCount));
458 Search.status.fadeIn(500);
465 $(document).ready(function() {