From 5631e40fd67b3f338ed1809e0efeb2112dd5ab40 Mon Sep 17 00:00:00 2001 From: cosmicBboy Date: Thu, 6 May 2021 10:36:24 -0400 Subject: [PATCH] add edit on github link Signed-off-by: cosmicBboy --- src/furo/__init__.py | 43 ++++++++++++++++++++++++++++++++++ src/furo/theme/furo/base.html | 2 ++ src/furo/theme/furo/page.html | 16 ++++++------- src/furo/theme/furo/theme.conf | 6 +++++ 4 files changed, 59 insertions(+), 8 deletions(-) diff --git a/src/furo/__init__.py b/src/furo/__init__.py index e2eb3606f..0a2591040 100644 --- a/src/furo/__init__.py +++ b/src/furo/__init__.py @@ -4,6 +4,9 @@ import hashlib import logging +import re +import os +import warnings from functools import lru_cache from pathlib import Path from typing import Any, Dict @@ -124,6 +127,16 @@ def furo_asset_hash(path: str) -> str: return f"_static/{path}?digest={digest}" +def get_github_url(app, view, path): + return 'https://github.com/{username}/{repo}/{view}/{commit}/{path}'.format( + username=app.config.html_theme_options["github_username"], + repo=app.config.html_theme_options["github_repo"], + view=view, + commit=app.config.html_theme_options["github_commit"], + path=path + ) + + def _html_page_context( app: sphinx.application.Sphinx, pagename: str, @@ -176,6 +189,36 @@ def _html_page_context( else: context["furo_assets"]["style"] = "_static/" + context["style"] + if templatename != 'page.html': + return + + if not app.config.html_theme_options["github_repo"]: + warnings.warn("github_repo not specified") + return + + if not app.config.html_theme_options["github_username"]: + warnings.warn("github_username not specified") + return + + path = os.path.relpath(doctree.get('source'), app.builder.srcdir) + if path.startswith(app.config.html_theme_options["sphinx_gallery_dest_dir"]): + # sphinx gallery examples show redirect to sphinx gallery example python scripts + path = re.sub( + f"^{app.config.html_theme_options['sphinx_gallery_dest_dir']}", + app.config.html_theme_options["sphinx_gallery_src_dir"], + path + ) + path = ( + # auto-generated index files should redirect to README.rst file + re.sub("index.rst$", "README.rst", path) + if path.endswith("index.rst") + else re.sub(".rst$", ".py", path) + ) + else: + path = f"{app.config.html_theme_options['docs_path']}/{path}" + + context['show_on_github_url'] = get_github_url(app, 'blob', path) + def _builder_inited(app: sphinx.application.Sphinx) -> None: if app.config.html_theme != "furo": diff --git a/src/furo/theme/furo/base.html b/src/furo/theme/furo/base.html index 75ef9240a..dfdaeed3e 100644 --- a/src/furo/theme/furo/base.html +++ b/src/furo/theme/furo/base.html @@ -75,6 +75,8 @@ {#- Custom front matter #} {%- block extrahead -%}{%- endblock -%} + + {% block body %}{% endblock %} diff --git a/src/furo/theme/furo/page.html b/src/furo/theme/furo/page.html index 4643c4c75..694a1e232 100644 --- a/src/furo/theme/furo/page.html +++ b/src/furo/theme/furo/page.html @@ -104,17 +104,17 @@ | {% trans -%} Built with Sphinx - and - @pradyunsg's + and the Furo theme. {%- endtrans %} {%- endif %} - {%- if show_source and has_source and sourcename %} - | - - {{ _('Show Source') }} - + {%- if show_source and show_on_github_url %} + | + + {{ _('Edit on Github') }} + + {%- endif %} {% endblock footer %} diff --git a/src/furo/theme/furo/theme.conf b/src/furo/theme/furo/theme.conf index 7c3f22e1e..858919352 100644 --- a/src/furo/theme/furo/theme.conf +++ b/src/furo/theme/furo/theme.conf @@ -19,3 +19,9 @@ dark_logo = light_css_variables = light_logo = sidebar_hide_name = +github_repo = +github_username = +github_commit = +docs_path = +sphinx_gallery_src_dir = +sphinx_gallery_dest_dir = \ No newline at end of file