frontmatter.parse(text, encoding='utf-8', handler=None, **defaults)

Parse text with frontmatter, return metadata and content. Pass in optional metadata defaults as keyword args.

If frontmatter is not found, returns an empty metadata dictionary (or defaults) and original text content.

>>> with open('tests/hello-world.markdown') as f:
...     metadata, content = frontmatter.parse(f.read())
>>> print(metadata['title'])
Hello, world!
frontmatter.load(fd, encoding='utf-8', handler=None, **defaults)

Load and parse a file-like object or filename, return a post.

>>> post = frontmatter.load('tests/hello-world.markdown')
>>> with open('tests/hello-world.markdown') as f:
...     post = frontmatter.load(f)
frontmatter.loads(text, encoding='utf-8', handler=None, **defaults)

Parse text (binary or unicode) and return a post.

>>> with open('tests/hello-world.markdown') as f:
...     post = frontmatter.loads(f.read())


frontmatter.dump(post, fd, encoding='utf-8', handler=None, **kwargs)

Serialize post to a string and write to a file-like object. Text will be encoded on the way out (utf-8 by default).

>>> from io import BytesIO
>>> f = BytesIO()
>>> frontmatter.dump(post, f)
>>> print(f.getvalue())
excerpt: tl;dr
layout: post
title: Hello, world!
Well, hello there, world.
frontmatter.dumps(post, handler=None, **kwargs)

Serialize a post to a string and return text. This always returns unicode text, which can then be encoded.

Passing handler will change how metadata is turned into text. A handler passed as an argument will override post.handler, with YAMLHandler used as a default.

>>> print(frontmatter.dumps(post))
excerpt: tl;dr
layout: post
title: Hello, world!
Well, hello there, world.

Post objects

class frontmatter.Post(content, handler=None, **metadata)

A post contains content and metadata from Front Matter. This is what gets returned by load and loads. Passing this to dump or dumps will turn it back into text.

For convenience, metadata values are available as proxied item lookups.


Delete a metadata key


Get metadata key

__setitem__(name, value)

Set a metadata key

get(key, default=None)

Get a key, fallback to default


Return metadata keys


Post as a dict, for serializing


Return metadata values


class frontmatter.default_handlers.BaseHandler(fm_boundary=None, start_delimiter=None, end_delimiter=None)

BaseHandler lays out all the steps to detecting, splitting, parsing and exporting front matter metadata.

All default handlers are subclassed from BaseHandler.


Decide whether this handler can parse the given text, and return True or False.

Note that this is not called when passing a handler instance to frontmatter.load or loads.

export(metadata, **kwargs)

Turn metadata back into text


Parse frontmatter and return a dict


Split text into frontmatter and content

class frontmatter.default_handlers.YAMLHandler(fm_boundary=None, start_delimiter=None, end_delimiter=None)

Load and export YAML metadata. By default, this handler uses YAML’s “safe” mode, though it’s possible to override that.

class frontmatter.default_handlers.JSONHandler(fm_boundary=None, start_delimiter=None, end_delimiter=None)

Load and export JSON metadata.

Note that changing START_DELIMITER or END_DELIMITER may break JSON parsing.

class frontmatter.default_handlers.TOMLHandler(fm_boundary=None, start_delimiter=None, end_delimiter=None)

Load and export TOML metadata.

By default, split based on +++.