From cd8621726a6a207944e2b9b0d4c5e8703441bacf Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Mon, 2 Jun 2008 02:31:21 +0000 Subject: [PATCH] module-restricted searches, improved filtering, help messages svn: r10080 --- .../scribblings/main/private/make-search.ss | 118 ++++++++++++++---- 1 file changed, 91 insertions(+), 27 deletions(-) diff --git a/collects/scribblings/main/private/make-search.ss b/collects/scribblings/main/private/make-search.ss index 893941ab62..337bb562b1 100644 --- a/collects/scribblings/main/private/make-search.ss +++ b/collects/scribblings/main/private/make-search.ss @@ -113,16 +113,18 @@ @(add-between (map (lambda (x) (format "~s" (car x))) (reverse span-classes)) ",\n ")]; - // this array has an entry for each index link: [text, url, html, from-lib] + // this array has an entry for each index link: [text, url, html, from_lib] // - text is a string holding the indexed text // - url holds the link (">" prefix means relative to plt_main_url) // - html holds either a string, or [idx, html] where idx is an // index into plt_span_classes (note: this is recursive) + // - from_lib is an array of module names for bound identifiers, + // or the string "module" for a module entry plt_search_data = [ @(add-between l ",\n")]; // Globally visible bindings - var key_handler, toggle_help_pref, hide_prefs, + var key_handler, toggle_help_pref, hide_prefs, new_query, set_results_num, set_type_delay, set_highlight_color; (function(){ @@ -158,9 +160,18 @@ +' border-top: 0px; padding: 0.5em;' +' background-color: #f0f0f0;"' +'>' - +'• Hit PageUp/PageDown and Enter' - +' to scroll through the results.
' - +'
' + +'
' +'Preferences:
' +'Results per page:' +' <<' +'' - +'' - +' ' - +'' + +' ' +'' +' results_num) results_container.removeChild(result_links.pop()); while (result_links.length < results_num) { @@ -237,41 +247,77 @@ } } + // `rexact' is for an actual exact match, so we know that we matched + // *something* and can show exact matches as such + var C_fail = 0, C_match = 1, C_prefix = 2, C_exact = 3, C_rexact = 4; + function Compare(pat, str) { + var i = str.indexOf(pat); + if (i < 0) return C_fail; + else if (i > 0) return C_match; + else if (pat.length == str.length) return C_rexact; + else return C_prefix; + } + function MaxCompares(pat, strs) { + var r = C_fail; + for (var i=0@";" i= C_exact) ? C_exact : C_fail; + } + case "M": return function(text,info) { + if (!info) return C_fail; + if (info == "module") return Compare(term,text); // rexact allowed! + return (MaxCompares(term,info) >= C_match) ? C_exact : C_fail; + } + default: return function(text,info) { + switch (Compare(term,text)) { + case C_fail: return C_fail; + case C_match: case C_prefix: return C_match; + case C_exact: case C_rexact: return (info ? C_rexact : C_match); + } + } + } + } + var last_search_term, last_search_term_raw; var search_results = [], first_search_result, exact_results_num; function DoSearch() { var term = query.value; if (term == last_search_term_raw) return; last_search_term_raw = term; - term = term.toLowerCase() - .replace(/\s\s*/g," ") // single spaces + term = term.replace(/\s\s*/g," ") // single spaces .replace(/^\s/g,"").replace(/\s$/g,""); // trim edge spaces if (term == last_search_term) return; last_search_term = term; status.innerHTML = "Searching " + plt_search_data.length + " entries"; var terms = (term=="") ? [] : term.split(/ /); + for (var i=0@";" i= C_rexact) exact_results.push(plt_search_data[i]); + else if (r > C_fail) search_results.push(plt_search_data[i]); } exact_results_num = exact_results.length; - while (exact_results.length > 0) - search_results.unshift(exact_results.pop()); + if (exact_results.length > 0) + search_results = exact_results.concat(search_results); } first_search_result = 0; status.innerHTML = "" + search_results.length + " entries found"; @@ -310,8 +356,12 @@ if ((desc instanceof Array) && (desc.length > 0)) { note = 'provided from '; for (var j=0@";" j' + desc[j] + ''; + note += + (j==0 ? "" : ", ") + + '' + + desc[j] + ''; } else if (desc == "module") { note = 'module'; } @@ -396,6 +446,20 @@ } key_handler = HandleKeyEvent; + // use this one to set the query field without jumping to the current + // url again, since some browsers will reload the whole page for that + // (it would be nice if there was a way to add it to the history too) + function NewQuery(node) { + var m = node.href.search(/[?]q=[^?&@";"]+$/); + if (m < 0) return true; + else { + query.value = decodeURIComponent(node.href.substring(m+3)); + DoSearch(); + return false; + } + } + new_query = NewQuery; + var panel_shown = false; function TogglePanel() { panel_shown = !panel_shown;