From bccb999e2be510c879a4412389b5c1c0675338b7 Mon Sep 17 00:00:00 2001 From: Phil Elson Date: Mon, 26 Sep 2022 10:05:31 +0200 Subject: [PATCH 1/3] 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..373d5c846 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 when rendering the templates. + 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 %} From f9984d132944e398aaa265c0eee6f9f2415c01f0 Mon Sep 17 00:00:00 2001 From: Phil Elson Date: Wed, 28 Sep 2022 08:57:23 +0200 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> --- sphinx_rtd_theme/__init__.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sphinx_rtd_theme/__init__.py b/sphinx_rtd_theme/__init__.py index 373d5c846..f07a76811 100644 --- a/sphinx_rtd_theme/__init__.py +++ b/sphinx_rtd_theme/__init__.py @@ -7,7 +7,6 @@ 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 @@ -34,8 +33,8 @@ def config_initiated(app, config): 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 + # Add ``sphinx_version_info`` tuple for use in Jinja templates + context['sphinx_version_info'] = sphinx_version[:3] # See http://www.sphinx-doc.org/en/stable/theming.html#distribute-your-theme-as-a-python-package From dc753f1667ba558c4119b9c87bc180bf4e68c72f Mon Sep 17 00:00:00 2001 From: Phil Elson Date: Wed, 28 Sep 2022 09:07:42 +0200 Subject: [PATCH 3/3] No reason to only have the first three elements from the sphinx version --- setup.cfg | 2 -- sphinx_rtd_theme/__init__.py | 2 +- sphinx_rtd_theme/layout.html | 12 +++++------- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/setup.cfg b/setup.cfg index f325a355e..f63239aed 100644 --- a/setup.cfg +++ b/setup.cfg @@ -37,9 +37,7 @@ 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 f07a76811..9ea323f0c 100644 --- a/sphinx_rtd_theme/__init__.py +++ b/sphinx_rtd_theme/__init__.py @@ -34,7 +34,7 @@ def config_initiated(app, config): def extend_html_context(app, pagename, templatename, context, doctree): # Add ``sphinx_version_info`` tuple for use in Jinja templates - context['sphinx_version_info'] = sphinx_version[:3] + context['sphinx_version_info'] = sphinx_version # See http://www.sphinx-doc.org/en/stable/theming.html#distribute-your-theme-as-a-python-package diff --git a/sphinx_rtd_theme/layout.html b/sphinx_rtd_theme/layout.html index 6a4de9464..99ec01b71 100644 --- a/sphinx_rtd_theme/layout.html +++ b/sphinx_rtd_theme/layout.html @@ -9,8 +9,6 @@ {%- set lang_attr = 'en' if language == None else (language | replace('_', '-')) %} {%- set sphinx_writer = 'writer-html5' if html5_doctype else 'writer-html4' -%} -{%- set sphinx_vn = parse_version(sphinx_version) -%} - @@ -22,7 +20,7 @@ {%- endblock -%} {#- CSS #} - {%- if sphinx_vn < parse_version("4.0") -%} + {%- if sphinx_version_info < (4, 0) -%} {%- endif %} @@ -40,7 +38,7 @@ {#- FAVICON #} {%- if favicon %} - {%- if sphinx_vn < parse_version("4.0") -%} + {%- if sphinx_version_info < (4, 0) -%} {%- else %} @@ -64,8 +62,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_vn >= parse_version("1.8") -%} - {%- if sphinx_vn < parse_version("4.0") -%} + {%- if sphinx_version_info >= (1, 8) -%} + {%- if sphinx_version_info < (4, 0) -%} {%- endif -%} {%- for scriptfile in script_files %} @@ -141,7 +139,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_vn < parse_version("4.0") -%} + {%- if sphinx_version_info < (4, 0) -%} {%- else %}