refactor(Sheet): create parse method

Move `Frontmatter.Parse` to `Sheet.parse`, and delete the `frontmatter`
package. `Sheet.parse` more accurately describes the parser's behavior.
This commit is contained in:
Christopher Allen Lane 2022-08-26 12:20:58 -04:00
parent f0bfeda47a
commit 2d635293c5
3 changed files with 19 additions and 21 deletions

View File

@ -1,4 +1,4 @@
package frontmatter
package sheet
import (
"fmt"
@ -8,14 +8,8 @@ import (
"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) {
func parse(markdown string) (frontmatter, string, error) {
// determine the appropriate line-break for the platform
linebreak := "\n"
@ -27,11 +21,11 @@ func Parse(markdown string) (string, Frontmatter, error) {
delim := fmt.Sprintf("---%s", linebreak)
// initialize a frontmatter struct
var fm Frontmatter
var fm frontmatter
// if the markdown does not contain frontmatter, pass it through unmodified
if !strings.HasPrefix(markdown, delim) {
return markdown, fm, nil
return fm, markdown, nil
}
// otherwise, split the frontmatter and cheatsheet text
@ -39,13 +33,13 @@ func Parse(markdown string) (string, Frontmatter, error) {
// return an error if the frontmatter parses into the wrong number of parts
if len(parts) != 3 {
return markdown, fm, fmt.Errorf("failed to delimit frontmatter")
return fm, markdown, fmt.Errorf("failed to delimit frontmatter")
}
// return an error if the YAML cannot be unmarshalled
if err := yaml.Unmarshal([]byte(parts[1]), &fm); err != nil {
return markdown, fm, fmt.Errorf("failed to unmarshal frontmatter: %v", err)
return fm, markdown, fmt.Errorf("failed to unmarshal frontmatter: %v", err)
}
return parts[2], fm, nil
return fm, parts[2], nil
}

View File

@ -1,4 +1,4 @@
package frontmatter
package sheet
import (
"testing"
@ -16,7 +16,7 @@ tags: [ test ]
To foo the bar: baz`
// parse the frontmatter
text, fm, err := Parse(markdown)
fm, text, err := parse(markdown)
// assert expectations
if err != nil {
@ -50,7 +50,7 @@ func TestHasNoFrontmatter(t *testing.T) {
markdown := "To foo the bar: baz"
// parse the frontmatter
text, fm, err := Parse(markdown)
fm, text, err := parse(markdown)
// assert expectations
if err != nil {
@ -81,7 +81,7 @@ tags: [ test ]
To foo the bar: baz`
// parse the frontmatter
text, _, err := Parse(markdown)
_, text, err := parse(markdown)
// assert that an error was returned
if err == nil {

View File

@ -4,10 +4,14 @@ import (
"fmt"
"os"
"sort"
"github.com/cheat/cheat/internal/frontmatter"
)
// Frontmatter encapsulates cheatsheet frontmatter data
type frontmatter struct {
Tags []string
Syntax string
}
// Sheet encapsulates sheet information
type Sheet struct {
Title string
@ -34,8 +38,8 @@ func New(
return Sheet{}, fmt.Errorf("failed to read file: %s, %v", path, err)
}
// parse the cheatsheet frontmatter
text, fm, err := frontmatter.Parse(string(markdown))
// parse the raw cheatsheet text
fm, text, err := parse(string(markdown))
if err != nil {
return Sheet{}, fmt.Errorf("failed to parse front-matter: %v", err)
}