Ensure we're running in an RTD environment to prevent local doc builds from breaking.

This commit is contained in:
OrfeasZ 2017-03-20 04:53:09 +02:00
parent b1b121d8d0
commit da11202bf1

View File

@ -1,150 +1,152 @@
Search.query = function(query) { if (typeof window.SphinxRtdTheme !== 'undefined') {
var i; Search.query = function(query) {
var stopwords = {{ search_language_stop_words }}; var i;
var stopwords = {{ search_language_stop_words }};
// stem the searchterms and add them to the correct list // stem the searchterms and add them to the correct list
var stemmer = new Stemmer(); var stemmer = new Stemmer();
var searchterms = []; var searchterms = [];
var excluded = []; var excluded = [];
var hlterms = []; var hlterms = [];
var tmp = query.split(/\s+/); var tmp = query.split(/\s+/);
var objectterms = []; var objectterms = [];
for (i = 0; i < tmp.length; i++) { for (i = 0; i < tmp.length; i++) {
if (tmp[i] !== "") { if (tmp[i] !== "") {
objectterms.push(tmp[i].toLowerCase()); objectterms.push(tmp[i].toLowerCase());
}
if ($u.indexOf(stopwords, tmp[i].toLowerCase()) != -1 || tmp[i].match(/^\d+$/) ||
tmp[i] === "") {
// skip this "word"
continue;
}
// stem the word
var word = stemmer.stemWord(tmp[i].toLowerCase());
var toAppend;
// select the correct list
if (word[0] == '-') {
toAppend = excluded;
word = word.substr(1);
}
else {
toAppend = searchterms;
hlterms.push(tmp[i].toLowerCase());
}
// only add if not already in the list
if (!$u.contains(toAppend, word))
toAppend.push(word);
}
var highlightstring = '?highlight=' + $.urlencode(hlterms.join(" "));
// prepare search
var terms = this._index.terms;
var titleterms = this._index.titleterms;
// array of [filename, title, anchor, descr, score]
var results = [];
$('#search-progress').empty();
// lookup as object
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));
} }
if ($u.indexOf(stopwords, tmp[i].toLowerCase()) != -1 || tmp[i].match(/^\d+$/) || // lookup as search terms in fulltext
tmp[i] === "") { results = results.concat(this.performTermsSearch(searchterms, excluded, terms, Scorer.term))
// skip this "word" .concat(this.performTermsSearch(searchterms, excluded, titleterms, Scorer.title));
continue;
// let the scorer override scores with a custom scoring function
if (Scorer.score) {
for (i = 0; i < results.length; i++)
results[i][4] = Scorer.score(results[i]);
} }
// stem the word
var word = stemmer.stemWord(tmp[i].toLowerCase());
var toAppend;
// select the correct list
if (word[0] == '-') {
toAppend = excluded;
word = word.substr(1);
}
else {
toAppend = searchterms;
hlterms.push(tmp[i].toLowerCase());
}
// only add if not already in the list
if (!$u.contains(toAppend, word))
toAppend.push(word);
}
var highlightstring = '?highlight=' + $.urlencode(hlterms.join(" "));
// prepare search // now sort the results by score (in opposite order of appearance, since the
var terms = this._index.terms; // display function below uses pop() to retrieve items) and then
var titleterms = this._index.titleterms; // alphabetically
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 {
// same score: sort alphabetically
left = a[1].toLowerCase();
right = b[1].toLowerCase();
return (left > right) ? -1 : ((left < right) ? 1 : 0);
}
});
// array of [filename, title, anchor, descr, score] // for debugging
var results = []; //Search.lastresults = results.slice(); // a copy
$('#search-progress').empty(); //console.info('search results:', Search.lastresults);
// lookup as object // print the results
for (i = 0; i < objectterms.length; i++) { var resultCount = results.length;
var others = [].concat(objectterms.slice(0, i), function displayNextItem() {
objectterms.slice(i+1, objectterms.length)); // results left, load the summary and display it
results = results.concat(this.performObjectSearch(objectterms[i], others)); if (results.length) {
} var item = results.pop();
var listItem = $('<li style="display:none"></li>');
// lookup as search terms in fulltext if (DOCUMENTATION_OPTIONS.FILE_SUFFIX === '') {
results = results.concat(this.performTermsSearch(searchterms, excluded, terms, Scorer.term)) // dirhtml builder
.concat(this.performTermsSearch(searchterms, excluded, titleterms, Scorer.title)); var dirname = item[0].replace(/\.rst$/, '') + '/';
if (dirname.match(/\/index\/$/)) {
// let the scorer override scores with a custom scoring function dirname = dirname.substring(0, dirname.length-6);
if (Scorer.score) { } else if (dirname == 'index/') {
for (i = 0; i < results.length; i++) dirname = '';
results[i][4] = Scorer.score(results[i]); }
} listItem.append($('<a/>').attr('href',
DOCUMENTATION_OPTIONS.URL_ROOT + dirname +
// now sort the results by score (in opposite order of appearance, since the highlightstring + item[2]).html(item[1]));
// display function below uses pop() to retrieve items) and then } else {
// alphabetically // normal html builders
results.sort(function(a, b) { listItem.append($('<a/>').attr('href',
var left = a[4]; item[0].replace(/\.rst$/, '') + DOCUMENTATION_OPTIONS.FILE_SUFFIX +
var right = b[4]; highlightstring + item[2]).html(item[1]));
if (left > right) { }
return 1; if (item[3]) {
} else if (left < right) { listItem.append($('<span> (' + item[3] + ')</span>'));
return -1; Search.output.append(listItem);
} else { listItem.slideDown(5, function() {
// same score: sort alphabetically displayNextItem();
left = a[1].toLowerCase(); });
right = b[1].toLowerCase(); } else if (DOCUMENTATION_OPTIONS.HAS_SOURCE) {
return (left > right) ? -1 : ((left < right) ? 1 : 0); $.ajax({url: DOCUMENTATION_OPTIONS.URL_ROOT + '_sources/' + item[0] + '.txt',
} dataType: "text",
}); complete: function(jqxhr, textstatus) {
var data = jqxhr.responseText;
// for debugging if (data !== '' && data !== undefined) {
//Search.lastresults = results.slice(); // a copy listItem.append(Search.makeSearchSummary(data, searchterms, hlterms));
//console.info('search results:', Search.lastresults); }
Search.output.append(listItem);
// print the results listItem.slideDown(5, function() {
var resultCount = results.length; displayNextItem();
function displayNextItem() { });
// results left, load the summary and display it }});
if (results.length) { } else {
var item = results.pop(); // no source available, just display title
var listItem = $('<li style="display:none"></li>'); Search.output.append(listItem);
if (DOCUMENTATION_OPTIONS.FILE_SUFFIX === '') { listItem.slideDown(5, function() {
// dirhtml builder displayNextItem();
var dirname = item[0].replace(/\.rst$/, '') + '/'; });
if (dirname.match(/\/index\/$/)) {
dirname = dirname.substring(0, dirname.length-6);
} else if (dirname == 'index/') {
dirname = '';
} }
listItem.append($('<a/>').attr('href',
DOCUMENTATION_OPTIONS.URL_ROOT + dirname +
highlightstring + item[2]).html(item[1]));
} else {
// normal html builders
listItem.append($('<a/>').attr('href',
item[0].replace(/\.rst$/, '') + DOCUMENTATION_OPTIONS.FILE_SUFFIX +
highlightstring + item[2]).html(item[1]));
} }
if (item[3]) { // search finished, update title and status message
listItem.append($('<span> (' + item[3] + ')</span>')); else {
Search.output.append(listItem); Search.stopPulse();
listItem.slideDown(5, function() { Search.title.text(_('Search Results'));
displayNextItem(); 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 if (DOCUMENTATION_OPTIONS.HAS_SOURCE) { else
$.ajax({url: DOCUMENTATION_OPTIONS.URL_ROOT + '_sources/' + item[0] + '.txt', Search.status.text(_('Search finished, found %s page(s) matching the search query.').replace('%s', resultCount));
dataType: "text", Search.status.fadeIn(500);
complete: function(jqxhr, textstatus) {
var data = jqxhr.responseText;
if (data !== '' && data !== undefined) {
listItem.append(Search.makeSearchSummary(data, searchterms, hlterms));
}
Search.output.append(listItem);
listItem.slideDown(5, function() {
displayNextItem();
});
}});
} else {
// no source available, just display title
Search.output.append(listItem);
listItem.slideDown(5, function() {
displayNextItem();
});
} }
} }
// search finished, update title and status message displayNextItem();
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();
};