Frontmatter Documentation, Release 1.0.0
By default, split based on +++.
2.2 API
2.2.1 Reading
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/yaml/hello-world.txt') as f:
... metadata, content = frontmatter.parse(f.read())
>>> print(metadata['title'])
Hello, world!
frontmatter.check(fd, encoding=’utf-8’)
Check if a file-like object or filename has a frontmatter, return True if exists, False otherwise.
If it contains a frontmatter but it is empty, return True as well.
>>> frontmatter.check('tests/yaml/hello-world.txt')
True
frontmatter.checks(text, encoding=’utf-8’)
Check if a text (binary or unicode) has a frontmatter, return True if exists, False otherwise.
If it contains a frontmatter but it is empty, return True as well.
>>> with open('tests/yaml/hello-world.txt') as f:
... frontmatter.checks(f.read())
True
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/yaml/hello-world.txt')
>>> with open('tests/yaml/hello-world.txt') 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/yaml/hello-world.txt') as f:
... post = frontmatter.loads(f.read())
2.2.2 Writing
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
>>> post = frontmatter.load('tests/yaml/hello-world.txt')
>>> f = BytesIO()
(continues on next page)
2.2. API 9