From 3917414dccdd1573236cc300ac0160dce920d64c Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 29 Jun 2016 10:40:20 +0100 Subject: [PATCH] some updates and fixes --- directory/editor.go | 12 +++++---- directory/update.go | 31 +++++++++++----------- frontmatter/frontmatter.go | 53 +++++++++++++++++++++++--------------- 3 files changed, 55 insertions(+), 41 deletions(-) diff --git a/directory/editor.go b/directory/editor.go index f02c95ec..7ccb4ddd 100644 --- a/directory/editor.go +++ b/directory/editor.go @@ -42,7 +42,7 @@ func (i *Info) GetEditor() (*Editor, error) { // Handle the content depending on the file extension switch editor.Mode { case "markdown", "asciidoc", "rst": - if editor.hasFrontMatterRune(i.Raw) { + if HasFrontMatterRune(i.Raw) { // Starts a new buffer and parses the file using Hugo's functions buffer := bytes.NewBuffer(i.Raw) page, err = parser.ReadFrom(buffer) @@ -64,10 +64,10 @@ func (i *Info) GetEditor() (*Editor, error) { editor.Class = "frontmatter-only" // Checks if the file already has the frontmatter rune and parses it - if editor.hasFrontMatterRune(i.Raw) { + if HasFrontMatterRune(i.Raw) { editor.FrontMatter, _, err = frontmatter.Pretty(i.Raw) } else { - editor.FrontMatter, _, err = frontmatter.Pretty(editor.appendFrontMatterRune(i.Raw, editor.Mode)) + editor.FrontMatter, _, err = frontmatter.Pretty(AppendFrontMatterRune(i.Raw, editor.Mode)) } // Check if there were any errors @@ -82,13 +82,15 @@ func (i *Info) GetEditor() (*Editor, error) { return editor, nil } -func (e Editor) hasFrontMatterRune(file []byte) bool { +// HasFrontMatterRune checks if the file has the frontmatter rune +func HasFrontMatterRune(file []byte) bool { return strings.HasPrefix(string(file), "---") || strings.HasPrefix(string(file), "+++") || strings.HasPrefix(string(file), "{") } -func (e Editor) appendFrontMatterRune(frontmatter []byte, language string) []byte { +// AppendFrontMatterRune appends the frontmatter rune to a file +func AppendFrontMatterRune(frontmatter []byte, language string) []byte { switch language { case "yaml": return []byte("---\n" + string(frontmatter) + "\n---") diff --git a/directory/update.go b/directory/update.go index 9241d0cf..ea183879 100644 --- a/directory/update.go +++ b/directory/update.go @@ -64,7 +64,22 @@ func (i *Info) Update(w http.ResponseWriter, r *http.Request, c *config.Config) func parseFrontMatterOnlyFile(data interface{}, filename string) ([]byte, int, error) { frontmatter := strings.TrimPrefix(filepath.Ext(filename), ".") - return parseFrontMatter(data, frontmatter) + f, code, err := parseFrontMatter(data, frontmatter) + fString := string(f) + + // If it's toml or yaml, strip frontmatter identifier + if frontmatter == "toml" { + fString = strings.TrimSuffix(fString, "+++\n") + fString = strings.TrimPrefix(fString, "+++\n") + } + + if frontmatter == "yaml" { + fString = strings.TrimSuffix(fString, "---\n") + fString = strings.TrimPrefix(fString, "---\n") + } + + f = []byte(fString) + return f, code, err } func parseFrontMatter(data interface{}, frontmatter string) ([]byte, int, error) { @@ -82,20 +97,6 @@ func parseFrontMatter(data interface{}, frontmatter string) ([]byte, int, error) } f, err := parser.InterfaceToFrontMatter(data, mark) - fString := string(f) - - // If it's toml or yaml, strip frontmatter identifier - if frontmatter == "toml" { - fString = strings.TrimSuffix(fString, "+++\n") - fString = strings.TrimPrefix(fString, "+++\n") - } - - if frontmatter == "yaml" { - fString = strings.TrimSuffix(fString, "---\n") - fString = strings.TrimPrefix(fString, "---\n") - } - - f = []byte(fString) if err != nil { return []byte{}, http.StatusInternalServerError, err diff --git a/frontmatter/frontmatter.go b/frontmatter/frontmatter.go index 6290942f..ef51b3f4 100644 --- a/frontmatter/frontmatter.go +++ b/frontmatter/frontmatter.go @@ -26,28 +26,10 @@ var mainTitle = "" // Pretty creates a new FrontMatter object func Pretty(content []byte) (*Content, string, error) { - mark := rune(content[0]) - var data interface{} + data, err := Unmarshal(content) - switch mark { - case '-': - // If it's YAML - if err := yaml.Unmarshal(content, &data); err != nil { - return &Content{}, "", err - } - case '+': - // If it's TOML - content = bytes.Replace(content, []byte("+"), []byte(""), -1) - if _, err := toml.Decode(string(content), &data); err != nil { - return &Content{}, "", err - } - case '{', '[': - // If it's JSON - if err := json.Unmarshal(content, &data); err != nil { - return &Content{}, "", err - } - default: - return &Content{}, "", errors.New("Invalid frontmatter type.") + if err != nil { + return &Content{}, "", err } kind := reflect.ValueOf(data).Kind() @@ -65,6 +47,35 @@ func Pretty(content []byte) (*Content, string, error) { return rawToPretty(data, object), mainTitle, nil } +// Unmarshal returns the data of the frontmatter +func Unmarshal(content []byte) (interface{}, error) { + mark := rune(content[0]) + var data interface{} + + switch mark { + case '-': + // If it's YAML + if err := yaml.Unmarshal(content, &data); err != nil { + return nil, err + } + case '+': + // If it's TOML + content = bytes.Replace(content, []byte("+"), []byte(""), -1) + if _, err := toml.Decode(string(content), &data); err != nil { + return nil, err + } + case '{', '[': + // If it's JSON + if err := json.Unmarshal(content, &data); err != nil { + return nil, err + } + default: + return nil, errors.New("Invalid frontmatter type.") + } + + return data, nil +} + // Content is the block content type Content struct { Other interface{}