Skip to content

Commit 6a6d7a2

Browse files
Refactor stripping logic + strip MyST notebooks
1 parent 0401426 commit 6a6d7a2

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

jupyterlite_sphinx/jupyterlite_sphinx.py

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,40 @@ def _get_target_name(self, source_path: Path, notebooks_dir: Path) -> str:
327327
base_target = f"{source_path.stem}.ipynb"
328328
converted_target = f"{source_path.stem}.converted.ipynb"
329329

330-
# For MyST-flavoured files, check if a similarly-named IPyNB exists
331-
# If it does, we will append ".converted.ipynb" to the target name.
330+
# For MyST-flavoured files, check for the edge case where an IPyNB
331+
# of the same name exists.
332+
# If it does, we will append ".converted.ipynb" to the target name
333+
# so that both can coexist in the same directory.
332334
if source_path.suffix.lower() == ".md":
333335
if (notebooks_dir / base_target).exists():
334336
return converted_target
335337
return base_target
336338

339+
def _strip_notebook_cells(
340+
self, nb: nbformat.NotebookNode
341+
) -> List[nbformat.NotebookNode]:
342+
"""Strip cells based on the presence of the "jupyterlite_sphinx_strip" tag
343+
in the metadata. The content meant to be stripped must be inside its own cell
344+
cell so that the cell itself gets removed from the notebooks. This is so that
345+
we don't end up removing useful data or directives that are not meant to be
346+
removed.
347+
348+
Parameters
349+
----------
350+
nb : nbformat.NotebookNode
351+
The notebook object to be stripped.
352+
353+
Returns
354+
-------
355+
List[nbformat.NotebookNode]
356+
A list of cells that are not meant to be stripped.
357+
"""
358+
return [
359+
cell
360+
for cell in nb.cells
361+
if "jupyterlite_sphinx_strip" not in cell.metadata.get("tags", [])
362+
]
363+
337364
def run(self):
338365
width = self.options.pop("width", "100%")
339366
height = self.options.pop("height", "1000px")
@@ -365,12 +392,16 @@ def run(self):
365392
target_name = self._get_target_name(notebook_path, notebooks_dir)
366393
target_path = notebooks_dir / target_name
367394

395+
notebook_is_stripped: bool = self.env.config.strip_tagged_cells
396+
368397
# For MyST Markdown notebooks, we create a unique target filename
369398
# via _get_target_name() to avoid collisions with other IPyNB files
370399
# that may have the same name.
371400
if notebook_path.suffix.lower() == ".md":
372401
if self._should_convert_notebook(notebook_path, target_path):
373402
nb = jupytext.read(str(notebook_path))
403+
if notebook_is_stripped:
404+
nb.cells = self._strip_notebook_cells(nb)
374405
with open(target_path, "w", encoding="utf-8") as f:
375406
nbformat.write(nb, f, version=4)
376407

@@ -380,23 +411,10 @@ def run(self):
380411
notebook_name = notebook_path.name
381412
target_path = notebooks_dir / notebook_name
382413

383-
notebook_is_stripped: bool = self.env.config.strip_tagged_cells
384-
385414
if notebook_is_stripped:
386-
# Note: the directives meant to be stripped must be inside their own
387-
# cell so that the cell itself gets removed from the notebook. This
388-
# is so that we don't end up removing useful data or directives that
389-
# are not meant to be removed.
390-
391415
nb = nbformat.read(notebook, as_version=4)
392-
nb.cells = [
393-
cell
394-
for cell in nb.cells
395-
if "jupyterlite_sphinx_strip"
396-
not in cell.metadata.get("tags", [])
397-
]
416+
nb.cells = self._strip_notebook_cells(nb)
398417
nbformat.write(nb, target_path, version=4)
399-
400418
# If notebook_is_stripped is False, then copy the notebook(s) to notebooks_dir.
401419
# If it is True, then they have already been copied to notebooks_dir by the
402420
# nbformat.write() function above.

0 commit comments

Comments
 (0)