diff --git a/docs/scripts/notebook_convert.py b/docs/scripts/notebook_convert.py index c606c98e037..97f2dca2534 100644 --- a/docs/scripts/notebook_convert.py +++ b/docs/scripts/notebook_convert.py @@ -7,7 +7,7 @@ from typing import Iterable, Tuple import nbformat from nbconvert.exporters import MarkdownExporter -from nbconvert.preprocessors import Preprocessor, RegexRemovePreprocessor +from nbconvert.preprocessors import Preprocessor class EscapePreprocessor(Preprocessor): @@ -79,11 +79,30 @@ class ExtractAttachmentsPreprocessor(Preprocessor): return cell, resources +class CustomRegexRemovePreprocessor(Preprocessor): + def check_conditions(self, cell): + pattern = re.compile(r"(?s)(?:\s*\Z)|(?:.*#\s*\|\s*output:\s*false.*)") + rtn = not pattern.match(cell.source) + if not rtn: + print("--remove--") + print(cell.source) + return False + else: + print("--keep--") + print(cell.source) + return True + + def preprocess(self, nb, resources): + nb.cells = [cell for cell in nb.cells if self.check_conditions(cell)] + + return nb, resources + + exporter = MarkdownExporter( preprocessors=[ EscapePreprocessor, ExtractAttachmentsPreprocessor, - RegexRemovePreprocessor(patterns=[r"^\s*$"]), + CustomRegexRemovePreprocessor, ], template_name="mdoutput", extra_template_basedirs=["./scripts/notebook_convert_templates"],