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