Skip to content

Commit 5246e6a

Browse files
committed
fixes #1580
1 parent 153fc90 commit 5246e6a

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

nbdev/frontmatter.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
_re_fm_nb = re.compile(_RE_FM_BASE+'$', flags=re.DOTALL)
2323
_re_fm_md = re.compile(_RE_FM_BASE, flags=re.DOTALL)
2424

25+
# %% ../nbs/api/09_frontmatter.ipynb #1725f3b9
2526
def _fm2dict(s:str, nb=True):
2627
"Load YAML frontmatter into a `dict`"
2728
re_fm = _re_fm_nb if nb else _re_fm_md
@@ -42,12 +43,14 @@ def _md2dict(s:str):
4243
except Exception as e: warn(f'Failed to create YAML dict for:\n{r}\n\n{e}\n')
4344
return res
4445

46+
# %% ../nbs/api/09_frontmatter.ipynb #4ba11b21
4547
def nb_frontmatter(nb):
4648
"Get frontmatter dict from `nb` without modifying cells"
47-
for c in nb.cells:
48-
if c.cell_type=='raw': return _fm2dict(c.source)
49-
if c.cell_type=='markdown': return _md2dict(c.source)
50-
return {}
49+
raw = first(c for c in nb.cells if c.cell_type=='raw')
50+
md = first(c for c in nb.cells if c.cell_type=='markdown')
51+
res = _md2dict(md.source) if md else {}
52+
if raw: res.update(_fm2dict(raw.source))
53+
return res
5154

5255
# %% ../nbs/api/09_frontmatter.ipynb #1b5d9d32
5356
def _dict2fm(d): return f'---\n{yaml.dump(d)}\n---\n\n'

nbs/api/09_frontmatter.ipynb

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,17 @@
7373
"---\\s*'''\n",
7474
"\n",
7575
"_re_fm_nb = re.compile(_RE_FM_BASE+'$', flags=re.DOTALL)\n",
76-
"_re_fm_md = re.compile(_RE_FM_BASE, flags=re.DOTALL)\n",
77-
"\n",
76+
"_re_fm_md = re.compile(_RE_FM_BASE, flags=re.DOTALL)"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": null,
82+
"id": "1725f3b9",
83+
"metadata": {},
84+
"outputs": [],
85+
"source": [
86+
"#| export\n",
7887
"def _fm2dict(s:str, nb=True):\n",
7988
" \"Load YAML frontmatter into a `dict`\"\n",
8089
" re_fm = _re_fm_nb if nb else _re_fm_md\n",
@@ -93,14 +102,24 @@
93102
" if r:\n",
94103
" try: res.update(yaml.safe_load('\\n'.join(r)))\n",
95104
" except Exception as e: warn(f'Failed to create YAML dict for:\\n{r}\\n\\n{e}\\n')\n",
96-
" return res\n",
97-
"\n",
105+
" return res"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": null,
111+
"id": "4ba11b21",
112+
"metadata": {},
113+
"outputs": [],
114+
"source": [
115+
"#| export\n",
98116
"def nb_frontmatter(nb):\n",
99117
" \"Get frontmatter dict from `nb` without modifying cells\"\n",
100-
" for c in nb.cells:\n",
101-
" if c.cell_type=='raw': return _fm2dict(c.source)\n",
102-
" if c.cell_type=='markdown': return _md2dict(c.source)\n",
103-
" return {}"
118+
" raw = first(c for c in nb.cells if c.cell_type=='raw')\n",
119+
" md = first(c for c in nb.cells if c.cell_type=='markdown')\n",
120+
" res = _md2dict(md.source) if md else {}\n",
121+
" if raw: res.update(_fm2dict(raw.source))\n",
122+
" return res"
104123
]
105124
},
106125
{

0 commit comments

Comments
 (0)