Reputation: 1143
In a Sphinx project, how does one prevent documents in the _static directory of the project from appearing in the project search results?
Upvotes: 2
Views: 383
Reputation: 50947
There is no configuration option for excluding files in a particular directory from the search index.
However, you can do it by modifying the IndexBuilder.feed()
method. One of the arguments to this method is doctree
(an instance of the Docutils document
class). The path to the .rst document being processed is the value of doctree.attributes['source']
.
Add the following monkey patch to conf.py:
from sphinx.search import IndexBuilder, WordCollector
def feed(self, filename, title, doctree):
"""Feed a doctree to the index."""
# Patch: if '_static' is in the path, don't use the file to
# populate the search index
source = doctree.attributes["source"]
if "_static" in source:
return
self._titles[filename] = title
visitor = WordCollector(doctree, self.lang)
doctree.walk(visitor)
def add_term(word, stem=self.lang.stem):
word = stem(word)
if self.lang.word_filter(word):
self._mapping.setdefault(word, set()).add(filename)
for word in self.lang.split(title):
add_term(word)
for word in visitor.found_words:
add_term(word)
IndexBuilder.feed = feed
Tested with Sphinx 1.1.3.
Upvotes: 3