From 51aaaf3423763f915fa46c78b9cd11fc5e0e5540 Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Wed, 6 Nov 2019 06:27:02 -0500 Subject: [PATCH] chore: removes the `tj/front` dependency Removes the `tj/front` dependency due to licensing concerns. See #490. --- go.mod | 3 +- go.sum | 2 - internal/frontmatter/frontmatter.go | 34 ++++++++++++ internal/frontmatter/frontmatter_test.go | 71 ++++++++++++++++++++++++ internal/sheet/sheet.go | 16 ++---- vendor/github.com/tj/front/Readme.md | 16 ------ vendor/github.com/tj/front/front.go | 24 -------- 7 files changed, 110 insertions(+), 56 deletions(-) create mode 100644 internal/frontmatter/frontmatter.go create mode 100644 internal/frontmatter/frontmatter_test.go delete mode 100644 vendor/github.com/tj/front/Readme.md delete mode 100644 vendor/github.com/tj/front/front.go diff --git a/go.mod b/go.mod index b909caa..4b0eb06 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,6 @@ require ( github.com/mattn/go-isatty v0.0.10 github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b github.com/mitchellh/go-homedir v1.1.0 - github.com/tj/front v0.0.0-20170212063142-739be213b0a1 - gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0 // indirect + gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0 gopkg.in/yaml.v2 v2.2.4 ) diff --git a/go.sum b/go.sum index 293f08d..3722200 100644 --- a/go.sum +++ b/go.sum @@ -50,8 +50,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/tj/front v0.0.0-20170212063142-739be213b0a1 h1:lA+aPRvltlx2fwv/BnxyYSDQo3pIeqzHgMO5GvK0T9E= -github.com/tj/front v0.0.0-20170212063142-739be213b0a1/go.mod h1:deJrtusCTptAW4EUn5vBLpl3dhNqPqUwEjWJz5UNxpQ= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35 h1:YAFjXN64LMvktoUZH9zgY4lGc/msGN7HQfoSuKCgaDU= diff --git a/internal/frontmatter/frontmatter.go b/internal/frontmatter/frontmatter.go new file mode 100644 index 0000000..b3e7813 --- /dev/null +++ b/internal/frontmatter/frontmatter.go @@ -0,0 +1,34 @@ +package frontmatter + +import ( + "strings" + + "gopkg.in/yaml.v1" +) + +// Frontmatter encapsulates cheatsheet frontmatter data +type Frontmatter struct { + Tags []string + Syntax string +} + +// Parse parses cheatsheet frontmatter +func Parse(markdown string) (string, Frontmatter, error) { + + // specify the frontmatter delimiter + delim := "---" + + // initialize a frontmatter struct + var fm Frontmatter + + // if the markdown does not contain frontmatter, pass it through unmodified + if !strings.HasPrefix(markdown, delim) { + return strings.TrimSpace(markdown), fm, nil + } + + // otherwise, split the frontmatter and cheatsheet text + parts := strings.SplitN(markdown, delim, 3) + err := yaml.Unmarshal([]byte(parts[1]), &fm) + + return strings.TrimSpace(parts[2]), fm, err +} diff --git a/internal/frontmatter/frontmatter_test.go b/internal/frontmatter/frontmatter_test.go new file mode 100644 index 0000000..d6c8e32 --- /dev/null +++ b/internal/frontmatter/frontmatter_test.go @@ -0,0 +1,71 @@ +package frontmatter + +import ( + "testing" +) + +// TestHasFrontmatter asserts that markdown is properly parsed when it contains +// frontmatter +func TestHasFrontmatter(t *testing.T) { + + // stub our cheatsheet content + markdown := `--- +syntax: go +tags: [ test ] +--- +To foo the bar: baz` + + // parse the frontmatter + text, fm, err := Parse(markdown) + + // assert expectations + if err != nil { + t.Errorf("failed to parse markdown: %v", err) + } + + want := "To foo the bar: baz" + if text != want { + t.Errorf("failed to parse text: want: %s, got: %s", want, text) + } + + want = "go" + if fm.Syntax != want { + t.Errorf("failed to parse syntax: want: %s, got: %s", want, fm.Syntax) + } + + want = "test" + if fm.Tags[0] != want { + t.Errorf("failed to parse tags: want: %s, got: %s", want, fm.Tags[0]) + } + if len(fm.Tags) != 1 { + t.Errorf("failed to parse tags: want: len 0, got: len %d", len(fm.Tags)) + } +} + +// TestHasFrontmatter asserts that markdown is properly parsed when it does not +// contain frontmatter +func TestHasNoFrontmatter(t *testing.T) { + + // stub our cheatsheet content + markdown := "To foo the bar: baz" + + // parse the frontmatter + text, fm, err := Parse(markdown) + + // assert expectations + if err != nil { + t.Errorf("failed to parse markdown: %v", err) + } + + if text != markdown { + t.Errorf("failed to parse text: want: %s, got: %s", markdown, text) + } + + if fm.Syntax != "" { + t.Errorf("failed to parse syntax: want: '', got: %s", fm.Syntax) + } + + if len(fm.Tags) != 0 { + t.Errorf("failed to parse tags: want: len 0, got: len %d", len(fm.Tags)) + } +} diff --git a/internal/sheet/sheet.go b/internal/sheet/sheet.go index 45f6b9a..9d6ade1 100644 --- a/internal/sheet/sheet.go +++ b/internal/sheet/sheet.go @@ -4,17 +4,10 @@ import ( "fmt" "io/ioutil" "sort" - "strings" - "github.com/tj/front" + "github.com/cheat/cheat/internal/frontmatter" ) -// frontmatter is an un-exported helper struct used in parsing cheatsheets -type frontmatter struct { - Tags []string - Syntax string -} - // Sheet encapsulates sheet information type Sheet struct { Title string @@ -39,9 +32,8 @@ func New( return Sheet{}, fmt.Errorf("failed to read file: %s, %v", path, err) } - // parse the front-matter - var fm frontmatter - text, err := front.Unmarshal(markdown, &fm) + // parse the cheatsheet frontmatter + text, fm, err := frontmatter.Parse(string(markdown)) if err != nil { return Sheet{}, fmt.Errorf("failed to parse front-matter: %v", err) } @@ -56,7 +48,7 @@ func New( return Sheet{ Title: title, Path: path, - Text: strings.TrimSpace(string(text)) + "\n", + Text: text + "\n", Tags: tags, Syntax: fm.Syntax, ReadOnly: readOnly, diff --git a/vendor/github.com/tj/front/Readme.md b/vendor/github.com/tj/front/Readme.md deleted file mode 100644 index 6789165..0000000 --- a/vendor/github.com/tj/front/Readme.md +++ /dev/null @@ -1,16 +0,0 @@ -# Front - -Frontmatter unmarshaller, couldn't find one without a weird API. - -## Badges - -[![GoDoc](https://godoc.org/github.com/tj/front?status.svg)](https://godoc.org/github.com/tj/front) -![](https://img.shields.io/badge/license-MIT-blue.svg) -![](https://img.shields.io/badge/status-stable-green.svg) -[![](http://apex.sh/images/badge.svg)](https://apex.sh/ping/) - ---- - -> [tjholowaychuk.com](http://tjholowaychuk.com)  ·  -> GitHub [@tj](https://github.com/tj)  ·  -> Twitter [@tjholowaychuk](https://twitter.com/tjholowaychuk) diff --git a/vendor/github.com/tj/front/front.go b/vendor/github.com/tj/front/front.go deleted file mode 100644 index e0382b5..0000000 --- a/vendor/github.com/tj/front/front.go +++ /dev/null @@ -1,24 +0,0 @@ -// Package front provides YAML frontmatter unmarshalling. -package front - -import ( - "bytes" - - "gopkg.in/yaml.v1" -) - -// Delimiter. -var delim = []byte("---") - -// Unmarshal parses YAML frontmatter and returns the content. When no -// frontmatter delimiters are present the original content is returned. -func Unmarshal(b []byte, v interface{}) (content []byte, err error) { - if !bytes.HasPrefix(b, delim) { - return b, nil - } - - parts := bytes.SplitN(b, delim, 3) - content = parts[2] - err = yaml.Unmarshal(parts[1], v) - return -}