2

在 Sphinx 项目中,如何防止项目的 _static 目录中的文档出现在项目搜索结果中?

4

1 回答 1

3

没有用于从搜索索引中排除特定目录中的文件的配置选项。

但是,您可以通过修改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 测试。

于 2012-10-02T15:07:44.390 回答