chore: removes the tj/front dependency

Removes the `tj/front` dependency due to licensing concerns. See #490.
This commit is contained in:
Chris Lane 2019-11-06 06:27:02 -05:00
parent 197ff58796
commit 51aaaf3423
7 changed files with 110 additions and 56 deletions

3
go.mod
View File

@ -9,7 +9,6 @@ require (
github.com/mattn/go-isatty v0.0.10 github.com/mattn/go-isatty v0.0.10
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b
github.com/mitchellh/go-homedir v1.1.0 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
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0 // indirect
gopkg.in/yaml.v2 v2.2.4 gopkg.in/yaml.v2 v2.2.4
) )

2
go.sum
View File

@ -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.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 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/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35 h1:YAFjXN64LMvktoUZH9zgY4lGc/msGN7HQfoSuKCgaDU= golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35 h1:YAFjXN64LMvktoUZH9zgY4lGc/msGN7HQfoSuKCgaDU=

View File

@ -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
}

View File

@ -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))
}
}

View File

@ -4,17 +4,10 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"sort" "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 // Sheet encapsulates sheet information
type Sheet struct { type Sheet struct {
Title string Title string
@ -39,9 +32,8 @@ func New(
return Sheet{}, fmt.Errorf("failed to read file: %s, %v", path, err) return Sheet{}, fmt.Errorf("failed to read file: %s, %v", path, err)
} }
// parse the front-matter // parse the cheatsheet frontmatter
var fm frontmatter text, fm, err := frontmatter.Parse(string(markdown))
text, err := front.Unmarshal(markdown, &fm)
if err != nil { if err != nil {
return Sheet{}, fmt.Errorf("failed to parse front-matter: %v", err) return Sheet{}, fmt.Errorf("failed to parse front-matter: %v", err)
} }
@ -56,7 +48,7 @@ func New(
return Sheet{ return Sheet{
Title: title, Title: title,
Path: path, Path: path,
Text: strings.TrimSpace(string(text)) + "\n", Text: text + "\n",
Tags: tags, Tags: tags,
Syntax: fm.Syntax, Syntax: fm.Syntax,
ReadOnly: readOnly, ReadOnly: readOnly,

16
vendor/github.com/tj/front/Readme.md generated vendored
View File

@ -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)

24
vendor/github.com/tj/front/front.go generated vendored
View File

@ -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
}