diff --git a/docs/user_guide/source-buttons.rst b/docs/user_guide/source-buttons.rst index 16bfe2ace..a4db469ad 100644 --- a/docs/user_guide/source-buttons.rst +++ b/docs/user_guide/source-buttons.rst @@ -4,8 +4,8 @@ Source Buttons Source buttons are links to the source of your page's content (either on your site, or on hosting sites like GitHub). -Add an Edit this Page button -============================ +Add an edit button +================== You can add a button to each page that will allow users to edit the page text directly and submit a pull request to update the documentation. To include this @@ -80,6 +80,37 @@ any other context values. "some_other_arg": "?some-other-arg" } +With the predefined providers, the link text reads "Edit on GitHub/GitLab/Bitbucket". +By default, a simple "Edit" is used if you use a custom URL. However, you can set +a provider name like this: + +.. code:: python + + html_context = { + "edit_page_url_template": "...", + "edit_page_provider_name": "Provider", + } + +This will turn the link into "Edit on Provider". + + +Custom link text +---------------- + +You can change the default text by extending the ``edit-this-page.html`` +template. For example, if you have ``templates_path = ["_templates"]`` +in your Sphinx configuration, you could put this code in +`_templates/edit-this-page.html`: + +.. code:: html+jinja + + {% extends "!components/edit-this-page.html" %} + + {% block edit_this_page_text %} + Edit this page + {% endblock %} + + View Source link ================ diff --git a/src/pydata_sphinx_theme/__init__.py b/src/pydata_sphinx_theme/__init__.py index 315507bae..48641d8be 100644 --- a/src/pydata_sphinx_theme/__init__.py +++ b/src/pydata_sphinx_theme/__init__.py @@ -779,8 +779,8 @@ def extract_level_recursive(ul, navs_list): def setup_edit_url(app, pagename, templatename, context, doctree): """Add a function that jinja can access for returning the edit URL of a page.""" - def get_edit_url(): - """Return a URL for an "edit this page" link.""" + def get_edit_provider_and_url(): + """Return a provider name and a URL for an "edit this page" link.""" file_name = f"{pagename}{context['page_source_suffix']}" # Make sure that doc_path has a path separator only if it exists (to avoid //) @@ -794,7 +794,7 @@ def get_edit_url(): "gitlab_url": "https://gitlab.com", } - edit_url_attrs = {} + edit_attrs = {} # ensure custom URL is checked first, if given url_template = context.get("edit_page_url_template") @@ -806,23 +806,26 @@ def get_edit_url(): "Ensure `file_name` appears in `edit_page_url_template`: " f"{url_template}" ) + provider_name = context.get("edit_page_provider_name") + edit_attrs[("edit_page_url_template",)] = (provider_name, url_template) - edit_url_attrs[("edit_page_url_template",)] = url_template - - edit_url_attrs.update( + edit_attrs.update( { ("bitbucket_user", "bitbucket_repo", "bitbucket_version"): ( + "Bitbucket", "{{ bitbucket_url }}/{{ bitbucket_user }}/{{ bitbucket_repo }}" "/src/{{ bitbucket_version }}" - "/{{ doc_path }}{{ file_name }}?mode=edit" + "/{{ doc_path }}{{ file_name }}?mode=edit", ), ("github_user", "github_repo", "github_version"): ( + "GitHub", "{{ github_url }}/{{ github_user }}/{{ github_repo }}" - "/edit/{{ github_version }}/{{ doc_path }}{{ file_name }}" + "/edit/{{ github_version }}/{{ doc_path }}{{ file_name }}", ), ("gitlab_user", "gitlab_repo", "gitlab_version"): ( + "GitLab", "{{ gitlab_url }}/{{ gitlab_user }}/{{ gitlab_repo }}" - "/-/edit/{{ gitlab_version }}/{{ doc_path }}{{ file_name }}" + "/-/edit/{{ gitlab_version }}/{{ doc_path }}{{ file_name }}", ), } ) @@ -831,17 +834,17 @@ def get_edit_url(): doc_context.update(context) doc_context.update(doc_path=doc_path, file_name=file_name) - for attrs, url_template in edit_url_attrs.items(): + for attrs, (provider, url_template) in edit_attrs.items(): if all(doc_context.get(attr) not in [None, "None"] for attr in attrs): - return jinja2.Template(url_template).render(**doc_context) + return provider, jinja2.Template(url_template).render(**doc_context) raise ExtensionError( "Missing required value for `use_edit_page_button`. " "Ensure one set of the following in your `html_context` " - f"configuration: {sorted(edit_url_attrs.keys())}" + f"configuration: {sorted(edit_attrs.keys())}" ) - context["get_edit_url"] = get_edit_url + context["get_edit_provider_and_url"] = get_edit_provider_and_url # Ensure that the max TOC level is an integer context["theme_show_toc_level"] = int(context.get("theme_show_toc_level", 1)) diff --git a/src/pydata_sphinx_theme/theme/pydata_sphinx_theme/components/edit-this-page.html b/src/pydata_sphinx_theme/theme/pydata_sphinx_theme/components/edit-this-page.html index 2d1e8819c..5bc0f1fca 100644 --- a/src/pydata_sphinx_theme/theme/pydata_sphinx_theme/components/edit-this-page.html +++ b/src/pydata_sphinx_theme/theme/pydata_sphinx_theme/components/edit-this-page.html @@ -1,8 +1,16 @@ {% if sourcename is defined and theme_use_edit_page_button==true and page_source_suffix %} {% set src = sourcename.split('.') %}