在Sphinx PDF文档中指向外部git存储库的超链接

时间:2013-09-01 15:42:55

标签: python python-sphinx

形式提供给git存储库的外部超链接
.. _Git repo: git://github.com/migueldvb/repo.git

在HTML文档中正确显示,但它们不会显示在PDF文件中。运行python setup.py sphinx_build -b latex以生成LaTeX文件时会出现警告:

unusable reference target found git://github.com/...

如何在Sphinx生成的PDF文档中包含指向git存储库的超链接?

1 个答案:

答案 0 :(得分:4)

这似乎有效:

.. raw:: latex

    \href{git://github.com}{GitHub}

.. raw:: html

    <a href="git://github.com">GitHub</a>

编辑:我查看了sphinx的源代码,并在visit_reference方法的sphinx / writers / latex.py文件中生成了警告消息,该方法定义如下:

def visit_reference(self, node):
        uri = node.get('refuri', '')
        # ...
        elif uri.startswith('mailto:') or uri.startswith('http:') or \
                 uri.startswith('https:') or uri.startswith('ftp:'):
            self.body.append('\\href{%s}{' % self.encode_uri(uri))
        else:
            self.builder.warn('unusable reference target found: %s' % uri,
                              (self.curfilestack[-1], node.line))
            self.context.append('')

因此协议是硬编码的,因此我认为没有选择或简单的方法而不使用原始数据或更改sphinx源以使“未知”链接起作用。

我在源代码中添加了git协议的一行:

def visit_reference(self, node):
        uri = node.get('refuri', '')
        # ...
        elif uri.startswith('mailto:') or uri.startswith('http:') or \
                 uri.startswith('git:') or \
                 uri.startswith('https:') or uri.startswith('ftp:'):
            self.body.append('\\href{%s}{' % self.encode_uri(uri))

现在'make latexpdf'和'make html'正在生成带有以下来源的工作git链接的文档:

Test Link
=========

This is a paragraph that contains `a link`_.

.. _a link: git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git