Skip to content

Commit

Permalink
add @save_all and @hide_all
Browse files Browse the repository at this point in the history
  • Loading branch information
Mu Li committed Nov 10, 2020
1 parent 21433e2 commit 1950193
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
1 change: 1 addition & 0 deletions d2lbook/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions d2lbook/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 5 additions & 2 deletions d2lbook/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 1950193

Please sign in to comment.