python/mock-1.0.0/html/_static/searchtools.js

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial