Skip to content

Commit 61c64bf

Browse files
committed
switch to code links
1 parent 40a8922 commit 61c64bf

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

docs/conf.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1+
# flake8: noqa
12
# -*- coding: utf-8 -*-
23

34
# If your documentation needs a minimal Sphinx version, state it here.
45
#
56
# needs_sphinx = "1.0"
67

7-
from sphinx.ext.napoleon.docstring import NumpyDocstring
8+
import sys
9+
import os
10+
import inspect
11+
import importlib
812

913
import sphinx_compas_theme
14+
from sphinx.ext.napoleon.docstring import NumpyDocstring
15+
16+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../src'))
1017

1118
# -- General configuration ------------------------------------------------
1219

@@ -40,7 +47,8 @@
4047
"sphinx.ext.intersphinx",
4148
"sphinx.ext.mathjax",
4249
"sphinx.ext.napoleon",
43-
"sphinx.ext.viewcode",
50+
"sphinx.ext.linkcode",
51+
"sphinx.ext.extlinks",
4452
"sphinx.ext.githubpages",
4553
"sphinx.ext.coverage",
4654
"sphinx.ext.inheritance_diagram",
@@ -147,6 +155,7 @@ def parse_class_attributes_section(self, section):
147155
# assigned to the _section dict
148156
def patched_parse(self):
149157
self._sections["keys"] = self._parse_keys_section
158+
self._sections["attributes"] = self._parse_attributes_section
150159
self._sections["class attributes"] = self._parse_class_attributes_section
151160
self._unpatched_parse()
152161

@@ -261,6 +270,44 @@ def patched_parse(self):
261270
"compas": ("https://compas.dev/compas/latest/", None),
262271
}
263272

273+
# linkcode
274+
275+
def linkcode_resolve(domain, info):
276+
if domain != 'py':
277+
return None
278+
if not info['module']:
279+
return None
280+
if not info['fullname']:
281+
return None
282+
283+
package = info['module'].split('.')[0]
284+
if not package.startswith('compas'):
285+
return None
286+
287+
module = importlib.import_module(info['module'])
288+
parts = info['fullname'].split('.')
289+
290+
if len(parts) == 1:
291+
obj = getattr(module, info['fullname'])
292+
filename = inspect.getmodule(obj).__name__.replace('.', '/')
293+
lineno = inspect.getsourcelines(obj)[1]
294+
elif len(parts) == 2:
295+
obj_name, attr_name = parts
296+
obj = getattr(module, obj_name)
297+
attr = getattr(obj, attr_name)
298+
if inspect.isfunction(attr):
299+
filename = inspect.getmodule(obj).__name__.replace('.', '/')
300+
lineno = inspect.getsourcelines(attr)[1]
301+
else:
302+
return None
303+
else:
304+
return None
305+
306+
return f"https://github.com/compas-dev/compas/blob/master/src/{filename}.py#L{lineno}"
307+
308+
# extlinks
309+
310+
extlinks = {}
264311

265312
# -- Options for HTML output ----------------------------------------------
266313

@@ -274,7 +321,7 @@ def patched_parse(self):
274321
}
275322
html_context = {}
276323
html_static_path = []
277-
html_extra_path = [".nojekyll"]
324+
html_extra_path = []
278325
html_last_updated_fmt = ""
279326
html_copy_source = False
280327
html_show_sourcelink = False

0 commit comments

Comments
 (0)