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 ¶
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 ¶
Read a String out of a field identified by tag/tagvalue pair
func Unmarshal ¶
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 ¶
WriteString set 'txt' on the field tagged with 'tag'
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.