将自定义边栏模板映射到任何名为“索引.rst"

2024-05-08 10:54:57 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有以下项目结构:

/
    index.rst
    1/
        index.rst
        a.rst
    2/
        index.rst
        b.rst
        3/
            index.rst
            c.rst

如何在Sphinx的conf.py中创建一个html_sidebars规则,该规则适用于所有名为index.rst的文件?如果可能,请提供Sphinx支持文档,解释可与html_sidebars一起使用的各种通配符。你知道吗

或者我必须明确指定每个文件。。。你知道吗

html_sidebars = {'**': ['non-index-template.html'],
                 'index.rst': ['index-template.html'],
                 '1/index.rst': ['index-template.html'],
                 '2/index.rst': ['index-template.html'],
                 '3/index.rst': ['index-template.html']
}

切题是,有没有一种方法可以引用字典中字典键的值?E、 g

html_sidebars = {'**': ['non-index-template.html'],
                 'index.rst': ['index-template.html'],
                 '1/index.rst': [this['index.rst']],
                 '2/index.rst': [this['index.rst']],
                 '3/index.rst': [this['index.rst']]
}

Tags: 文件项目pyindex字典规则confhtml
2条回答

The build configuration file的脚注中:

A note on available globbing syntax: you can use the standard shell constructs *, ?, [...] and [!...] with the feature that these all don’t match slashes. A double star ** can be used to match any sequence of characters including slashes.

因此,以下内容将完成我的目标(如果可用语法有更简洁的方法,请告诉我):

html_sidebars = {'**[!i][!n][!d][!e][!x]': ['non-index-sidebar.html'],
                 '**index': ['index-sidebar.html'],
}

我也在sphinx-users谷歌群里发布了这个问题。下面是来自Takayuki Shimizukawa的答案。你知道吗

这可能有效,但请注意,它会发出警告:

html_sidebars = { 
    '**': ['non-index-template.html'], 
    '**/index': ['index-template.html'], 
    'index': ['index-template.html']
} 

回复:引用该词典中词典键的值:

html_sidebars = { 
    '**': ['non-index-template.html'], 
    'index.rst': ['index-template.html']
} 
html_sidebars['1/index.rst'] = html_sidebars['index.rst'] 
html_sidebars['2/index.rst'] = html_sidebars['index.rst'] 
html_sidebars['3/index.rst'] = html_sidebars['index.rst'] 

或者

index_sidebars = ['index-template.html'] 
html_sidebars = { 
    '**': ['non-index-template.html'], 
    'index.rst': index_sidebars, 
    '1/index.rst': index_sidebars, 
    '2/index.rst': index_sidebars, 
    '3/index.rst': index_sidebars
} 

相关问题 更多 >