Skip to content

Commit 8a348ae

Browse files
committed
Link source documents to github
1 parent 6786f43 commit 8a348ae

File tree

1 file changed

+77
-1
lines changed

1 file changed

+77
-1
lines changed

doc/conf.py

+77-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
'sphinx.ext.inheritance_diagram',
5454
'sphinx.ext.intersphinx',
5555
'sphinx.ext.ifconfig',
56-
'sphinx.ext.viewcode',
5756
'IPython.sphinxext.ipython_console_highlighting',
5857
'IPython.sphinxext.ipython_directive',
5958
'numpydoc', # Needs to be loaded *after* autodoc.
@@ -530,3 +529,80 @@ def setup(app):
530529
else:
531530
bld_type = 'rel'
532531
app.add_config_value('releaselevel', bld_type, 'env')
532+
533+
# -----------------------------------------------------------------------------
534+
# Source code links
535+
# -----------------------------------------------------------------------------
536+
link_github = True
537+
# You can add viewcode extension to build old way with link_github = False
538+
# extensions.append('sphinx.ext.viewcode')
539+
if link_github:
540+
import re
541+
import inspect
542+
from os.path import relpath, dirname
543+
for name in ['sphinx.ext.linkcode', 'linkcode', 'numpydoc.linkcode']:
544+
try:
545+
__import__(name)
546+
extensions.append(name)
547+
break
548+
except ImportError:
549+
pass
550+
else:
551+
print("NOTE: linkcode extension not found -- no links to source generated")
552+
553+
def linkcode_resolve(domain, info):
554+
"""
555+
Determine the URL corresponding to Python object
556+
"""
557+
if domain != 'py':
558+
return None
559+
560+
modname = info['module']
561+
fullname = info['fullname']
562+
563+
submod = sys.modules.get(modname)
564+
if submod is None:
565+
return None
566+
567+
obj = submod
568+
for part in fullname.split('.'):
569+
try:
570+
obj = getattr(obj, part)
571+
except Exception:
572+
return None
573+
574+
try:
575+
fn = inspect.getsourcefile(obj)
576+
except Exception:
577+
fn = None
578+
if not fn:
579+
try:
580+
fn = inspect.getsourcefile(sys.modules[obj.__module__])
581+
except Exception:
582+
fn = None
583+
if not fn:
584+
return None
585+
586+
try:
587+
source, lineno = inspect.getsourcelines(obj)
588+
except Exception:
589+
lineno = None
590+
591+
if lineno:
592+
linespec = "#L%d-L%d" % (lineno, lineno + len(source) - 1)
593+
else:
594+
linespec = ""
595+
596+
startdir = os.path.abspath(os.path.join(dirname(matplotlib.__file__), '..'))
597+
fn = relpath(fn, start=startdir).replace(os.path.sep, '/')
598+
599+
if fn.startswith('matplotlib/'):
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+
return None

0 commit comments

Comments
 (0)