Changeset ada37fa in ammosreader for doc/html/_static/searchtools.js
- Timestamp:
- 05/04/22 12:00:45 (3 years ago)
- Branches:
- AmmosSource, guix
- Children:
- 64565fe
- Parents:
- 1846087
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/html/_static/searchtools.js
r1846087 rada37fa 5 5 * Sphinx JavaScript utilities for the full-text search. 6 6 * 7 * :copyright: Copyright 2007-20 19by the Sphinx team, see AUTHORS.7 * :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS. 8 8 * :license: BSD, see LICENSE for details. 9 9 * … … 37 37 // query found in title 38 38 title: 15, 39 partialTitle: 7, 39 40 // query found in terms 40 term: 5 41 term: 5, 42 partialTerm: 2 41 43 }; 42 44 } … … 56 58 _queued_query : null, 57 59 _pulse_status : -1, 60 61 htmlToText : function(htmlString) { 62 var virtualDocument = document.implementation.createHTMLDocument('virtual'); 63 var htmlElement = $(htmlString, virtualDocument); 64 htmlElement.find('.headerlink').remove(); 65 docContent = htmlElement.find('[role=main]')[0]; 66 if(docContent === undefined) { 67 console.warn("Content block not found. Sphinx search tries to obtain it " + 68 "via '[role=main]'. Could you check your theme or template."); 69 return ""; 70 } 71 return docContent.textContent || docContent.innerText; 72 }, 58 73 59 74 init : function() { … … 121 136 this.title = $('<h2>' + _('Searching') + '</h2>').appendTo(this.out); 122 137 this.dots = $('<span></span>').appendTo(this.title); 123 this.status = $('<p style="display: none"></p>').appendTo(this.out);138 this.status = $('<p class="search-summary"> </p>').appendTo(this.out); 124 139 this.output = $('<ul class="search"/>').appendTo(this.out); 125 140 … … 152 167 } 153 168 154 if ($u.indexOf(stopwords, tmp[i].toLowerCase()) != -1 || tmp[i].match(/^\d+$/) || 155 tmp[i] === "") { 169 if ($u.indexOf(stopwords, tmp[i].toLowerCase()) != -1 || tmp[i] === "") { 156 170 // skip this "word" 157 171 continue; … … 235 249 if (results.length) { 236 250 var item = results.pop(); 237 var listItem = $('<li style="display:none"></li>'); 238 if (DOCUMENTATION_OPTIONS.FILE_SUFFIX === '') { 251 var listItem = $('<li></li>'); 252 var requestUrl = ""; 253 var linkUrl = ""; 254 if (DOCUMENTATION_OPTIONS.BUILDER === 'dirhtml') { 239 255 // dirhtml builder 240 256 var dirname = item[0] + '/'; … … 244 260 dirname = ''; 245 261 } 246 listItem.append($('<a/>').attr('href',247 DOCUMENTATION_OPTIONS.URL_ROOT + dirname +248 highlightstring + item[2]).html(item[1])); 262 requestUrl = DOCUMENTATION_OPTIONS.URL_ROOT + dirname; 263 linkUrl = requestUrl; 264 249 265 } else { 250 266 // normal html builders 251 listItem.append($('<a/>').attr('href', 252 item[0] + DOCUMENTATION_OPTIONS.FILE_SUFFIX + 267 requestUrl = DOCUMENTATION_OPTIONS.URL_ROOT + item[0] + DOCUMENTATION_OPTIONS.FILE_SUFFIX; 268 linkUrl = item[0] + DOCUMENTATION_OPTIONS.LINK_SUFFIX; 269 } 270 listItem.append($('<a/>').attr('href', 271 linkUrl + 253 272 highlightstring + item[2]).html(item[1])); 254 }255 273 if (item[3]) { 256 274 listItem.append($('<span> (' + item[3] + ')</span>')); 257 275 Search.output.append(listItem); 258 listItem.slideDown(5,function() {276 setTimeout(function() { 259 277 displayNextItem(); 260 } );278 }, 5); 261 279 } else if (DOCUMENTATION_OPTIONS.HAS_SOURCE) { 262 var suffix = DOCUMENTATION_OPTIONS.SOURCELINK_SUFFIX; 263 if (suffix === undefined) { 264 suffix = '.txt'; 265 } 266 $.ajax({url: DOCUMENTATION_OPTIONS.URL_ROOT + '_sources/' + item[5] + (item[5].slice(-suffix.length) === suffix ? '' : suffix), 280 $.ajax({url: requestUrl, 267 281 dataType: "text", 268 282 complete: function(jqxhr, textstatus) { 269 283 var data = jqxhr.responseText; 270 284 if (data !== '' && data !== undefined) { 271 listItem.append(Search.makeSearchSummary(data, searchterms, hlterms)); 285 var summary = Search.makeSearchSummary(data, searchterms, hlterms); 286 if (summary) { 287 listItem.append(summary); 288 } 272 289 } 273 290 Search.output.append(listItem); 274 listItem.slideDown(5,function() {291 setTimeout(function() { 275 292 displayNextItem(); 276 } );293 }, 5); 277 294 }}); 278 295 } else { 279 296 // no source available, just display title 280 297 Search.output.append(listItem); 281 listItem.slideDown(5,function() {298 setTimeout(function() { 282 299 displayNextItem(); 283 } );300 }, 5); 284 301 } 285 302 } … … 314 331 for (var name in objects[prefix]) { 315 332 var fullname = (prefix ? prefix + '.' : '') + name; 316 if (fullname.toLowerCase().indexOf(object) > -1) { 333 var fullnameLower = fullname.toLowerCase() 334 if (fullnameLower.indexOf(object) > -1) { 317 335 var score = 0; 318 var parts = fullname .split('.');336 var parts = fullnameLower.split('.'); 319 337 // check for different match types: exact matches of full name or 320 338 // "last name" (i.e. last dotted part) 321 if (fullname == object || parts[parts.length - 1] == object) {339 if (fullnameLower == object || parts[parts.length - 1] == object) { 322 340 score += Scorer.objNameMatch; 323 341 // matches in last name … … 366 384 367 385 /** 386 * See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions 387 */ 388 escapeRegExp : function(string) { 389 return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string 390 }, 391 392 /** 368 393 * search for full-text terms in the index 369 394 */ … … 386 411 {files: titleterms[word], score: Scorer.title} 387 412 ]; 413 // add support for partial matches 414 if (word.length > 2) { 415 var word_regex = this.escapeRegExp(word); 416 for (var w in terms) { 417 if (w.match(word_regex) && !terms[word]) { 418 _o.push({files: terms[w], score: Scorer.partialTerm}) 419 } 420 } 421 for (var w in titleterms) { 422 if (w.match(word_regex) && !titleterms[word]) { 423 _o.push({files: titleterms[w], score: Scorer.partialTitle}) 424 } 425 } 426 } 388 427 389 428 // no match but word was a required one … … 405 444 file = _files[j]; 406 445 if (!(file in scoreMap)) 407 scoreMap[file] = {} 446 scoreMap[file] = {}; 408 447 scoreMap[file][word] = o.score; 409 448 } … … 413 452 for (j = 0; j < files.length; j++) { 414 453 file = files[j]; 415 if (file in fileMap )454 if (file in fileMap && fileMap[file].indexOf(word) === -1) 416 455 fileMap[file].push(word); 417 456 else … … 425 464 426 465 // check if all requirements are matched 427 if (fileMap[file].length != searchterms.length) 428 continue; 466 var filteredTermCount = // as search terms with length < 3 are discarded: ignore 467 searchterms.filter(function(term){return term.length > 2}).length 468 if ( 469 fileMap[file].length != searchterms.length && 470 fileMap[file].length != filteredTermCount 471 ) continue; 429 472 430 473 // ensure that none of the excluded terms is in the search result … … 457 500 * latter for highlighting it. 458 501 */ 459 makeSearchSummary : function(text, keywords, hlwords) { 502 makeSearchSummary : function(htmlText, keywords, hlwords) { 503 var text = Search.htmlToText(htmlText); 504 if (text == "") { 505 return null; 506 } 460 507 var textLower = text.toLowerCase(); 461 508 var start = 0; … … 469 516 $.trim(text.substr(start, 240)) + 470 517 ((start + 240 - text.length) ? '...' : ''); 471 var rv = $('< div class="context"></div>').text(excerpt);518 var rv = $('<p class="context"></p>').text(excerpt); 472 519 $.each(hlwords, function() { 473 520 rv = rv.highlightText(this, 'highlighted');
Note:
See TracChangeset
for help on using the changeset viewer.