@@ -329,13 +329,40 @@ def _get_target_name(self, source_path: Path, notebooks_dir: Path) -> str:
329
329
base_target = f"{ source_path .stem } .ipynb"
330
330
converted_target = f"{ source_path .stem } .converted.ipynb"
331
331
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.
334
336
if source_path .suffix .lower () == ".md" :
335
337
if (notebooks_dir / base_target ).exists ():
336
338
return converted_target
337
339
return base_target
338
340
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
+
339
366
def run (self ):
340
367
width = self .options .pop ("width" , "100%" )
341
368
height = self .options .pop ("height" , "1000px" )
@@ -367,12 +394,16 @@ def run(self):
367
394
target_name = self ._get_target_name (notebook_path , notebooks_dir )
368
395
target_path = notebooks_dir / target_name
369
396
397
+ notebook_is_stripped : bool = self .env .config .strip_tagged_cells
398
+
370
399
# For MyST Markdown notebooks, we create a unique target filename
371
400
# via _get_target_name() to avoid collisions with other IPyNB files
372
401
# that may have the same name.
373
402
if notebook_path .suffix .lower () == ".md" :
374
403
if self ._should_convert_notebook (notebook_path , target_path ):
375
404
nb = jupytext .read (str (notebook_path ))
405
+ if notebook_is_stripped :
406
+ nb .cells = self ._strip_notebook_cells (nb )
376
407
with open (target_path , "w" , encoding = "utf-8" ) as f :
377
408
nbformat .write (nb , f , version = 4 )
378
409
@@ -382,23 +413,10 @@ def run(self):
382
413
notebook_name = notebook_path .name
383
414
target_path = notebooks_dir / notebook_name
384
415
385
- notebook_is_stripped : bool = self .env .config .strip_tagged_cells
386
-
387
416
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
-
393
417
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 )
400
419
nbformat .write (nb , target_path , version = 4 )
401
-
402
420
# If notebook_is_stripped is False, then copy the notebook(s) to notebooks_dir.
403
421
# If it is True, then they have already been copied to notebooks_dir by the
404
422
# nbformat.write() function above.
0 commit comments