|
2345 | 2345 | { |
2346 | 2346 | "data": { |
2347 | 2347 | "text/plain": [ |
2348 | | - "['f', 'h', 'g', 'e', 'd', 'b', 'a', 'c']" |
| 2348 | + "['e', 'h', 'd', 'c', 'a', 'f', 'b', 'g']" |
2349 | 2349 | ] |
2350 | 2350 | }, |
2351 | 2351 | "execution_count": null, |
|
2708 | 2708 | { |
2709 | 2709 | "cell_type": "code", |
2710 | 2710 | "execution_count": null, |
2711 | | - "id": "20e906c7", |
| 2711 | + "id": "d2757e2a", |
2712 | 2712 | "metadata": {}, |
2713 | 2713 | "outputs": [], |
2714 | 2714 | "source": [ |
2715 | 2715 | "#| 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", |
2718 | 2718 | " 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", |
2722 | 2722 | " 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)" |
2725 | 2725 | ] |
2726 | 2726 | }, |
2727 | 2727 | { |
2728 | 2728 | "cell_type": "code", |
2729 | 2729 | "execution_count": null, |
2730 | | - "id": "b8165cbd", |
| 2730 | + "id": "0648a1fe", |
2731 | 2731 | "metadata": {}, |
2732 | 2732 | "outputs": [], |
2733 | 2733 | "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'))" |
2739 | 2739 | ] |
2740 | 2740 | }, |
2741 | 2741 | { |
|
0 commit comments