chiborg
chiborg

Reputation: 28084

Can I keep all my external links in a separate file with Sphinx?

I have a Sphinx project with a collection of files that contain external links as references at the bottom of each file like this:

Some text with `my link`_, more text
100 lines of text ...

.. _my link: http://example.com

I'd like to reorganize my files, splitting them, merging them and reusing external links. To make this easier, I'd like to keep my links in a separate file, each one with a unique id that I can reference in the text. Is there a way to do this? The rendered output should still create an external link, not a footnote like it was suggested in How do I collect all external links in Sphinx?

Upvotes: 4

Views: 645

Answers (1)

chiborg
chiborg

Reputation: 28084

This can be solved with the right settings in conf.py:

# Exclude link file
exclude_patterns = ['_build', 'links.rst']

# make rst_epilog a variable, so you can add other epilog parts to it
rst_epilog =""
# Read link all targets from file
with open('links.rst') as f:
     rst_epilog += f.read()

links.rst looks like this:

.. _my link: http://example.com
.. _my other link: http://example2.com

With this setup, I'm flexible to use either the unique ID as the link label or provide a custom link label:

Some text with `my link`_ and a `custom label<my other link>`_

Upvotes: 7

Related Questions