mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
match dfn terms with incoming search keywords and jump to the containing section
This commit is contained in:
@@ -1,7 +1,69 @@
|
||||
/*
|
||||
The following three functions are taken from
|
||||
http://code.google.com/p/javascript-search-term-highlighter/
|
||||
Copyright 2004 Dave Lemen
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
function getSearchTerms() {
|
||||
var highlighterParameters = 'q as_q as_epq as_oq query search';
|
||||
var a = new Array();
|
||||
var params = getParamValues(document.referrer/*document.location.href*/, highlighterParameters);
|
||||
var terms;
|
||||
for (i = 0; i < params.length; i++) {
|
||||
terms = parseTerms(params[i]);
|
||||
for (j = 0; j < terms.length; j++) {
|
||||
if (terms[j] != '') {
|
||||
a.push(terms[j].toLowerCase());
|
||||
}
|
||||
}
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
function parseTerms(query) {
|
||||
var s = query + '';
|
||||
s = s.replace(/(^|\s)(site|related|link|info|cache):[^\s]*(\s|$)/ig, ' ');
|
||||
s = s.replace(/[^a-z0-9_-]/ig, ' '); // word chars only.
|
||||
s = s.replace(/(^|\s)-/g, ' '); // +required -excluded ~synonyms
|
||||
s = s.replace(/\b(and|not|or)\b/ig, ' ');
|
||||
s = s.replace(/\b[a-z0-9]\b/ig, ' '); // one char terms
|
||||
return s.split(/\s+/);
|
||||
}
|
||||
|
||||
function getParamValues(url, parameters) {
|
||||
var params = new Array();
|
||||
var p = parameters.replace(/,/, ' ').split(/\s+/);
|
||||
if (url.indexOf('?') > 0) {
|
||||
var qs = url.substr(url.indexOf('?') + 1);
|
||||
var qsa = qs.split('&');
|
||||
for (i = 0; i < qsa.length; i++) {
|
||||
nameValue = qsa[i].split('=');
|
||||
if (nameValue.length != 2) continue;
|
||||
for (j = 0; j < p.length; j++) {
|
||||
if (nameValue[0] == p[j]) {
|
||||
params.push(unescape(nameValue[1]).toLowerCase().replace(/\+/g, ' '));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
"Dive Into Python 3" scripts
|
||||
|
||||
The rest of this script is
|
||||
Copyright (c) 2009, Mark Pilgrim, All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
@@ -92,6 +154,20 @@ $(document).ready(function() {
|
||||
});
|
||||
});
|
||||
|
||||
/* match <dfn> terms with incoming search keywords and jump to the containing section */
|
||||
var searchTerms = getSearchTerms();
|
||||
$("dfn").each(function() {
|
||||
var dfn = $(this);
|
||||
var dfnTerm = dfn.text().toLowerCase();
|
||||
if ($.inArray(dfnTerm, searchTerms) != -1) {
|
||||
var section = dfn.parents("p,table,ul,ol,blockquote").prevAll("*:header").get(0);
|
||||
if (section) {
|
||||
window.setTimeout(function() {document.location.hash = section.id;}, 0);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}); /* document.ready */
|
||||
|
||||
function toggleCodeBlock(id) {
|
||||
|
||||
Reference in New Issue
Block a user