Skip to content

Commit 4835747

Browse files
committed
allow trailing : in directives for YAML compliance
1 parent 923f2b0 commit 4835747

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

‎nbdev/process.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def _quarto_re(lang=None): return re.compile(_dir_pre(lang) + r'\s*[\w|-]+\s*:')
3333
# %% ../nbs/api/03_process.ipynb 11
3434
def _directive(s, lang='python'):
3535
s = re.sub('^'+_dir_pre(lang), f"{langs[lang]}|", s)
36+
if s.strip().endswith(':'): s = s.replace(':', '') # You can append colon at the end to be Quarto compliant. Ex: #|hide:
3637
if ':' in s: s = s.replace(':', ': ')
3738
s = (s.strip()[2:]).strip().split()
3839
if not s: return None
@@ -70,22 +71,22 @@ def extract_directives(cell, remove=True, lang='python'):
7071
cell['source'] = ''.join([_norm_quarto(o, lang) for o in dirs if _quarto_re(lang).match(o) or _cell_mgc.match(o)] + code)
7172
return dict(L(_directive(s, lang) for s in dirs).filter())
7273

73-
# %% ../nbs/api/03_process.ipynb 22
74+
# %% ../nbs/api/03_process.ipynb 21
7475
def opt_set(var, newval):
7576
"newval if newval else var"
7677
return newval if newval else var
7778

78-
# %% ../nbs/api/03_process.ipynb 23
79+
# %% ../nbs/api/03_process.ipynb 22
7980
def instantiate(x, **kwargs):
8081
"Instantiate `x` if it's a type"
8182
return x(**kwargs) if isinstance(x,type) else x
8283

8384
def _mk_procs(procs, nb): return L(procs).map(instantiate, nb=nb)
8485

85-
# %% ../nbs/api/03_process.ipynb 24
86+
# %% ../nbs/api/03_process.ipynb 23
8687
def _is_direc(f): return getattr(f, '__name__', '-')[-1]=='_'
8788

88-
# %% ../nbs/api/03_process.ipynb 25
89+
# %% ../nbs/api/03_process.ipynb 24
8990
class NBProcessor:
9091
"Process cells and nbdev comments in a notebook"
9192
def __init__(self, path=None, procs=None, nb=None, debug=False, rm_directives=True, process=False):
@@ -125,7 +126,7 @@ def process(self):
125126
"Process all cells with all processors"
126127
for proc in self.procs: self._proc(proc)
127128

128-
# %% ../nbs/api/03_process.ipynb 35
129+
# %% ../nbs/api/03_process.ipynb 34
129130
class Processor:
130131
"Base class for processors"
131132
def __init__(self, nb): self.nb = nb

‎nbs/api/03_process.ipynb‎

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
"#|export\n",
149149
"def _directive(s, lang='python'):\n",
150150
" s = re.sub('^'+_dir_pre(lang), f\"{langs[lang]}|\", s)\n",
151+
" if s.strip().endswith(':'): s = s.replace(':', '') # You can append colon at the end to be Quarto compliant. Ex: #|hide:\n",
151152
" if ':' in s: s = s.replace(':', ': ')\n",
152153
" s = (s.strip()[2:]).strip().split()\n",
153154
" if not s: return None\n",
@@ -296,25 +297,21 @@
296297
"# |woo: baz\n",
297298
"1+2\n",
298299
"#bar\"\"\")\n",
299-
"test_eq(extract_directives(exp), {'export':['module'], 'hide':[], 'eval:': ['false'], 'foo': ['bar'], 'woo:': ['baz']})\n",
300-
"test_eq(exp.source, '#|eval: false\\n# |woo: baz\\n1+2\\n#bar')"
301-
]
302-
},
303-
{
304-
"cell_type": "code",
305-
"execution_count": null,
306-
"id": "e03d189c-0629-487c-8cc5-e34268aab5f9",
307-
"metadata": {},
308-
"outputs": [],
309-
"source": [
310-
"#|hide\n",
311-
"exp = AttrDict(source = \"\"\"\n",
312-
"⍝|hide\n",
313-
"⍝| foo: bar\n",
300+
"\n",
301+
"# this one has #|hide: with a colon at the end, wich is quarto compliant\n",
302+
"exp2 = AttrDict(source = \"\"\"#|export module\n",
303+
"#|eval:false\n",
304+
"#| hide:\n",
305+
"# | foo bar\n",
314306
"# |woo: baz\n",
315307
"1+2\n",
316-
"⍝bar\"\"\")\n",
317-
"test_eq(extract_directives(exp, lang='apl'), {'hide': [], 'foo:': ['bar']})"
308+
"#bar\"\"\")\n",
309+
"\n",
310+
"_answer = {'export':['module'], 'hide':[], 'eval:': ['false'], 'foo': ['bar'], 'woo:': ['baz']}\n",
311+
"\n",
312+
"test_eq(extract_directives(exp), _answer)\n",
313+
"test_eq(extract_directives(exp2), _answer)\n",
314+
"test_eq(exp.source, '#|eval: false\\n# |woo: baz\\n1+2\\n#bar')"
318315
]
319316
},
320317
{

0 commit comments

Comments
 (0)