diff --git a/commands/templates.go b/commands/templates.go index 1b67c53..1598f47 100644 --- a/commands/templates.go +++ b/commands/templates.go @@ -1,3 +1,4 @@ +// TODO consider making this another package package commands import ( @@ -5,6 +6,7 @@ import ( "bytes" "errors" "fmt" + "io" "os" "path/filepath" "strings" @@ -56,7 +58,7 @@ func render(sourcePath string) (string, string, error) { return html, targetPath, nil } -func extractFrontMatter(file *os.File) ([]byte, map[string]interface{}, error) { +func extractFrontMatter(file io.Reader) ([]byte, map[string]interface{}, error) { const FM_SEPARATOR = "---" var outContent, yamlContent []byte @@ -91,7 +93,7 @@ func extractFrontMatter(file *os.File) ([]byte, map[string]interface{}, error) { if len(yamlContent) != 0 { err := yaml.Unmarshal([]byte(yamlContent), &frontMatter) if err != nil { - return nil, nil, fmt.Errorf("invalid yaml: ", err) + return nil, nil, fmt.Errorf("invalid yaml: %s", err) } } diff --git a/commands/templates_test.go b/commands/templates_test.go new file mode 100644 index 0000000..2dab12a --- /dev/null +++ b/commands/templates_test.go @@ -0,0 +1,50 @@ +package commands + +import ( + "strings" + "testing" +) + +func TestExtractFrontMatter(t *testing.T) { + input := `--- +title: my new post +subtitle: a blog post +tags: ["software", "web"] +--- +

Hello World!

` + + outContent, yaml, err := extractFrontMatter(strings.NewReader(input)) + assertEqual(t, err, nil) + assertEqual(t, string(outContent), "

Hello World!

") + assertEqual(t, yaml["title"], "my new post") + assertEqual(t, yaml["subtitle"], "a blog post") + assertEqual(t, yaml["tags"].([]interface{})[0], "software") + assertEqual(t, yaml["tags"].([]interface{})[1], "web") +} + +func TestInvalidFrontMatterDelimiter(t *testing.T) { + // TODO +} + +func TestInvalidFrontMatterYaml(t *testing.T) { + // TODO +} + +func TestFrontMatterNotAtTop(t *testing.T) { + // TODO +} + +func TestRenderHtml(t *testing.T) { + // TODO +} + +func TestRenderOrg(t *testing.T) { + // TODO +} + +// TODO move to assert package +func assertEqual(t *testing.T, a interface{}, b interface{}) { + if a != b { + t.Fatalf("%s != %s", a, b) + } +}