frontmatter
A YAML and formatted-markdown frontmatter processor
FrontmatterProc
def FrontmatterProc(
nb
):
A YAML and formatted-markdown frontmatter processor
extract_nb_frontmatter
def extract_nb_frontmatter(
nb, # `Notebook` object to read metadata from
strip:bool=False, # Clear consumed frontmatter cells' `source` in place?
)->dict:
Extract combined YAML and Markdown frontmatter from nb, optionally clearing consumed cells.
YAML frontmatter can be added to notebooks in one of these ways:
- By adding a raw notebook cell with
---as the first and last lines, and YAML between them, or - A markdown cell that is entirely a literal
---block (same as a raw cell), or - A specially formatted markdown cell. The first line should be start with a single
#(creating an H1 heading), and becomes the title. Then, optionally, a line beginning with>(creating a quote block), which becomes the description. Finally, zero or more lines beginning with-(creating a list), each of which contains YAML. (If you already have "title" defined in frontmatter in a raw cell, then markdown cells will be ignored.)
For instance, our test notebook contains the following markdown cell:
# a title
> A description
- key1: value1
- key2: value2
- categories: [c1, c2]
It also contains the following raw cell:
---
execute:
echo: false
---
When we process with FrontmatterProc, these are stripped from the cells and combined into the notebook's frontmatter_ dict:
nbp = NBProcessor(_test_file, procs=FrontmatterProc)
nbp.process()
---
categories:
- c1
- c2
description: A description
execute:
echo: false
key1: value1
key2: value2
output-file: docs_test.html
title: a title
---
d = nbp.nb.frontmatter_
d
{'execute': {'echo': False},
'title': 'a title',
'description': 'A description',
'key1': 'value1',
'key2': 'value2',
'categories': ['c1', 'c2'],
'output-file': 'docs_test.html'}