Skip to content

Commit 212f4ed

Browse files
committed
fixes #790
1 parent cf46168 commit 212f4ed

2 files changed

Lines changed: 23 additions & 23 deletions

File tree

fastcore/xtras.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -596,16 +596,16 @@ def asave_iter(g):
596596
def _(*args, **kwargs): return _save_iter(g, *args, **kwargs)
597597
return _
598598

599-
# %% ../nbs/03_xtras.ipynb #20e906c7
600-
def frontmatter(txt:str)->dict:
601-
"Dict contained in frontmatter in `txt`, if present"
599+
# %% ../nbs/03_xtras.ipynb #d2757e2a
600+
def frontmatter(txt:str)->tuple:
601+
"Tuple of (dict, body) from frontmatter in `txt`; invalid/missing frontmatter returns ({}, txt)"
602602
import yaml
603-
if not txt.startswith('---\n'): return {}
604-
fm,part,_ = txt[4:].partition('\n---\n')
605-
if not part: return {}
603+
if not txt.startswith('---\n'): return {},txt
604+
fm,part,body = txt[4:].partition('\n---\n')
605+
if not part: return {},txt
606606
try: res = yaml.safe_load(fm)
607-
except yaml.parser.ParserError: return {}
608-
return res if isinstance(res,dict) else {}
607+
except yaml.parser.ParserError: return {},txt
608+
return (res,body) if isinstance(res,dict) else ({},txt)
609609

610610
# %% ../nbs/03_xtras.ipynb #45eb5141
611611
def clean_cli_output(txt:str, strip:bool=True):

nbs/03_xtras.ipynb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2345,7 +2345,7 @@
23452345
{
23462346
"data": {
23472347
"text/plain": [
2348-
"['f', 'h', 'g', 'e', 'd', 'b', 'a', 'c']"
2348+
"['e', 'h', 'd', 'c', 'a', 'f', 'b', 'g']"
23492349
]
23502350
},
23512351
"execution_count": null,
@@ -2708,34 +2708,34 @@
27082708
{
27092709
"cell_type": "code",
27102710
"execution_count": null,
2711-
"id": "20e906c7",
2711+
"id": "d2757e2a",
27122712
"metadata": {},
27132713
"outputs": [],
27142714
"source": [
27152715
"#| export\n",
2716-
"def frontmatter(txt:str)->dict:\n",
2717-
" \"Dict contained in frontmatter in `txt`, if present\"\n",
2716+
"def frontmatter(txt:str)->tuple:\n",
2717+
" \"Tuple of (dict, body) from frontmatter in `txt`; invalid/missing frontmatter returns ({}, txt)\"\n",
27182718
" import yaml\n",
2719-
" if not txt.startswith('---\\n'): return {}\n",
2720-
" fm,part,_ = txt[4:].partition('\\n---\\n')\n",
2721-
" if not part: return {}\n",
2719+
" if not txt.startswith('---\\n'): return {},txt\n",
2720+
" fm,part,body = txt[4:].partition('\\n---\\n')\n",
2721+
" if not part: return {},txt\n",
27222722
" try: res = yaml.safe_load(fm)\n",
2723-
" except yaml.parser.ParserError: return {}\n",
2724-
" return res if isinstance(res,dict) else {}"
2723+
" except yaml.parser.ParserError: return {},txt\n",
2724+
" return (res,body) if isinstance(res,dict) else ({},txt)"
27252725
]
27262726
},
27272727
{
27282728
"cell_type": "code",
27292729
"execution_count": null,
2730-
"id": "b8165cbd",
2730+
"id": "0648a1fe",
27312731
"metadata": {},
27322732
"outputs": [],
27332733
"source": [
2734-
"test_eq(frontmatter('---\\ntitle: \"Hi there\"\\ntags: [a,b]\\n---\\nBody'), {'title':'Hi there', 'tags':['a','b']})\n",
2735-
"test_eq(frontmatter('---\\nbroken: [a,'), {})\n",
2736-
"test_eq(frontmatter('No frontmatter here'), {})\n",
2737-
"test_eq(frontmatter('---\\njust text no closing'), {})\n",
2738-
"test_eq(frontmatter('---\\n---\\nBody'), {})"
2734+
"test_eq(frontmatter('---\\ntitle: \"Hi there\"\\ntags: [a,b]\\n---\\nBody'), ({'title':'Hi there', 'tags':['a','b']}, 'Body'))\n",
2735+
"test_eq(frontmatter('---\\nbroken: [a,'), ({}, '---\\nbroken: [a,'))\n",
2736+
"test_eq(frontmatter('No frontmatter here'), ({}, 'No frontmatter here'))\n",
2737+
"test_eq(frontmatter('---\\njust text no closing'), ({}, '---\\njust text no closing'))\n",
2738+
"test_eq(frontmatter('---\\n---\\nBody'), ({}, '---\\n---\\nBody'))"
27392739
]
27402740
},
27412741
{

0 commit comments

Comments
 (0)