在Sphinx项目中,如何防止项目_static目录下的文档出现在项目搜索结果中?
最佳答案
没有用于从搜索索引中排除特定目录中的文件的配置选项。
但是,您可以通过修改 IndexBuilder.feed()
来做到这一点方法。此方法的参数之一是 doctree
(Docutils document
类的实例)。正在处理的 .rst 文档的路径值为 doctree.attributes['source']
.
将以下猴子补丁添加到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
使用 Sphinx 1.1.3 进行测试。
关于python - 如何在 Python 项目中从 Sphinx 的搜索索引中隐藏 _static 目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12536094/