From 87af6b6a1e56a85903d94962a7a960e057cb72b3 Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Tue, 14 Jun 2022 11:30:56 +0200 Subject: [PATCH] =?UTF-8?q?=E2=AC=86=EF=B8=8F=20Support=20Sphinx=20v5,=20d?= =?UTF-8?q?rop=20v3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 22 ++++++++++++++++++++++ pyproject.toml | 12 ++++++------ sphinx_design/_compat.py | 11 +++++++++++ sphinx_design/cards.py | 5 +++-- sphinx_design/dropdown.py | 3 ++- sphinx_design/extension.py | 7 ++++--- tests/conftest.py | 3 ++- tox.ini | 2 +- 8 files changed, 51 insertions(+), 14 deletions(-) create mode 100644 sphinx_design/_compat.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2b5b5de..292f76a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -55,6 +55,28 @@ jobs: file: ./coverage.xml fail_ci_if_error: true + tests-sphinx4: + + strategy: + matrix: + os: [ubuntu-latest] + python-version: ["3.7", "3.10"] + + runs-on: ${{ matrix.os }} + + steps: + - uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -e .[testing] sphinx~=4.5 + - name: Run pytest + run: pytest + publish: name: Publish to PyPi diff --git a/pyproject.toml b/pyproject.toml index 249309a..30b4780 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,7 +27,7 @@ classifiers = [ ] keywords = ["sphinx", "extension", "material design", "web components"] requires-python = ">=3.7" -dependencies = ["sphinx>=3,<5"] +dependencies = ["sphinx>=4,<6"] [project.urls] Homepage = "https://github.com/executablebooks/sphinx-design" @@ -35,15 +35,15 @@ Documentation = "https://sphinx-design.readthedocs.io" [project.optional-dependencies] code_style = ["pre-commit~=2.12"] -rtd = ["myst-parser~=0.17.0"] +rtd = ["myst-parser~=0.18.0"] testing = [ - "myst-parser~=0.17.0", - "pytest~=6.2", + "myst-parser~=0.18.0", + "pytest~=7.1", "pytest-cov", "pytest-regressions", ] -theme_furo = ["furo==2022.04.07"] -theme_pydata = ["pydata-sphinx-theme~=0.8.1"] +theme_furo = ["furo>=2022.06.04,<2022.07"] +theme_pydata = ["pydata-sphinx-theme~=0.9.0"] theme_rtd = ["sphinx-rtd-theme~=1.0"] theme_sbt = ["sphinx-book-theme~=0.3.0"] diff --git a/sphinx_design/_compat.py b/sphinx_design/_compat.py new file mode 100644 index 0000000..d29cf4d --- /dev/null +++ b/sphinx_design/_compat.py @@ -0,0 +1,11 @@ +"""Helpers for cross compatibility across dependency versions.""" +from typing import Callable, Iterable + +from docutils.nodes import Element + + +def findall(node: Element) -> Callable[..., Iterable[Element]]: + """Iterate through""" + # findall replaces traverse in docutils v0.18 + # note a difference is that findall is an iterator + return getattr(node, "findall", node.traverse) diff --git a/sphinx_design/cards.py b/sphinx_design/cards.py index 923c67e..98b3e56 100644 --- a/sphinx_design/cards.py +++ b/sphinx_design/cards.py @@ -9,6 +9,7 @@ from sphinx.util.docutils import SphinxDirective from sphinx.util.logging import getLogger +from ._compat import findall from .shared import ( WARNING_TYPE, PassthroughTextElement, @@ -229,11 +230,11 @@ def _create_component( @staticmethod def add_card_child_classes(node): """Add classes to specific child nodes.""" - for para in node.traverse(nodes.paragraph): + for para in findall(node)(nodes.paragraph): para["classes"] = ([] if "classes" not in para else para["classes"]) + [ "sd-card-text" ] - # for title in node.traverse(nodes.title): + # for title in findall(node)(nodes.title): # title["classes"] = ([] if "classes" not in title else title["classes"]) + [ # "sd-card-title" # ] diff --git a/sphinx_design/dropdown.py b/sphinx_design/dropdown.py index 37162b4..874d7e4 100644 --- a/sphinx_design/dropdown.py +++ b/sphinx_design/dropdown.py @@ -14,6 +14,7 @@ margin_option, ) +from ._compat import findall from .icons import get_octicon, list_octicons @@ -217,7 +218,7 @@ def run(self): children=body_children, ) if use_card: - for para in body_node.traverse(nodes.paragraph): + for para in findall(body_node)(nodes.paragraph): para["classes"] = ([] if "classes" in para else para["classes"]) + [ "sd-card-text" ] diff --git a/sphinx_design/extension.py b/sphinx_design/extension.py index 648a65e..4756b00 100644 --- a/sphinx_design/extension.py +++ b/sphinx_design/extension.py @@ -10,6 +10,7 @@ from sphinx.util.docutils import SphinxDirective from . import compiled as static_module +from ._compat import findall from .article_info import setup_article_info from .badges_buttons import setup_badges_and_buttons from .cards import setup_cards @@ -143,15 +144,15 @@ class AddFirstTitleCss(SphinxTransform): def apply(self): hide = False - for docinfo in self.document.traverse(nodes.docinfo): - for name in docinfo.traverse(nodes.field_name): + for docinfo in findall(self.document)(nodes.docinfo): + for name in findall(docinfo)(nodes.field_name): if name.astext() == "sd_hide_title": hide = True break break if not hide: return - for section in self.document.traverse(nodes.section): + for section in findall(self.document)(nodes.section): if isinstance(section.children[0], nodes.title): if "classes" in section.children[0]: section.children[0]["classes"].append("sd-d-none") diff --git a/tests/conftest.py b/tests/conftest.py index 7d3fa8e..0b9c78d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,6 +7,7 @@ from sphinx.testing.path import path as sphinx_path from sphinx.testing.util import SphinxTestApp +from sphinx_design._compat import findall from sphinx_design.tabs import TabSetHtmlTransform pytest_plugins = "sphinx.testing.fixtures" @@ -46,7 +47,7 @@ def get_doctree( if post_transforms: self.app.env.apply_post_transforms(doctree, docname) # make source path consistent for test comparisons - for node in doctree.traverse(include_self=True): + for node in findall(doctree)(include_self=True): if not ("source" in node and node["source"]): continue node["source"] = Path(node["source"]).relative_to(self.src_path).as_posix() diff --git a/tox.ini b/tox.ini index 7503ab4..12c5777 100644 --- a/tox.ini +++ b/tox.ini @@ -9,7 +9,7 @@ envlist = py38 [testenv] usedevelop = true -[testenv:py{36,37,38,39}] +[testenv:py{37,38,39,310}] description = Run unit tests with this Python version extras = testing