我正在使用Sphinx编写一个包含大量引用的文档:
.. _human-factor:
The Human Factor
================
...
(see :ref:`human-factor` for details)
编译后的文档包含以下内容:
(详见人为因素)
相反,我希望将其格式化为:
(详见 5.1人为因素)
我试图谷歌解决方案,我发现乳胶hyperref包可以做到这一点,但我不知道如何将其添加到Sphinx构建。
答案 0 :(得分:2)
我基本上从这里使用numsec.py来解决它:https://github.com/jterrace/sphinxtr
我不得不用这个替换doctree_resolved函数来获得节号+标题(例如“5.1人为因素”)。
def doctree_resolved(app, doctree, docname):
secnums = app.builder.env.toc_secnumbers
for node in doctree.traverse(nodes.reference):
if 'refdocname' in node:
refdocname = node['refdocname']
if refdocname in secnums:
secnum = secnums[refdocname]
emphnode = node.children[0]
textnode = emphnode.children[0]
toclist = app.builder.env.tocs[refdocname]
anchorname = None
for refnode in toclist.traverse(nodes.reference):
if refnode.astext() == textnode.astext():
anchorname = refnode['anchorname']
if anchorname is None:
continue
linktext = '.'.join(map(str, secnum[anchorname]))
node.replace(emphnode, nodes.Text(linktext
+ ' ' + textnode))
要使其工作,需要在conf.py中包含numsec扩展名,并在toctree中添加:numbers:如下所示:
.. toctree::
:maxdepth: 1
:numbered: