From 2d635293c52646a635de652c4eaf4029711553d0 Mon Sep 17 00:00:00 2001 From: Christopher Allen Lane Date: Fri, 26 Aug 2022 12:20:58 -0400 Subject: [PATCH] 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. --- .../frontmatter.go => sheet/parse.go} | 20 +++++++------------ .../parse_test.go} | 8 ++++---- internal/sheet/sheet.go | 12 +++++++---- 3 files changed, 19 insertions(+), 21 deletions(-) rename internal/{frontmatter/frontmatter.go => sheet/parse.go} (67%) rename internal/{frontmatter/frontmatter_test.go => sheet/parse_test.go} (94%) diff --git a/internal/frontmatter/frontmatter.go b/internal/sheet/parse.go similarity index 67% rename from internal/frontmatter/frontmatter.go rename to internal/sheet/parse.go index 47a245b..0489c4c 100644 --- a/internal/frontmatter/frontmatter.go +++ b/internal/sheet/parse.go @@ -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 } diff --git a/internal/frontmatter/frontmatter_test.go b/internal/sheet/parse_test.go similarity index 94% rename from internal/frontmatter/frontmatter_test.go rename to internal/sheet/parse_test.go index 4a0159a..ca0925c 100644 --- a/internal/frontmatter/frontmatter_test.go +++ b/internal/sheet/parse_test.go @@ -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 { diff --git a/internal/sheet/sheet.go b/internal/sheet/sheet.go index ece0f50..afaf515 100644 --- a/internal/sheet/sheet.go +++ b/internal/sheet/sheet.go @@ -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) }