diff --git a/doc/_templates/sourcelink.html b/doc/_templates/sourcelink.html new file mode 100644 index 0000000000000..751f08ab31922 --- /dev/null +++ b/doc/_templates/sourcelink.html @@ -0,0 +1,15 @@ +{%- if show_source and has_source and sourcename %} +

{{ _('This Page') }}

+ +{%- endif %} diff --git a/doc/source/conf.py b/doc/source/conf.py index f222a228531ff..0a65786ce9ab6 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -69,6 +69,7 @@ "sphinx.ext.mathjax", "sphinx.ext.todo", "nbsphinx", + "edit_on_github", ] exclude_patterns = [ @@ -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: @@ -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"] diff --git a/doc/sphinxext/edit_on_github.py b/doc/sphinxext/edit_on_github.py new file mode 100644 index 0000000000000..e8d5bbf7d8849 --- /dev/null +++ b/doc/sphinxext/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)