Skip to content

Commit

Permalink
Merge branch 'master' into pre-commit-ci-update-config
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 authored Oct 13, 2023
2 parents 94f7499 + f4b845d commit 68b72d1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: >
Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# this is only required by coconut kernel
ipython<=8.15.0
ipython<=8.16.0
2 changes: 1 addition & 1 deletion myst_nb/sphinx_.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 21 additions & 7 deletions myst_nb/sphinx_ext.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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")

Expand All @@ -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"
)
Expand All @@ -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)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ testing = [
# for issue with 8.1.0 see https://github.com/ipython/ipython/issues/13554
# TODO ipython 8.5 subtly changes output of test regressions
# see https://ipython.readthedocs.io/en/stable/whatsnew/version8.html#restore-line-numbers-for-input
"ipython!=8.1.0,<8.16",
"ipython!=8.1.0,<8.17",
"ipywidgets>=8",
"jupytext>=1.11.2,<1.16.0",
"matplotlib>=3.5.3,<3.6", # TODO mpl 3.6 subtly changes output of test regressions
Expand Down

0 comments on commit 68b72d1

Please sign in to comment.