Skip to content

Commit 11542de

Browse files
committed
preprocess_rm_cell
1 parent 41f69e4 commit 11542de

File tree

4 files changed

+357
-127
lines changed

4 files changed

+357
-127
lines changed

nbprocess/_nbdev.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
"clean_nb": "05_clean.ipynb",
4141
"nbdev_clean_nbs": "05_clean.ipynb",
4242
"default_pp_cfg": "06_mdx.ipynb",
43+
"preprocess_cell": "06_mdx.ipynb",
44+
"preprocess": "06_mdx.ipynb",
45+
"preprocess_rm_cell": "06_mdx.ipynb",
4346
"InjectMeta": "06_mdx.ipynb",
4447
"StripAnsi": "06_mdx.ipynb",
4548
"InsertWarning": "06_mdx.ipynb",

nbprocess/mdx.py

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/06_mdx.ipynb (unless otherwise specified).
22

3-
__all__ = ['default_pp_cfg', 'InjectMeta', 'StripAnsi', 'InsertWarning', 'RmEmptyCode', 'UpdateTags', 'HideInputLines',
4-
'FilterOutput', 'CleanFlags', 'CleanMagics', 'BashIdentify', 'CleanShowDoc', 'HTMLEscape', 'RmHeaderDash',
5-
'default_pps', 'mdx_exporter', 'nb2md']
3+
__all__ = ['default_pp_cfg', 'preprocess_cell', 'preprocess', 'preprocess_rm_cell', 'InjectMeta', 'StripAnsi',
4+
'InsertWarning', 'RmEmptyCode', 'UpdateTags', 'HideInputLines', 'FilterOutput', 'CleanFlags', 'CleanMagics',
5+
'BashIdentify', 'CleanShowDoc', 'HTMLEscape', 'RmHeaderDash', 'default_pps', 'mdx_exporter', 'nb2md']
66

77
# Cell
88
import re, uuid
9-
10-
from .read import get_config
11-
from .processor import *
12-
from nbconvert.preprocessors import ExtractOutputPreprocessor
13-
9+
from functools import wraps
1410
from fastcore.basics import *
1511
from fastcore.foundation import *
1612
from traitlets.config import Config
1713
from pathlib import Path
1814
from html.parser import HTMLParser
1915

16+
from .read import get_config
17+
from .processor import *
18+
19+
from nbconvert.preprocessors import ExtractOutputPreprocessor,Preprocessor,TagRemovePreprocessor
20+
from nbconvert import MarkdownExporter
21+
from .extract_attachments import ExtractAttachmentsPreprocessor
22+
from nbconvert.writers import FilesWriter
23+
2024
# Cell
2125
def default_pp_cfg():
2226
"Default Preprocessor Config for MDX export"
@@ -26,9 +30,38 @@ def default_pp_cfg():
2630
c.TagRemovePreprocessor.remove_input_tags = ('remove_input', 'remove_inputs', "hide_input", "hide_inputs")
2731
return c
2832

33+
# Cell
34+
def preprocess_cell(func):
35+
"Decorator to create a `preprocess_cell` `Preprocessor` for cells"
36+
@wraps(func, updated=())
37+
class _C(Preprocessor):
38+
def preprocess_cell(self, cell, resources, index):
39+
res = func(cell)
40+
if res: cell = res
41+
return cell, resources
42+
return _C
43+
44+
# Cell
45+
def preprocess(func):
46+
"Decorator to create a `preprocess` `Preprocessor` for notebooks"
47+
@wraps(func, updated=())
48+
class _C(Preprocessor):
49+
def preprocess(self, nb, resources):
50+
res = func(nb)
51+
if res: nb = res
52+
nb.cells = list(nb.cells)
53+
return nb, resources
54+
return _C
55+
56+
# Cell
57+
def preprocess_rm_cell(func):
58+
"Like `preprocess_cell` but remove cells where function returns `True`"
59+
@preprocess
60+
def _inner(nb): nb.cells = [cell for cell in nb.cells if not func(cell)]
61+
return _inner
62+
2963
# Cell
3064
def _mdx_exporter(pps, cfg=None, tpl_file='ob.tpl'):
31-
"An mdx notebook exporter which composes preprocessors"
3265
cfg = cfg or default_pp_cfg()
3366
cfg.MarkdownExporter.preprocessors = pps or []
3467
tmp_dir = Path(__file__).parent/'templates/'
@@ -39,7 +72,6 @@ def _mdx_exporter(pps, cfg=None, tpl_file='ob.tpl'):
3972

4073
# Cell
4174
def _run_preprocessor(pps, fname, display=False):
42-
"An mdx notebook exporter which composes preprocessors"
4375
exp = _mdx_exporter(pps)
4476
result = exp.from_filename(fname)
4577
if display: print(result[0])
@@ -191,14 +223,11 @@ def HTMLEscape(cell):
191223
# Cell
192224
_re_hdr_dash = re.compile(r'^#+\s+.*\s+-\s*$', re.MULTILINE)
193225

194-
@preprocess_cell
226+
@preprocess_rm_cell
195227
def RmHeaderDash(cell):
196228
"Remove headings that end with a dash -"
197-
if cell.cell_type == 'markdown':
198-
exclude = {l.strip() for l in _re_hdr_dash.findall(cell.source)}
199-
if exclude:
200-
lines = [l for l in cell.source.splitlines() if l not in exclude]
201-
cell.source = '\n'.join(lines)
229+
src = cell.source.strip()
230+
return cell.cell_type == 'markdown' and src.startswith('#') and src.endswith(' -')
202231

203232
# Cell
204233
def default_pps(c):

0 commit comments

Comments
 (0)