mirror of
https://github.com/cheat/cheat.git
synced 2024-11-22 05:51:35 +01:00
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:
parent
f0bfeda47a
commit
2d635293c5
@ -1,4 +1,4 @@
|
|||||||
package frontmatter
|
package sheet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -8,14 +8,8 @@ import (
|
|||||||
"gopkg.in/yaml.v1"
|
"gopkg.in/yaml.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Frontmatter encapsulates cheatsheet frontmatter data
|
|
||||||
type Frontmatter struct {
|
|
||||||
Tags []string
|
|
||||||
Syntax string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse parses cheatsheet frontmatter
|
// 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
|
// determine the appropriate line-break for the platform
|
||||||
linebreak := "\n"
|
linebreak := "\n"
|
||||||
@ -27,11 +21,11 @@ func Parse(markdown string) (string, Frontmatter, error) {
|
|||||||
delim := fmt.Sprintf("---%s", linebreak)
|
delim := fmt.Sprintf("---%s", linebreak)
|
||||||
|
|
||||||
// initialize a frontmatter struct
|
// initialize a frontmatter struct
|
||||||
var fm Frontmatter
|
var fm frontmatter
|
||||||
|
|
||||||
// if the markdown does not contain frontmatter, pass it through unmodified
|
// if the markdown does not contain frontmatter, pass it through unmodified
|
||||||
if !strings.HasPrefix(markdown, delim) {
|
if !strings.HasPrefix(markdown, delim) {
|
||||||
return markdown, fm, nil
|
return fm, markdown, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// otherwise, split the frontmatter and cheatsheet text
|
// 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
|
// return an error if the frontmatter parses into the wrong number of parts
|
||||||
if len(parts) != 3 {
|
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
|
// return an error if the YAML cannot be unmarshalled
|
||||||
if err := yaml.Unmarshal([]byte(parts[1]), &fm); err != nil {
|
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
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package frontmatter
|
package sheet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
@ -16,7 +16,7 @@ tags: [ test ]
|
|||||||
To foo the bar: baz`
|
To foo the bar: baz`
|
||||||
|
|
||||||
// parse the frontmatter
|
// parse the frontmatter
|
||||||
text, fm, err := Parse(markdown)
|
fm, text, err := parse(markdown)
|
||||||
|
|
||||||
// assert expectations
|
// assert expectations
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -50,7 +50,7 @@ func TestHasNoFrontmatter(t *testing.T) {
|
|||||||
markdown := "To foo the bar: baz"
|
markdown := "To foo the bar: baz"
|
||||||
|
|
||||||
// parse the frontmatter
|
// parse the frontmatter
|
||||||
text, fm, err := Parse(markdown)
|
fm, text, err := parse(markdown)
|
||||||
|
|
||||||
// assert expectations
|
// assert expectations
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -81,7 +81,7 @@ tags: [ test ]
|
|||||||
To foo the bar: baz`
|
To foo the bar: baz`
|
||||||
|
|
||||||
// parse the frontmatter
|
// parse the frontmatter
|
||||||
text, _, err := Parse(markdown)
|
_, text, err := parse(markdown)
|
||||||
|
|
||||||
// assert that an error was returned
|
// assert that an error was returned
|
||||||
if err == nil {
|
if err == nil {
|
@ -4,10 +4,14 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/frontmatter"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Frontmatter encapsulates cheatsheet frontmatter data
|
||||||
|
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
|
||||||
@ -34,8 +38,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 cheatsheet frontmatter
|
// parse the raw cheatsheet text
|
||||||
text, fm, err := frontmatter.Parse(string(markdown))
|
fm, text, err := parse(string(markdown))
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user