2022-11-19 20:25:34 +00:00
/ *
* searchtools . js
* ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
*
* Sphinx JavaScript utilities for the full - text search .
*
2022-11-24 22:47:54 +00:00
* : copyright : Copyright 2007 - 2021 by the Sphinx team , see AUTHORS .
2022-11-19 20:25:34 +00:00
* : license : BSD , see LICENSE for details .
*
* /
2022-11-24 22:47:54 +00:00
if ( ! Scorer ) {
/ * *
* Simple result scoring code .
* /
2022-11-19 20:25:34 +00:00
var Scorer = {
// Implement the following function to further tweak the score for each result
2022-11-24 22:47:54 +00:00
// The function takes a result array [filename, title, anchor, descr, score]
2022-11-19 20:25:34 +00:00
// and returns the new score.
/ *
2022-11-24 22:47:54 +00:00
score : function ( result ) {
return result [ 4 ] ;
2022-11-19 20:25:34 +00:00
} ,
* /
// query matches the full name of an object
objNameMatch : 11 ,
// or matches in the last dotted part of the object name
objPartialMatch : 6 ,
// Additive scores depending on the priority of the object
2022-11-24 22:47:54 +00:00
objPrio : { 0 : 15 , // used to be importantResults
1 : 5 , // used to be objectResults
2 : - 5 } , // used to be unimportantResults
2022-11-19 20:25:34 +00:00
// Used when the priority is not in the mapping.
objPrioDefault : 0 ,
// query found in title
title : 15 ,
partialTitle : 7 ,
// query found in terms
term : 5 ,
2022-11-24 22:47:54 +00:00
partialTerm : 2
2022-11-19 20:25:34 +00:00
} ;
}
2022-11-24 22:47:54 +00:00
if ( ! splitQuery ) {
function splitQuery ( query ) {
return query . split ( /\s+/ ) ;
2022-11-19 20:25:34 +00:00
}
}
/ * *
* Search Module
* /
2022-11-24 22:47:54 +00:00
var Search = {
_index : null ,
_queued _query : null ,
_pulse _status : - 1 ,
htmlToText : function ( htmlString ) {
var virtualDocument = document . implementation . createHTMLDocument ( 'virtual' ) ;
var htmlElement = $ ( htmlString , virtualDocument ) ;
htmlElement . find ( '.headerlink' ) . remove ( ) ;
docContent = htmlElement . find ( '[role=main]' ) [ 0 ] ;
if ( docContent === undefined ) {
console . warn ( "Content block not found. Sphinx search tries to obtain it " +
"via '[role=main]'. Could you check your theme or template." ) ;
return "" ;
}
return docContent . textContent || docContent . innerText ;
2022-11-19 20:25:34 +00:00
} ,
2022-11-24 22:47:54 +00:00
init : function ( ) {
var params = $ . getQueryParameters ( ) ;
if ( params . q ) {
var query = params . q [ 0 ] ;
$ ( 'input[name="q"]' ) [ 0 ] . value = query ;
this . performSearch ( query ) ;
}
2022-11-19 20:25:34 +00:00
} ,
2022-11-24 22:47:54 +00:00
loadIndex : function ( url ) {
$ . ajax ( { type : "GET" , url : url , data : null ,
dataType : "script" , cache : true ,
complete : function ( jqxhr , textstatus ) {
if ( textstatus != "success" ) {
document . getElementById ( "searchindexloader" ) . src = url ;
}
} } ) ;
} ,
2022-11-19 20:25:34 +00:00
2022-11-24 22:47:54 +00:00
setIndex : function ( index ) {
var q ;
this . _index = index ;
if ( ( q = this . _queued _query ) !== null ) {
this . _queued _query = null ;
Search . query ( q ) ;
2022-11-19 20:25:34 +00:00
}
} ,
2022-11-24 22:47:54 +00:00
hasIndex : function ( ) {
return this . _index !== null ;
} ,
2022-11-19 20:25:34 +00:00
2022-11-24 22:47:54 +00:00
deferQuery : function ( query ) {
this . _queued _query = query ;
} ,
2022-11-19 20:25:34 +00:00
2022-11-24 22:47:54 +00:00
stopPulse : function ( ) {
this . _pulse _status = 0 ;
} ,
2022-11-19 20:25:34 +00:00
2022-11-24 22:47:54 +00:00
startPulse : function ( ) {
if ( this . _pulse _status >= 0 )
return ;
function pulse ( ) {
var i ;
2022-11-19 20:25:34 +00:00
Search . _pulse _status = ( Search . _pulse _status + 1 ) % 4 ;
2022-11-24 22:47:54 +00:00
var dotString = '' ;
for ( i = 0 ; i < Search . _pulse _status ; i ++ )
dotString += '.' ;
Search . dots . text ( dotString ) ;
if ( Search . _pulse _status > - 1 )
window . setTimeout ( pulse , 500 ) ;
}
2022-11-19 20:25:34 +00:00
pulse ( ) ;
} ,
/ * *
* perform a search for something ( or wait until index is loaded )
* /
2022-11-24 22:47:54 +00:00
performSearch : function ( query ) {
2022-11-19 20:25:34 +00:00
// create the required interface elements
2022-11-24 22:47:54 +00:00
this . out = $ ( '#search-results' ) ;
this . title = $ ( '<h2>' + _ ( 'Searching' ) + '</h2>' ) . appendTo ( this . out ) ;
this . dots = $ ( '<span></span>' ) . appendTo ( this . title ) ;
this . status = $ ( '<p class="search-summary"> </p>' ) . appendTo ( this . out ) ;
this . output = $ ( '<ul class="search"/>' ) . appendTo ( this . out ) ;
$ ( '#search-progress' ) . text ( _ ( 'Preparing search...' ) ) ;
this . startPulse ( ) ;
2022-11-19 20:25:34 +00:00
// index already loaded, the browser was quick!
2022-11-24 22:47:54 +00:00
if ( this . hasIndex ( ) )
this . query ( query ) ;
else
this . deferQuery ( query ) ;
2022-11-19 20:25:34 +00:00
} ,
/ * *
* execute search ( requires search index to be loaded )
* /
2022-11-24 22:47:54 +00:00
query : function ( query ) {
var i ;
// stem the searchterms and add them to the correct list
var stemmer = new Stemmer ( ) ;
var searchterms = [ ] ;
var excluded = [ ] ;
var hlterms = [ ] ;
var tmp = splitQuery ( query ) ;
var objectterms = [ ] ;
for ( i = 0 ; i < tmp . length ; i ++ ) {
if ( tmp [ i ] !== "" ) {
objectterms . push ( tmp [ i ] . toLowerCase ( ) ) ;
}
2022-11-19 20:25:34 +00:00
2022-11-24 22:47:54 +00:00
if ( $u . indexOf ( stopwords , tmp [ i ] . toLowerCase ( ) ) != - 1 || tmp [ i ] === "" ) {
// skip this "word"
continue ;
}
2022-11-19 20:25:34 +00:00
// stem the word
2022-11-24 22:47:54 +00:00
var word = stemmer . stemWord ( tmp [ i ] . toLowerCase ( ) ) ;
// prevent stemmer from cutting word smaller than two chars
if ( word . length < 3 && tmp [ i ] . length >= 3 ) {
word = tmp [ i ] ;
}
var toAppend ;
2022-11-19 20:25:34 +00:00
// select the correct list
2022-11-24 22:47:54 +00:00
if ( word [ 0 ] == '-' ) {
toAppend = excluded ;
word = word . substr ( 1 ) ;
}
2022-11-19 20:25:34 +00:00
else {
2022-11-24 22:47:54 +00:00
toAppend = searchterms ;
hlterms . push ( tmp [ i ] . toLowerCase ( ) ) ;
2022-11-19 20:25:34 +00:00
}
2022-11-24 22:47:54 +00:00
// only add if not already in the list
if ( ! $u . contains ( toAppend , word ) )
toAppend . push ( word ) ;
2022-11-19 20:25:34 +00:00
}
2022-11-24 22:47:54 +00:00
var highlightstring = '?highlight=' + $ . urlencode ( hlterms . join ( " " ) ) ;
2022-11-19 20:25:34 +00:00
2022-11-24 22:47:54 +00:00
// console.debug('SEARCH: searching for:');
// console.info('required: ', searchterms);
// console.info('excluded: ', excluded);
2022-11-19 20:25:34 +00:00
2022-11-24 22:47:54 +00:00
// prepare search
var terms = this . _index . terms ;
var titleterms = this . _index . titleterms ;
// array of [filename, title, anchor, descr, score]
var results = [ ] ;
$ ( '#search-progress' ) . empty ( ) ;
2022-11-19 20:25:34 +00:00
// lookup as object
2022-11-24 22:47:54 +00:00
for ( i = 0 ; i < objectterms . length ; i ++ ) {
var others = [ ] . concat ( objectterms . slice ( 0 , i ) ,
objectterms . slice ( i + 1 , objectterms . length ) ) ;
results = results . concat ( this . performObjectSearch ( objectterms [ i ] , others ) ) ;
}
2022-11-19 20:25:34 +00:00
// lookup as search terms in fulltext
2022-11-24 22:47:54 +00:00
results = results . concat ( this . performTermsSearch ( searchterms , excluded , terms , titleterms ) ) ;
2022-11-19 20:25:34 +00:00
// let the scorer override scores with a custom scoring function
2022-11-24 22:47:54 +00:00
if ( Scorer . score ) {
for ( i = 0 ; i < results . length ; i ++ )
results [ i ] [ 4 ] = Scorer . score ( results [ i ] ) ;
}
2022-11-19 20:25:34 +00:00
// now sort the results by score (in opposite order of appearance, since the
// display function below uses pop() to retrieve items) and then
// alphabetically
2022-11-24 22:47:54 +00:00
results . sort ( function ( a , b ) {
var left = a [ 4 ] ;
var right = b [ 4 ] ;
if ( left > right ) {
return 1 ;
} else if ( left < right ) {
return - 1 ;
} else {
2022-11-19 20:25:34 +00:00
// same score: sort alphabetically
2022-11-24 22:47:54 +00:00
left = a [ 1 ] . toLowerCase ( ) ;
right = b [ 1 ] . toLowerCase ( ) ;
return ( left > right ) ? - 1 : ( ( left < right ) ? 1 : 0 ) ;
2022-11-19 20:25:34 +00:00
}
} ) ;
// for debugging
//Search.lastresults = results.slice(); // a copy
2022-11-24 22:47:54 +00:00
//console.info('search results:', Search.lastresults);
2022-11-19 20:25:34 +00:00
// print the results
2022-11-24 22:47:54 +00:00
var resultCount = results . length ;
function displayNextItem ( ) {
// results left, load the summary and display it
if ( results . length ) {
var item = results . pop ( ) ;
var listItem = $ ( '<li></li>' ) ;
var requestUrl = "" ;
var linkUrl = "" ;
if ( DOCUMENTATION _OPTIONS . BUILDER === 'dirhtml' ) {
// dirhtml builder
var dirname = item [ 0 ] + '/' ;
if ( dirname . match ( /\/index\/$/ ) ) {
dirname = dirname . substring ( 0 , dirname . length - 6 ) ;
} else if ( dirname == 'index/' ) {
dirname = '' ;
}
requestUrl = DOCUMENTATION _OPTIONS . URL _ROOT + dirname ;
linkUrl = requestUrl ;
} else {
// normal html builders
requestUrl = DOCUMENTATION _OPTIONS . URL _ROOT + item [ 0 ] + DOCUMENTATION _OPTIONS . FILE _SUFFIX ;
linkUrl = item [ 0 ] + DOCUMENTATION _OPTIONS . LINK _SUFFIX ;
}
listItem . append ( $ ( '<a/>' ) . attr ( 'href' ,
linkUrl +
highlightstring + item [ 2 ] ) . html ( item [ 1 ] ) ) ;
if ( item [ 3 ] ) {
listItem . append ( $ ( '<span> (' + item [ 3 ] + ')</span>' ) ) ;
Search . output . append ( listItem ) ;
setTimeout ( function ( ) {
displayNextItem ( ) ;
} , 5 ) ;
} else if ( DOCUMENTATION _OPTIONS . HAS _SOURCE ) {
$ . ajax ( { url : requestUrl ,
dataType : "text" ,
complete : function ( jqxhr , textstatus ) {
var data = jqxhr . responseText ;
if ( data !== '' && data !== undefined ) {
var summary = Search . makeSearchSummary ( data , searchterms , hlterms ) ;
if ( summary ) {
listItem . append ( summary ) ;
}
}
Search . output . append ( listItem ) ;
setTimeout ( function ( ) {
displayNextItem ( ) ;
} , 5 ) ;
} } ) ;
} else {
// no source available, just display title
Search . output . append ( listItem ) ;
setTimeout ( function ( ) {
displayNextItem ( ) ;
} , 5 ) ;
}
}
// search finished, update title and status message
else {
Search . stopPulse ( ) ;
Search . title . text ( _ ( 'Search Results' ) ) ;
if ( ! resultCount )
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.' ) ) ;
else
Search . status . text ( _ ( 'Search finished, found %s page(s) matching the search query.' ) . replace ( '%s' , resultCount ) ) ;
Search . status . fadeIn ( 500 ) ;
}
}
displayNextItem ( ) ;
2022-11-19 20:25:34 +00:00
} ,
/ * *
* search for object names
* /
2022-11-24 22:47:54 +00:00
performObjectSearch : function ( object , otherterms ) {
var filenames = this . _index . filenames ;
var docnames = this . _index . docnames ;
var objects = this . _index . objects ;
var objnames = this . _index . objnames ;
var titles = this . _index . titles ;
var i ;
var results = [ ] ;
for ( var prefix in objects ) {
if ( ! ( objects [ prefix ] instanceof Array ) ) {
objects [ prefix ] = Object . entries ( objects [ prefix ] ) . map ( ( [ name , match ] ) => [ ... match , name ] ) ;
2022-11-19 20:25:34 +00:00
}
2022-11-24 22:47:54 +00:00
for ( var iMatch = 0 ; iMatch != objects [ prefix ] . length ; ++ iMatch ) {
var match = objects [ prefix ] [ iMatch ] ;
var name = match [ 4 ] ;
var fullname = ( prefix ? prefix + '.' : '' ) + name ;
var fullnameLower = fullname . toLowerCase ( )
if ( fullnameLower . indexOf ( object ) > - 1 ) {
var score = 0 ;
var parts = fullnameLower . split ( '.' ) ;
// check for different match types: exact matches of full name or
// "last name" (i.e. last dotted part)
if ( fullnameLower == object || parts [ parts . length - 1 ] == object ) {
score += Scorer . objNameMatch ;
// matches in last name
} else if ( parts [ parts . length - 1 ] . indexOf ( object ) > - 1 ) {
score += Scorer . objPartialMatch ;
}
var objname = objnames [ match [ 1 ] ] [ 2 ] ;
var title = titles [ match [ 0 ] ] ;
// If more than one term searched for, we require other words to be
// found in the name/title/description
if ( otherterms . length > 0 ) {
var haystack = ( prefix + ' ' + name + ' ' +
objname + ' ' + title ) . toLowerCase ( ) ;
var allfound = true ;
for ( i = 0 ; i < otherterms . length ; i ++ ) {
if ( haystack . indexOf ( otherterms [ i ] ) == - 1 ) {
allfound = false ;
break ;
}
}
if ( ! allfound ) {
continue ;
}
}
var descr = objname + _ ( ', in ' ) + title ;
var anchor = match [ 3 ] ;
if ( anchor === '' )
anchor = fullname ;
else if ( anchor == '-' )
anchor = objnames [ match [ 1 ] ] [ 1 ] + '-' + fullname ;
// add custom score for some objects according to scorer
if ( Scorer . objPrio . hasOwnProperty ( match [ 2 ] ) ) {
score += Scorer . objPrio [ match [ 2 ] ] ;
} else {
score += Scorer . objPrioDefault ;
}
results . push ( [ docnames [ match [ 0 ] ] , fullname , '#' + anchor , descr , score , filenames [ match [ 0 ] ] ] ) ;
}
}
}
2022-11-19 20:25:34 +00:00
return results ;
} ,
2022-11-24 22:47:54 +00:00
/ * *
* See https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
* /
escapeRegExp : function ( string ) {
return string . replace ( /[.*+\-?^${}()|[\]\\]/g , '\\$&' ) ; // $& means the whole matched string
} ,
2022-11-19 20:25:34 +00:00
/ * *
* search for full - text terms in the index
* /
2022-11-24 22:47:54 +00:00
performTermsSearch : function ( searchterms , excluded , terms , titleterms ) {
var docnames = this . _index . docnames ;
var filenames = this . _index . filenames ;
var titles = this . _index . titles ;
2022-11-19 20:25:34 +00:00
2022-11-24 22:47:54 +00:00
var i , j , file ;
var fileMap = { } ;
var scoreMap = { } ;
var results = [ ] ;
2022-11-19 20:25:34 +00:00
// perform the search on the required terms
2022-11-24 22:47:54 +00:00
for ( i = 0 ; i < searchterms . length ; i ++ ) {
var word = searchterms [ i ] ;
var files = [ ] ;
var _o = [
{ files : terms [ word ] , score : Scorer . term } ,
{ files : titleterms [ word ] , score : Scorer . title }
2022-11-19 20:25:34 +00:00
] ;
// add support for partial matches
if ( word . length > 2 ) {
2022-11-24 22:47:54 +00:00
var word _regex = this . escapeRegExp ( word ) ;
for ( var w in terms ) {
if ( w . match ( word _regex ) && ! terms [ word ] ) {
_o . push ( { files : terms [ w ] , score : Scorer . partialTerm } )
}
}
for ( var w in titleterms ) {
if ( w . match ( word _regex ) && ! titleterms [ word ] ) {
_o . push ( { files : titleterms [ w ] , score : Scorer . partialTitle } )
}
}
2022-11-19 20:25:34 +00:00
}
// no match but word was a required one
2022-11-24 22:47:54 +00:00
if ( $u . every ( _o , function ( o ) { return o . files === undefined ; } ) ) {
break ;
}
2022-11-19 20:25:34 +00:00
// found search word in contents
2022-11-24 22:47:54 +00:00
$u . each ( _o , function ( o ) {
var _files = o . files ;
if ( _files === undefined )
return
if ( _files . length === undefined )
_files = [ _files ] ;
files = files . concat ( _files ) ;
// set score for the word in each file to Scorer.term
for ( j = 0 ; j < _files . length ; j ++ ) {
file = _files [ j ] ;
if ( ! ( file in scoreMap ) )
scoreMap [ file ] = { } ;
scoreMap [ file ] [ word ] = o . score ;
}
2022-11-19 20:25:34 +00:00
} ) ;
// create the mapping
2022-11-24 22:47:54 +00:00
for ( j = 0 ; j < files . length ; j ++ ) {
file = files [ j ] ;
if ( file in fileMap && fileMap [ file ] . indexOf ( word ) === - 1 )
fileMap [ file ] . push ( word ) ;
else
fileMap [ file ] = [ word ] ;
}
}
2022-11-19 20:25:34 +00:00
// now check if the files don't contain excluded terms
2022-11-24 22:47:54 +00:00
for ( file in fileMap ) {
var valid = true ;
2022-11-19 20:25:34 +00:00
2022-11-24 22:47:54 +00:00
// check if all requirements are matched
var filteredTermCount = // as search terms with length < 3 are discarded: ignore
searchterms . filter ( function ( term ) { return term . length > 2 } ) . length
2022-11-19 20:25:34 +00:00
if (
2022-11-24 22:47:54 +00:00
fileMap [ file ] . length != searchterms . length &&
fileMap [ file ] . length != filteredTermCount
) continue ;
2022-11-19 20:25:34 +00:00
// ensure that none of the excluded terms is in the search result
2022-11-24 22:47:54 +00:00
for ( i = 0 ; i < excluded . length ; i ++ ) {
if ( terms [ excluded [ i ] ] == file ||
titleterms [ excluded [ i ] ] == file ||
$u . contains ( terms [ excluded [ i ] ] || [ ] , file ) ||
$u . contains ( titleterms [ excluded [ i ] ] || [ ] , file ) ) {
valid = false ;
break ;
}
}
2022-11-19 20:25:34 +00:00
2022-11-24 22:47:54 +00:00
// if we have still a valid result we can add it to the result list
if ( valid ) {
// select one (max) score for the file.
// for better ranking, we should calculate ranking by using words statistics like basic tf-idf...
var score = $u . max ( $u . map ( fileMap [ file ] , function ( w ) { return scoreMap [ file ] [ w ] } ) ) ;
results . push ( [ docnames [ file ] , titles [ file ] , '' , null , score , filenames [ file ] ] ) ;
}
2022-11-19 20:25:34 +00:00
}
return results ;
} ,
/ * *
* helper function to return a node containing the
* search summary for a given text . keywords is a list
2022-11-24 22:47:54 +00:00
* of stemmed words , hlwords is the list of normal , unstemmed
* words . the first one is used to find the occurrence , the
* latter for highlighting it .
2022-11-19 20:25:34 +00:00
* /
2022-11-24 22:47:54 +00:00
makeSearchSummary : function ( htmlText , keywords , hlwords ) {
var text = Search . htmlToText ( htmlText ) ;
if ( text == "" ) {
return null ;
}
var textLower = text . toLowerCase ( ) ;
var start = 0 ;
$ . each ( keywords , function ( ) {
var i = textLower . indexOf ( this . toLowerCase ( ) ) ;
if ( i > - 1 )
start = i ;
} ) ;
start = Math . max ( start - 120 , 0 ) ;
var excerpt = ( ( start > 0 ) ? '...' : '' ) +
$ . trim ( text . substr ( start , 240 ) ) +
( ( start + 240 - text . length ) ? '...' : '' ) ;
var rv = $ ( '<p class="context"></p>' ) . text ( excerpt ) ;
$ . each ( hlwords , function ( ) {
rv = rv . highlightText ( this , 'highlighted' ) ;
} ) ;
return rv ;
}
2022-11-19 20:25:34 +00:00
} ;
2022-11-24 22:47:54 +00:00
$ ( document ) . ready ( function ( ) {
Search . init ( ) ;
} ) ;