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)

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.

Project details


Download files

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

Source Distribution

python-frontmatter-0.1.1.tar.gz (4.6 kB view details)

Uploaded Source

File details

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

File metadata

File hashes

Hashes for python-frontmatter-0.1.1.tar.gz
Algorithm Hash digest
SHA256 dafe2ac8332cabbdcf659f32d297093db4a4fe88e0dfe24acbc07b5082bb41b7
MD5 ff57741b4141fbe1981e8df0340d2607
BLAKE2b-256 cd487f877ba08783445594b36382798dd02527efa8fa992a2e80cdc57659965c

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