Parse and manage posts with YAML frontmatter
Project description
[Jekyll](http://jekyllrb.com/)-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 front matter.
[](https://travis-ci.org/eyeseast/python-frontmatter)
Usage:
>>> import frontmatter
Load a post from a filename:
>>> post = frontmatter.load('tests/hello-world.markdown')
Or a file (or file-like object):
>>> with open('tests/hello-world.markdown') as f:
... post = frontmatter.load(f)
Or load from text:
>>> with open('tests/hello-world.markdown') as f:
... post = frontmatter.loads(f.read())
Access content:
>>> print post.content 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:
>>> post.keys() ['layout', 'title']>>> post['excerpt'] = 'tl;dr' >>> post.metadata {'excerpt': 'tl;dr', 'layout': 'post', 'title': 'Hello, world!'}
If you don’t need the whole post object, just parse:
>>> with open('tests/hello-world.markdown') 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 cStringIO import StringIO >>> f = StringIO() >>> frontmatter.dump(post, f) >>> print f.getvalue() # doctest: +NORMALIZE_WHITESPACE --- excerpt: tl;dr layout: post title: Hello, world! --- Well, hello there, world.
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
File details
Details for the file python-frontmatter-0.1.2.tar.gz.
File metadata
- Download URL: python-frontmatter-0.1.2.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aadc02f1d6d065bef46d565ddd13620a17a6fe20161321367930c0a58494a74e
|
|
| MD5 |
eaa1ab58ff88d7dae8d382fe0df46da3
|
|
| BLAKE2b-256 |
73261a6e7f8a9360e6a1173ef3dfcca449498ec6ac374534990a28fccc098407
|