Skip to content

Commit

Permalink
Merge pull request #290 from f0k/fix-sphinx-github-links
Browse files Browse the repository at this point in the history
Fix github source code links generated by Sphinx
  • Loading branch information
benanne committed Jun 8, 2015
2 parents e3a07e5 + 67aec21 commit c461525
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,26 +108,27 @@

# Resolve function for the linkcode extension.
def linkcode_resolve(domain, info):
def find_line():
# try to find the correct line number, based on code from numpy:
def find_source():
# try to find the file and line number, based on code from numpy:
# https://github.com/numpy/numpy/blob/master/doc/source/conf.py#L286
obj = sys.modules[info['module']]
for part in info['fullname'].split('.'):
obj = getattr(obj, part)
import inspect
import os
fn = inspect.getsourcefile(obj)
source, lineno = inspect.findsource(obj)
return lineno + 1
fn = os.path.relpath(fn, start=os.path.dirname(lasagne.__file__))
source, lineno = inspect.getsourcelines(obj)
return fn, lineno, lineno + len(source) - 1

if domain != 'py' or not info['module']:
return None
filename = info['module'].replace('.', '/')
tag = 'master' if 'dev' in release else ('v' + release)
url = "https://github.com/Lasagne/Lasagne/blob/%s/%s.py" % (tag, filename)
try:
return url + '#L%d' % find_line()
filename = 'lasagne/%s#L%d-L%d' % find_source()
except Exception:
return url
filename = info['module'].replace('.', '/') + '.py'
tag = 'master' if 'dev' in release else ('v' + release)
return "https://github.com/Lasagne/Lasagne/blob/%s/%s" % (tag, filename)


# -- Options for HTML output ----------------------------------------------
Expand Down

0 comments on commit c461525

Please sign in to comment.