Skip to content

Commit

Permalink
add edit on github link
Browse files Browse the repository at this point in the history
Signed-off-by: cosmicBboy <[email protected]>
  • Loading branch information
cosmicBboy committed May 6, 2021
1 parent 2c6eb97 commit 5631e40
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
43 changes: 43 additions & 0 deletions src/furo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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":
Expand Down
2 changes: 2 additions & 0 deletions src/furo/theme/furo/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@

{#- Custom front matter #}
{%- block extrahead -%}{%- endblock -%}

<script src="https://kit.fontawesome.com/ef4deb7d2f.js" crossorigin="anonymous"></script>
</head>
<body dir="{{ direction }}">
{% block body %}{% endblock %}
Expand Down
16 changes: 8 additions & 8 deletions src/furo/theme/furo/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,17 @@
|
{% trans -%}
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a>
and
<a class="muted-link" href="https://pradyunsg.me">@pradyunsg</a>'s
and the
<a href="https://github.com/pradyunsg/furo">Furo theme</a>.
{%- endtrans %}
{%- endif %}
{%- if show_source and has_source and sourcename %}
|
<a class="muted-link" href="{{ pathto('_sources/' + sourcename, true)|e }}"
rel="nofollow">
{{ _('Show Source') }}
</a>
{%- if show_source and show_on_github_url %}
|
<a class="muted-link" href="{{ show_on_github_url }}"
rel="nofollow">
{{ _('Edit on Github') }}
<i class="fab fa-github"></i>
</a>
{%- endif %}
</div>
{% endblock footer %}
Expand Down
6 changes: 6 additions & 0 deletions src/furo/theme/furo/theme.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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 =

0 comments on commit 5631e40

Please sign in to comment.