Skip to content

Commit a7dd4db

Browse files
Refactor stripping logic + strip MyST notebooks
1 parent d7749b0 commit a7dd4db

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
@@ -329,13 +329,40 @@ def _get_target_name(self, source_path: Path, notebooks_dir: Path) -> str:
329329
base_target = f"{source_path.stem}.ipynb"
330330
converted_target = f"{source_path.stem}.converted.ipynb"
331331

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

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

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

@@ -382,23 +413,10 @@ def run(self):
382413
notebook_name = notebook_path.name
383414
target_path = notebooks_dir / notebook_name
384415

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

0 commit comments

Comments
 (0)