Skip to content

Commit 68d8c75

Browse files
committed
Add linkcode to sphinx
1 parent 1890347 commit 68d8c75

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

doc/conf.py

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
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

9+
import tmuxp
10+
811
# Get the project root dir, which is the parent dir of this
912
cwd = os.getcwd()
1013
project_root = os.path.dirname(cwd)
@@ -23,6 +26,7 @@
2326
'sphinx.ext.intersphinx',
2427
'sphinx.ext.todo',
2528
'sphinxcontrib.napoleon',
29+
'sphinx.ext.linkcode',
2630
'aafig',
2731
'releases',
2832
'alagitpull',
@@ -121,3 +125,74 @@
121125
aafig_format = dict(latex='pdf', html='gif')
122126

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

0 commit comments

Comments
 (0)