Skip to content

Commit e33338f

Browse files
committed
add small extension to get the edit on github button working
1 parent e2b5bcf commit e33338f

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

_ext/edit_on_github.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
Sphinx extension to add ReadTheDocs-style "Edit on GitHub" links to the
3+
sidebar.
4+
5+
Loosely based on https://github.com/astropy/astropy/pull/347
6+
"""
7+
8+
import os
9+
import warnings
10+
11+
12+
__licence__ = 'BSD (3 clause)'
13+
14+
15+
def get_github_url(app, view, path):
16+
return 'https://github.com/{project}/{view}/{branch}/{path}'.format(
17+
project=app.config.edit_on_github_project,
18+
view=view,
19+
branch=app.config.edit_on_github_branch,
20+
path=path)
21+
22+
23+
def html_page_context(app, pagename, templatename, context, doctree):
24+
if templatename != 'page.html':
25+
return
26+
27+
if not app.config.edit_on_github_project:
28+
warnings.warn("edit_on_github_project not specified")
29+
return
30+
31+
path = os.path.relpath(doctree.get('source'), app.builder.srcdir)
32+
show_url = get_github_url(app, 'blob', path)
33+
edit_url = get_github_url(app, 'edit', path)
34+
35+
# context['show_on_github_url'] = show_url
36+
context['edit_on_github_url'] = edit_url
37+
38+
39+
def setup(app):
40+
app.add_config_value('edit_on_github_project', '', True)
41+
app.add_config_value('edit_on_github_branch', 'master', True)
42+
app.connect('html-page-context', html_page_context)

conf.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
'sphinxcontrib.bibtex',
4242
'sphinx_rtd_theme',
4343
'matplotlib.sphinxext.plot_directive',
44+
'edit_on_github',
4445
'purpose',
4546
'question',
4647
'geosciapp',
@@ -83,7 +84,7 @@
8384
#
8485
# This is also used if you do content translation via gettext catalogs.
8586
# Usually you set "language" from the command line for these cases.
86-
language = None
87+
language = "en"
8788

8889
# There are two options for replacing |today|: either, you set today to some
8990
# non-false value, then it is used:
@@ -156,6 +157,11 @@
156157
# number figures
157158
numfig = True
158159

160+
# -- Edit on Github Extension ---------------------------------------------
161+
162+
edit_on_github_project = 'geoscixyz/em'
163+
edit_on_github_branch = 'main'
164+
159165

160166
# -- Options for HTML output ----------------------------------------------
161167

@@ -164,13 +170,13 @@
164170

165171
import sphinx_rtd_theme
166172
html_theme = 'sphinx_rtd_theme'
167-
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
168-
html_context = {
169-
'display_github': True,
170-
'github_user': 'geoscixyz',
171-
'github_repo': 'em',
172-
'github_version': 'main'
173-
}
173+
# html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
174+
# html_context = {
175+
# 'display_github': True,
176+
# 'github_user': 'geoscixyz',
177+
# 'github_repo': 'em',
178+
# 'github_version': 'main'
179+
# }
174180
# The name for this set of Sphinx documents. If None, it defaults to
175181
# "<project> v<release> documentation".
176182
html_title = 'Electromagnetic Geophysics'

0 commit comments

Comments
 (0)