/* search-redirect.js Redirect functionality. Test if the given query is of the form "name: terms" or "search name for terms" where name is one of a list of search engines. If this is the case, redirect to a search using this engine. External objects: RedirectQuery - Class to redirect queries to other engines. RedirectQuery.handle(query) - Test if the query string has the form "name: terms" where name is one of the names specified in redirect_searhches. If so search with this search engine. Semi-external objects: RedirectQuery.searches - associative array mapping search engine names to a pair [URL, separator] used to redirect. The URL has the form "...#{keywords}...". Where #{keywords} is replaced with a -separated list of keywords. All objects prefixed with redirect_. */ var RedirectQuery = { searches : { "ask" : ["http://ask.com/web?q=#{keywords}", "+"], "ask images": ["http://ask.com/pictures?q=#{keywords}", "+"], "ask news": ["http://ask.com/news?q=#{keywords}", "+"], "ask images": ["http://uk.ask.com/pictures?q=#{keywords}", "+"], "ebay" : ["http://search.ebay.com/search/search.dll?satitle=#{keywords}", "+"], "freedictionary": ["http://www.thefreedictionary.com/#{keywords}", "+"], "google": ["http://www.google.com/search?q=#{keywords}", "+"], "google code": ["http://code.google.com/#q=#{keywords}", "+"], "google images": ["http://images.google.com/images?q=#{keywords}", "+"], "google news": ["http://news.google.com/news?q=#{keywords}", "+"], "google patents": ["http://www.google.com/patents?q=#{keywords}", "+"], "google scholar": ["http://scholar.google.com/scholar?q=#{keywords}", "+"], "google shopping": ["http://www.google.com/products?q=#{keywords}", "+"], "google maps": ["http://maps.google.com/maps?q=#{keywords}", "+"], "google video": ["http://video.google.com/videosearch?q=#{keywords}", "+"], "slashdot": ["http://slashdot.org/search.pl?query=#{keywords}", "+"], "delicious": ["http://del.icio.us/search/?p=#{keywords}", "+"], "wiktionary": ["http://en.wiktionary.org/wiki/cats", "_"], "wikiquote" : ["http://en.wikiquote.org/wiki/", "_"], "wikipedia": ["http://en.wikipedia.org/wiki/#{keywords}", '_'], "yahoo": ["http://search.yahoo.com/search?p=#{keywords}", "+"], "youtube": ["http://www.youtube.com/results?search_query=#{keywords}&search=Search", "+"] }, /* See if the query is meant to be diverted to a different search engine. If so redirect. */ handle : function (query) { for (name in this.searches) { var URL_template = this.searches[name][0]; var separator = this.searches[name][1]; var re_str1 = '^\\s*search\\s+#{name}\\s+for\\s+(.*)'; var re_str2 = '^\\s*#{name}:\\s*(.*)'; var re_str3 = '^\\s*using\\s+#{name}\\s+find\\s+(.*)'; var re_str4 = '\\s*find\\s+(.*)\\s+on\\s+#{name}'; var re_str5 = '\\s*look\\s+for\\s+(.*)\\s+on\\s+#{name}'; this._search_verify(name, URL_template, query, separator, re_str1); this._search_verify(name, URL_template, query, separator, re_str2); this._search_verify(name, URL_template, query, separator, re_str3); this._search_verify(name, URL_template, query, separator, re_str4); this._search_verify(name, URL_template, query, separator, re_str5); // This should become a loop if you add any more regexps. } }, /* Verifies if the search term tells search wikia to search with a different search engine and redirect. If the search term is of the form : other terms, then the function redirects to the URL specified by search_template - obtained by replacing any instance of #{keywods} with a concatenated list of search terms. name : The name of the optional seach engine. search_template : The URL template for searching with this search engine. A URL with #{keywords} in place of the list of keywords. newq: The search term as a string. sep (optional): The seperator used when created the list of keywords for search_template. The default value is '+' */ _search_verify: function(name, search_template, newq, sep, re_str) { if (!sep) { sep = '+'; } expr = new RegExp(re_str.interpolate({name: name})); if ((groups = expr.exec(newq))) { var terms = groups[1].split(/\s+/); var term_str = terms.join(sep); window.location.assign(search_template.interpolate({'keywords': term_str})); } } };