Skip to content

Commit 6dfb520

Browse files
committed
Link source documents to github
1 parent 2c3bc26 commit 6dfb520

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

doc/conf.py

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
'sphinx.ext.inheritance_diagram',
5757
'sphinx.ext.intersphinx',
5858
'sphinx.ext.ifconfig',
59-
'sphinx.ext.viewcode',
6059
'IPython.sphinxext.ipython_console_highlighting',
6160
'IPython.sphinxext.ipython_directive',
6261
'numpydoc', # Needs to be loaded *after* autodoc.
@@ -536,3 +535,74 @@ def setup(app):
536535
else:
537536
bld_type = 'rel'
538537
app.add_config_value('releaselevel', bld_type, 'env')
538+
539+
# -----------------------------------------------------------------------------
540+
# Source code links
541+
# -----------------------------------------------------------------------------
542+
link_github = True
543+
# You can add build old with link_github = False
544+
545+
if link_github:
546+
import re
547+
import inspect
548+
549+
extensions.append('sphinx.ext.linkcode')
550+
551+
def linkcode_resolve(domain, info):
552+
"""
553+
Determine the URL corresponding to Python object
554+
"""
555+
if domain != 'py':
556+
return None
557+
558+
modname = info['module']
559+
fullname = info['fullname']
560+
561+
submod = sys.modules.get(modname)
562+
if submod is None:
563+
return None
564+
565+
obj = submod
566+
for part in fullname.split('.'):
567+
try:
568+
obj = getattr(obj, part)
569+
except AttributeError:
570+
return None
571+
572+
try:
573+
fn = inspect.getsourcefile(obj)
574+
except TypeError:
575+
fn = None
576+
if not fn or fn.endswith('__init__.py'):
577+
try:
578+
fn = inspect.getsourcefile(sys.modules[obj.__module__])
579+
except (TypeError, AttributeError, KeyError):
580+
fn = None
581+
if not fn:
582+
return None
583+
584+
try:
585+
source, lineno = inspect.getsourcelines(obj)
586+
except (OSError, TypeError):
587+
lineno = None
588+
589+
if lineno:
590+
linespec = "#L%d-L%d" % (lineno, lineno + len(source) - 1)
591+
else:
592+
linespec = ""
593+
594+
startdir = Path(matplotlib.__file__).parent.parent
595+
fn = os.path.relpath(fn, start=startdir).replace(os.path.sep, '/')
596+
597+
if not fn.startswith(('matplotlib/', 'mpl_toolkits/')):
598+
return None
599+
600+
m = re.match(r'^.*post[0-9]+\+\w([a-z0-9]+).\w+$', matplotlib.__version__)
601+
if m:
602+
return "https://github.com/matplotlib/matplotlib/blob/%s/lib/%s%s" % (
603+
m.group(1), fn, linespec)
604+
else:
605+
return "https://github.com/matplotlib/matplotlib/blob/v%s/lib/%s%s" % (
606+
matplotlib.__version__, fn, linespec)
607+
else:
608+
extensions.append('sphinx.ext.viewcode')

0 commit comments

Comments
 (0)