python - 如何在 Python 项目中从 Sphinx 的搜索索引中隐藏 _static 目录?

标签 python python-sphinx

在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/

相关文章:

python - 无法在 Windows 10 : Errors in zbar. h 上为 Python 2.7.9 安装 ZBar

python - 子进程打开文件和管道 awk 命令

python - 如何在python中初始化一个二维字符串DataFrame数组

python-sphinx - 在 Sphinx 的表中添加一个类?

python - 如何读取 Sphinx 扩展节点中的 conf.py 设置?

python - 保持 User.email 在 Django Admin 中唯一

python - 使用 flask 重定向获取下一个 URL

python - 如何在标准库文档中放置指向任意方法的 intersphinx 链接?

python - Sphinx 中尖括号旁边的替换

python-sphinx - 如何为restructuredText表添加标题?