|
581 | 581 | { |
582 | 582 | "cell_type": "code", |
583 | 583 | "execution_count": null, |
584 | | - "id": "faba8516-7f94-4e5c-ae33-41b27cf350b9", |
| 584 | + "id": "99a8a186-47be-4b66-b1f7-14727f4932e1", |
585 | 585 | "metadata": {}, |
586 | 586 | "outputs": [], |
587 | 587 | "source": [ |
|
593 | 593 | "def _celltyp(nb, cell_type): return nb.cells.filter(lambda c: c.cell_type == cell_type)\n", |
594 | 594 | "def _frontmatter(nb): return _celltyp(nb, 'raw').filter(lambda c: _re_fm.search(c.get('source', '')))\n", |
595 | 595 | "\n", |
596 | | - "def _title(nb): \n", |
597 | | - " \"Get the title and description from a notebook from the H1\"\n", |
| 596 | + "def _fm(nb): \n", |
| 597 | + " \"Infer the front matter from a notebook's markdown formatting\"\n", |
598 | 598 | " md_cells = _celltyp(nb, 'markdown').filter(lambda c: _re_title.search(c.get('source', '')))\n", |
599 | 599 | " if not md_cells: return None,None\n", |
600 | 600 | " cell = md_cells[0]\n", |
601 | 601 | " title,desc=_re_title.match(cell.source).groups()\n", |
| 602 | + " flags = re.findall('^-\\s+(.*)', cell.source, flags=re.MULTILINE)\n", |
| 603 | + " flags = [s.split(':') for s in flags if ':' in s]\n", |
| 604 | + " flags = {k:v for k,v in flags if k and v}\n", |
602 | 605 | " cell['source'] = None\n", |
603 | | - " return title,desc\n", |
604 | | - "\n", |
| 606 | + " return title,desc,flags" |
| 607 | + ] |
| 608 | + }, |
| 609 | + { |
| 610 | + "cell_type": "code", |
| 611 | + "execution_count": null, |
| 612 | + "id": "77a021a4-a51a-4f18-acd5-5382ef8312c5", |
| 613 | + "metadata": {}, |
| 614 | + "outputs": [], |
| 615 | + "source": [ |
| 616 | + "#|hide\n", |
| 617 | + "test_eq(_fm(read_nb('../tests/docs_test.ipynb')),\n", |
| 618 | + " ('a title', 'A description', {'key1': ' value1', 'key2': ' value2'})\n", |
| 619 | + " )" |
| 620 | + ] |
| 621 | + }, |
| 622 | + { |
| 623 | + "cell_type": "code", |
| 624 | + "execution_count": null, |
| 625 | + "id": "412799d3-a241-44bd-abc7-2a3bf1334781", |
| 626 | + "metadata": {}, |
| 627 | + "outputs": [], |
| 628 | + "source": [ |
605 | 629 | "def add_frontmatter(nb):\n", |
606 | 630 | " \"Insert front matter if it doesn't exist\"\n", |
607 | 631 | " if _frontmatter(nb): return\n", |
608 | | - " title,desc = _title(nb)\n", |
| 632 | + " title,desc,_ = _fm(nb)\n", |
609 | 633 | " code_src = nb.cells.filter(lambda x: x.cell_type == 'code').attrgot('source')\n", |
610 | 634 | " default_exp = first(code_src.filter().map(_re_defaultexp.search).filter())\n", |
611 | 635 | " default_exp = default_exp.group(1) if default_exp else None\n", |
|
655 | 679 | { |
656 | 680 | "cell_type": "code", |
657 | 681 | "execution_count": null, |
658 | | - "id": "8fff5b3f-f977-4301-892e-312936cb7a4e", |
| 682 | + "id": "174d4398-7446-4c27-b1aa-743bcbb419d4", |
659 | 683 | "metadata": {}, |
660 | 684 | "outputs": [], |
661 | 685 | "source": [] |
|
0 commit comments