Python sphinx 用于引用的自定义sphinx角色

Python sphinx 用于引用的自定义sphinx角色,python-sphinx,docutils,Python Sphinx,Docutils,我想创建一个sphinx角色:config:`param`,它显示为文本config[“param”],同时是一个链接 我有一个基本的运行版本: def config_role(name, rawtext, text, lineno, inliner, options={}, content=[]): rendered = nodes.Text('config["{}"]'.format(text)) rel_source = inliner.document.attribut

我想创建一个sphinx角色
:config:`param`
,它显示为文本
config[“param”]
,同时是一个链接

我有一个基本的运行版本:

def config_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
    rendered = nodes.Text('config["{}"]'.format(text))

    rel_source = inliner.document.attributes['source'].split('/doc/', 1)[1]
    levels = rel_source.count('/')
    refuri = ('../' * levels +
              'tutorials/introductory/customizing.html#config')

    ref = nodes.reference(rawtext, rendered, refuri=refuri)
    return [nodes.literal('', '', ref)], []


def setup(app):
    app.add_role("config", config_role)
    return {"parallel_read_safe": True, "parallel_write_safe": True}
然而,有两个问题:

  • 链接的目标位于文档的另一部分。因此,我必须显式地创建
    refuri
    。是否可以像使用.rst中的
    :ref:`config`
    那样使用链接锚
  • nodes.reference
    有一个不必要的效果,即双引号被印刷引号替换。因为它是在一个文字块,我宁愿保留简单的双引号。这有可能吗

对于印刷引号,您使用的是哪种版本的Sphinx?您的
conf.py
中是否有
html\u use\u smartypants=True
smartquotes=True
设置?Sphinx 1.7.5。这两个变量均未在
conf.py
中定义。您需要显式添加它并将其设置为
False
。对不起,我不知道关于这个角色的主要问题。我无法解析那个问题。谢谢!在我上面的代码中,是否有某种方法可以暂时关闭该选项(仅针对引用节点)?当使用选项调用
sphinx build
时,命令行上的所有配置选项都可能被覆盖。