Skip to content

Commit 1daef06

Browse files
committed
Add code linking in API docs
1 parent a262bfc commit 1daef06

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

doc/conf.py

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# -*- coding: utf-8 -*-
2-
2+
import inspect
33
import os
44
import sys
5+
from os.path import dirname, relpath
56

67
import alagitpull
78

@@ -25,6 +26,7 @@
2526
'sphinx.ext.autodoc',
2627
'sphinx.ext.intersphinx',
2728
'sphinx.ext.todo',
29+
'sphinx.ext.linkcode',
2830
'sphinxcontrib.napoleon',
2931
'releases',
3032
'alagitpull',
@@ -116,3 +118,74 @@
116118
]
117119

118120
intersphinx_mapping = {'http://docs.python.org/': None}
121+
122+
123+
def linkcode_resolve(domain, info): # NOQA: C901
124+
"""
125+
Determine the URL corresponding to Python object
126+
127+
Notes
128+
-----
129+
From https://github.com/numpy/numpy/blob/v1.15.1/doc/source/conf.py, 7c49cfa
130+
on Jul 31. License BSD-3. https://github.com/numpy/numpy/blob/v1.15.1/LICENSE.txt
131+
"""
132+
if domain != 'py':
133+
return None
134+
135+
modname = info['module']
136+
fullname = info['fullname']
137+
138+
submod = sys.modules.get(modname)
139+
if submod is None:
140+
return None
141+
142+
obj = submod
143+
for part in fullname.split('.'):
144+
try:
145+
obj = getattr(obj, part)
146+
except Exception:
147+
return None
148+
149+
# strip decorators, which would resolve to the source of the decorator
150+
# possibly an upstream bug in getsourcefile, bpo-1764286
151+
try:
152+
unwrap = inspect.unwrap
153+
except AttributeError:
154+
pass
155+
else:
156+
obj = unwrap(obj)
157+
158+
try:
159+
fn = inspect.getsourcefile(obj)
160+
except Exception:
161+
fn = None
162+
if not fn:
163+
return None
164+
165+
try:
166+
source, lineno = inspect.getsourcelines(obj)
167+
except Exception:
168+
lineno = None
169+
170+
if lineno:
171+
linespec = "#L%d-L%d" % (lineno, lineno + len(source) - 1)
172+
else:
173+
linespec = ""
174+
175+
fn = relpath(fn, start=dirname(libtmux.__file__))
176+
177+
if 'dev' in about['__version__']:
178+
return "%s/blob/master/%s/%s%s" % (
179+
about['__github__'],
180+
about['__package_name__'],
181+
fn,
182+
linespec,
183+
)
184+
else:
185+
return "%s/blob/v%s/%s/%s%s" % (
186+
about['__github__'],
187+
about['__version__'],
188+
about['__package_name__'],
189+
fn,
190+
linespec,
191+
)

0 commit comments

Comments
 (0)