diff --git a/d2lbook/build.py b/d2lbook/build.py index 279cf28..1be57f9 100644 --- a/d2lbook/build.py +++ b/d2lbook/build.py @@ -10,6 +10,7 @@ import regex import subprocess import hashlib +import pathlib from d2lbook.utils import * # TODO(mli), don't report * from d2lbook.sphinx import prepare_sphinx_env from d2lbook.config import Config diff --git a/d2lbook/library.py b/d2lbook/library.py index 958a356..0c05d7e 100644 --- a/d2lbook/library.py +++ b/d2lbook/library.py @@ -20,16 +20,16 @@ def save_file(root_dir: str, nbfile: str): nb = notebook.read_markdown(f.read()) saved = [] + save_all = False for cell in nb.cells: if cell.cell_type == 'code': - m = re.search('# *@save_cell', cell.source.lstrip()) - if m: - if m.span()[0] == 0: - saved.append(cell.source.lstrip()[m.span()[1]:].lstrip()) - else: - logging.warning(f'{m[0]} should be on the first line of the code cell:\n\n{cell.source} ') + src = cell.source.lstrip() + if re.search('# *@save_all', src): + save_all = True + if save_all or re.search('# *@save_cell', src): + saved.append(src) else: - blk = _save_block(cell.source, '@save') + blk = _save_block(src, '@save') if blk: saved.append(blk) if saved: diff --git a/d2lbook/rst.py b/d2lbook/rst.py index 8275eb1..143d9e4 100644 --- a/d2lbook/rst.py +++ b/d2lbook/rst.py @@ -39,12 +39,15 @@ def _process_nb(nb): else: new_cells.append(cell) # hide/show + hide_all = False for cell in new_cells: if cell.cell_type == 'code': src = cell.source.lower() - if '# hide outputs' in src or ('#@hide' in src and '#@hide_code' not in src) or '#@hide_output' in src: + if '#@hide_all' in src: + hide_all = True + if hide_all or '# hide outputs' in src or ('#@hide' in src and '#@hide_code' not in src) or '#@hide_output' in src: cell.outputs = [] - if '# hide code' in src or ('#@hide' in src and '#@hide_output' not in src) or '#@hide_code' in src: + if hide_all or '# hide code' in src or ('#@hide' in src and '#@hide_output' not in src) or '#@hide_code' in src: cell.source = '' return notebook.create_new_notebook(nb, new_cells)