michael@0: /*
michael@0: * searchtools.js_t
michael@0: * ~~~~~~~~~~~~~~~~
michael@0: *
michael@0: * Sphinx JavaScript utilties for the full-text search.
michael@0: *
michael@0: * :copyright: Copyright 2007-2011 by the Sphinx team, see AUTHORS.
michael@0: * :license: BSD, see LICENSE for details.
michael@0: *
michael@0: */
michael@0:
michael@0: /**
michael@0: * helper function to return a node containing the
michael@0: * search summary for a given text. keywords is a list
michael@0: * of stemmed words, hlwords is the list of normal, unstemmed
michael@0: * words. the first one is used to find the occurance, the
michael@0: * latter for highlighting it.
michael@0: */
michael@0:
michael@0: jQuery.makeSearchSummary = function(text, keywords, hlwords) {
michael@0: var textLower = text.toLowerCase();
michael@0: var start = 0;
michael@0: $.each(keywords, function() {
michael@0: var i = textLower.indexOf(this.toLowerCase());
michael@0: if (i > -1)
michael@0: start = i;
michael@0: });
michael@0: start = Math.max(start - 120, 0);
michael@0: var excerpt = ((start > 0) ? '...' : '') +
michael@0: $.trim(text.substr(start, 240)) +
michael@0: ((start + 240 - text.length) ? '...' : '');
michael@0: var rv = $('
').text(excerpt);
michael@0: $.each(hlwords, function() {
michael@0: rv = rv.highlightText(this, 'highlighted');
michael@0: });
michael@0: return rv;
michael@0: }
michael@0:
michael@0:
michael@0: /**
michael@0: * Porter Stemmer
michael@0: */
michael@0: var Stemmer = function() {
michael@0:
michael@0: var step2list = {
michael@0: ational: 'ate',
michael@0: tional: 'tion',
michael@0: enci: 'ence',
michael@0: anci: 'ance',
michael@0: izer: 'ize',
michael@0: bli: 'ble',
michael@0: alli: 'al',
michael@0: entli: 'ent',
michael@0: eli: 'e',
michael@0: ousli: 'ous',
michael@0: ization: 'ize',
michael@0: ation: 'ate',
michael@0: ator: 'ate',
michael@0: alism: 'al',
michael@0: iveness: 'ive',
michael@0: fulness: 'ful',
michael@0: ousness: 'ous',
michael@0: aliti: 'al',
michael@0: iviti: 'ive',
michael@0: biliti: 'ble',
michael@0: logi: 'log'
michael@0: };
michael@0:
michael@0: var step3list = {
michael@0: icate: 'ic',
michael@0: ative: '',
michael@0: alize: 'al',
michael@0: iciti: 'ic',
michael@0: ical: 'ic',
michael@0: ful: '',
michael@0: ness: ''
michael@0: };
michael@0:
michael@0: var c = "[^aeiou]"; // consonant
michael@0: var v = "[aeiouy]"; // vowel
michael@0: var C = c + "[^aeiouy]*"; // consonant sequence
michael@0: var V = v + "[aeiou]*"; // vowel sequence
michael@0:
michael@0: var mgr0 = "^(" + C + ")?" + V + C; // [C]VC... is m>0
michael@0: var meq1 = "^(" + C + ")?" + V + C + "(" + V + ")?$"; // [C]VC[V] is m=1
michael@0: var mgr1 = "^(" + C + ")?" + V + C + V + C; // [C]VCVC... is m>1
michael@0: var s_v = "^(" + C + ")?" + v; // vowel in stem
michael@0:
michael@0: this.stemWord = function (w) {
michael@0: var stem;
michael@0: var suffix;
michael@0: var firstch;
michael@0: var origword = w;
michael@0:
michael@0: if (w.length < 3)
michael@0: return w;
michael@0:
michael@0: var re;
michael@0: var re2;
michael@0: var re3;
michael@0: var re4;
michael@0:
michael@0: firstch = w.substr(0,1);
michael@0: if (firstch == "y")
michael@0: w = firstch.toUpperCase() + w.substr(1);
michael@0:
michael@0: // Step 1a
michael@0: re = /^(.+?)(ss|i)es$/;
michael@0: re2 = /^(.+?)([^s])s$/;
michael@0:
michael@0: if (re.test(w))
michael@0: w = w.replace(re,"$1$2");
michael@0: else if (re2.test(w))
michael@0: w = w.replace(re2,"$1$2");
michael@0:
michael@0: // Step 1b
michael@0: re = /^(.+?)eed$/;
michael@0: re2 = /^(.+?)(ed|ing)$/;
michael@0: if (re.test(w)) {
michael@0: var fp = re.exec(w);
michael@0: re = new RegExp(mgr0);
michael@0: if (re.test(fp[1])) {
michael@0: re = /.$/;
michael@0: w = w.replace(re,"");
michael@0: }
michael@0: }
michael@0: else if (re2.test(w)) {
michael@0: var fp = re2.exec(w);
michael@0: stem = fp[1];
michael@0: re2 = new RegExp(s_v);
michael@0: if (re2.test(stem)) {
michael@0: w = stem;
michael@0: re2 = /(at|bl|iz)$/;
michael@0: re3 = new RegExp("([^aeiouylsz])\\1$");
michael@0: re4 = new RegExp("^" + C + v + "[^aeiouwxy]$");
michael@0: if (re2.test(w))
michael@0: w = w + "e";
michael@0: else if (re3.test(w)) {
michael@0: re = /.$/;
michael@0: w = w.replace(re,"");
michael@0: }
michael@0: else if (re4.test(w))
michael@0: w = w + "e";
michael@0: }
michael@0: }
michael@0:
michael@0: // Step 1c
michael@0: re = /^(.+?)y$/;
michael@0: if (re.test(w)) {
michael@0: var fp = re.exec(w);
michael@0: stem = fp[1];
michael@0: re = new RegExp(s_v);
michael@0: if (re.test(stem))
michael@0: w = stem + "i";
michael@0: }
michael@0:
michael@0: // Step 2
michael@0: re = /^(.+?)(ational|tional|enci|anci|izer|bli|alli|entli|eli|ousli|ization|ation|ator|alism|iveness|fulness|ousness|aliti|iviti|biliti|logi)$/;
michael@0: if (re.test(w)) {
michael@0: var fp = re.exec(w);
michael@0: stem = fp[1];
michael@0: suffix = fp[2];
michael@0: re = new RegExp(mgr0);
michael@0: if (re.test(stem))
michael@0: w = stem + step2list[suffix];
michael@0: }
michael@0:
michael@0: // Step 3
michael@0: re = /^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/;
michael@0: if (re.test(w)) {
michael@0: var fp = re.exec(w);
michael@0: stem = fp[1];
michael@0: suffix = fp[2];
michael@0: re = new RegExp(mgr0);
michael@0: if (re.test(stem))
michael@0: w = stem + step3list[suffix];
michael@0: }
michael@0:
michael@0: // Step 4
michael@0: re = /^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$/;
michael@0: re2 = /^(.+?)(s|t)(ion)$/;
michael@0: if (re.test(w)) {
michael@0: var fp = re.exec(w);
michael@0: stem = fp[1];
michael@0: re = new RegExp(mgr1);
michael@0: if (re.test(stem))
michael@0: w = stem;
michael@0: }
michael@0: else if (re2.test(w)) {
michael@0: var fp = re2.exec(w);
michael@0: stem = fp[1] + fp[2];
michael@0: re2 = new RegExp(mgr1);
michael@0: if (re2.test(stem))
michael@0: w = stem;
michael@0: }
michael@0:
michael@0: // Step 5
michael@0: re = /^(.+?)e$/;
michael@0: if (re.test(w)) {
michael@0: var fp = re.exec(w);
michael@0: stem = fp[1];
michael@0: re = new RegExp(mgr1);
michael@0: re2 = new RegExp(meq1);
michael@0: re3 = new RegExp("^" + C + v + "[^aeiouwxy]$");
michael@0: if (re.test(stem) || (re2.test(stem) && !(re3.test(stem))))
michael@0: w = stem;
michael@0: }
michael@0: re = /ll$/;
michael@0: re2 = new RegExp(mgr1);
michael@0: if (re.test(w) && re2.test(w)) {
michael@0: re = /.$/;
michael@0: w = w.replace(re,"");
michael@0: }
michael@0:
michael@0: // and turn initial Y back to y
michael@0: if (firstch == "y")
michael@0: w = firstch.toLowerCase() + w.substr(1);
michael@0: return w;
michael@0: }
michael@0: }
michael@0:
michael@0:
michael@0: /**
michael@0: * Search Module
michael@0: */
michael@0: var Search = {
michael@0:
michael@0: _index : null,
michael@0: _queued_query : null,
michael@0: _pulse_status : -1,
michael@0:
michael@0: init : function() {
michael@0: var params = $.getQueryParameters();
michael@0: if (params.q) {
michael@0: var query = params.q[0];
michael@0: $('input[name="q"]')[0].value = query;
michael@0: this.performSearch(query);
michael@0: }
michael@0: },
michael@0:
michael@0: loadIndex : function(url) {
michael@0: $.ajax({type: "GET", url: url, data: null, success: null,
michael@0: dataType: "script", cache: true});
michael@0: },
michael@0:
michael@0: setIndex : function(index) {
michael@0: var q;
michael@0: this._index = index;
michael@0: if ((q = this._queued_query) !== null) {
michael@0: this._queued_query = null;
michael@0: Search.query(q);
michael@0: }
michael@0: },
michael@0:
michael@0: hasIndex : function() {
michael@0: return this._index !== null;
michael@0: },
michael@0:
michael@0: deferQuery : function(query) {
michael@0: this._queued_query = query;
michael@0: },
michael@0:
michael@0: stopPulse : function() {
michael@0: this._pulse_status = 0;
michael@0: },
michael@0:
michael@0: startPulse : function() {
michael@0: if (this._pulse_status >= 0)
michael@0: return;
michael@0: function pulse() {
michael@0: Search._pulse_status = (Search._pulse_status + 1) % 4;
michael@0: var dotString = '';
michael@0: for (var i = 0; i < Search._pulse_status; i++)
michael@0: dotString += '.';
michael@0: Search.dots.text(dotString);
michael@0: if (Search._pulse_status > -1)
michael@0: window.setTimeout(pulse, 500);
michael@0: };
michael@0: pulse();
michael@0: },
michael@0:
michael@0: /**
michael@0: * perform a search for something
michael@0: */
michael@0: performSearch : function(query) {
michael@0: // create the required interface elements
michael@0: this.out = $('#search-results');
michael@0: this.title = $('' + _('Searching') + '
').appendTo(this.out);
michael@0: this.dots = $('').appendTo(this.title);
michael@0: this.status = $('').appendTo(this.out);
michael@0: this.output = $('').appendTo(this.out);
michael@0:
michael@0: $('#search-progress').text(_('Preparing search...'));
michael@0: this.startPulse();
michael@0:
michael@0: // index already loaded, the browser was quick!
michael@0: if (this.hasIndex())
michael@0: this.query(query);
michael@0: else
michael@0: this.deferQuery(query);
michael@0: },
michael@0:
michael@0: query : function(query) {
michael@0: var stopwords = ["and","then","into","it","as","are","in","if","for","no","there","their","was","is","be","to","that","but","they","not","such","with","by","a","on","these","of","will","this","near","the","or","at"];
michael@0:
michael@0: // Stem the searchterms and add them to the correct list
michael@0: var stemmer = new Stemmer();
michael@0: var searchterms = [];
michael@0: var excluded = [];
michael@0: var hlterms = [];
michael@0: var tmp = query.split(/\s+/);
michael@0: var objectterms = [];
michael@0: for (var i = 0; i < tmp.length; i++) {
michael@0: if (tmp[i] != "") {
michael@0: objectterms.push(tmp[i].toLowerCase());
michael@0: }
michael@0:
michael@0: if ($u.indexOf(stopwords, tmp[i]) != -1 || tmp[i].match(/^\d+$/) ||
michael@0: tmp[i] == "") {
michael@0: // skip this "word"
michael@0: continue;
michael@0: }
michael@0: // stem the word
michael@0: var word = stemmer.stemWord(tmp[i]).toLowerCase();
michael@0: // select the correct list
michael@0: if (word[0] == '-') {
michael@0: var toAppend = excluded;
michael@0: word = word.substr(1);
michael@0: }
michael@0: else {
michael@0: var toAppend = searchterms;
michael@0: hlterms.push(tmp[i].toLowerCase());
michael@0: }
michael@0: // only add if not already in the list
michael@0: if (!$.contains(toAppend, word))
michael@0: toAppend.push(word);
michael@0: };
michael@0: var highlightstring = '?highlight=' + $.urlencode(hlterms.join(" "));
michael@0:
michael@0: // console.debug('SEARCH: searching for:');
michael@0: // console.info('required: ', searchterms);
michael@0: // console.info('excluded: ', excluded);
michael@0:
michael@0: // prepare search
michael@0: var filenames = this._index.filenames;
michael@0: var titles = this._index.titles;
michael@0: var terms = this._index.terms;
michael@0: var fileMap = {};
michael@0: var files = null;
michael@0: // different result priorities
michael@0: var importantResults = [];
michael@0: var objectResults = [];
michael@0: var regularResults = [];
michael@0: var unimportantResults = [];
michael@0: $('#search-progress').empty();
michael@0:
michael@0: // lookup as object
michael@0: for (var i = 0; i < objectterms.length; i++) {
michael@0: var others = [].concat(objectterms.slice(0,i),
michael@0: objectterms.slice(i+1, objectterms.length))
michael@0: var results = this.performObjectSearch(objectterms[i], others);
michael@0: // Assume first word is most likely to be the object,
michael@0: // other words more likely to be in description.
michael@0: // Therefore put matches for earlier words first.
michael@0: // (Results are eventually used in reverse order).
michael@0: objectResults = results[0].concat(objectResults);
michael@0: importantResults = results[1].concat(importantResults);
michael@0: unimportantResults = results[2].concat(unimportantResults);
michael@0: }
michael@0:
michael@0: // perform the search on the required terms
michael@0: for (var i = 0; i < searchterms.length; i++) {
michael@0: var word = searchterms[i];
michael@0: // no match but word was a required one
michael@0: if ((files = terms[word]) == null)
michael@0: break;
michael@0: if (files.length == undefined) {
michael@0: files = [files];
michael@0: }
michael@0: // create the mapping
michael@0: for (var j = 0; j < files.length; j++) {
michael@0: var file = files[j];
michael@0: if (file in fileMap)
michael@0: fileMap[file].push(word);
michael@0: else
michael@0: fileMap[file] = [word];
michael@0: }
michael@0: }
michael@0:
michael@0: // now check if the files don't contain excluded terms
michael@0: for (var file in fileMap) {
michael@0: var valid = true;
michael@0:
michael@0: // check if all requirements are matched
michael@0: if (fileMap[file].length != searchterms.length)
michael@0: continue;
michael@0:
michael@0: // ensure that none of the excluded terms is in the
michael@0: // search result.
michael@0: for (var i = 0; i < excluded.length; i++) {
michael@0: if (terms[excluded[i]] == file ||
michael@0: $.contains(terms[excluded[i]] || [], file)) {
michael@0: valid = false;
michael@0: break;
michael@0: }
michael@0: }
michael@0:
michael@0: // if we have still a valid result we can add it
michael@0: // to the result list
michael@0: if (valid)
michael@0: regularResults.push([filenames[file], titles[file], '', null]);
michael@0: }
michael@0:
michael@0: // delete unused variables in order to not waste
michael@0: // memory until list is retrieved completely
michael@0: delete filenames, titles, terms;
michael@0:
michael@0: // now sort the regular results descending by title
michael@0: regularResults.sort(function(a, b) {
michael@0: var left = a[1].toLowerCase();
michael@0: var right = b[1].toLowerCase();
michael@0: return (left > right) ? -1 : ((left < right) ? 1 : 0);
michael@0: });
michael@0:
michael@0: // combine all results
michael@0: var results = unimportantResults.concat(regularResults)
michael@0: .concat(objectResults).concat(importantResults);
michael@0:
michael@0: // print the results
michael@0: var resultCount = results.length;
michael@0: function displayNextItem() {
michael@0: // results left, load the summary and display it
michael@0: if (results.length) {
michael@0: var item = results.pop();
michael@0: var listItem = $('');
michael@0: if (DOCUMENTATION_OPTIONS.FILE_SUFFIX == '') {
michael@0: // dirhtml builder
michael@0: var dirname = item[0] + '/';
michael@0: if (dirname.match(/\/index\/$/)) {
michael@0: dirname = dirname.substring(0, dirname.length-6);
michael@0: } else if (dirname == 'index/') {
michael@0: dirname = '';
michael@0: }
michael@0: listItem.append($('').attr('href',
michael@0: DOCUMENTATION_OPTIONS.URL_ROOT + dirname +
michael@0: highlightstring + item[2]).html(item[1]));
michael@0: } else {
michael@0: // normal html builders
michael@0: listItem.append($('').attr('href',
michael@0: item[0] + DOCUMENTATION_OPTIONS.FILE_SUFFIX +
michael@0: highlightstring + item[2]).html(item[1]));
michael@0: }
michael@0: if (item[3]) {
michael@0: listItem.append($(' (' + item[3] + ')'));
michael@0: Search.output.append(listItem);
michael@0: listItem.slideDown(5, function() {
michael@0: displayNextItem();
michael@0: });
michael@0: } else if (DOCUMENTATION_OPTIONS.HAS_SOURCE) {
michael@0: $.get(DOCUMENTATION_OPTIONS.URL_ROOT + '_sources/' +
michael@0: item[0] + '.txt', function(data) {
michael@0: if (data != '') {
michael@0: listItem.append($.makeSearchSummary(data, searchterms, hlterms));
michael@0: Search.output.append(listItem);
michael@0: }
michael@0: listItem.slideDown(5, function() {
michael@0: displayNextItem();
michael@0: });
michael@0: }, "text");
michael@0: } else {
michael@0: // no source available, just display title
michael@0: Search.output.append(listItem);
michael@0: listItem.slideDown(5, function() {
michael@0: displayNextItem();
michael@0: });
michael@0: }
michael@0: }
michael@0: // search finished, update title and status message
michael@0: else {
michael@0: Search.stopPulse();
michael@0: Search.title.text(_('Search Results'));
michael@0: if (!resultCount)
michael@0: 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.'));
michael@0: else
michael@0: Search.status.text(_('Search finished, found %s page(s) matching the search query.').replace('%s', resultCount));
michael@0: Search.status.fadeIn(500);
michael@0: }
michael@0: }
michael@0: displayNextItem();
michael@0: },
michael@0:
michael@0: performObjectSearch : function(object, otherterms) {
michael@0: var filenames = this._index.filenames;
michael@0: var objects = this._index.objects;
michael@0: var objnames = this._index.objnames;
michael@0: var titles = this._index.titles;
michael@0:
michael@0: var importantResults = [];
michael@0: var objectResults = [];
michael@0: var unimportantResults = [];
michael@0:
michael@0: for (var prefix in objects) {
michael@0: for (var name in objects[prefix]) {
michael@0: var fullname = (prefix ? prefix + '.' : '') + name;
michael@0: if (fullname.toLowerCase().indexOf(object) > -1) {
michael@0: var match = objects[prefix][name];
michael@0: var objname = objnames[match[1]][2];
michael@0: var title = titles[match[0]];
michael@0: // If more than one term searched for, we require other words to be
michael@0: // found in the name/title/description
michael@0: if (otherterms.length > 0) {
michael@0: var haystack = (prefix + ' ' + name + ' ' +
michael@0: objname + ' ' + title).toLowerCase();
michael@0: var allfound = true;
michael@0: for (var i = 0; i < otherterms.length; i++) {
michael@0: if (haystack.indexOf(otherterms[i]) == -1) {
michael@0: allfound = false;
michael@0: break;
michael@0: }
michael@0: }
michael@0: if (!allfound) {
michael@0: continue;
michael@0: }
michael@0: }
michael@0: var descr = objname + _(', in ') + title;
michael@0: anchor = match[3];
michael@0: if (anchor == '')
michael@0: anchor = fullname;
michael@0: else if (anchor == '-')
michael@0: anchor = objnames[match[1]][1] + '-' + fullname;
michael@0: result = [filenames[match[0]], fullname, '#'+anchor, descr];
michael@0: switch (match[2]) {
michael@0: case 1: objectResults.push(result); break;
michael@0: case 0: importantResults.push(result); break;
michael@0: case 2: unimportantResults.push(result); break;
michael@0: }
michael@0: }
michael@0: }
michael@0: }
michael@0:
michael@0: // sort results descending
michael@0: objectResults.sort(function(a, b) {
michael@0: return (a[1] > b[1]) ? -1 : ((a[1] < b[1]) ? 1 : 0);
michael@0: });
michael@0:
michael@0: importantResults.sort(function(a, b) {
michael@0: return (a[1] > b[1]) ? -1 : ((a[1] < b[1]) ? 1 : 0);
michael@0: });
michael@0:
michael@0: unimportantResults.sort(function(a, b) {
michael@0: return (a[1] > b[1]) ? -1 : ((a[1] < b[1]) ? 1 : 0);
michael@0: });
michael@0:
michael@0: return [importantResults, objectResults, unimportantResults]
michael@0: }
michael@0: }
michael@0:
michael@0: $(document).ready(function() {
michael@0: Search.init();
michael@0: });