Skip to content

Commit

Permalink
fixes #1408
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Apr 29, 2024
1 parent 0bf2508 commit cc4793b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 43 deletions.
2 changes: 2 additions & 0 deletions nbdev/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@
'nbdev.processors.insert_warning': ('api/processors.html#insert_warning', 'nbdev/processors.py'),
'nbdev.processors.insert_warning.begin': ( 'api/processors.html#insert_warning.begin',
'nbdev/processors.py'),
'nbdev.processors.mv_exports': ('api/processors.html#mv_exports', 'nbdev/processors.py'),
'nbdev.processors.mv_exports.begin': ('api/processors.html#mv_exports.begin', 'nbdev/processors.py'),
'nbdev.processors.populate_language': ('api/processors.html#populate_language', 'nbdev/processors.py'),
'nbdev.processors.populate_language.begin': ( 'api/processors.html#populate_language.begin',
'nbdev/processors.py'),
Expand Down
52 changes: 32 additions & 20 deletions nbdev/processors.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/api/10_processors.ipynb.

# %% auto 0
__all__ = ['populate_language', 'insert_warning', 'cell_lang', 'add_show_docs', 'add_links', 'add_fold', 'strip_ansi',
'strip_hidden_metadata', 'hide_', 'hide_line', 'filter_stream_', 'clean_magics', 'rm_header_dash',
'rm_export', 'clean_show_doc', 'exec_show_docs', 'FilterDefaults']
__all__ = ['populate_language', 'insert_warning', 'cell_lang', 'add_show_docs', 'mv_exports', 'add_links', 'add_fold',
'strip_ansi', 'strip_hidden_metadata', 'hide_', 'hide_line', 'filter_stream_', 'clean_magics',
'rm_header_dash', 'rm_export', 'clean_show_doc', 'exec_show_docs', 'FilterDefaults']

# %% ../nbs/api/10_processors.ipynb 2
import ast
Expand Down Expand Up @@ -76,10 +76,22 @@ def begin(self):
for cell in reversed(exports):
if cell_lang(cell) != 'python': raise ValueError(f"{cell.metadata.language} can't export:\n{cell.source}")
nms = _def_names(cell, shown_docs)
for nm in nms: nb.cells.insert(cell.idx_+1, mk_cell(f'show_doc({nm})'))
for nm in nms:
new_cell = mk_cell(f'show_doc({nm})')
new_cell.has_sd = True
nb.cells.insert(cell.idx_+1, new_cell)
nb.has_docs_ = shown_docs or exports

# %% ../nbs/api/10_processors.ipynb 17
class mv_exports(Processor):
"Move `exports` cells to after the `show_doc`"
def begin(self):
cells = self.nb.cells
exports = L(c for c in cells if c.cell_type=='code' and 'exports' in c.directives_)
for cell in reversed(exports):
if getattr(cells[cell.idx_+1], 'has_sd', 0): cells.insert(cell.idx_+1, cells.pop(cell.idx_))

# %% ../nbs/api/10_processors.ipynb 18
_re_defaultexp = re.compile(r'^\s*#\|\s*default_exp\s+(\S+)', flags=re.MULTILINE)

def _default_exp(nb):
Expand All @@ -88,7 +100,7 @@ def _default_exp(nb):
default_exp = first(code_src.filter().map(_re_defaultexp.search).filter())
return default_exp.group(1) if default_exp else None

# %% ../nbs/api/10_processors.ipynb 19
# %% ../nbs/api/10_processors.ipynb 20
def add_links(cell):
"Add links to markdown cells"
nl = NbdevLookup()
Expand All @@ -97,31 +109,31 @@ def add_links(cell):
if hasattr(o, 'data') and hasattr(o['data'], 'text/markdown'):
o.data['text/markdown'] = [nl.link_line(s) for s in o.data['text/markdown']]

# %% ../nbs/api/10_processors.ipynb 21
# %% ../nbs/api/10_processors.ipynb 22
def add_fold(cell):
"Add `code-fold` to `exports` cells"
if cell.cell_type != 'code' or 'exports' not in cell.directives_: return
cell.source = f'#| code-fold: show\n#| code-summary: "Exported source"\n{cell.source}'

# %% ../nbs/api/10_processors.ipynb 24
# %% ../nbs/api/10_processors.ipynb 25
_re_ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')

def strip_ansi(cell):
"Strip Ansi Characters."
for outp in cell.get('outputs', []):
if outp.get('name')=='stdout': outp['text'] = [_re_ansi_escape.sub('', o) for o in outp.text]

# %% ../nbs/api/10_processors.ipynb 26
# %% ../nbs/api/10_processors.ipynb 27
def strip_hidden_metadata(cell):
'''Strips "hidden" metadata property from code cells so it doesn't interfere with docs rendering'''
if cell.cell_type == 'code' and 'metadata' in cell: cell.metadata.pop('hidden',None)

