Georg Brandl avatar Georg Brandl committed f7d3921

#445: Fix links to result pages when using the search function of HTML built with the ``dirhtml`` builder.

Comments (0)

Files changed (4)

 Release 0.6.8 (in development)
 ==============================
 
+* #445: Fix links to result pages when using the search function
+  of HTML built with the ``dirhtml`` builder.
+
 * #444: In templates, properly re-escape values treated with the
   "striptags" Jinja filter.
 

sphinx/builders/html.py

 
         return outfilename
 
+    def prepare_writing(self, docnames):
+        StandaloneHTMLBuilder.prepare_writing(self, docnames)
+        self.globalcontext['no_search_suffix'] = True
+
 
 class SerializingHTMLBuilder(StandaloneHTMLBuilder):
     """

sphinx/themes/basic/layout.html

         URL_ROOT:    '{{ url_root }}',
         VERSION:     '{{ release|e }}',
         COLLAPSE_MODINDEX: false,
-        FILE_SUFFIX: '{{ file_suffix }}',
+        FILE_SUFFIX: '{{ '' if no_search_suffix else file_suffix }}',
         HAS_SOURCE:  {{ has_source|lower }}
       };
     </script>

sphinx/themes/basic/static/searchtools.js

       if (results.length) {
         var item = results.pop();
         var listItem = $('<li style="display:none"></li>');
-        listItem.append($('<a/>').attr(
-          'href',
-          item[0] + DOCUMENTATION_OPTIONS.FILE_SUFFIX +
-          highlightstring + item[2]).html(item[1]));
+        if (DOCUMENTATION_OPTIONS.FILE_SUFFIX == '') {
+          // dirhtml builder
+          var dirname = item[0] + '/';
+          if (dirname.match(/\/index\/$/)) {
+            dirname = dirname.substring(0, dirname.length-6);
+          } else if (dirname == 'index/') {
+            dirname = '';
+          }
+          listItem.append($('<a/>').attr('href',
+            DOCUMENTATION_OPTIONS.URL_ROOT + dirname +
+            highlightstring + item[2]).html(item[1]));
+        } else {
+          // normal html builders
+          listItem.append($('<a/>').attr('href',
+            item[0] + DOCUMENTATION_OPTIONS.FILE_SUFFIX +
+            highlightstring + item[2]).html(item[1]));
+        }
         if (item[3]) {
           listItem.append($('<span> (' + item[3] + ')</span>'));
           Search.output.append(listItem);
         } else if (DOCUMENTATION_OPTIONS.HAS_SOURCE) {
           $.get(DOCUMENTATION_OPTIONS.URL_ROOT + '_sources/' +
                 item[0] + '.txt', function(data) {
-            listItem.append($.makeSearchSummary(data, searchterms, hlterms));
-            Search.output.append(listItem);
+            if (data != '') {
+              listItem.append($.makeSearchSummary(data, searchterms, hlterms));
+              Search.output.append(listItem);
+            }
             listItem.slideDown(5, function() {
               displayNextItem();
             });
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.