Skip to content

DOC: add button to edit on GitHub #61997

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

Closed
Closed
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
15 changes: 15 additions & 0 deletions doc/_templates/sourcelink.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{%- if show_source and has_source and sourcename %}
<h3>{{ _('This Page') }}</h3>
<ul class="this-page-menu">
<li><a href="{{ pathto('_sources/' + sourcename, true)|e }}"
rel="nofollow">{{ _('Show Source') }}</a></li>
{%- if show_on_github_url %}
<li><a href="{{ show_on_github_url }}"
rel="nofollow">{{ _('Show on GitHub') }}</a></li>
{%- endif %}
{%- if edit_on_github_url %}
<li><a href="{{ edit_on_github_url }}"
rel="nofollow">{{ _('Edit on GitHub') }}</a></li>
{%- endif %}
</ul>
{%- endif %}
7 changes: 5 additions & 2 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"sphinx.ext.mathjax",
"sphinx.ext.todo",
"nbsphinx",
"edit_on_github",
]

exclude_patterns = [
Expand Down Expand Up @@ -114,8 +115,6 @@
):
exclude_patterns.append(rel_fname)
elif single_doc and rel_fname != pattern:
if "\\" in rel_fname:
rel_fname = rel_fname.replace("\\", "/")
exclude_patterns.append(rel_fname)

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

# Configure the "Edit on GitHub links for Sphinx" extention
edit_on_github_project = "pandas-dev/pandas"
edit_on_github_branch = "main/doc/source"

# Add any paths that contain templates here, relative to this directory.
templates_path = ["../_templates"]

Expand Down
42 changes: 42 additions & 0 deletions doc/sphinxext/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)
Loading