Commit: e4c9d8fc2fad2a6b179510f4deea5485ce2ccfaa Author: Hannes Magnusson <[email protected]> Fri, 31 Dec 2010 10:07:10 +0000 Parents: e5a2500c17ec7f2f83651866678965a89b9cb5a8 Branches: master
Link: http://git.php.net/?p=web/php.git;a=commitdiff;h=e4c9d8fc2fad2a6b179510f4deea5485ce2ccfaa Log: - Use the generated search index from PhD (generated at the same time as the docs) - Add descriptions into the search results - Only sort possible matches, rather then the entire index everytime - Use "poor-mans-categorization" - with the 'proper categorization' code commented out. Maybe provide an my.php option in the future for it? Changed paths: M js/common.js A search-index.php M styles/structure.css Diff: diff --git a/js/common.js b/js/common.js index d618895..f441e54 100644 --- a/js/common.js +++ b/js/common.js @@ -33,35 +33,86 @@ $(document).ready(function() { }); // load the search index and enable auto-complete. - jQuery.getScript("/js/search-index-" + getLanguage() + ".js", function(){ - $('#headsearch-keywords').autocomplete({ + jQuery.getScript("/search-index.php?lang=" + getLanguage(), function(){ + var haveDesc = 0; + jQuery.getScript("/search-index.php?lang=" + getLanguage() + "&description=1", function(){ + haveDesc = 1; + }); + $.widget("custom.catcomplete", $.ui.autocomplete, { + /* + * Print out category headers rather then in () after the match + _renderMenu: function(ul, items) { + var self = this, currentCategory = ""; + $.each(items, function(index, item) { + var cat = self._resolveIndexName(item.category); + if (cat != currentCategory) { + ul.append("<li class='ui-autocomplete-category'>" + cat + "</li>"); + currentCategory = cat; + } + + self._renderItem(ul, item); + }); + }, + */ + _renderItem: function(ul, item) { + var n = $("<li></li>").data("item.autocomplete", item); + var cat = this._resolveIndexName(item.category); + if (item.desc) { + n.append("<a>" + item.label + " (" + cat + ")<br>" + item.desc + "</a>"); + } + else { + n.append("<a>" + item.label + " (" + cat + ") </a>"); + } + + return n.appendTo(ul); + }, + // Resolve the element names to human understandable things + _resolveIndexName: function(key) { + var indexes = { + 'phpdoc:varentry': 'Variables', + 'phpdoc:classref': 'Classes', + 'phpdoc:exceptionref': 'Exceptions', + 'refentry': 'Functions' + }; + return indexes[key]; + } + }); + $('#headsearch-keywords').catcomplete({ delay: 50, - minScore: 75, + minScore: 50, maxResults: 50, source: function(request, response){ - // sort the search index by similarity to the user's query var term = request.term; + var minScore = this.options.minScore; + + // Score an possible match var score = function(item){ - var match = item.name.search(new RegExp(term, "i")); + var match = item.search(new RegExp(term, "i")); if (match < 0) return 0; - return 100 - (match * 2) - (item.name.length - term.length); + return 100 - (match * 2) - (item.length - term.length); }; - searchIndex.sort(function(a, b){ - return score(b) - score(a); - }); - - // display the best matches + var results = []; - for (var i = 0; i < this.options.maxResults; i++){ - var item = searchIndex[i]; - if (item && score(item) > this.options.minScore){ + $.each(searchIndex, function(idx, item) { + var itemScore = score(item[0]); + if (item && itemScore > minScore) { results.push({ - label: item.name, - value: item.page + label: item[0], + value: item[1], + desc: haveDesc ? descriptionIndex[item[1]] : '', + category: item[2], + score: itemScore }); } - } - response(results); + }); + + // Only sort the matches + results.sort(function(a, b){ + return b.score - a.score; + }); + + // Return at most maxResults + response(results.slice(0, this.options.maxResults)); }, focus: function(event, ui) { $('#headsearch-keywords').val(ui.item.label); diff --git a/search-index.php b/search-index.php new file mode 100644 index 0000000..ab53c64 --- /dev/null +++ b/search-index.php @@ -0,0 +1,22 @@ +<?php +require dirname(__FILE__) . "/include/languages.inc"; + +$langcode = language_convert(isset($_GET["lang"]) ? $_GET["lang"] : "en"); + +if (isset($_GET["description"])) { + $filename = "search-description"; + $varname = "descriptionIndex"; +} +else { + $filename = "search-index"; + $varname = "searchIndex"; +} + +header("Content-Type: application/javascript"); + +// Will send out the proper header, if the browser supports it at all +ob_start("ob_gzhandler"); + +echo "$varname = "; +readfile(dirname(__FILE__) . "/manual/$langcode/$filename.json"); + diff --git a/styles/structure.css b/styles/structure.css index a051ca0..9fc01db 100644 --- a/styles/structure.css +++ b/styles/structure.css @@ -208,12 +208,21 @@ footer .footmenu li { */ line-height: 16px; overflow: hidden; + border-bottom: 1px solid #c3add9 ; } .ui-autocomplete a { display: block; padding: 2px 5px; } +.ui-autocomplete-category { + font-weight: bold; + padding: .2em .4em; + margin: .8em 0 .2em; + line-height: 1.5; + background-color: #987db3; + color: #fff; +} aside.tips { -- PHP Webmaster List Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
