front

package module
Version: v0.0.0-...-8a0b0a7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2021 License: MIT Imports: 7 Imported by: 13

README

front Build Status GoDocCoverage Status

extracts frontmatter from text files with ease.

Features

  • Custom delimiters. You are free to register any delimiter of your choice. Provided its a three character string. e.g +++, $$$, ---, %%%
  • Custom Handlers. Anything that implements HandlerFunc can be used to decode the values from the frontmatter text, you can see the JSONHandler for how to implement one.
  • Support YAML frontmatter
  • Support JSON frontmatter.

Installation

go get github.com/gernest/front

How to use

package main

import (
	"fmt"
	"strings"

	"github.com/gernest/front"
)

var txt = `+++
{
    "title":"front"
}
+++

# Body
Over my dead body
`

func main() {
	m := front.NewMatter()
	m.Handle("+++", front.JSONHandler)
	f, body, err := m.Parse(strings.NewReader(txt))
	if err != nil {
		panic(err)
	}

	fmt.Printf("The front matter is:\n%#v\n", f)
	fmt.Printf("The body is:\n%q\n", body)
}

Please see the tests formore details

Licence

This project is under the MIT Licence. See the LICENCE file for the full license text.

Documentation

Overview

Package front is a frontmatter extraction library.

Index

Constants

This section is empty.

Variables

View Source
var (
	//ErrIsEmpty is an error indicating no front matter was found
	ErrIsEmpty = errors.New("front: an empty file")

	//ErrUnknownDelim is returned when the delimiters are not known by the
	//FrontMatter implementation.
	ErrUnknownDelim = errors.New("front: unknown delim")
)

Functions

func JSONHandler

func JSONHandler(front string) (map[string]interface{}, error)

JSONHandler implements HandlerFunc interface. It extracts front matter data from the given string argument by interpreting it as a json string.

func YAMLHandler

func YAMLHandler(front string) (map[string]interface{}, error)

YAMLHandler decodes ymal string into a go map[string]interface{}

Types

type HandlerFunc

type HandlerFunc func(string) (map[string]interface{}, error)

HandlerFunc is an interface for a function that process front matter text.

type Matter

type Matter struct {
	// contains filtered or unexported fields
}

Matter is all what matters here.

func NewMatter

func NewMatter() *Matter

NewMatter creates a new Matter instance

func (*Matter) Handle

func (m *Matter) Handle(delim string, fn HandlerFunc)

Handle registers a handler for the given frontmatter delimiter

func (*Matter) Parse

func (m *Matter) Parse(input io.Reader) (front map[string]interface{}, body string, err error)

Parse parses the input and extract the frontmatter

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL