Process a directory of files with frontmatter and middleware
Project description
# Metalsmyth
[![Build Status](https://travis-ci.org/eyeseast/python-metalsmyth.svg)](https://travis-ci.org/eyeseast/python-metalsmyth)
This is a little library to process a directory of files with a stack of middleware. It is based on [metalsmith](http://www.metalsmith.io/), which is built in and for Node. This version uses the same three-step process:
1. Read all the files in a source directory.
2. Invoke a series of plugins that manipulate the files.
3. Write the results to a destination directory (or do something else!)
Each plugin is simply a callable that takes a dictionary of files, plus a `Stack` instance, and does something. It doesn't actually have to operate on (or return) the files. Each file is parsed for YAML Frontmatter, with file paths as keys (relative to the source directory).
## How is this useful?
I build news applications and interactive stories for a living, often working with other journalists who write text, gather photos and edit video. That content needs to be formatted, cleaned up, organized, combined with other data and whatnot. Then it needs to be put online.
Metalsmyth is a way of organizing that processing pipeline, from raw text to HTML (or other formats). I'm building it with the intention of using it with [Tarbell](http://tarbell.io) or other static site generators. This can also work as a generator on its own.
A few plugins are included by default:
- drafts: filter out posts where `draft` is `true`
- dates: convert a date field to a Python `datetime.datetime` object
- markdown: convert post content to HTML using markdown
- bleach: run `bleach.clean` on post content
- linkify: run `bleach.linkify` on post content
## Install
$ pip install metalsmyth
By itself, Metalsmyth only needs [Python Frontmatter][fm], which itself relies on [PyYAML][]. If you want to use the bundled plugins, you'll need a few extra libraries:
$ pip install markdown # for markdown plugin
$ pip install bleach # for bleach and linkify plugins
$ pip install jinja2 # for jinja template plugin
$ pip install python-dateutil # for dates plugin
[fm]: https://github.com/eyeseast/python-frontmatter
[PyYAML]: http://pyyaml.org
## Usage
```python
from metalsmyth import Stack
from metalsmyth.plugins.dates import Dates
from metalsmyth.plugins.markup import Bleach, Markdown
# create a stack with a source directory and middleware
stack = Stack('tests/markup',
Dates('date'),
Bleach(strip=True),
Markdown(output_format='html5')
)
# get processed files
files = stack.run()
# or build to a destination directory
stack.build('tests/tmp')
```
### Adding middleware
If you know all the middleware functions you'll be using when you create a new stack,
just list them as positional arguments when you initialize.
```python
stack = Stack('src', Markdown(), Jinja())
```
In addition, there's a `stack.use` decorator that will push new funcions onto the end of the stack.
This is useful for one-off plugins that don't need any configuration. For (a totally contrived) example,
a specific project might need to ensure that the `stack` instance always knows how many files it's dealing with.
```python
# define a function and push it onto the stack
@stack.use
def count_files(files, stack):
stack.metadata['count'] = len(files)
# or use pre-defined middleware callables
stack.use(Markdown())
stack.use(Jinja())
stack.use(some_other_function)
```
Finally, `Stack.middleware` is just a list, so you can edit it any way you'd edit a list.
```python
stack = Stack('src')
stack.middleware = [
Bleach(),
Markdown(output_format='html5')
]
stack.middleware.append(Jinja())
```
[![Build Status](https://travis-ci.org/eyeseast/python-metalsmyth.svg)](https://travis-ci.org/eyeseast/python-metalsmyth)
This is a little library to process a directory of files with a stack of middleware. It is based on [metalsmith](http://www.metalsmith.io/), which is built in and for Node. This version uses the same three-step process:
1. Read all the files in a source directory.
2. Invoke a series of plugins that manipulate the files.
3. Write the results to a destination directory (or do something else!)
Each plugin is simply a callable that takes a dictionary of files, plus a `Stack` instance, and does something. It doesn't actually have to operate on (or return) the files. Each file is parsed for YAML Frontmatter, with file paths as keys (relative to the source directory).
## How is this useful?
I build news applications and interactive stories for a living, often working with other journalists who write text, gather photos and edit video. That content needs to be formatted, cleaned up, organized, combined with other data and whatnot. Then it needs to be put online.
Metalsmyth is a way of organizing that processing pipeline, from raw text to HTML (or other formats). I'm building it with the intention of using it with [Tarbell](http://tarbell.io) or other static site generators. This can also work as a generator on its own.
A few plugins are included by default:
- drafts: filter out posts where `draft` is `true`
- dates: convert a date field to a Python `datetime.datetime` object
- markdown: convert post content to HTML using markdown
- bleach: run `bleach.clean` on post content
- linkify: run `bleach.linkify` on post content
## Install
$ pip install metalsmyth
By itself, Metalsmyth only needs [Python Frontmatter][fm], which itself relies on [PyYAML][]. If you want to use the bundled plugins, you'll need a few extra libraries:
$ pip install markdown # for markdown plugin
$ pip install bleach # for bleach and linkify plugins
$ pip install jinja2 # for jinja template plugin
$ pip install python-dateutil # for dates plugin
[fm]: https://github.com/eyeseast/python-frontmatter
[PyYAML]: http://pyyaml.org
## Usage
```python
from metalsmyth import Stack
from metalsmyth.plugins.dates import Dates
from metalsmyth.plugins.markup import Bleach, Markdown
# create a stack with a source directory and middleware
stack = Stack('tests/markup',
Dates('date'),
Bleach(strip=True),
Markdown(output_format='html5')
)
# get processed files
files = stack.run()
# or build to a destination directory
stack.build('tests/tmp')
```
### Adding middleware
If you know all the middleware functions you'll be using when you create a new stack,
just list them as positional arguments when you initialize.
```python
stack = Stack('src', Markdown(), Jinja())
```
In addition, there's a `stack.use` decorator that will push new funcions onto the end of the stack.
This is useful for one-off plugins that don't need any configuration. For (a totally contrived) example,
a specific project might need to ensure that the `stack` instance always knows how many files it's dealing with.
```python
# define a function and push it onto the stack
@stack.use
def count_files(files, stack):
stack.metadata['count'] = len(files)
# or use pre-defined middleware callables
stack.use(Markdown())
stack.use(Jinja())
stack.use(some_other_function)
```
Finally, `Stack.middleware` is just a list, so you can edit it any way you'd edit a list.
```python
stack = Stack('src')
stack.middleware = [
Bleach(),
Markdown(output_format='html5')
]
stack.middleware.append(Jinja())
```
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
metalsmyth-0.1.6.zip
(22.9 kB
view details)
metalsmyth-0.1.6.tar.gz
(11.9 kB
view details)
File details
Details for the file metalsmyth-0.1.6.zip
.
File metadata
- Download URL: metalsmyth-0.1.6.zip
- Upload date:
- Size: 22.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8d7179d03dc5842cc72b1670806000527a4db1befaf4b8bab2b5cbfd55b28e7 |
|
MD5 | 8e80185392fcdc7ea9ab4db6e96f0fe0 |
|
BLAKE2b-256 | bb31561817b77a09e99973c48a8d377246663c5b18d55bdc7ca1b48ccc281881 |
File details
Details for the file metalsmyth-0.1.6.tar.gz
.
File metadata
- Download URL: metalsmyth-0.1.6.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dbce25a6e1f5936b0e71e6427dbfe3946707104138bc3598f696bc11570753ff |
|
MD5 | f55ebdc28e1d0b29fa07c091ccaaa29a |
|
BLAKE2b-256 | 5e131f2693f990dedb50a03b8c5a4f888fa97fd88c3942d037bc443336b1df93 |