Skip to content

Commit

Permalink
Added YAML support
Browse files Browse the repository at this point in the history
  • Loading branch information
gernest committed Aug 12, 2015
1 parent 784a3fc commit 3065608
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ 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

Expand Down
12 changes: 12 additions & 0 deletions front.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"errors"
"io"
"strings"

"gopkg.in/yaml.v2"
)

var (
Expand Down Expand Up @@ -124,3 +126,13 @@ func JSONHandler(front string) (map[string]interface{}, error) {
}
return rst.(map[string]interface{}), nil
}

//YAMLHandler decodes ymal string into a go map[string]interface{}
func YAMLHandler(front string) (map[string]interface{}, error) {
out := make(map[string]interface{})
err := yaml.Unmarshal([]byte(front), out)
if err != nil {
return nil, err
}
return out, nil
}
14 changes: 14 additions & 0 deletions front_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,17 @@ func TestMatter(t *testing.T) {
t.Error("expected front matter to contain title got nil instead")
}
}

func TestYAMLHandler(t *testing.T) {
data, err := ioutil.ReadFile("testdata/sample.yml")
if err != nil {
t.Fatal(err)
}
f, err := YAMLHandler(string(data))
if err != nil {
t.Errorf("handling yaml %v", err)
}
if _, ok := f["language"]; !ok {
t.Errorf("expected language got nil instead")
}
}
12 changes: 12 additions & 0 deletions testdata/sample.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: go
go:
- 1.2
- 1.3
- release
- tip
before_install:
- go get github.com/axw/gocov/gocov
- go get github.com/mattn/goveralls
- if ! go get code.google.com/p/go.tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi
script:
- $HOME/gopath/bin/goveralls -service=travis-ci -repotoken=$COVERALLS

0 comments on commit 3065608

Please sign in to comment.