searchtools.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /*
  2. * searchtools.js_t
  3. * ~~~~~~~~~~~~~~~~
  4. *
  5. * Sphinx JavaScript utilities for the full-text search.
  6. *
  7. * :copyright: Copyright 2007-2017 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. var splitChars = (function() {
  204. var result = {};
  205. var singles = [96, 180, 187, 191, 215, 247, 749, 885, 903, 907, 909, 930, 1014, 1648,
  206. 1748, 1809, 2416, 2473, 2481, 2526, 2601, 2609, 2612, 2615, 2653, 2702,
  207. 2706, 2729, 2737, 2740, 2857, 2865, 2868, 2910, 2928, 2948, 2961, 2971,
  208. 2973, 3085, 3089, 3113, 3124, 3213, 3217, 3241, 3252, 3295, 3341, 3345,
  209. 3369, 3506, 3516, 3633, 3715, 3721, 3736, 3744, 3748, 3750, 3756, 3761,
  210. 3781, 3912, 4239, 4347, 4681, 4695, 4697, 4745, 4785, 4799, 4801, 4823,
  211. 4881, 5760, 5901, 5997, 6313, 7405, 8024, 8026, 8028, 8030, 8117, 8125,
  212. 8133, 8181, 8468, 8485, 8487, 8489, 8494, 8527, 11311, 11359, 11687, 11695,
  213. 11703, 11711, 11719, 11727, 11735, 12448, 12539, 43010, 43014, 43019, 43587,
  214. 43696, 43713, 64286, 64297, 64311, 64317, 64319, 64322, 64325, 65141];
  215. var i, j, start, end;
  216. for (i = 0; i < singles.length; i++) {
  217. result[singles[i]] = true;
  218. }
  219. var ranges = [[0, 47], [58, 64], [91, 94], [123, 169], [171, 177], [182, 184], [706, 709],
  220. [722, 735], [741, 747], [751, 879], [888, 889], [894, 901], [1154, 1161],
  221. [1318, 1328], [1367, 1368], [1370, 1376], [1416, 1487], [1515, 1519], [1523, 1568],
  222. [1611, 1631], [1642, 1645], [1750, 1764], [1767, 1773], [1789, 1790], [1792, 1807],
  223. [1840, 1868], [1958, 1968], [1970, 1983], [2027, 2035], [2038, 2041], [2043, 2047],
  224. [2070, 2073], [2075, 2083], [2085, 2087], [2089, 2307], [2362, 2364], [2366, 2383],
  225. [2385, 2391], [2402, 2405], [2419, 2424], [2432, 2436], [2445, 2446], [2449, 2450],
  226. [2483, 2485], [2490, 2492], [2494, 2509], [2511, 2523], [2530, 2533], [2546, 2547],
  227. [2554, 2564], [2571, 2574], [2577, 2578], [2618, 2648], [2655, 2661], [2672, 2673],
  228. [2677, 2692], [2746, 2748], [2750, 2767], [2769, 2783], [2786, 2789], [2800, 2820],
  229. [2829, 2830], [2833, 2834], [2874, 2876], [2878, 2907], [2914, 2917], [2930, 2946],
  230. [2955, 2957], [2966, 2968], [2976, 2978], [2981, 2983], [2987, 2989], [3002, 3023],
  231. [3025, 3045], [3059, 3076], [3130, 3132], [3134, 3159], [3162, 3167], [3170, 3173],
  232. [3184, 3191], [3199, 3204], [3258, 3260], [3262, 3293], [3298, 3301], [3312, 3332],
  233. [3386, 3388], [3390, 3423], [3426, 3429], [3446, 3449], [3456, 3460], [3479, 3481],
  234. [3518, 3519], [3527, 3584], [3636, 3647], [3655, 3663], [3674, 3712], [3717, 3718],
  235. [3723, 3724], [3726, 3731], [3752, 3753], [3764, 3772], [3774, 3775], [3783, 3791],
  236. [3802, 3803], [3806, 3839], [3841, 3871], [3892, 3903], [3949, 3975], [3980, 4095],
  237. [4139, 4158], [4170, 4175], [4182, 4185], [4190, 4192], [4194, 4196], [4199, 4205],
  238. [4209, 4212], [4226, 4237], [4250, 4255], [4294, 4303], [4349, 4351], [4686, 4687],
  239. [4702, 4703], [4750, 4751], [4790, 4791], [4806, 4807], [4886, 4887], [4955, 4968],
  240. [4989, 4991], [5008, 5023], [5109, 5120], [5741, 5742], [5787, 5791], [5867, 5869],
  241. [5873, 5887], [5906, 5919], [5938, 5951], [5970, 5983], [6001, 6015], [6068, 6102],
  242. [6104, 6107], [6109, 6111], [6122, 6127], [6138, 6159], [6170, 6175], [6264, 6271],
  243. [6315, 6319], [6390, 6399], [6429, 6469], [6510, 6511], [6517, 6527], [6572, 6592],
  244. [6600, 6607], [6619, 6655], [6679, 6687], [6741, 6783], [6794, 6799], [6810, 6822],
  245. [6824, 6916], [6964, 6980], [6988, 6991], [7002, 7042], [7073, 7085], [7098, 7167],
  246. [7204, 7231], [7242, 7244], [7294, 7400], [7410, 7423], [7616, 7679], [7958, 7959],
  247. [7966, 7967], [8006, 8007], [8014, 8015], [8062, 8063], [8127, 8129], [8141, 8143],
  248. [8148, 8149], [8156, 8159], [8173, 8177], [8189, 8303], [8306, 8307], [8314, 8318],
  249. [8330, 8335], [8341, 8449], [8451, 8454], [8456, 8457], [8470, 8472], [8478, 8483],
  250. [8506, 8507], [8512, 8516], [8522, 8525], [8586, 9311], [9372, 9449], [9472, 10101],
  251. [10132, 11263], [11493, 11498], [11503, 11516], [11518, 11519], [11558, 11567],
  252. [11622, 11630], [11632, 11647], [11671, 11679], [11743, 11822], [11824, 12292],
  253. [12296, 12320], [12330, 12336], [12342, 12343], [12349, 12352], [12439, 12444],
  254. [12544, 12548], [12590, 12592], [12687, 12689], [12694, 12703], [12728, 12783],
  255. [12800, 12831], [12842, 12880], [12896, 12927], [12938, 12976], [12992, 13311],
  256. [19894, 19967], [40908, 40959], [42125, 42191], [42238, 42239], [42509, 42511],
  257. [42540, 42559], [42592, 42593], [42607, 42622], [42648, 42655], [42736, 42774],
  258. [42784, 42785], [42889, 42890], [42893, 43002], [43043, 43055], [43062, 43071],
  259. [43124, 43137], [43188, 43215], [43226, 43249], [43256, 43258], [43260, 43263],
  260. [43302, 43311], [43335, 43359], [43389, 43395], [43443, 43470], [43482, 43519],
  261. [43561, 43583], [43596, 43599], [43610, 43615], [43639, 43641], [43643, 43647],
  262. [43698, 43700], [43703, 43704], [43710, 43711], [43715, 43738], [43742, 43967],
  263. [44003, 44015], [44026, 44031], [55204, 55215], [55239, 55242], [55292, 55295],
  264. [57344, 63743], [64046, 64047], [64110, 64111], [64218, 64255], [64263, 64274],
  265. [64280, 64284], [64434, 64466], [64830, 64847], [64912, 64913], [64968, 65007],
  266. [65020, 65135], [65277, 65295], [65306, 65312], [65339, 65344], [65371, 65381],
  267. [65471, 65473], [65480, 65481], [65488, 65489], [65496, 65497]];
  268. for (i = 0; i < ranges.length; i++) {
  269. start = ranges[i][0];
  270. end = ranges[i][1];
  271. for (j = start; j <= end; j++) {
  272. result[j] = true;
  273. }
  274. }
  275. return result;
  276. })();
  277. function splitQuery(query) {
  278. var result = [];
  279. var start = -1;
  280. for (var i = 0; i < query.length; i++) {
  281. if (splitChars[query.charCodeAt(i)]) {
  282. if (start !== -1) {
  283. result.push(query.slice(start, i));
  284. start = -1;
  285. }
  286. } else if (start === -1) {
  287. start = i;
  288. }
  289. }
  290. if (start !== -1) {
  291. result.push(query.slice(start));
  292. }
  293. return result;
  294. }
  295. /**
  296. * Search Module
  297. */
  298. var Search = {
  299. _index : null,
  300. _queued_query : null,
  301. _pulse_status : -1,
  302. init : function() {
  303. var params = $.getQueryParameters();
  304. if (params.q) {
  305. var query = params.q[0];
  306. $('input[name="q"]')[0].value = query;
  307. this.performSearch(query);
  308. }
  309. },
  310. loadIndex : function(url) {
  311. $.ajax({type: "GET", url: url, data: null,
  312. dataType: "script", cache: true,
  313. complete: function(jqxhr, textstatus) {
  314. if (textstatus != "success") {
  315. document.getElementById("searchindexloader").src = url;
  316. }
  317. }});
  318. },
  319. setIndex : function(index) {
  320. var q;
  321. this._index = index;
  322. if ((q = this._queued_query) !== null) {
  323. this._queued_query = null;
  324. Search.query(q);
  325. }
  326. },
  327. hasIndex : function() {
  328. return this._index !== null;
  329. },
  330. deferQuery : function(query) {
  331. this._queued_query = query;
  332. },
  333. stopPulse : function() {
  334. this._pulse_status = 0;
  335. },
  336. startPulse : function() {
  337. if (this._pulse_status >= 0)
  338. return;
  339. function pulse() {
  340. var i;
  341. Search._pulse_status = (Search._pulse_status + 1) % 4;
  342. var dotString = '';
  343. for (i = 0; i < Search._pulse_status; i++)
  344. dotString += '.';
  345. Search.dots.text(dotString);
  346. if (Search._pulse_status > -1)
  347. window.setTimeout(pulse, 500);
  348. }
  349. pulse();
  350. },
  351. /**
  352. * perform a search for something (or wait until index is loaded)
  353. */
  354. performSearch : function(query) {
  355. // create the required interface elements
  356. this.out = $('#search-results');
  357. this.title = $('<h2>' + _('Searching') + '</h2>').appendTo(this.out);
  358. this.dots = $('<span></span>').appendTo(this.title);
  359. this.status = $('<p style="display: none"></p>').appendTo(this.out);
  360. this.output = $('<ul class="search"/>').appendTo(this.out);
  361. $('#search-progress').text(_('Preparing search...'));
  362. this.startPulse();
  363. // index already loaded, the browser was quick!
  364. if (this.hasIndex())
  365. this.query(query);
  366. else
  367. this.deferQuery(query);
  368. },
  369. /**
  370. * execute search (requires search index to be loaded)
  371. */
  372. query : function(query) {
  373. var i;
  374. 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"];
  375. // stem the searchterms and add them to the correct list
  376. var stemmer = new Stemmer();
  377. var searchterms = [];
  378. var excluded = [];
  379. var hlterms = [];
  380. var tmp = splitQuery(query);
  381. var objectterms = [];
  382. for (i = 0; i < tmp.length; i++) {
  383. if (tmp[i] !== "") {
  384. objectterms.push(tmp[i].toLowerCase());
  385. }
  386. if ($u.indexOf(stopwords, tmp[i].toLowerCase()) != -1 || tmp[i].match(/^\d+$/) ||
  387. tmp[i] === "") {
  388. // skip this "word"
  389. continue;
  390. }
  391. // stem the word
  392. var word = stemmer.stemWord(tmp[i].toLowerCase());
  393. // prevent stemmer from cutting word smaller than two chars
  394. if(word.length < 3 && tmp[i].length >= 3) {
  395. word = tmp[i];
  396. }
  397. var toAppend;
  398. // select the correct list
  399. if (word[0] == '-') {
  400. toAppend = excluded;
  401. word = word.substr(1);
  402. }
  403. else {
  404. toAppend = searchterms;
  405. hlterms.push(tmp[i].toLowerCase());
  406. }
  407. // only add if not already in the list
  408. if (!$u.contains(toAppend, word))
  409. toAppend.push(word);
  410. }
  411. var highlightstring = '?highlight=' + $.urlencode(hlterms.join(" "));
  412. // console.debug('SEARCH: searching for:');
  413. // console.info('required: ', searchterms);
  414. // console.info('excluded: ', excluded);
  415. // prepare search
  416. var terms = this._index.terms;
  417. var titleterms = this._index.titleterms;
  418. // array of [filename, title, anchor, descr, score]
  419. var results = [];
  420. $('#search-progress').empty();
  421. // lookup as object
  422. for (i = 0; i < objectterms.length; i++) {
  423. var others = [].concat(objectterms.slice(0, i),
  424. objectterms.slice(i+1, objectterms.length));
  425. results = results.concat(this.performObjectSearch(objectterms[i], others));
  426. }
  427. // lookup as search terms in fulltext
  428. results = results.concat(this.performTermsSearch(searchterms, excluded, terms, titleterms));
  429. // let the scorer override scores with a custom scoring function
  430. if (Scorer.score) {
  431. for (i = 0; i < results.length; i++)
  432. results[i][4] = Scorer.score(results[i]);
  433. }
  434. // now sort the results by score (in opposite order of appearance, since the
  435. // display function below uses pop() to retrieve items) and then
  436. // alphabetically
  437. results.sort(function(a, b) {
  438. var left = a[4];
  439. var right = b[4];
  440. if (left > right) {
  441. return 1;
  442. } else if (left < right) {
  443. return -1;
  444. } else {
  445. // same score: sort alphabetically
  446. left = a[1].toLowerCase();
  447. right = b[1].toLowerCase();
  448. return (left > right) ? -1 : ((left < right) ? 1 : 0);
  449. }
  450. });
  451. // for debugging
  452. //Search.lastresults = results.slice(); // a copy
  453. //console.info('search results:', Search.lastresults);
  454. // print the results
  455. var resultCount = results.length;
  456. function displayNextItem() {
  457. // results left, load the summary and display it
  458. if (results.length) {
  459. var item = results.pop();
  460. var listItem = $('<li style="display:none"></li>');
  461. if (DOCUMENTATION_OPTIONS.FILE_SUFFIX === '') {
  462. // dirhtml builder
  463. var dirname = item[0] + '/';
  464. if (dirname.match(/\/index\/$/)) {
  465. dirname = dirname.substring(0, dirname.length-6);
  466. } else if (dirname == 'index/') {
  467. dirname = '';
  468. }
  469. listItem.append($('<a/>').attr('href',
  470. DOCUMENTATION_OPTIONS.URL_ROOT + dirname +
  471. highlightstring + item[2]).html(item[1]));
  472. } else {
  473. // normal html builders
  474. listItem.append($('<a/>').attr('href',
  475. item[0] + DOCUMENTATION_OPTIONS.FILE_SUFFIX +
  476. highlightstring + item[2]).html(item[1]));
  477. }
  478. if (item[3]) {
  479. listItem.append($('<span> (' + item[3] + ')</span>'));
  480. Search.output.append(listItem);
  481. listItem.slideDown(5, function() {
  482. displayNextItem();
  483. });
  484. } else if (DOCUMENTATION_OPTIONS.HAS_SOURCE) {
  485. var suffix = DOCUMENTATION_OPTIONS.SOURCELINK_SUFFIX;
  486. $.ajax({url: DOCUMENTATION_OPTIONS.URL_ROOT + '_sources/' + item[5] + (item[5].slice(-suffix.length) === suffix ? '' : suffix),
  487. dataType: "text",
  488. complete: function(jqxhr, textstatus) {
  489. var data = jqxhr.responseText;
  490. if (data !== '' && data !== undefined) {
  491. listItem.append(Search.makeSearchSummary(data, searchterms, hlterms));
  492. }
  493. Search.output.append(listItem);
  494. listItem.slideDown(5, function() {
  495. displayNextItem();
  496. });
  497. }});
  498. } else {
  499. // no source available, just display title
  500. Search.output.append(listItem);
  501. listItem.slideDown(5, function() {
  502. displayNextItem();
  503. });
  504. }
  505. }
  506. // search finished, update title and status message
  507. else {
  508. Search.stopPulse();
  509. Search.title.text(_('Search Results'));
  510. if (!resultCount)
  511. 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.'));
  512. else
  513. Search.status.text(_('Search finished, found %s page(s) matching the search query.').replace('%s', resultCount));
  514. Search.status.fadeIn(500);
  515. }
  516. }
  517. displayNextItem();
  518. },
  519. /**
  520. * search for object names
  521. */
  522. performObjectSearch : function(object, otherterms) {
  523. var filenames = this._index.filenames;
  524. var docnames = this._index.docnames;
  525. var objects = this._index.objects;
  526. var objnames = this._index.objnames;
  527. var titles = this._index.titles;
  528. var i;
  529. var results = [];
  530. for (var prefix in objects) {
  531. for (var name in objects[prefix]) {
  532. var fullname = (prefix ? prefix + '.' : '') + name;
  533. if (fullname.toLowerCase().indexOf(object) > -1) {
  534. var score = 0;
  535. var parts = fullname.split('.');
  536. // check for different match types: exact matches of full name or
  537. // "last name" (i.e. last dotted part)
  538. if (fullname == object || parts[parts.length - 1] == object) {
  539. score += Scorer.objNameMatch;
  540. // matches in last name
  541. } else if (parts[parts.length - 1].indexOf(object) > -1) {
  542. score += Scorer.objPartialMatch;
  543. }
  544. var match = objects[prefix][name];
  545. var objname = objnames[match[1]][2];
  546. var title = titles[match[0]];
  547. // If more than one term searched for, we require other words to be
  548. // found in the name/title/description
  549. if (otherterms.length > 0) {
  550. var haystack = (prefix + ' ' + name + ' ' +
  551. objname + ' ' + title).toLowerCase();
  552. var allfound = true;
  553. for (i = 0; i < otherterms.length; i++) {
  554. if (haystack.indexOf(otherterms[i]) == -1) {
  555. allfound = false;
  556. break;
  557. }
  558. }
  559. if (!allfound) {
  560. continue;
  561. }
  562. }
  563. var descr = objname + _(', in ') + title;
  564. var anchor = match[3];
  565. if (anchor === '')
  566. anchor = fullname;
  567. else if (anchor == '-')
  568. anchor = objnames[match[1]][1] + '-' + fullname;
  569. // add custom score for some objects according to scorer
  570. if (Scorer.objPrio.hasOwnProperty(match[2])) {
  571. score += Scorer.objPrio[match[2]];
  572. } else {
  573. score += Scorer.objPrioDefault;
  574. }
  575. results.push([docnames[match[0]], fullname, '#'+anchor, descr, score, filenames[match[0]]]);
  576. }
  577. }
  578. }
  579. return results;
  580. },
  581. /**
  582. * search for full-text terms in the index
  583. */
  584. performTermsSearch : function(searchterms, excluded, terms, titleterms) {
  585. var docnames = this._index.docnames;
  586. var filenames = this._index.filenames;
  587. var titles = this._index.titles;
  588. var i, j, file;
  589. var fileMap = {};
  590. var scoreMap = {};
  591. var results = [];
  592. // perform the search on the required terms
  593. for (i = 0; i < searchterms.length; i++) {
  594. var word = searchterms[i];
  595. var files = [];
  596. var _o = [
  597. {files: terms[word], score: Scorer.term},
  598. {files: titleterms[word], score: Scorer.title}
  599. ];
  600. // no match but word was a required one
  601. if ($u.every(_o, function(o){return o.files === undefined;})) {
  602. break;
  603. }
  604. // found search word in contents
  605. $u.each(_o, function(o) {
  606. var _files = o.files;
  607. if (_files === undefined)
  608. return
  609. if (_files.length === undefined)
  610. _files = [_files];
  611. files = files.concat(_files);
  612. // set score for the word in each file to Scorer.term
  613. for (j = 0; j < _files.length; j++) {
  614. file = _files[j];
  615. if (!(file in scoreMap))
  616. scoreMap[file] = {}
  617. scoreMap[file][word] = o.score;
  618. }
  619. });
  620. // create the mapping
  621. for (j = 0; j < files.length; j++) {
  622. file = files[j];
  623. if (file in fileMap)
  624. fileMap[file].push(word);
  625. else
  626. fileMap[file] = [word];
  627. }
  628. }
  629. // now check if the files don't contain excluded terms
  630. for (file in fileMap) {
  631. var valid = true;
  632. // check if all requirements are matched
  633. if (fileMap[file].length != searchterms.length)
  634. continue;
  635. // ensure that none of the excluded terms is in the search result
  636. for (i = 0; i < excluded.length; i++) {
  637. if (terms[excluded[i]] == file ||
  638. titleterms[excluded[i]] == file ||
  639. $u.contains(terms[excluded[i]] || [], file) ||
  640. $u.contains(titleterms[excluded[i]] || [], file)) {
  641. valid = false;
  642. break;
  643. }
  644. }
  645. // if we have still a valid result we can add it to the result list
  646. if (valid) {
  647. // select one (max) score for the file.
  648. // for better ranking, we should calculate ranking by using words statistics like basic tf-idf...
  649. var score = $u.max($u.map(fileMap[file], function(w){return scoreMap[file][w]}));
  650. results.push([docnames[file], titles[file], '', null, score, filenames[file]]);
  651. }
  652. }
  653. return results;
  654. },
  655. /**
  656. * helper function to return a node containing the
  657. * search summary for a given text. keywords is a list
  658. * of stemmed words, hlwords is the list of normal, unstemmed
  659. * words. the first one is used to find the occurrence, the
  660. * latter for highlighting it.
  661. */
  662. makeSearchSummary : function(text, keywords, hlwords) {
  663. var textLower = text.toLowerCase();
  664. var start = 0;
  665. $.each(keywords, function() {
  666. var i = textLower.indexOf(this.toLowerCase());
  667. if (i > -1)
  668. start = i;
  669. });
  670. start = Math.max(start - 120, 0);
  671. var excerpt = ((start > 0) ? '...' : '') +
  672. $.trim(text.substr(start, 240)) +
  673. ((start + 240 - text.length) ? '...' : '');
  674. var rv = $('<div class="context"></div>').text(excerpt);
  675. $.each(hlwords, function() {
  676. rv = rv.highlightText(this, 'highlighted');
  677. });
  678. return rv;
  679. }
  680. };
  681. $(document).ready(function() {
  682. Search.init();
  683. });