Skip to main content

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.

[![Build Status](https://travis-ci.org/eyeseast/python-frontmatter.svg?branch=master)](https://travis-ci.org/eyeseast/python-frontmatter)

Install:

pip install 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.

# 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/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 io 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

python-frontmatter-0.3.1.zip (12.9 kB view details)

Uploaded Source

python-frontmatter-0.3.1.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file python-frontmatter-0.3.1.zip.

File metadata

File hashes

Hashes for python-frontmatter-0.3.1.zip
Algorithm Hash digest
SHA256 c9cec59860647837b4251d564031b67db08e45dace7f8835ca02388870c0f448
MD5 9e5623ea344962d245388aff8241aaf4
BLAKE2b-256 42928c59c638d0e9e9d6120b5866bea11c8b75070ead5690ac943c53642c5dfc

See more details on using hashes here.

File details

Details for the file python-frontmatter-0.3.1.tar.gz.

File metadata

File hashes

Hashes for python-frontmatter-0.3.1.tar.gz
Algorithm Hash digest
SHA256 614d255d2ea34280393fd4c24a9d75d510adb73aba453c1687c77ea75af0caa9
MD5 6aa88cd6e3610edabedccd7e775f75c4
BLAKE2b-256 06235abf0a3a3aa490cd2ef77c2448c32dde7dd5d870825e767465ca079ed39b

See more details on using hashes here.

File details

Details for the file python-frontmatter-0.3.1.macosx-10.10-intel.tar.gz.

File metadata

File hashes

Hashes for python-frontmatter-0.3.1.macosx-10.10-intel.tar.gz
Algorithm Hash digest
SHA256 058312da2ac69a604f5643426e2bbce1fbd7731815d075abfe79603e15906763
MD5 b810350f2d1b2687dc20085179166418
BLAKE2b-256 720dc13fd48e0f3c8c202259e9640769036b8a51be743886bd3efca99ab1956b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page