Skip to content

Commit f5b502c

Browse files
authored
Merge pull request #1258 from tsalo/master
[DOC] Enable table text wrap and link docstrings to code on GitHub
2 parents c82cae4 + 5a81f8c commit f5b502c

File tree

4 files changed

+112
-2
lines changed

4 files changed

+112
-2
lines changed

.circleci/config.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ jobs:
257257
-e FMRIPREP_REGRESSION_SOURCE=/tmp/data/fmriprep_bold_truncated \
258258
-e FMRIPREP_REGRESSION_TARGETS=/tmp/data/fmriprep_bold_mask \
259259
--entrypoint="py.test" poldracklab/fmriprep:latest \
260-
/root/src/fmriprep/ --doctest-modules --ignore=docs --ignore=setup.py
260+
/root/src/fmriprep/ \
261+
--doctest-modules --ignore=/root/src/fmriprep/docs --ignore=setup.py
261262
- run:
262263
name: Test fmriprep-wrapper (Python 2)
263264
command: |

docs/_static/theme_overrides.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* override table width restrictions */
2+
@media screen and (min-width: 767px) {
3+
4+
.wy-table-responsive table td {
5+
/* !important prevents the common CSS stylesheets from overriding
6+
this as on RTD they are loaded after this stylesheet */
7+
white-space: normal !important;
8+
}
9+
10+
.wy-table-responsive {
11+
overflow: visible !important;
12+
}
13+
}

docs/conf.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
sys.path.append(os.path.abspath('sphinxext'))
2525
sys.path.insert(0, os.path.abspath('../wrapper'))
2626

27+
from github_link import make_linkcode_resolve
28+
2729
# -- General configuration ------------------------------------------------
2830

2931
# If your documentation needs a minimal Sphinx version, state it here.
@@ -38,7 +40,7 @@
3840
'sphinx.ext.intersphinx',
3941
'sphinx.ext.coverage',
4042
'sphinx.ext.mathjax',
41-
'sphinx.ext.viewcode',
43+
'sphinx.ext.linkcode',
4244
'sphinxarg.ext', # argparse extension
4345
'nipype.sphinxext.plot_workflow',
4446
'nbsphinx',
@@ -314,6 +316,11 @@
314316
# If true, do not generate a @detailmenu in the "Top" node's menu.
315317
# texinfo_no_detailmenu = False
316318

319+
# The following is used by sphinx.ext.linkcode to provide links to github
320+
linkcode_resolve = make_linkcode_resolve('fmriprep',
321+
u'https://github.com/poldracklab/'
322+
'fmriprep/blob/{revision}/'
323+
'{package}/{path}#L{lineno}')
317324

318325
# Example configuration for intersphinx: refer to the Python standard library.
319326
intersphinx_mapping = {'https://docs.python.org/': None}
@@ -322,5 +329,6 @@
322329

323330

324331
def setup(app):
332+
app.add_stylesheet('theme_overrides.css')
325333
# We need this for the boilerplate script
326334
app.add_javascript("https://cdn.rawgit.com/chrisfilo/zenodo.js/v0.1/zenodo.js")

docs/sphinxext/github_link.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""
2+
This script comes from scikit-learn:
3+
https://github.com/scikit-learn/scikit-learn/blob/master/doc/sphinxext/github_link.py
4+
"""
5+
from operator import attrgetter
6+
import inspect
7+
import subprocess
8+
import os
9+
import sys
10+
from functools import partial
11+
12+
REVISION_CMD = 'git rev-parse --short HEAD'
13+
14+
15+
def _get_git_revision():
16+
try:
17+
revision = subprocess.check_output(REVISION_CMD.split()).strip()
18+
except (subprocess.CalledProcessError, OSError):
19+
print('Failed to execute git to get revision')
20+
return None
21+
return revision.decode('utf-8')
22+
23+
24+
def _linkcode_resolve(domain, info, package, url_fmt, revision):
25+
"""Determine a link to online source for a class/method/function
26+
27+
This is called by sphinx.ext.linkcode
28+
29+
An example with a long-untouched module that everyone has
30+
>>> _linkcode_resolve('py', {'module': 'tty',
31+
... 'fullname': 'setraw'},
32+
... package='tty',
33+
... url_fmt='http://hg.python.org/cpython/file/'
34+
... '{revision}/Lib/{package}/{path}#L{lineno}',
35+
... revision='xxxx')
36+
'http://hg.python.org/cpython/file/xxxx/Lib/tty/tty.py#L18'
37+
"""
38+
39+
if revision is None:
40+
return
41+
if domain not in ('py', 'pyx'):
42+
return
43+
if not info.get('module') or not info.get('fullname'):
44+
return
45+
46+
class_name = info['fullname'].split('.')[0]
47+
if type(class_name) != str:
48+
# Python 2 only
49+
class_name = class_name.encode('utf-8')
50+
module = __import__(info['module'], fromlist=[class_name])
51+
obj = attrgetter(info['fullname'])(module)
52+
53+
try:
54+
fn = inspect.getsourcefile(obj)
55+
except Exception:
56+
fn = None
57+
if not fn:
58+
try:
59+
fn = inspect.getsourcefile(sys.modules[obj.__module__])
60+
except Exception:
61+
fn = None
62+
if not fn:
63+
return
64+
65+
fn = os.path.relpath(fn,
66+
start=os.path.dirname(__import__(package).__file__))
67+
try:
68+
lineno = inspect.getsourcelines(obj)[1]
69+
except Exception:
70+
lineno = ''
71+
return url_fmt.format(revision=revision, package=package,
72+
path=fn, lineno=lineno)
73+
74+
75+
def make_linkcode_resolve(package, url_fmt):
76+
"""Returns a linkcode_resolve function for the given URL format
77+
78+
revision is a git commit reference (hash or name)
79+
80+
package is the name of the root module of the package
81+
82+
url_fmt is along the lines of ('https://github.com/USER/PROJECT/'
83+
'blob/{revision}/{package}/'
84+
'{path}#L{lineno}')
85+
"""
86+
revision = _get_git_revision()
87+
return partial(_linkcode_resolve, revision=revision, package=package,
88+
url_fmt=url_fmt)

0 commit comments

Comments
 (0)