From 15e46f99c745757eab86f329caebd3bba102f4dc Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 18 Jan 2017 18:40:20 +0000 Subject: [PATCH] rewrite some frontmatter and editor-related stuff --- frontmatter/runes.go | 23 ++++++++++-------- handlers/download.go | 2 +- handlers/editor.go | 58 ++++++++++++++++++++++++++------------------ handlers/put.go | 3 ++- page/page.go | 2 +- 5 files changed, 51 insertions(+), 37 deletions(-) diff --git a/frontmatter/runes.go b/frontmatter/runes.go index da2e4831..49b96dd6 100644 --- a/frontmatter/runes.go +++ b/frontmatter/runes.go @@ -2,6 +2,7 @@ package frontmatter import ( "bytes" + "errors" "strings" ) @@ -28,28 +29,30 @@ func AppendRune(frontmatter []byte, mark rune) []byte { return frontmatter } -func RuneToStringFormat(mark rune) string { +// RuneToStringFormat converts the rune to a string with the format +func RuneToStringFormat(mark rune) (string, error) { switch mark { case '-': - return "yaml" + return "yaml", nil case '+': - return "toml" + return "toml", nil case '{': - return "json" + return "json", nil default: - return "" + return "", errors.New("Unsupported format type.") } } -func StringFormatToRune(format string) rune { +// StringFormatToRune converts the format name to its rune +func StringFormatToRune(format string) (rune, error) { switch format { case "yaml": - return '-' + return '-', nil case "toml": - return '+' + return '+', nil case "json": - return '{' + return '{', nil default: - return '0' + return '0', errors.New("Unsupported format type.") } } diff --git a/handlers/download.go b/handlers/download.go index f297472c..1f768630 100644 --- a/handlers/download.go +++ b/handlers/download.go @@ -14,7 +14,7 @@ import ( "github.com/mholt/archiver" ) -// Download creates an archieve in one of the supported formats (zip, tar, +// Download creates an archive in one of the supported formats (zip, tar, // tar.gz or tar.bz2) and sends it to be downloaded. func Download(w http.ResponseWriter, r *http.Request, c *config.Config, i *file.Info) (int, error) { query := r.URL.Query().Get("download") diff --git a/handlers/editor.go b/handlers/editor.go index 496b1931..7f2609aa 100644 --- a/handlers/editor.go +++ b/handlers/editor.go @@ -29,52 +29,62 @@ func GetEditor(r *http.Request, i *file.Info) (*Editor, error) { var err error // Create a new editor variable and set the mode - editor := new(Editor) - editor.Mode = editorMode(i.Name) - editor.Class = editorClass(editor.Mode) + e := new(Editor) + e.Mode = editorMode(i.Name) + e.Class = editorClass(e.Mode) - if editor.Class == "frontmatter-only" || editor.Class == "complete" { - editor.Visual = true + if e.Class == "frontmatter-only" || e.Class == "complete" { + e.Visual = true } if r.URL.Query().Get("visual") == "false" { - editor.Class = "content-only" + e.Class = "content-only" } - if editor.Class == "frontmatter-only" { - // Checks if the file already has the frontmatter rune and parses it - if frontmatter.HasRune(i.Content) { - editor.FrontMatter.Content, _, err = frontmatter.Pretty(i.Content) - } else { - editor.FrontMatter.Rune = frontmatter.StringFormatToRune(editor.Mode) - editor.FrontMatter.Content, _, err = frontmatter.Pretty(frontmatter.AppendRune(i.Content, editor.FrontMatter.Rune)) + hasRune := frontmatter.HasRune(i.Content) + + if e.Class == "frontmatter-only" && !hasRune { + e.FrontMatter.Rune, err = frontmatter.StringFormatToRune(e.Mode) + if err != nil { + goto Error + } + i.Content = frontmatter.AppendRune(i.Content, e.FrontMatter.Rune) + } + + if e.Class == "frontmatter-only" && hasRune { + e.FrontMatter.Content, _, err = frontmatter.Pretty(i.Content) + if err != nil { + goto Error } } - if editor.Class == "complete" && frontmatter.HasRune(i.Content) { + if e.Class == "complete" && hasRune { var page parser.Page // Starts a new buffer and parses the file using Hugo's functions buffer := bytes.NewBuffer(i.Content) page, err = parser.ReadFrom(buffer) - if err == nil { - // Parses the page content and the frontmatter - editor.Content = strings.TrimSpace(string(page.Content())) - editor.FrontMatter.Rune = rune(i.Content[0]) - editor.FrontMatter.Content, _, err = frontmatter.Pretty(page.FrontMatter()) + if err != nil { + goto Error } + + // Parses the page content and the frontmatter + e.Content = strings.TrimSpace(string(page.Content())) + e.FrontMatter.Rune = rune(i.Content[0]) + e.FrontMatter.Content, _, err = frontmatter.Pretty(page.FrontMatter()) } - if editor.Class == "complete" && !frontmatter.HasRune(i.Content) { + if e.Class == "complete" && !hasRune { err = errors.New("Complete but without rune") } - if editor.Class == "content-only" || err != nil { - editor.Class = "content-only" - editor.Content = i.StringifyContent() +Error: + if e.Class == "content-only" || err != nil { + e.Class = "content-only" + e.Content = i.StringifyContent() } - return editor, nil + return e, nil } func editorClass(mode string) string { diff --git a/handlers/put.go b/handlers/put.go index 08a091ea..4b98e59b 100644 --- a/handlers/put.go +++ b/handlers/put.go @@ -52,7 +52,8 @@ func PreProccessPUT( var mark rune if v := r.Header.Get("Rune"); v != "" { - n, err := strconv.Atoi(v) + var n int + n, err = strconv.Atoi(v) if err != nil { return err } diff --git a/page/page.go b/page/page.go index 65ac2908..d8110508 100644 --- a/page/page.go +++ b/page/page.go @@ -160,7 +160,7 @@ func (p Page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, erro return http.StatusOK, err } -// PrintAsJSON prints the current Page infromation in JSON +// PrintAsJSON prints the current Page information in JSON func (p Page) PrintAsJSON(w http.ResponseWriter) (int, error) { marsh, err := json.MarshalIndent(p.Info.Data, "", " ") if err != nil {