# %% ../nbs/api/10_processors.ipynb 27
# %% ../nbs/api/10_processors.ipynb 28
def hide_(cell):
"Hide cell from output"
del(cell['source'])

# %% ../nbs/api/10_processors.ipynb 29
# %% ../nbs/api/10_processors.ipynb 30
def _re_hideline(lang=None): return re.compile(fr'{langs[lang]}\|\s*hide_line\s*$', re.MULTILINE)

def hide_line(cell):
Expand All @@ -130,22 +142,22 @@ def hide_line(cell):
if cell.cell_type == 'code' and _re_hideline(lang).search(cell.source):
cell.source = '\n'.join([c for c in cell.source.splitlines() if not _re_hideline(lang).search(c)])

# %% ../nbs/api/10_processors.ipynb 32
# %% ../nbs/api/10_processors.ipynb 33
def filter_stream_(cell, *words):
"Remove output lines containing any of `words` in `cell` stream output"
if not words: return
for outp in cell.get('outputs', []):
if outp.output_type == 'stream':
outp['text'] = [l for l in outp.text if not re.search('|'.join(words), l)]

# %% ../nbs/api/10_processors.ipynb 34
# %% ../nbs/api/10_processors.ipynb 35
_magics_pattern = re.compile(r'^\s*(%%|%).*', re.MULTILINE)

def clean_magics(cell):
"A preprocessor to remove cell magic commands"
if cell.cell_type == 'code': cell.source = _magics_pattern.sub('', cell.source).strip()

# %% ../nbs/api/10_processors.ipynb 36
# %% ../nbs/api/10_processors.ipynb 37
_re_hdr_dash = re.compile(r'^#+\s+.*\s+-\s*$', re.MULTILINE)

def rm_header_dash(cell):
Expand All @@ -154,14 +166,14 @@ def rm_header_dash(cell):
src = cell.source.strip()
if cell.cell_type == 'markdown' and src.startswith('#') and src.endswith(' -'): del(cell['source'])

# %% ../nbs/api/10_processors.ipynb 38
# %% ../nbs/api/10_processors.ipynb 39
_hide_dirs = {'export','exporti', 'hide','default_exp'}

def rm_export(cell):
"Remove cells that are exported or hidden"
if cell.directives_ and (cell.directives_.keys() & _hide_dirs): del(cell['source'])

# %% ../nbs/api/10_processors.ipynb 40
# %% ../nbs/api/10_processors.ipynb 41
_re_showdoc = re.compile(r'^show_doc', re.MULTILINE)
def _is_showdoc(cell): return cell['cell_type'] == 'code' and _re_showdoc.search(cell.source)
def _add_directives(cell, d):
Expand All @@ -173,7 +185,7 @@ def clean_show_doc(cell):
if not _is_showdoc(cell): return
_add_directives(cell, {'output':'asis','echo':'false'})

# %% ../nbs/api/10_processors.ipynb 41
# %% ../nbs/api/10_processors.ipynb 42
def _ast_contains(trees, types):
for tree in trees:
for node in ast.walk(tree):
Expand All @@ -194,7 +206,7 @@ def _do_eval(cell):
return True
if _show_docs(trees): return True

# %% ../nbs/api/10_processors.ipynb 42
# %% ../nbs/api/10_processors.ipynb 43
class exec_show_docs(Processor):
"Execute cells needed for `show_docs` output, including exported cells and imports"
def begin(self):
Expand All @@ -221,13 +233,13 @@ def end(self):
widgets = {**old, **new, 'state': {**old.get('state', {}), **new['state']}}
self.nb.metadata['widgets'] = {mimetype: widgets}

# %% ../nbs/api/10_processors.ipynb 44
# %% ../nbs/api/10_processors.ipynb 45
def _import_obj(s):
mod_nm, obj_nm = s.split(':')
mod = importlib.import_module(mod_nm)
return getattr(mod, obj_nm)

# %% ../nbs/api/10_processors.ipynb 45
# %% ../nbs/api/10_processors.ipynb 46
class FilterDefaults:
"Override `FilterDefaults` to change which notebook processors are used"
def xtra_procs(self):
Expand All @@ -237,7 +249,7 @@ def xtra_procs(self):
def base_procs(self):
return [FrontmatterProc, populate_language, add_show_docs, insert_warning,
strip_ansi, hide_line, filter_stream_, rm_header_dash,
clean_show_doc, exec_show_docs, rm_export, clean_magics, hide_, add_links, add_fold, strip_hidden_metadata]
clean_show_doc, exec_show_docs, rm_export, clean_magics, hide_, add_links, add_fold, mv_exports, strip_hidden_metadata]

def procs(self):
"Processors for export"
Expand Down
Loading

0 comments on commit cc4793b

Please sign in to comment.