Moved searchindex to the bottom to not lock the search page, prepared index for title searching.

This commit is contained in:
Armin Ronacher 2008-09-10 11:26:07 +00:00
parent 3debdc2c2a
commit b69e1a4fe7
3 changed files with 392 additions and 395 deletions

View File

@ -59,6 +59,7 @@ class Stemmer(PorterStemmer):
""" """
def stem(self, word): def stem(self, word):
word = word.lower()
return PorterStemmer.stem(self, word, 0, len(word) - 1) return PorterStemmer.stem(self, word, 0, len(word) - 1)
@ -139,9 +140,17 @@ class IndexBuilder(object):
visitor = WordCollector(doctree) visitor = WordCollector(doctree)
doctree.walk(visitor) doctree.walk(visitor)
for word in word_re.findall(title) + visitor.found_words:
self._mapping.setdefault(self._stemmer.stem(word.lower()), def add_term(word, prefix=''):
set()).add(filename) word = self._stemmer.stem(word)
self._mapping.setdefault(prefix + word, set()).add(filename)
for word in word_re.findall(title):
add_term(word)
add_term(word, 'T')
for word in visitor.found_words:
add_term(word)
class SearchFrontend(object): class SearchFrontend(object):
@ -177,6 +186,6 @@ class SearchFrontend(object):
word = word[1:] word = word[1:]
else: else:
storage = required storage = required
storage.add(self._stemmer.stem(word.lower())) storage.add(self._stemmer.stem(word))
return self.query(required, excluded) return self.query(required, excluded)

View File

