Skip to content

Commit

Permalink
add small extension to get the edit on github button working
Browse files Browse the repository at this point in the history
  • Loading branch information
lheagy committed Nov 29, 2024
1 parent e2b5bcf commit e33338f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
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

0 comments on commit e33338f

Please sign in to comment.