Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow trailing : in directives for YAML compliance #1312

Merged
merged 1 commit into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions nbdev/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def _quarto_re(lang=None): return re.compile(_dir_pre(lang) + r'\s*[\w|-]+\s*:')
# %% ../nbs/api/03_process.ipynb 11
def _directive(s, lang='python'):
s = re.sub('^'+_dir_pre(lang), f"{langs[lang]}|", s)
if s.strip().endswith(':'): s = s.replace(':', '') # You can append colon at the end to be Quarto compliant. Ex: #|hide:
if ':' in s: s = s.replace(':', ': ')
s = (s.strip()[2:]).strip().split()
if not s: return None
Expand Down Expand Up @@ -70,22 +71,22 @@ def extract_directives(cell, remove=True, lang='python'):
cell['source'] = ''.join([_norm_quarto(o, lang) for o in dirs if _quarto_re(lang).match(o) or _cell_mgc.match(o)] + code)
return dict(L(_directive(s, lang) for s in dirs).filter())

# %% ../nbs/api/03_process.ipynb 22
# %% ../nbs/api/03_process.ipynb 21
def opt_set(var, newval):
"newval if newval else var"
return newval if newval else var

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

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

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

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

# %% ../nbs/api/03_process.ipynb 35
# %% ../nbs/api/03_process.ipynb 34
class Processor:
"Base class for processors"
def __init__(self, nb): self.nb = nb
Expand Down
31 changes: 14 additions & 17 deletions nbs/api/03_process.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
"#|export\n",
"def _directive(s, lang='python'):\n",
" s = re.sub('^'+_dir_pre(lang), f\"{langs[lang]}|\", s)\n",
" if s.strip().endswith(':'): s = s.replace(':', '') # You can append colon at the end to be Quarto compliant. Ex: #|hide:\n",
" if ':' in s: s = s.replace(':', ': ')\n",
" s = (s.strip()[2:]).strip().split()\n",
" if not s: return None\n",
Expand Down Expand Up @@ -296,25 +297,21 @@
"# |woo: baz\n",
"1+2\n",
"#bar\"\"\")\n",
"test_eq(extract_directives(exp), {'export':['module'], 'hide':[], 'eval:': ['false'], 'foo': ['bar'], 'woo:': ['baz']})\n",
"test_eq(exp.source, '#|eval: false\\n# |woo: baz\\n1+2\\n#bar')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e03d189c-0629-487c-8cc5-e34268aab5f9",
"metadata": {},
"outputs": [],
"source": [
"#|hide\n",
"exp = AttrDict(source = \"\"\"\n",
"⍝|hide\n",
"⍝| foo: bar\n",
"\n",
"# this one has #|hide: with a colon at the end, wich is quarto compliant\n",
"exp2 = AttrDict(source = \"\"\"#|export module\n",
"#|eval:false\n",
"#| hide:\n",
"# | foo bar\n",
"# |woo: baz\n",
"1+2\n",
"⍝bar\"\"\")\n",
"test_eq(extract_directives(exp, lang='apl'), {'hide': [], 'foo:': ['bar']})"
"#bar\"\"\")\n",
"\n",
"_answer = {'export':['module'], 'hide':[], 'eval:': ['false'], 'foo': ['bar'], 'woo:': ['baz']}\n",
"\n",
"test_eq(extract_directives(exp), _answer)\n",
"test_eq(extract_directives(exp2), _answer)\n",
"test_eq(exp.source, '#|eval: false\\n# |woo: baz\\n1+2\\n#bar')"
]
},
{
Expand Down