Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add small extension to get the edit on github button working #575

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions _ext/edit_on_github.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
Sphinx extension to add ReadTheDocs-style "Edit on GitHub" links to the
sidebar.

Loosely based on https://github.com/astropy/astropy/pull/347
"""

import os
import warnings


__licence__ = 'BSD (3 clause)'


def get_github_url(app, view, path):
return 'https://github.com/{project}/{view}/{branch}/{path}'.format(
project=app.config.edit_on_github_project,
view=view,
branch=app.config.edit_on_github_branch,
path=path)


def html_page_context(app, pagename, templatename, context, doctree):
if templatename != 'page.html':
return

if not app.config.edit_on_github_project:
warnings.warn("edit_on_github_project not specified")
return

path = os.path.relpath(doctree.get('source'), app.builder.srcdir)
show_url = get_github_url(app, 'blob', path)
edit_url = get_github_url(app, 'edit', path)

# context['show_on_github_url'] = show_url
context['edit_on_github_url'] = edit_url


def setup(app):
app.add_config_value('edit_on_github_project', '', True)
app.add_config_value('edit_on_github_branch', 'master', True)
app.connect('html-page-context', html_page_context)
22 changes: 14 additions & 8 deletions conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'sphinxcontrib.bibtex',
'sphinx_rtd_theme',
'matplotlib.sphinxext.plot_directive',
'edit_on_github',
'purpose',
'question',
'geosciapp',
Expand Down Expand Up @@ -83,7 +84,7 @@
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None
language = "en"

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

# -- Edit on Github Extension ---------------------------------------------

edit_on_github_project = 'geoscixyz/em'
edit_on_github_branch = 'main'


# -- Options for HTML output ----------------------------------------------

Expand All @@ -164,13 +170,13 @@

import sphinx_rtd_theme
html_theme = 'sphinx_rtd_theme'
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
html_context = {
'display_github': True,
'github_user': 'geoscixyz',
'github_repo': 'em',
'github_version': 'main'
}
# html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
# html_context = {
# 'display_github': True,
# 'github_user': 'geoscixyz',
# 'github_repo': 'em',
# 'github_version': 'main'
# }
# The name for this set of Sphinx documents. If None, it defaults to
# "<project> v<release> documentation".
html_title = 'Electromagnetic Geophysics'
Expand Down
Loading