diff --git a/_ext/edit_on_github.py b/_ext/edit_on_github.py new file mode 100644 index 00000000..6c8aca1d --- /dev/null +++ b/_ext/edit_on_github.py @@ -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) diff --git a/conf.py b/conf.py index 2953bcba..6ff2d819 100644 --- a/conf.py +++ b/conf.py @@ -41,6 +41,7 @@ 'sphinxcontrib.bibtex', 'sphinx_rtd_theme', 'matplotlib.sphinxext.plot_directive', + 'edit_on_github', 'purpose', 'question', 'geosciapp', @@ -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: @@ -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 ---------------------------------------------- @@ -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 # " v documentation". html_title = 'Electromagnetic Geophysics'