svn: /web/php/trunk/ js/common.js search-index.php styles/structure.css

[email protected] (Hannes Magnusson)
Newsgroups php.webmaster
Message-ID <[email protected]>
bjori                                    Fri, 31 Dec 2010 10:07:10 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=306909

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:
    U   web/php/trunk/js/common.js
    A   web/php/trunk/search-index.php
    U   web/php/trunk/styles/structure.css

Modified: web/php/trunk/js/common.js
===================================================================
--- web/php/trunk/js/common.js	2010-12-31 09:31:05 UTC (rev 306908)
+++ web/php/trunk/js/common.js	2010-12-31 10:07:10 UTC (rev 306909)
@@ -33,35 +33,86 @@
     });

     // 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);

Added: web/php/trunk/search-index.php
===================================================================
--- web/php/trunk/search-index.php	                        (rev 0)
+++ web/php/trunk/search-index.php	2010-12-31 10:07:10 UTC (rev 306909)
@@ -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");
+

Modified: web/php/trunk/styles/structure.css
===================================================================
--- web/php/trunk/styles/structure.css	2010-12-31 09:31:05 UTC (rev 306908)
+++ web/php/trunk/styles/structure.css	2010-12-31 10:07:10 UTC (rev 306909)
@@ -208,12 +208,21 @@
     */
     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 {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.