File: apilinks.py

package info (click to toggle)
python-tubes 0.2.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 740 kB
  • sloc: python: 3,215; makefile: 149
file content (68 lines) | stat: -rw-r--r-- 1,904 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Sphinx/docutils extension to create links to pyDoctor documentation using a
RestructuredText interpreted text role that looks like this::

    :api:`python_object_to_link_to <label>`

for example::

    :api:`twisted.internet.defer.Deferred <Deferred>`

"""

from twisted.python.reflect import namedAny
from types import FunctionType


def make_api_link(name, rawtext, text, lineno, inliner,
                  options={}, content=[]):

    from docutils import nodes, utils

    # quick, dirty, and ugly...
    if '<' in text and '>' in text:
        full_name, label = text.split('<')
        full_name = full_name.strip()
        label = label.strip('>').strip()
    else:
        full_name = label = text

    #get the base url for api links from the config file
    env = inliner.document.settings.env
    if full_name.startswith("twisted"):
        base_url = env.config.apilinks_base_url
    else:
        base_url = "https://twisted.org/tubes/docs/"

    # not really sufficient, but just testing...
    # ...hmmm, maybe this is good enough after all
    try:
        consider = namedAny(full_name)
    except:
        consider = None
    if isinstance(consider, (type(None), FunctionType)):
        dot_path, fragment = full_name.rsplit(".", 1)
        if fragment:
            fragment = "#" + fragment
    else:
        dot_path = full_name
        fragment = ""
    ref = ''.join((base_url, dot_path, '.html', fragment))

    options.update(classes=["api"])

    node = nodes.reference(rawtext, utils.unescape(label), refuri=ref,
                           **options)

    nodes = [node]
    sys_msgs = []
    return nodes, sys_msgs


# setup function to register the extension

def setup(app):
    app.add_config_value('apilinks_base_url',
                         'https://twistedmatrix.com/documents/current/api/',
                         'env')
    app.add_role('api', make_api_link)