frontmatter

package module
v0.0.0-...-46863cd Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2020 License: Apache-2.0 Imports: 4 Imported by: 11

README

Build Status GoDoc

Frontmatter

frontmatter is a golang package that provides a Marshaler/Unmarshaler for frontmatter files.

A frontmatter file is a file with any textual content, and a yaml frontmatter block for metadata, as defined by Jekyll

The frontmatter File must have the following format:

  `---\n`
  <yaml content>
  `\n---\n`
  <text content>

Where, the 'yaml content' is handled by http://gopkg.in/yaml.v2

And where, the text content is 'content' field's value.

The 'content' field must:

  exist
  tagged `fm:"content"`
  be exported
  be of the correct type.

A correct type is:

 - string, *string
 - any convertible to the above two.

see go doc for details.

Installation

first get go then go get github.com/ericaro/frontmatter

This package depends on http://gopkg.in/yaml.v2

License

frontmatter is available under the Apache License, Version 2.0.

Branches

master: Build Status against go versions:

  • 1.3
  • 1.4
  • tip

Documentation

Overview

package frontmatter provide a Marshaler/Unmarshaler for frontmatter files.

A frontmatter file is a file with any textual content, and a yaml frontmatter block for metadata, as defined by Jekyll http://jekyllrb.com/docs/frontmatter/

The frontmatter File must have the following format:

`---\n`
<yaml content>
`\n---\n`
<text content>

Where, the 'yaml content' is handled by http://gopkg.in/yaml.v2

And where, the text content is 'content' field's value.

The 'content' field must:

exist
tagged `fm:"content"`
be exported
be of the correct type.

A correct type is:

  • string, *string
  • any convertible to the above two.

See example for details.

Index

Examples

Constants

View Source
const (
	Tag       = "fm"      //Tag used to find the content field
	Content   = "content" // value used to identify the content field
	Header    = "---\n"   // front matter file header
	Separator = "\n---\n" // front matter metadata/content separator
)

Variables

View Source
var (
	ErrMissingSeparator      = errors.New("found a heading '---' without separator '---'")
	ErrUnexported            = errors.New("cannot set an unexported content field")
	ErrNoContentField        = errors.New("missing content field")
	ErrWrongContentFieldType = errors.New("No content field with the right type")
)

Functions

func Marshal

func Marshal(v interface{}) ([]byte, error)

Marshal any object.

Example
v := Example{
	Name:     "toto",
	Variants: []string{"titi", "tutu"},
	Content:  "Hello!",
}

data, err := Marshal(v)
if err != nil {
	fmt.Printf("err! %s", err.Error())
}
fmt.Println(string(data))
Output:

---
name: toto
variants: [titi, tutu]
---
Hello!

func ReadString

func ReadString(v interface{}, tag, tagval string) (txt string, err error)

Read a String out of a field identified by tag/tagvalue pair

func Unmarshal

func Unmarshal(data []byte, v interface{}) (err error)

Unmarshal any object from 'data'.

Example
data := `---
name: toto
variants: [titi, tutu]
---
Hello!`

v := new(Example)
err := Unmarshal(([]byte)(data), v)
if err != nil {
	fmt.Printf("err! %s", err.Error())
}

if v.Name == "toto" && v.Variants[0] == "titi" && v.Variants[1] == "tutu" && v.Content == "Hello!" {
	fmt.Println("Ok")
} else {
	fmt.Printf("%v, %v, %v, %v, %v\n", v, v.Name == "toto", v.Variants[0] == "titi", v.Variants[1] == "tutu", v.Content == "Hello!")
}
Output:

Ok

func WriteString

func WriteString(v interface{}, tag, tagval, txt string) error

WriteString set 'txt' on the field tagged with 'tag'

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL