Skip to content

Commit 5b4522d

Browse files
committed
fixes #691
1 parent 9b77295 commit 5b4522d

File tree

7 files changed

+90
-67
lines changed

7 files changed

+90
-67
lines changed

nbdev/_modidx.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@
5050
'readme_nb': 'getting_started.ipynb',
5151
'recursive': 'False',
5252
'repo': 'nbdev',
53-
'requirements': 'fastcore>=1.5.8 execnb astunparse',
53+
'requirements': 'fastcore>=1.5.8 execnb astunparse ghapi',
5454
'status': '2',
5555
'title': 'nbdev',
5656
'tst_flags': 'notest',
5757
'user': 'fastai',
58-
'version': '2.0.5'},
58+
'version': '2.0.6'},
5959
'syms': { 'nbdev.clean': { 'nbdev.clean.clean_nb': 'https://nbdev.fast.ai/clean.html#clean_nb',
6060
'nbdev.clean.nbdev_clean': 'https://nbdev.fast.ai/clean.html#nbdev_clean',
6161
'nbdev.clean.nbdev_install_hooks': 'https://nbdev.fast.ai/clean.html#nbdev_install_hooks',
@@ -155,7 +155,8 @@
155155
'nbdev.processors.rm_export': 'https://nbdev.fast.ai/processors.html#rm_export',
156156
'nbdev.processors.rm_header_dash': 'https://nbdev.fast.ai/processors.html#rm_header_dash',
157157
'nbdev.processors.strip_ansi': 'https://nbdev.fast.ai/processors.html#strip_ansi',
158-
'nbdev.processors.strip_hidden_metadata': 'https://nbdev.fast.ai/processors.html#strip_hidden_metadata'},
158+
'nbdev.processors.strip_hidden_metadata': 'https://nbdev.fast.ai/processors.html#strip_hidden_metadata',
159+
'nbdev.processors.yaml_str': 'https://nbdev.fast.ai/processors.html#yaml_str'},
159160
'nbdev.read': { 'nbdev.read.add_init': 'https://nbdev.fast.ai/read.html#add_init',
160161
'nbdev.read.basic_export_nb': 'https://nbdev.fast.ai/read.html#basic_export_nb',
161162
'nbdev.read.config_key': 'https://nbdev.fast.ai/read.html#config_key',

nbdev/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ def nbdev_quarto(
344344
files = nbdev_sidebar.__wrapped__(path, symlinks=symlinks, file_re=file_re, folder_re=folder_re,
345345
skip_file_glob=skip_file_glob, skip_file_re=skip_file_re, returnit=True)
346346
shutil.rmtree(doc_path, ignore_errors=True)
347-
cmd = 'preview' if preview else 'render'
348-
_sprun(f'cd {path} && quarto {cmd} --no-execute')
347+
if preview: os.system(f'cd {path} && quarto preview --no-execute')
348+
else: _sprun(f'cd {path} && quarto render --no-execute')
349349
if not preview:
350350
nbdev_readme.__wrapped__(path, doc_path)
351351
if tmp_doc_path.parent != cfg_path: # move docs folder to root of repo if it doesn't exist there

nbdev/processors.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
# %% auto 0
44
__all__ = ['DEFAULT_FM_KEYS', 'nbflags_', 'cell_lang', 'add_links', 'strip_ansi', 'strip_hidden_metadata', 'hide_', 'hide_line',
55
'filter_stream_', 'clean_magics', 'lang_identify', 'rm_header_dash', 'rm_export', 'clean_show_doc',
6-
'exec_show_docs', 'populate_language', 'insert_warning', 'add_show_docs', 'is_frontmatter', 'nb_fmdict',
7-
'construct_fm', 'insert_frontmatter', 'infer_frontmatter']
6+
'exec_show_docs', 'populate_language', 'insert_warning', 'add_show_docs', 'is_frontmatter', 'yaml_str',
7+
'nb_fmdict', 'construct_fm', 'insert_frontmatter', 'infer_frontmatter']
88

99
# %% ../nbs/09_processors.ipynb 2
1010
import ast
@@ -202,6 +202,13 @@ def _default_exp(nb):
202202
return default_exp.group(1) if default_exp else None
203203

204204
# %% ../nbs/09_processors.ipynb 52
205+
def yaml_str(s:str):
206+
"Create a valid YAML string from `s`"
207+
if s[0]=='"' and s[-1]=='"': return s
208+
res = s.replace('\\', '\\\\').replace('"', r'\"')
209+
return f'"{res}"'
210+
211+
# %% ../nbs/09_processors.ipynb 53
205212
def nb_fmdict(nb, remove=True):
206213
"Infer the front matter from a notebook's markdown formatting"
207214
md_cells = _celltyp(nb, 'markdown').filter(_istitle)
@@ -212,26 +219,26 @@ def nb_fmdict(nb, remove=True):
212219
flags = re.findall('^-\s+(.*)', cell.source, flags=re.MULTILINE)
213220
flags = [s.split(':', 1) for s in flags if ':' in s] if flags else []
214221
flags = merge({k:v for k,v in flags if k and v},
215-
{'title':title}, {'description':desc} if desc else {})
222+
{'title':yaml_str(title)}, {'description':yaml_str(desc)} if desc else {})
216223
if remove: cell['source'] = None
217224
return flags
218225
else: return {}
219226

220-
# %% ../nbs/09_processors.ipynb 55
227+
# %% ../nbs/09_processors.ipynb 56
221228
DEFAULT_FM_KEYS = ['title', 'description', 'author', 'image', 'categories', 'output-file', 'aliases']
222229

223230
def construct_fm(fmdict:dict, keys = DEFAULT_FM_KEYS):
224231
"construct front matter from a dictionary, but only for `keys`"
225232
if not fmdict: return None
226233
return '---\n'+'\n'.join([f"{k}: {fmdict[k]}" for k in keys if k in fmdict])+'\n---'
227234

228-
# %% ../nbs/09_processors.ipynb 57
235+
# %% ../nbs/09_processors.ipynb 58
229236
def insert_frontmatter(nb, fm_dict:dict, filter_keys:list=DEFAULT_FM_KEYS):
230237
"Add frontmatter into notebook based on `filter_keys` that exist in `fmdict`."
231238
fm = construct_fm(fm_dict, keys=filter_keys)
232239
if fm: nb.cells.insert(0, NbCell(0, dict(cell_type='raw', metadata={}, source=fm, directives_={})))
233240

234-
# %% ../nbs/09_processors.ipynb 58
241+
# %% ../nbs/09_processors.ipynb 59
235242
def infer_frontmatter(nb):
236243
"Insert front matter if it doesn't exist automatically from nbdev styled markdown."
237244
if is_frontmatter(nb): return

nbdev/tutorial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# %% auto 0
44
__all__ = ['say_hello', 'HelloSayer']
55

6-
# %% ../nbs/tutorial.ipynb 26
6+
# %% ../nbs/tutorial.ipynb 25
77
def say_hello(to):
88
"Say hello to somebody"
99
return f'Hello {to}!'

nbs/09_processors.ipynb

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,21 @@
695695
"test_eq(_default_exp(_testnb), 'foobar')"
696696
]
697697
},
698+
{
699+
"cell_type": "code",
700+
"execution_count": null,
701+
"id": "5c95e328",
702+
"metadata": {},
703+
"outputs": [],
704+
"source": [
705+
"#|export\n",
706+
"def yaml_str(s:str):\n",
707+
" \"Create a valid YAML string from `s`\"\n",
708+
" if s[0]=='\"' and s[-1]=='\"': return s\n",
709+
" res = s.replace('\\\\', '\\\\\\\\').replace('\"', r'\\\"')\n",
710+
" return f'\"{res}\"'"
711+
]
712+
},
698713
{
699714
"cell_type": "code",
700715
"execution_count": null,
@@ -713,7 +728,7 @@
713728
" flags = re.findall('^-\\s+(.*)', cell.source, flags=re.MULTILINE)\n",
714729
" flags = [s.split(':', 1) for s in flags if ':' in s] if flags else []\n",
715730
" flags = merge({k:v for k,v in flags if k and v}, \n",
716-
" {'title':title}, {'description':desc} if desc else {})\n",
731+
" {'title':yaml_str(title)}, {'description':yaml_str(desc)} if desc else {})\n",
717732
" if remove: cell['source'] = None\n",
718733
" return flags\n",
719734
" else: return {}"
@@ -728,7 +743,7 @@
728743
"source": [
729744
"_testnb = read_nb('../tests/docs_test.ipynb')\n",
730745
"_res = nb_fmdict(_testnb)\n",
731-
"test_eq(_res, dict(key1=' value1', key2=' value2', categories=' [c1, c2]', title='a title', description='A description'))"
746+
"test_eq(_res, dict(key1=' value1', key2=' value2', categories=' [c1, c2]', title='\"a title\"', description='\"A description\"'))"
732747
]
733748
},
734749
{
@@ -770,8 +785,8 @@
770785
"output_type": "stream",
771786
"text": [
772787
"---\n",
773-
"title: a title\n",
774-
"description: A description\n",
788+
"title: \"a title\"\n",
789+
"description: \"A description\"\n",
775790
"categories: [c1, c2]\n",
776791
"---\n"
777792
]
@@ -824,7 +839,7 @@
824839
"_raw_res = _run_procs()\n",
825840
"_res = _run_procs(postprocs=infer_frontmatter)\n",
826841
"assert '# a title' in _raw_res and '# a title' not in _res\n",
827-
"assert r'description: A description\\n' in _res\n",
842+
"assert r'description: \"A description\"\\n' in _res\n",
828843
"assert r'categories: [c1, c2]\\n' in _res\n",
829844
"assert r'output-file: foobar.html\\n---' in _res"
830845
]

0 commit comments

Comments
 (0)