diff --git a/docs/_static/switcher.json b/docs/_static/switcher.json new file mode 100644 index 000000000..90a58e18b --- /dev/null +++ b/docs/_static/switcher.json @@ -0,0 +1,51 @@ +[ + { + "name": "v0.7.1 (stable)", + "version": "0.7.1" + }, + { + "version": "0.7.0" + }, + { + "version": "0.6.3" + }, + { + "version": "0.6.2" + }, + { + "version": "0.6.1" + }, + { + "version": "0.6.0" + }, + { + "version": "0.5.2" + }, + { + "version": "0.5.1" + }, + { + "version": "0.5.0" + }, + { + "version": "0.4.3" + }, + { + "version": "0.4.2" + }, + { + "version": "0.4.1" + }, + { + "version": "0.4.0" + }, + { + "version": "0.3.2" + }, + { + "version": "0.3.1" + }, + { + "version": "0.3.0" + } +] diff --git a/docs/conf.py b/docs/conf.py index da3335521..b7d234e21 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -24,7 +24,8 @@ import pydata_sphinx_theme -version = pydata_sphinx_theme.__version__.replace("dev0", "") +release = pydata_sphinx_theme.__version__ +version = release.replace("dev0", "") # -- General configuration --------------------------------------------------- @@ -96,8 +97,14 @@ # "navbar_align": "left", # [left, content, right] For testing that the navbar items align properly # "navbar_start": ["navbar-logo", "navbar-version"], # "navbar_center": ["navbar-nav", "navbar-version"], # Just for testing - # "navbar_end": ["navbar-icon-links", "navbar-version"] # Just for testing + "navbar_end": ["version-switcher", "navbar-icon-links"], # "footer_items": ["copyright", "sphinx-version", ""] + "switcher": { + # "json_url": "/_static/switcher.json", + "json_url": "https://pydata-sphinx-theme.readthedocs.io/en/latest/_static/switcher.json", + "url_template": "https://pydata-sphinx-theme.readthedocs.io/en/v{version}/", + "version_match": version, + }, } diff --git a/docs/user_guide/configuring.rst b/docs/user_guide/configuring.rst index d720f7a41..00d544615 100644 --- a/docs/user_guide/configuring.rst +++ b/docs/user_guide/configuring.rst @@ -277,6 +277,198 @@ at the bottom. You can hide these buttons with the following configuration: } +Add a dropdown to switch between docs versions +============================================== + +You can add a button to your site that allows users to +switch between versions of your documentation. The links in the version +switcher will differ depending on which page of the docs is being viewed. For +example, on the page ``https://mysite.org/en/v2.0/changelog.html``, the +switcher links will go to ``changelog.html`` in the other versions of your +docs. When clicked, the switcher will check for the existence of that page, and +if it doesn't exist, redirect to the homepage of that docs version instead. + +The switcher requires the following configuration steps: + +1. Add a JSON file containing a list of the documentation versions that the + switcher should show on each page. + +2. Add a configuration dictionary called ``switcher`` to the + ``html_theme_options`` dict in ``conf.py``. ``switcher`` should have 3 keys: + + - ``json_url``: the persistent location of the JSON file described above. + - ``url_template``: a template string used to generate the correct URLs for + the different documentation versions. + - ``version_match``: a string stating the version of the documentation that + is currently being browsed. + +3. Specify where to place the switcher in your page layout. For example, add + the ``"version-switcher"`` template to one of the layout lists in + ``html_theme_options`` (e.g., ``navbar_end``, ``footer_items``, etc). + +Below is a more in-depth description of each of these configuration steps. + + +Add a JSON file to define your switcher's versions +-------------------------------------------------- + +First, write a JSON file stating which versions of your docs will be listed in +the switcher's dropdown menu. That file should contain a list of entries that +each have one or two fields: + +- ``version``: a version string. This will be inserted into + ``switcher['url_template']`` to create the links to other docs versions, and + also checked against ``switcher['version_match']`` to provide styling to the + switcher. +- ``name``: an optional name to display in the switcher dropdown instead of the + version string (e.g., "latest", "stable", "dev", etc). + +Here is an example JSON file: + +.. code:: json + + [ + { + "name": "v2.1 (stable)", + "version": "2.1" + }, + { + "version": "2.0" + }, + { + "version": "1.0" + }, + ] + +See the discussion of ``switcher['json_url']`` (below) for options of where to +save the JSON file. + + +Configure ``switcher['json_url']`` +---------------------------------- + +The JSON file needs to be at a stable, persistent, fully-resolved URL (i.e., +not specified as a path relative to the sphinx root of the current doc build). +Each version of your documentation should point to the same URL, so that as new +versions are added to the JSON file all the older versions of the docs will +gain switcher dropdown entries linking to the new versions. This could be done +a few different ways: + +- The location could be one that is always associated with the most recent + documentation build (i.e., if your docs server aliases "latest" to the most + recent version, it could point to a location in the build tree of version + "latest"). For example: + + .. code:: python + + html_theme_options = { + ..., + "switcher": { + "json_url": "https://mysite.org/en/latest/_static/switcher.json", + } + } + + In this case the JSON is versioned alongside the rest of the docs pages but + only the most recent version is ever loaded (even by older versions of the + docs). + +- The JSON could be saved in a folder that is listed under your site's + ``html_static_path`` configuration. See `the Sphinx static path documentation + `_ + for more information. + +- The JSON could be stored outside the doc build trees. This probably means it + would be outside the software repo, and would require you to add new version + entries to the JSON file manually as part of your release process. Example: + + .. code:: python + + html_theme_options = { + ..., + "switcher": { + "json_url": "https://mysite.org/switcher.json", + } + } + + +Configure ``switcher['url_template']`` +-------------------------------------- + +The switcher's links to other versions of your docs are made by combining the +*version strings* from the JSON file with a *template string* you provide in +``switcher['url_template']``. The template string must contain a placeholder +``{version}`` and otherwise be a fully-resolved URL. For example: + +.. code:: python + + html_theme_options = { + ..., + "switcher": { + "url_template": "https://mysite.org/en/version-{version}/", + } + } + +The example above will result in a link to +``https://mysite.org/en/version-1.0/`` for the JSON entry for version +``"1.0"``. + + +Configure ``switcher['version_match']`` +--------------------------------------- + +This configuration value tells the switcher what docs version is currently +being viewed, and is used to style the switcher (i.e., to highlight the current +docs version in the switcher's dropdown menu, and to change the text displayed +on the switcher button). + +Typically you can re-use one of the sphinx variables ``version`` +or ``release`` as the value of ``switcher['version_match']``; which one you use +depends on how granular your docs versioning is. See +`the Sphinx "project info" documentation +`__ +for more information). Example: + +.. code:: python + + version = my_package_name.__version__.replace("dev0", "") # may differ + html_theme_options = { + ..., + "switcher": { + "version_match": version, + } + } + + +Specify where to display the switcher +------------------------------------- + +Finally, tell the theme where on your site's pages you want the switcher to +appear. There are many choices here: you can add ``"version-switcher"`` to one +of the locations in ``html_theme_options`` (e.g., ``navbar_end``, +``footer_items``, etc). For example: + +.. code:: python + + html_theme_options = { + ..., + "navbar_end": ["version-switcher"] + } + + +Alternatively, you could override one of the other templates to include the +version switcher in a sidebar. For example, you could define +``_templates/sidebar-nav-bs.html`` as: + +.. code:: jinja + + {%- include 'version-switcher.html' -%} + {{ super() }} + +to insert a version switcher at the top of the left sidebar, while still +keeping the default navigation below it. See :doc:`sections` for more +information. + + Add an Edit this Page button ============================ diff --git a/docs/user_guide/sections.rst b/docs/user_guide/sections.rst index c93c0314a..a4a6e8d47 100644 --- a/docs/user_guide/sections.rst +++ b/docs/user_guide/sections.rst @@ -118,6 +118,7 @@ will be named accordingly). - ``sidebar-ethical-ads.html`` - ``sidebar-nav-bs.html`` - ``sphinx-version.html`` +- ``version-switcher.html`` Add your own HTML templates to theme sections ============================================= diff --git a/src/pydata_sphinx_theme/theme/pydata_sphinx_theme/_templates/version-switcher.html b/src/pydata_sphinx_theme/theme/pydata_sphinx_theme/_templates/version-switcher.html new file mode 100644 index 000000000..10d6b75d5 --- /dev/null +++ b/src/pydata_sphinx_theme/theme/pydata_sphinx_theme/_templates/version-switcher.html @@ -0,0 +1,79 @@ + + + + + diff --git a/src/pydata_sphinx_theme/theme/pydata_sphinx_theme/theme.conf b/src/pydata_sphinx_theme/theme/pydata_sphinx_theme/theme.conf index 872a7fcbb..51fe3cae3 100644 --- a/src/pydata_sphinx_theme/theme/pydata_sphinx_theme/theme.conf +++ b/src/pydata_sphinx_theme/theme/pydata_sphinx_theme/theme.conf @@ -32,3 +32,4 @@ navbar_center = navbar-nav.html navbar_end = navbar-icon-links.html footer_items = copyright.html, sphinx-version.html page_sidebar_items = page-toc.html, edit-this-page.html +switcher =