From 96ef672917aaf35633d72b0137be6585365e2b8c Mon Sep 17 00:00:00 2001 From: Phil Elson Date: Mon, 26 Sep 2022 10:05:31 +0200 Subject: [PATCH] Update layout.html to support a sphinx version that is not three-integers. Useful for sphinx==5.2.0.post0 --- setup.cfg | 2 ++ sphinx_rtd_theme/__init__.py | 10 ++++++++++ sphinx_rtd_theme/layout.html | 14 ++++++-------- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/setup.cfg b/setup.cfg index f63239aed..f325a355e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -37,7 +37,9 @@ install_requires = sphinx >=1.6,<6 docutils <0.18 Jinja2 <3.1 + packaging tests_require = + readthedocs-sphinx-ext pytest [options.extras_require] diff --git a/sphinx_rtd_theme/__init__.py b/sphinx_rtd_theme/__init__.py index f31909865..c250348b0 100644 --- a/sphinx_rtd_theme/__init__.py +++ b/sphinx_rtd_theme/__init__.py @@ -7,6 +7,7 @@ from os import path from sys import version_info as python_version +import packaging.version from sphinx import version_info as sphinx_version from sphinx.locale import _ from sphinx.util.logging import getLogger @@ -31,6 +32,12 @@ def config_initiated(app, config): _('The canonical_url option is deprecated, use the html_baseurl option from Sphinx instead.') ) + +def extend_html_context(app, pagename, templatename, context, doctree): + # Extend the page context to be albe to parse versions correctly. + context['parse_version'] = packaging.version.parse + + # See http://www.sphinx-doc.org/en/stable/theming.html#distribute-your-theme-as-a-python-package def setup(app): if python_version[0] < 3: @@ -60,4 +67,7 @@ def setup(app): else: app.config.html_add_permalinks = "\uf0c1" + # Extend the default context for the read-the-docs theme. + app.connect("html-page-context", extend_html_context) + return {'parallel_read_safe': True, 'parallel_write_safe': True} diff --git a/sphinx_rtd_theme/layout.html b/sphinx_rtd_theme/layout.html index 1d7011976..6a4de9464 100644 --- a/sphinx_rtd_theme/layout.html +++ b/sphinx_rtd_theme/layout.html @@ -9,9 +9,7 @@ {%- set lang_attr = 'en' if language == None else (language | replace('_', '-')) %} {%- set sphinx_writer = 'writer-html5' if html5_doctype else 'writer-html4' -%} -{# Build sphinx_version_info tuple from sphinx_version string in pure Jinja #} -{%- set (_ver_major, _ver_minor, _ver_bugfix) = sphinx_version.split('.') | map('int') -%} -{%- set sphinx_version_info = (_ver_major, _ver_minor, _ver_bugfix) -%} +{%- set sphinx_vn = parse_version(sphinx_version) -%} @@ -24,7 +22,7 @@ {%- endblock -%} {#- CSS #} - {%- if sphinx_version_info < (4, 0) -%} + {%- if sphinx_vn < parse_version("4.0") -%} {%- endif %} @@ -42,7 +40,7 @@ {#- FAVICON #} {%- if favicon %} - {%- if sphinx_version_info < (4, 0) -%} + {%- if sphinx_vn < parse_version("4.0") -%} {%- else %} @@ -66,8 +64,8 @@ {%- if not embedded %} {# XXX Sphinx 1.8.0 made this an external js-file, quick fix until we refactor the template to inherert more blocks directly from sphinx #} - {%- if sphinx_version_info >= (1, 8) -%} - {%- if sphinx_version_info < (4, 0) -%} + {%- if sphinx_vn >= parse_version("1.8") -%} + {%- if sphinx_vn < parse_version("4.0") -%} {%- endif -%} {%- for scriptfile in script_files %} @@ -143,7 +141,7 @@ {#- Not strictly valid HTML, but it's the only way to display/scale it properly, without weird scripting or heaps of work #} - {%- if sphinx_version_info < (4, 0) -%} + {%- if sphinx_vn < parse_version("4.0") -%} {%- else %}