searchtools.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*
  2. * searchtools.js
  3. * ~~~~~~~~~~~~~~~~
  4. *
  5. * Sphinx JavaScript utilities for the full-text search.
  6. *
  7. * :copyright: Copyright 2007-2022 by the Sphinx team, see AUTHORS.
  8. * :license: BSD, see LICENSE for details.
  9. *
  10. */
  11. "use strict";
  12. /**
  13. * Simple result scoring code.
  14. */
  15. if (typeof Scorer === "undefined") {
  16. var Scorer = {
  17. // Implement the following function to further tweak the score for each result
  18. // The function takes a result array [docname, title, anchor, descr, score, filename]
  19. // and returns the new score.
  20. /*
  21. score: result => {
  22. const [docname, title, anchor, descr, score, filename] = result
  23. return score
  24. },
  25. */
  26. // query matches the full name of an object
  27. objNameMatch: 11,
  28. // or matches in the last dotted part of the object name
  29. objPartialMatch: 6,
  30. // Additive scores depending on the priority of the object
  31. objPrio: {
  32. 0: 15, // used to be importantResults
  33. 1: 5, // used to be objectResults
  34. 2: -5, // used to be unimportantResults
  35. },
  36. // Used when the priority is not in the mapping.
  37. objPrioDefault: 0,
  38. // query found in title
  39. title: 15,
  40. partialTitle: 7,
  41. // query found in terms
  42. term: 5,
  43. partialTerm: 2,
  44. };
  45. }
  46. const _removeChildren = (element) => {
  47. while (element && element.lastChild) element.removeChild(element.lastChild);
  48. };
  49. /**
  50. * See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
  51. */
  52. const _escapeRegExp = (string) =>
  53. string.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
  54. const _displayItem = (item, searchTerms) => {
  55. const docBuilder = DOCUMENTATION_OPTIONS.BUILDER;
  56. const docUrlRoot = DOCUMENTATION_OPTIONS.URL_ROOT;
  57. const docFileSuffix = DOCUMENTATION_OPTIONS.FILE_SUFFIX;
  58. const docLinkSuffix = DOCUMENTATION_OPTIONS.LINK_SUFFIX;
  59. const showSearchSummary = DOCUMENTATION_OPTIONS.SHOW_SEARCH_SUMMARY;
  60. const [docName, title, anchor, descr, score, _filename] = item;
  61. let listItem = document.createElement("li");
  62. let requestUrl;
  63. let linkUrl;
  64. if (docBuilder === "dirhtml") {
  65. // dirhtml builder
  66. let dirname = docName + "/";
  67. if (dirname.match(/\/index\/$/))
  68. dirname = dirname.substring(0, dirname.length - 6);
  69. else if (dirname === "index/") dirname = "";
  70. requestUrl = docUrlRoot + dirname;
  71. linkUrl = requestUrl;
  72. } else {
  73. // normal html builders
  74. requestUrl = docUrlRoot + docName + docFileSuffix;
  75. linkUrl = docName + docLinkSuffix;
  76. }
  77. let linkEl = listItem.appendChild(document.createElement("a"));
  78. linkEl.href = linkUrl + anchor;
  79. linkEl.dataset.score = score;
  80. linkEl.innerHTML = title;
  81. if (descr)
  82. listItem.appendChild(document.createElement("span")).innerHTML =
  83. " (" + descr + ")";
  84. else if (showSearchSummary)
  85. fetch(requestUrl)
  86. .then((responseData) => responseData.text())
  87. .then((data) => {
  88. if (data)
  89. listItem.appendChild(
  90. Search.makeSearchSummary(data, searchTerms)
  91. );
  92. });
  93. Search.output.appendChild(listItem);
  94. };
  95. const _finishSearch = (resultCount) => {
  96. Search.stopPulse();
  97. Search.title.innerText = _("Search Results");
  98. if (!resultCount)
  99. Search.status.innerText = Documentation.gettext(
  100. "Your search did not match any documents. Please make sure that all words are spelled correctly and that you've selected enough categories."
  101. );
  102. else
  103. Search.status.innerText = _(
  104. `Search finished, found ${resultCount} page(s) matching the search query.`
  105. );
  106. };
  107. const _displayNextItem = (
  108. results,
  109. resultCount,
  110. searchTerms
  111. ) => {
  112. // results left, load the summary and display it
  113. // this is intended to be dynamic (don't sub resultsCount)
  114. if (results.length) {
  115. _displayItem(results.pop(), searchTerms);
  116. setTimeout(
  117. () => _displayNextItem(results, resultCount, searchTerms),
  118. 5
  119. );
  120. }
  121. // search finished, update title and status message
  122. else _finishSearch(resultCount);
  123. };
  124. /**
  125. * Default splitQuery function. Can be overridden in ``sphinx.search`` with a
  126. * custom function per language.
  127. *
  128. * The regular expression works by splitting the string on consecutive characters
  129. * that are not Unicode letters, numbers, underscores, or emoji characters.
  130. * This is the same as ``\W+`` in Python, preserving the surrogate pair area.
  131. */
  132. if (typeof splitQuery === "undefined") {
  133. var splitQuery = (query) => query
  134. .split(/[^\p{Letter}\p{Number}_\p{Emoji_Presentation}]+/gu)
  135. .filter(term => term) // remove remaining empty strings
  136. }
  137. /**
  138. * Search Module
  139. */
  140. const Search = {
  141. _index: null,
  142. _queued_query: null,
  143. _pulse_status: -1,
  144. htmlToText: (htmlString) => {
  145. const htmlElement = new DOMParser().parseFromString(htmlString, 'text/html');
  146. htmlElement.querySelectorAll(".headerlink").forEach((el) => { el.remove() });
  147. const docContent = htmlElement.querySelector('[role="main"]');
  148. if (docContent !== undefined) return docContent.textContent;
  149. console.warn(
  150. "Content block not found. Sphinx search tries to obtain it via '[role=main]'. Could you check your theme or template."
  151. );
  152. return "";
  153. },
  154. init: () => {
  155. const query = new URLSearchParams(window.location.search).get("q");
  156. document
  157. .querySelectorAll('input[name="q"]')
  158. .forEach((el) => (el.value = query));
  159. if (query) Search.performSearch(query);
  160. },
  161. loadIndex: (url) =>
  162. (document.body.appendChild(document.createElement("script")).src = url),
  163. setIndex: (index) => {
  164. Search._index = index;
  165. if (Search._queued_query !== null) {
  166. const query = Search._queued_query;
  167. Search._queued_query = null;
  168. Search.query(query);
  169. }
  170. },
  171. hasIndex: () => Search._index !== null,
  172. deferQuery: (query) => (Search._queued_query = query),
  173. stopPulse: () => (Search._pulse_status = -1),
  174. startPulse: () => {
  175. if (Search._pulse_status >= 0) return;
  176. const pulse = () => {
  177. Search._pulse_status = (Search._pulse_status + 1) % 4;
  178. Search.dots.innerText = ".".repeat(Search._pulse_status);
  179. if (Search._pulse_status >= 0) window.setTimeout(pulse, 500);
  180. };
  181. pulse();
  182. },
  183. /**
  184. * perform a search for something (or wait until index is loaded)
  185. */
  186. performSearch: (query) => {
  187. // create the required interface elements
  188. const searchText = document.createElement("h2");
  189. searchText.textContent = _("Searching");
  190. const searchSummary = document.createElement("p");
  191. searchSummary.classList.add("search-summary");
  192. searchSummary.innerText = "";
  193. const searchList = document.createElement("ul");
  194. searchList.classList.add("search");
  195. const out = document.getElementById("search-results");
  196. Search.title = out.appendChild(searchText);
  197. Search.dots = Search.title.appendChild(document.createElement("span"));
  198. Search.status = out.appendChild(searchSummary);
  199. Search.output = out.appendChild(searchList);
  200. const searchProgress = document.getElementById("search-progress");
  201. // Some themes don't use the search progress node
  202. if (searchProgress) {
  203. searchProgress.innerText = _("Preparing search...");
  204. }
  205. Search.startPulse();
  206. // index already loaded, the browser was quick!
  207. if (Search.hasIndex()) Search.query(query);
  208. else Search.deferQuery(query);
  209. },
  210. /**
  211. * execute search (requires search index to be loaded)
  212. */
  213. query: (query) => {
  214. const filenames = Search._index.filenames;
  215. const docNames = Search._index.docnames;
  216. const titles = Search._index.titles;
  217. const allTitles = Search._index.alltitles;
  218. const indexEntries = Search._index.indexentries;
  219. // stem the search terms and add them to the correct list
  220. const stemmer = new Stemmer();
  221. const searchTerms = new Set();
  222. const excludedTerms = new Set();
  223. const highlightTerms = new Set();
  224. const objectTerms = new Set(splitQuery(query.toLowerCase().trim()));
  225. splitQuery(query.trim()).forEach((queryTerm) => {
  226. const queryTermLower = queryTerm.toLowerCase();
  227. // maybe skip this "word"
  228. // stopwords array is from language_data.js
  229. if (
  230. stopwords.indexOf(queryTermLower) !== -1 ||
  231. queryTerm.match(/^\d+$/)
  232. )
  233. return;
  234. // stem the word
  235. let word = stemmer.stemWord(queryTermLower);
  236. // select the correct list
  237. if (word[0] === "-") excludedTerms.add(word.substr(1));
  238. else {
  239. searchTerms.add(word);
  240. highlightTerms.add(queryTermLower);
  241. }
  242. });
  243. if (SPHINX_HIGHLIGHT_ENABLED) { // set in sphinx_highlight.js
  244. localStorage.setItem("sphinx_highlight_terms", [...highlightTerms].join(" "))
  245. }
  246. // console.debug("SEARCH: searching for:");
  247. // console.info("required: ", [...searchTerms]);
  248. // console.info("excluded: ", [...excludedTerms]);
  249. // array of [docname, title, anchor, descr, score, filename]
  250. let results = [];
  251. _removeChildren(document.getElementById("search-progress"));
  252. const queryLower = query.toLowerCase();
  253. for (const [title, foundTitles] of Object.entries(allTitles)) {
  254. if (title.toLowerCase().includes(queryLower) && (queryLower.length >= title.length/2)) {
  255. for (const [file, id] of foundTitles) {
  256. let score = Math.round(100 * queryLower.length / title.length)
  257. results.push([
  258. docNames[file],
  259. titles[file] !== title ? `${titles[file]} > ${title}` : title,
  260. id !== null ? "#" + id : "",
  261. null,
  262. score,
  263. filenames[file],
  264. ]);
  265. }
  266. }
  267. }
  268. // search for explicit entries in index directives
  269. for (const [entry, foundEntries] of Object.entries(indexEntries)) {
  270. if (entry.includes(queryLower) && (queryLower.length >= entry.length/2)) {
  271. for (const [file, id] of foundEntries) {
  272. let score = Math.round(100 * queryLower.length / entry.length)
  273. results.push([
  274. docNames[file],
  275. titles[file],
  276. id ? "#" + id : "",
  277. null,
  278. score,
  279. filenames[file],
  280. ]);
  281. }
  282. }
  283. }
  284. // lookup as object
  285. objectTerms.forEach((term) =>
  286. results.push(...Search.performObjectSearch(term, objectTerms))
  287. );
  288. // lookup as search terms in fulltext
  289. results.push(...Search.performTermsSearch(searchTerms, excludedTerms));
  290. // let the scorer override scores with a custom scoring function
  291. if (Scorer.score) results.forEach((item) => (item[4] = Scorer.score(item)));
  292. // now sort the results by score (in opposite order of appearance, since the
  293. // display function below uses pop() to retrieve items) and then
  294. // alphabetically
  295. results.sort((a, b) => {
  296. const leftScore = a[4];
  297. const rightScore = b[4];
  298. if (leftScore === rightScore) {
  299. // same score: sort alphabetically
  300. const leftTitle = a[1].toLowerCase();
  301. const rightTitle = b[1].toLowerCase();
  302. if (leftTitle === rightTitle) return 0;
  303. return leftTitle > rightTitle ? -1 : 1; // inverted is intentional
  304. }
  305. return leftScore > rightScore ? 1 : -1;
  306. });
  307. // remove duplicate search results
  308. // note the reversing of results, so that in the case of duplicates, the highest-scoring entry is kept
  309. let seen = new Set();
  310. results = results.reverse().reduce((acc, result) => {
  311. let resultStr = result.slice(0, 4).concat([result[5]]).map(v => String(v)).join(',');
  312. if (!seen.has(resultStr)) {
  313. acc.push(result);
  314. seen.add(resultStr);
  315. }
  316. return acc;
  317. }, []);
  318. results = results.reverse();
  319. // for debugging
  320. //Search.lastresults = results.slice(); // a copy
  321. // console.info("search results:", Search.lastresults);
  322. // print the results
  323. _displayNextItem(results, results.length, searchTerms);
  324. },
  325. /**
  326. * search for object names
  327. */
  328. performObjectSearch: (object, objectTerms) => {
  329. const filenames = Search._index.filenames;
  330. const docNames = Search._index.docnames;
  331. const objects = Search._index.objects;
  332. const objNames = Search._index.objnames;
  333. const titles = Search._index.titles;
  334. const results = [];
  335. const objectSearchCallback = (prefix, match) => {
  336. const name = match[4]
  337. const fullname = (prefix ? prefix + "." : "") + name;
  338. const fullnameLower = fullname.toLowerCase();
  339. if (fullnameLower.indexOf(object) < 0) return;
  340. let score = 0;
  341. const parts = fullnameLower.split(".");
  342. // check for different match types: exact matches of full name or
  343. // "last name" (i.e. last dotted part)
  344. if (fullnameLower === object || parts.slice(-1)[0] === object)
  345. score += Scorer.objNameMatch;
  346. else if (parts.slice(-1)[0].indexOf(object) > -1)
  347. score += Scorer.objPartialMatch; // matches in last name
  348. const objName = objNames[match[1]][2];
  349. const title = titles[match[0]];
  350. // If more than one term searched for, we require other words to be
  351. // found in the name/title/description
  352. const otherTerms = new Set(objectTerms);
  353. otherTerms.delete(object);
  354. if (otherTerms.size > 0) {
  355. const haystack = `${prefix} ${name} ${objName} ${title}`.toLowerCase();
  356. if (
  357. [...otherTerms].some((otherTerm) => haystack.indexOf(otherTerm) < 0)
  358. )
  359. return;
  360. }
  361. let anchor = match[3];
  362. if (anchor === "") anchor = fullname;
  363. else if (anchor === "-") anchor = objNames[match[1]][1] + "-" + fullname;
  364. const descr = objName + _(", in ") + title;
  365. // add custom score for some objects according to scorer
  366. if (Scorer.objPrio.hasOwnProperty(match[2]))
  367. score += Scorer.objPrio[match[2]];
  368. else score += Scorer.objPrioDefault;
  369. results.push([
  370. docNames[match[0]],
  371. fullname,
  372. "#" + anchor,
  373. descr,
  374. score,
  375. filenames[match[0]],
  376. ]);
  377. };
  378. Object.keys(objects).forEach((prefix) =>
  379. objects[prefix].forEach((array) =>
  380. objectSearchCallback(prefix, array)
  381. )
  382. );
  383. return results;
  384. },
  385. /**
  386. * search for full-text terms in the index
  387. */
  388. performTermsSearch: (searchTerms, excludedTerms) => {
  389. // prepare search
  390. const terms = Search._index.terms;
  391. const titleTerms = Search._index.titleterms;
  392. const filenames = Search._index.filenames;
  393. const docNames = Search._index.docnames;
  394. const titles = Search._index.titles;
  395. const scoreMap = new Map();
  396. const fileMap = new Map();
  397. // perform the search on the required terms
  398. searchTerms.forEach((word) => {
  399. const files = [];
  400. const arr = [
  401. { files: terms[word], score: Scorer.term },
  402. { files: titleTerms[word], score: Scorer.title },
  403. ];
  404. // add support for partial matches
  405. if (word.length > 2) {
  406. const escapedWord = _escapeRegExp(word);
  407. Object.keys(terms).forEach((term) => {
  408. if (term.match(escapedWord) && !terms[word])
  409. arr.push({ files: terms[term], score: Scorer.partialTerm });
  410. });
  411. Object.keys(titleTerms).forEach((term) => {
  412. if (term.match(escapedWord) && !titleTerms[word])
  413. arr.push({ files: titleTerms[word], score: Scorer.partialTitle });
  414. });
  415. }
  416. // no match but word was a required one
  417. if (arr.every((record) => record.files === undefined)) return;
  418. // found search word in contents
  419. arr.forEach((record) => {
  420. if (record.files === undefined) return;
  421. let recordFiles = record.files;
  422. if (recordFiles.length === undefined) recordFiles = [recordFiles];
  423. files.push(...recordFiles);
  424. // set score for the word in each file
  425. recordFiles.forEach((file) => {
  426. if (!scoreMap.has(file)) scoreMap.set(file, {});
  427. scoreMap.get(file)[word] = record.score;
  428. });
  429. });
  430. // create the mapping
  431. files.forEach((file) => {
  432. if (fileMap.has(file) && fileMap.get(file).indexOf(word) === -1)
  433. fileMap.get(file).push(word);
  434. else fileMap.set(file, [word]);
  435. });
  436. });
  437. // now check if the files don't contain excluded terms
  438. const results = [];
  439. for (const [file, wordList] of fileMap) {
  440. // check if all requirements are matched
  441. // as search terms with length < 3 are discarded
  442. const filteredTermCount = [...searchTerms].filter(
  443. (term) => term.length > 2
  444. ).length;
  445. if (
  446. wordList.length !== searchTerms.size &&
  447. wordList.length !== filteredTermCount
  448. )
  449. continue;
  450. // ensure that none of the excluded terms is in the search result
  451. if (
  452. [...excludedTerms].some(
  453. (term) =>
  454. terms[term] === file ||
  455. titleTerms[term] === file ||
  456. (terms[term] || []).includes(file) ||
  457. (titleTerms[term] || []).includes(file)
  458. )
  459. )
  460. break;
  461. // select one (max) score for the file.
  462. const score = Math.max(...wordList.map((w) => scoreMap.get(file)[w]));
  463. // add result to the result list
  464. results.push([
  465. docNames[file],
  466. titles[file],
  467. "",
  468. null,
  469. score,
  470. filenames[file],
  471. ]);
  472. }
  473. return results;
  474. },
  475. /**
  476. * helper function to return a node containing the
  477. * search summary for a given text. keywords is a list
  478. * of stemmed words.
  479. */
  480. makeSearchSummary: (htmlText, keywords) => {
  481. const text = Search.htmlToText(htmlText);
  482. if (text === "") return null;
  483. const textLower = text.toLowerCase();
  484. const actualStartPosition = [...keywords]
  485. .map((k) => textLower.indexOf(k.toLowerCase()))
  486. .filter((i) => i > -1)
  487. .slice(-1)[0];
  488. const startWithContext = Math.max(actualStartPosition - 120, 0);
  489. const top = startWithContext === 0 ? "" : "...";
  490. const tail = startWithContext + 240 < text.length ? "..." : "";
  491. let summary = document.createElement("p");
  492. summary.classList.add("context");
  493. summary.textContent = top + text.substr(startWithContext, 240).trim() + tail;
  494. return summary;
  495. },
  496. };
  497. _ready(Search.init);