Skip to content

Commit a2f1936

Browse files
committed
Added suggested fix from GitHub Issue
1 parent bd9f060 commit a2f1936

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

doc/_templates/sourcelink.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{%- if show_source and has_source and sourcename %}
2+
<h3>{{ _('This Page') }}</h3>
3+
<ul class="this-page-menu">
4+
<li><a href="{{ pathto('_sources/' + sourcename, true)|e }}"
5+
rel="nofollow">{{ _('Show Source') }}</a></li>
6+
{%- if show_on_github_url %}
7+
<li><a href="{{ show_on_github_url }}"
8+
rel="nofollow">{{ _('Show on GitHub') }}</a></li>
9+
{%- endif %}
10+
{%- if edit_on_github_url %}
11+
<li><a href="{{ edit_on_github_url }}"
12+
rel="nofollow">{{ _('Edit on GitHub') }}</a></li>
13+
{%- endif %}
14+
</ul>
15+
{%- endif %}

doc/source/conf.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
]
4545
)
4646

47+
# Edit on GitHub links
48+
sys.path.insert(0, os.path.abspath('_ext'))
49+
4750
# -- General configuration -----------------------------------------------
4851

4952
# Add any Sphinx extension module names here, as strings. They can be
@@ -69,6 +72,7 @@
6972
"sphinx.ext.mathjax",
7073
"sphinx.ext.todo",
7174
"nbsphinx",
75+
'edit_on_github',
7276
]
7377

7478
exclude_patterns = [
@@ -114,8 +118,6 @@
114118
):
115119
exclude_patterns.append(rel_fname)
116120
elif single_doc and rel_fname != pattern:
117-
if "\\" in rel_fname:
118-
rel_fname = rel_fname.replace("\\", "/")
119121
exclude_patterns.append(rel_fname)
120122

121123
with open(os.path.join(source_path, "index.rst.template"), encoding="utf-8") as f:
@@ -149,6 +151,10 @@
149151
# https://sphinx-toggleprompt.readthedocs.io/en/stable/#offset
150152
toggleprompt_offset_right = 35
151153

154+
# Configure the "Edit on GitHub links for Sphinx" extention
155+
edit_on_github_project = 'username/reponame'
156+
edit_on_github_branch = 'master'
157+
152158
# Add any paths that contain templates here, relative to this directory.
153159
templates_path = ["../_templates"]
154160

doc/sphinxext/edit_on_github.py

Lines changed: 42 additions & 0 deletions
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)

0 commit comments

Comments
 (0)