@ -5,14 +5,14 @@
* words. the first one is used to find the occurance, the * words. the first one is used to find the occurance, the
* latter for highlighting it. * latter for highlighting it.
*/ */
jQuery.makeSearchSummary = function(text, keywords, hlwords) { jQuery.makeSearchSummary = function(text, keywords, hlwords) {
var textLower = text.toLowerCase(); var textLower = text.toLowerCase();
var start = 0; var start = 0;
$.each(keywords, function() { $.each(keywords, function() {
var i = textLower.indexOf(this.toLowerCase()); var i = textLower.indexOf(this.toLowerCase());
if (i > -1) { if (i > -1)
start = i; start = i;
}
}); });
start = Math.max(start - 120, 0); start = Math.max(start - 120, 0);
var excerpt = ((start > 0) ? '...' : '') + var excerpt = ((start > 0) ? '...' : '') +
@ -80,9 +80,8 @@ var PorterStemmer = function() {
var firstch; var firstch;
var origword = w; var origword = w;
if (w.length < 3) { if (w.length < 3)
return w; return w;
}
var re; var re;
var re2; var re2;
@ -90,20 +89,17 @@ var PorterStemmer = function() {
var re4; var re4;
firstch = w.substr(0,1); firstch = w.substr(0,1);
if (firstch == "y") { if (firstch == "y")
w = firstch.toUpperCase() + w.substr(1); w = firstch.toUpperCase() + w.substr(1);
}
// Step 1a // Step 1a
re = /^(.+?)(ss|i)es$/; re = /^(.+?)(ss|i)es$/;
re2 = /^(.+?)([^s])s$/; re2 = /^(.+?)([^s])s$/;
if (re.test(w)) { if (re.test(w))
w = w.replace(re,"$1$2"); w = w.replace(re,"$1$2");
} else if (re2.test(w))
else if (re2.test(w)) {
w = w.replace(re2,"$1$2"); w = w.replace(re2,"$1$2");
}
// Step 1b // Step 1b
re = /^(.+?)eed$/; re = /^(.+?)eed$/;
@ -125,17 +121,16 @@ var PorterStemmer = function() {
re2 = /(at|bl|iz)$/; re2 = /(at|bl|iz)$/;
re3 = new RegExp("([^aeiouylsz])\\1$"); re3 = new RegExp("([^aeiouylsz])\\1$");
re4 = new RegExp("^" + C + v + "[^aeiouwxy]$"); re4 = new RegExp("^" + C + v + "[^aeiouwxy]$");
if (re2.test(w)) { if (re2.test(w))
w = w + "e"; w = w + "e";
}
else if (re3.test(w)) { else if (re3.test(w)) {
re = /.$/; w = w.replace(re,""); re = /.$/;
w = w.replace(re,"");
} }
else if (re4.test(w)) { else if (re4.test(w))
w = w + "e"; w = w + "e";
} }
} }
}
// Step 1c // Step 1c
re = /^(.+?)y$/; re = /^(.+?)y$/;
@ -143,7 +138,8 @@ var PorterStemmer = function() {
var fp = re.exec(w); var fp = re.exec(w);
stem = fp[1]; stem = fp[1];
re = new RegExp(s_v); re = new RegExp(s_v);
if (re.test(stem)) { w = stem + "i"; } if (re.test(stem))
w = stem + "i";
} }
// Step 2 // Step 2
@ -153,10 +149,9 @@ var PorterStemmer = function() {
stem = fp[1]; stem = fp[1];
suffix = fp[2]; suffix = fp[2];
re = new RegExp(mgr0); re = new RegExp(mgr0);
if (re.test(stem)) { if (re.test(stem))
w = stem + step2list[suffix]; w = stem + step2list[suffix];
} }
}
// Step 3 // Step 3
re = /^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/; re = /^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/;
@ -165,10 +160,9 @@ var PorterStemmer = function() {
stem = fp[1]; stem = fp[1];
suffix = fp[2]; suffix = fp[2];
re = new RegExp(mgr0); re = new RegExp(mgr0);
if (re.test(stem)) { if (re.test(stem))
w = stem + step3list[suffix]; w = stem + step3list[suffix];
} }
}
// Step 4 // Step 4
re = /^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$/; re = /^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$/;
@ -177,18 +171,16 @@ var PorterStemmer = function() {
var fp = re.exec(w); var fp = re.exec(w);
stem = fp[1]; stem = fp[1];
re = new RegExp(mgr1); re = new RegExp(mgr1);
if (re.test(stem)) { if (re.test(stem))
w = stem; w = stem;
} }
}
else if (re2.test(w)) { else if (re2.test(w)) {
var fp = re2.exec(w); var fp = re2.exec(w);
stem = fp[1] + fp[2]; stem = fp[1] + fp[2];
re2 = new RegExp(mgr1); re2 = new RegExp(mgr1);
if (re2.test(stem)) { if (re2.test(stem))
w = stem; w = stem;
} }
}
// Step 5 // Step 5
re = /^(.+?)e$/; re = /^(.+?)e$/;
@ -198,10 +190,9 @@ var PorterStemmer = function() {
re = new RegExp(mgr1); re = new RegExp(mgr1);
re2 = new RegExp(meq1); re2 = new RegExp(meq1);
re3 = new RegExp("^" + C + v + "[^aeiouwxy]$"); re3 = new RegExp("^" + C + v + "[^aeiouwxy]$");
if (re.test(stem) || (re2.test(stem) && !(re3.test(stem)))) { if (re.test(stem) || (re2.test(stem) && !(re3.test(stem))))
w = stem; w = stem;
} }
}
re = /ll$/; re = /ll$/;
re2 = new RegExp(mgr1); re2 = new RegExp(mgr1);
if (re.test(w) && re2.test(w)) { if (re.test(w) && re2.test(w)) {
@ -210,15 +201,13 @@ var PorterStemmer = function() {
} }
// and turn initial Y back to y // and turn initial Y back to y
if (firstch == "y") { if (firstch == "y")
w = firstch.toLowerCase() + w.substr(1); w = firstch.toLowerCase() + w.substr(1);
}
return w; return w;
} }
} }
/** /**
* Search Module * Search Module
*/ */
@ -267,13 +256,11 @@ var Search = {
function pulse() { function pulse() {
Search._pulse_status = (Search._pulse_status + 1) % 4; Search._pulse_status = (Search._pulse_status + 1) % 4;
var dotString = ''; var dotString = '';
for (var i = 0; i < Search._pulse_status; i++) { for (var i = 0; i < Search._pulse_status; i++)
dotString += '.'; dotString += '.';
}
Search.dots.text(dotString); Search.dots.text(dotString);
if (Search._pulse_status > -1) { if (Search._pulse_status > -1)
window.setTimeout(pulse, 500); window.setTimeout(pulse, 500);
}
}; };
pulse(); pulse();
}, },
@ -320,9 +307,8 @@ var Search = {
hlwords.push(tmp[i].toLowerCase()); hlwords.push(tmp[i].toLowerCase());
} }
// only add if not already in the list // only add if not already in the list
if (!$.contains(toAppend, word)) { if (!$.contains(toAppend, word))
toAppend.push(word); toAppend.push(word);
}
}; };
var highlightstring = '?highlight=' + $.urlencode(hlwords.join(" ")); var highlightstring = '?highlight=' + $.urlencode(hlwords.join(" "));
@ -413,12 +399,10 @@ var Search = {
else { else {
Search.stopPulse(); Search.stopPulse();
Search.title.text(_('Search Results')); Search.title.text(_('Search Results'));
if (!resultCount) { 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.')); 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
else {
Search.status.text(_('Search finished, found %s page(s) matching the search query.').replace('%s', resultCount)); Search.status.text(_('Search finished, found %s page(s) matching the search query.').replace('%s', resultCount));
}
Search.status.fadeIn(500); Search.status.fadeIn(500);
} }
} }

View File

@ -1,6 +1,6 @@
{% extends "layout.html" %} {% extends "layout.html" %}
{% set title = _('Search') %} {% set title = _('Search') %}
{% set script_files = script_files + ['_static/searchtools.js', 'searchindex.js'] %} {% set script_files = script_files + ['_static/searchtools.js'] %}
{% block body %} {% block body %}
<h1 id="search-documentation">{{ _('Search') }}</h1> <h1 id="search-documentation">{{ _('Search') }}</h1>
<p> <p>
@ -32,3 +32,7 @@
{% endif %} {% endif %}
</div> </div>
{% endblock %} {% endblock %}
{% block footer %}
{{ super() }}
<script type="text/javascript" src="searchindex.js"></script>
{% endblock %}