Substitutions inside links in reST / Sphinx

后端 未结 4 1735
忘掉有多难
忘掉有多难 2020-12-25 13:03

I am using Sphinx to document a webservice that will be deployed in different servers. The documentation is full of URL examples for the user to click and they should just w

4条回答
  •  一生所求
    2020-12-25 13:29

    Ok, here's how I did it. First, apilinks.py (the Sphinx extension):

    from docutils import nodes, utils
    
    def setup(app):
        def api_link_role(role, rawtext, text, lineno, inliner, options={},
                          content=[]):
            ref = app.config.apilinks_base + text
            node = nodes.reference(rawtext, utils.unescape(ref), refuri=ref,
                                   **options)
            return [node], []
        app.add_config_value('apilinks_base', 'http://localhost/', False)
        app.add_role('apilink', api_link_role)
    

    Now, in conf.py, add 'apilinks' to the extensions list and set an appropriate value for 'apilinks_base' (otherwise, it will default to 'http://localhost/'). My file looks like this:

    extensions = ['sphinx.ext.autodoc', 'apilinks']
    # lots of other stuff
    apilinks_base = 'http://host:88/base/'
    

    Usage:

    :apilink:`path`
    

    Output:

    http://host:88/base/path
    

提交回复
热议问题