|
| 1 | +""" |
| 2 | +Sphinx extension to add ReadTheDocs-style "Edit on GitHub" links to the |
| 3 | +sidebar. |
| 4 | +Loosely based on https://github.com/astropy/astropy/pull/347 |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | +import warnings |
| 9 | +from pprint import pprint |
| 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 | + if not app.config.current_docs: |
| 32 | + warnings.warn("current_docs not specified") |
| 33 | + return |
| 34 | + |
| 35 | + path = app.config.current_docs + '/' + os.path.relpath(doctree.get('source'), app.builder.srcdir) |
| 36 | + show_url = get_github_url(app, 'blob', path) |
| 37 | + edit_url = get_github_url(app, 'edit', path) |
| 38 | + |
| 39 | + context['show_on_github_url'] = show_url |
| 40 | + context['edit_on_github_url'] = edit_url |
| 41 | + |
| 42 | + |
| 43 | +def setup(app): |
| 44 | + app.add_config_value('edit_on_github_project', '', True) |
| 45 | + app.add_config_value('current_docs', '', True) |
| 46 | + app.add_config_value('edit_on_github_branch', 'master', True) |
| 47 | + app.connect('html-page-context', html_page_context) |
0 commit comments