|
73 | 73 | "---\\s*'''\n", |
74 | 74 | "\n", |
75 | 75 | "_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", |
78 | 87 | "def _fm2dict(s:str, nb=True):\n", |
79 | 88 | " \"Load YAML frontmatter into a `dict`\"\n", |
80 | 89 | " re_fm = _re_fm_nb if nb else _re_fm_md\n", |
|
93 | 102 | " if r:\n", |
94 | 103 | " try: res.update(yaml.safe_load('\\n'.join(r)))\n", |
95 | 104 | " 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", |
98 | 116 | "def nb_frontmatter(nb):\n", |
99 | 117 | " \"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" |
104 | 123 | ] |
105 | 124 | }, |
106 | 125 | { |
|
0 commit comments