<repo_name>camoy/fm<file_sep>/README.md
# fm

`fm` extracts or strips frontmatter data using `sed`.

## Usage

Given the following file in `example.md`.

	---
	title: Example
	...

	Whereof one cannot speak, thereof one must be silent.

It is possible to extract the frontmatter data.

	cat example.md | fm

The default document marker style is `---` to `...` and the
default operation is extraction.

* `-s` strips the frontmatter leaving only the body.
* `-d` uses the `---` to `---` document marker style.
* `-p` uses the `+++` to `+++` document marker style.

## Installing

Install the script to a directory in `$PATH` like `/usr/local/bin`.
You may need to run as root.

	wget https://raw.github.com/camoy/fm/master/fm -O /usr/local/bin/fm
	chmod +x /usr/local/bin/fm

## Legal

This software is in the public domain. Anyone is free to use and
distribute it for any purpose.

`fm` is maintained by <NAME>.
<file_sep>/fm
#!/bin/bash

extract=1
style=0

_fm_extract() {
	case $style in
		0) sed -n "/^---$/,/^\.\.\.$/p" | sed "1d;$ d";;
		1) sed -n "/^---$/,/^---$/p" | sed "1d;$ d";;
		2) sed -n "/^+++$/,/^+++$/p" | sed "1d;$ d";;
	esac
}

_fm_strip() {
	case $style in
		0) sed "/^---$/,/^\.\.\.$/d";;
		1) sed "/^---$/,/^---$/d";;
		2) sed "/^+++$/,/^+++$/d";;
	esac
}

while getopts 'sdp' flag; do
	case "${flag}" in
		s) extract=0 ;;
		d) style=1 ;;
		p) style=2 ;;
	esac
done

if [ $extract -eq 1 ]
then
	_fm_extract
else
	_fm_strip
fi

exit 0
