Parse and manage posts with YAML (or other) frontmatter
Project description
Python Frontmatter
Jekyll-style YAML front matter offers a useful way to add arbitrary, structured metadata to text documents, regardless of type.
This is a small package to load and parse files (or just text) with YAML (or JSON, TOML or other) front matter.
Install:
pip install python-frontmatter
Usage:
>>> import frontmatter
Load a post from a filename:
>>> post = frontmatter.load('tests/yaml/hello-world.txt')
Or a file (or file-like object):
>>> with open('tests/yaml/hello-world.txt') as f:
... post = frontmatter.load(f)
Or load from text:
>>> with open('tests/yaml/hello-world.txt') as f:
... post = frontmatter.loads(f.read())
If the file has a Byte-Order Mark (BOM), strip it off first. An easy way to do this is by using the utf-8-sig
encoding:
>>> with open('tests/yaml/hello-world.txt', encoding="utf-8-sig") as f:
... post = frontmatter.load(f)
Access content:
>>> print(post.content)
Well, hello there, world.
# this works, too
>>> print(post)
Well, hello there, world.
Use metadata (metadata gets proxied as post keys):
>>> print(post['title'])
Hello, world!
Metadata is a dictionary, with some handy proxies:
>>> sorted(post.keys())
['layout', 'title']
>>> from pprint import pprint
>>> post['excerpt'] = 'tl;dr'
>>> pprint(post.metadata)
{'excerpt': 'tl;dr', 'layout': 'post', 'title': 'Hello, world!'}
If you don't need the whole post object, just parse:
>>> with open('tests/yaml/hello-world.txt') as f:
... metadata, content = frontmatter.parse(f.read())
>>> print(metadata['title'])
Hello, world!
Write back to plain text, too:
>>> print(frontmatter.dumps(post)) # doctest: +NORMALIZE_WHITESPACE
---
excerpt: tl;dr
layout: post
title: Hello, world!
---
Well, hello there, world.
Or write to a file (or file-like object):
>>> from io import BytesIO
>>> f = BytesIO()
>>> frontmatter.dump(post, f)
>>> print(f.getvalue().decode('utf-8')) # doctest: +NORMALIZE_WHITESPACE
---
excerpt: tl;dr
layout: post
title: Hello, world!
---
Well, hello there, world.
For more examples, see files in the tests/
directory. Each sample file has a corresponding .result.json
file showing the expected parsed output. See also the examples/
directory, which covers more ways to customize input and output.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file python-frontmatter-1.1.0.tar.gz
.
File metadata
- Download URL: python-frontmatter-1.1.0.tar.gz
- Upload date:
- Size: 16.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7118d2bd56af9149625745c58c9b51fb67e8d1294a0c76796dafdc72c36e5f6d |
|
MD5 | f0d26bc7456a2ad868a6b5e5df2d6ee4 |
|
BLAKE2b-256 | 96de910fa208120314a12f9a88ea63e03707261692af782c99283f1a2c8a5e6f |
File details
Details for the file python_frontmatter-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: python_frontmatter-1.1.0-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 335465556358d9d0e6c98bbeb69b1c969f2a4a21360587b9873bfc3b213407c1 |
|
MD5 | d96feb205fa163c7eacf5cf5ccb40c36 |
|
BLAKE2b-256 | 49873c8da047b3ec5f99511d1b4d7a5bc72d4b98751c7e78492d14dc736319c5 |