Skip to content

Commit 4ba30ec

Browse files
committed
Link source documents to github
1 parent d2a9710 commit 4ba30ec

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed

doc/conf.py

+72-1
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.
@@ -534,3 +533,75 @@ def setup(app):
534533
else:
535534
bld_type = 'rel'
536535
app.add_config_value('releaselevel', bld_type, 'env')
536+
537+
# -----------------------------------------------------------------------------
538+
# Source code links
539+
# -----------------------------------------------------------------------------
540+
link_github = True
541+
# You can add build old with link_github = False
542+
543+
if link_github:
544+
import re
545+
import inspect
546+
from os.path import relpath, dirname
547+
548+
extensions.append('sphinx.ext.linkcode')
549+
550+
def linkcode_resolve(domain, info):
551+
"""
552+
Determine the URL corresponding to Python object
553+
"""
554+
if domain != 'py':
555+
return None
556+
557+
modname = info['module']
558+
fullname = info['fullname']
559+
560+
submod = sys.modules.get(modname)
561+
if submod is None:
562+
return None
563+
564+
obj = submod
565+
for part in fullname.split('.'):
566+
try:
567+
obj = getattr(obj, part)
568+
except Exception:
569+
return None
570+
571+
try:
572+
fn = inspect.getsourcefile(obj)
573+
except Exception:
574+
fn = None
575+
if not fn:
576+
try:
577+
fn = inspect.getsourcefile(sys.modules[obj.__module__])
578+
except Exception:
579+
fn = None
580+
if not fn:
581+
return None
582+
583+
try:
584+
source, lineno = inspect.getsourcelines(obj)
585+
except Exception:
586+
lineno = None
587+
588+
if lineno:
589+
linespec = "#L%d-L%d" % (lineno, lineno + len(source) - 1)
590+
else:
591+
linespec = ""
592+
593+
startdir = os.path.abspath(os.path.join(dirname(matplotlib.__file__), '..'))
594+
fn = relpath(fn, start=startdir).replace(os.path.sep, '/')
595+
596+
if fn.startswith('matplotlib/'):
597+
m = re.match(r'^.*post[0-9]+\+\w([a-z0-9]+).\w+$', matplotlib.__version__)
598+
if m:
599+
return "https://github.com/matplotlib/matplotlib/blob/%s/lib/%s%s" % (
600+
m.group(1), fn, linespec)
601+
else:
602+
return "https://github.com/matplotlib/matplotlib/blob/v%s/lib/%s%s" % (
603+
matplotlib.__version__, fn, linespec)
604+
else:
605+
return None
606+
else:
607+
extensions.append('sphinx.ext.viewcode')

0 commit comments

Comments
 (0)