Skip to content

Commit 4debce4

Browse files
committed
extend front matter retrieval
1 parent b24574f commit 4debce4

File tree

4 files changed

+42
-27
lines changed

4 files changed

+42
-27
lines changed

nbprocess/_modidx.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@
9494
'nbprocess.processors.exec_show_docs': 'https://nbprocess.fast.ai/processors#exec_show_docs',
9595
'nbprocess.processors.clean_show_doc': 'https://nbprocess.fast.ai/processors#clean_show_doc',
9696
'nbprocess.processors.insert_warning': 'https://nbprocess.fast.ai/processors#insert_warning',
97-
'nbprocess.processors.add_show_docs': 'https://nbprocess.fast.ai/processors#add_show_docs',
98-
'nbprocess.processors.add_frontmatter': 'https://nbprocess.fast.ai/processors#add_frontmatter'},
97+
'nbprocess.processors.add_show_docs': 'https://nbprocess.fast.ai/processors#add_show_docs'},
9998
'nbprocess.read': { 'nbprocess.read.NbCell': 'https://nbprocess.fast.ai/read#NbCell',
10099
'nbprocess.read.dict2nb': 'https://nbprocess.fast.ai/read#dict2nb',
101100
'nbprocess.read.read_nb': 'https://nbprocess.fast.ai/read#read_nb',

nbprocess/processors.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# %% auto 0
44
__all__ = ['add_links', 'strip_ansi', 'hide_', 'hide_line', 'filter_stream_', 'clean_magics', 'lang_identify', 'rm_header_dash',
5-
'rm_export', 'exec_show_docs', 'clean_show_doc', 'insert_warning', 'add_show_docs', 'add_frontmatter']
5+
'rm_export', 'exec_show_docs', 'clean_show_doc', 'insert_warning', 'add_show_docs']
66

77
# %% ../nbs/09_processors.ipynb 3
88
import ast
@@ -162,24 +162,14 @@ def add_show_docs(nb):
162162
def _celltyp(nb, cell_type): return nb.cells.filter(lambda c: c.cell_type == cell_type)
163163
def _frontmatter(nb): return _celltyp(nb, 'raw').filter(lambda c: _re_fm.search(c.get('source', '')))
164164

165-
def _title(nb):
166-
"Get the title and description from a notebook from the H1"
165+
def _fm(nb):
166+
"Infer the front matter from a notebook's markdown formatting"
167167
md_cells = _celltyp(nb, 'markdown').filter(lambda c: _re_title.search(c.get('source', '')))
168168
if not md_cells: return None,None
169169
cell = md_cells[0]
170170
title,desc=_re_title.match(cell.source).groups()
171+
flags = re.findall('^-\s+(.*)', cell.source, flags=re.MULTILINE)
172+
flags = [s.split(':') for s in flags if ':' in s]
173+
flags = {k:v for k,v in flags if k and v}
171174
cell['source'] = None
172-
return title,desc
173-
174-
def add_frontmatter(nb):
175-
"Insert front matter if it doesn't exist"
176-
if _frontmatter(nb): return
177-
title,desc = _title(nb)
178-
code_src = nb.cells.filter(lambda x: x.cell_type == 'code').attrgot('source')
179-
default_exp = first(code_src.filter().map(_re_defaultexp.search).filter())
180-
default_exp = default_exp.group(1) if default_exp else None
181-
if title:
182-
desc = f'description: "{desc}"\n' if desc else ''
183-
outfile = f'output-file: {default_exp}\n' if default_exp else ''
184-
content = f'---\ntitle: {title}\n{outfile}{desc}---\n'
185-
nb.cells.insert(0, NbCell(0, dict(cell_type='raw', metadata={}, source=content)))
175+
return title,desc,flags

nbs/09_processors.ipynb

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@
581581
{
582582
"cell_type": "code",
583583
"execution_count": null,
584-
"id": "faba8516-7f94-4e5c-ae33-41b27cf350b9",
584+
"id": "99a8a186-47be-4b66-b1f7-14727f4932e1",
585585
"metadata": {},
586586
"outputs": [],
587587
"source": [
@@ -593,19 +593,43 @@
593593
"def _celltyp(nb, cell_type): return nb.cells.filter(lambda c: c.cell_type == cell_type)\n",
594594
"def _frontmatter(nb): return _celltyp(nb, 'raw').filter(lambda c: _re_fm.search(c.get('source', '')))\n",
595595
"\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",
598598
" md_cells = _celltyp(nb, 'markdown').filter(lambda c: _re_title.search(c.get('source', '')))\n",
599599
" if not md_cells: return None,None\n",
600600
" cell = md_cells[0]\n",
601601
" 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",
602605
" 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": [
605629
"def add_frontmatter(nb):\n",
606630
" \"Insert front matter if it doesn't exist\"\n",
607631
" if _frontmatter(nb): return\n",
608-
" title,desc = _title(nb)\n",
632+
" title,desc,_ = _fm(nb)\n",
609633
" code_src = nb.cells.filter(lambda x: x.cell_type == 'code').attrgot('source')\n",
610634
" default_exp = first(code_src.filter().map(_re_defaultexp.search).filter())\n",
611635
" default_exp = default_exp.group(1) if default_exp else None\n",
@@ -655,7 +679,7 @@
655679
{
656680
"cell_type": "code",
657681
"execution_count": null,
658-
"id": "8fff5b3f-f977-4301-892e-312936cb7a4e",
682+
"id": "174d4398-7446-4c27-b1aa-743bcbb419d4",
659683
"metadata": {},
660684
"outputs": [],
661685
"source": []

tests/docs_test.ipynb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
"metadata": {},
77
"source": [
88
"# a title\n",
9-
"> A description"
9+
"> A description\n",
10+
"- key1: value1\n",
11+
"- key2: value2"
1012
]
1113
},
1214
{

0 commit comments

Comments
 (0)