diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 937dae19..05fcab1b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -48,8 +48,8 @@ repos: args: [--config-file=pyproject.toml] additional_dependencies: - importlib_metadata - - myst-parser~=1.0.0 - - "sphinx~=5.0" + - myst-parser~=2.0.0 + - "sphinx~=7.0" - nbclient - types-PyYAML files: > diff --git a/myst_nb/sphinx_.py b/myst_nb/sphinx_.py index 6bf07fa8..f6c550f3 100644 --- a/myst_nb/sphinx_.py +++ b/myst_nb/sphinx_.py @@ -140,7 +140,7 @@ def parse(self, inputstring: str, document: nodes.document) -> None: # so that roles/directives can access it document.attributes["nb_renderer"] = nb_renderer # we currently do this early, so that the nb_renderer has access to things - mdit_renderer.setup_render(mdit_parser.options, mdit_env) + mdit_renderer.setup_render(mdit_parser.options, mdit_env) # type: ignore # parse notebook structure to markdown-it tokens # note, this does not assume that the notebook has been executed yet diff --git a/myst_nb/sphinx_ext.py b/myst_nb/sphinx_ext.py index 4e821623..1044e457 100644 --- a/myst_nb/sphinx_ext.py +++ b/myst_nb/sphinx_ext.py @@ -1,11 +1,14 @@ """Setup for the myst-nb sphinx extension.""" from __future__ import annotations +import contextlib import hashlib from importlib import resources as import_resources import os from pathlib import Path -from typing import Any +import sys +from types import ModuleType +from typing import Any, Iterator from myst_parser.sphinx_ext.main import setup_sphinx as setup_myst_parser from sphinx.application import Sphinx @@ -184,9 +187,21 @@ def _get_file_hash(path: Path): return hashlib.sha256(path.read_bytes()).hexdigest() +@contextlib.contextmanager +def _import_resources_path(package: ModuleType, resource: str) -> Iterator[Path]: + if sys.version_info < (3, 9): + with import_resources.path(package, resource) as path: + yield path + else: + with import_resources.as_file( + import_resources.files(package).joinpath(resource) + ) as path: + yield path + + def add_css(app: Sphinx): """Add CSS for myst-nb.""" - with import_resources.path(static, "mystnb.css") as source_path: + with _import_resources_path(static, "mystnb.css") as source_path: hash = _get_file_hash(source_path) app.add_css_file(f"mystnb.{hash}.css") @@ -195,9 +210,8 @@ def add_global_html_resources(app: Sphinx, exception): """Add HTML resources that apply to all pages.""" # see https://github.com/sphinx-doc/sphinx/issues/1379 if app.builder is not None and app.builder.format == "html" and not exception: - with import_resources.path(static, "mystnb.css") as source_path: - with import_resources.path(static, "mystnb.css") as source_path: - hash = _get_file_hash(source_path) + with _import_resources_path(static, "mystnb.css") as source_path: + hash = _get_file_hash(source_path) destination = os.path.join( app.builder.outdir, "_static", f"mystnb.{hash}.css" ) @@ -210,6 +224,6 @@ def add_per_page_html_resources( """Add JS files for this page, identified from the parsing of the notebook.""" if app.env is None or app.builder is None or app.builder.format != "html": return - js_files = NbMetadataCollector.get_js_files(app.env, pagename) # type: ignore + js_files = NbMetadataCollector.get_js_files(app.env, pagename) for path, kwargs in js_files.values(): - app.add_js_file(path, **kwargs) # type: ignore + app.add_js_file(path, **kwargs)