From 3b9c687af7137a575b80a8d31904b1b0548933ec Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 18 Jan 2017 18:20:56 +0000 Subject: [PATCH] fix hacdias/caddy-hugo#105 Former-commit-id: e4a5da856ccf794aa1517ca44e8ee47d83ef0f2d --- _embed/public/js/editor.js | 6 +++-- _embed/templates/editor.tmpl | 4 ++-- config/config.go | 11 --------- config/user.go | 1 - frontmatter/runes.go | 43 +++++++++++++++++++++++++++++++----- handlers/editor.go | 14 +++++++----- handlers/put.go | 21 ++++++++++++++---- 7 files changed, 69 insertions(+), 31 deletions(-) diff --git a/_embed/public/js/editor.js b/_embed/public/js/editor.js index bcaf19fc..820663c6 100644 --- a/_embed/public/js/editor.js +++ b/_embed/public/js/editor.js @@ -188,7 +188,8 @@ document.addEventListener("DOMContentLoaded", (event) => { } let container = document.getElementById('editor'), - kind = container.dataset.kind; + kind = container.dataset.kind, + rune = container.dataset.rune; if(kind != 'frontmatter-only') { let editor = document.querySelector('.content #ace'), @@ -240,7 +241,8 @@ document.addEventListener("DOMContentLoaded", (event) => { buttons.setLoading('save') webdav.put(window.location.pathname, JSON.stringify(data), { - 'Kind': kind + 'Kind': kind, + 'Rune': rune }) .then(() => { buttons.setDone('save') diff --git a/_embed/templates/editor.tmpl b/_embed/templates/editor.tmpl index bc8c8f44..d02238d8 100644 --- a/_embed/templates/editor.tmpl +++ b/_embed/templates/editor.tmpl @@ -1,12 +1,12 @@ {{ define "content" }} {{- with .Data }} -
+ {{- if or (eq .Class "frontmatter-only") (eq .Class "complete") }} {{- if (eq .Class "complete")}}

Metadata

{{- end }}
- {{- template "blocks" .FrontMatter }} + {{- template "blocks" .FrontMatter.Content }}
Add field
{{- end }} diff --git a/config/config.go b/config/config.go index 3db2e40f..8c444707 100644 --- a/config/config.go +++ b/config/config.go @@ -66,7 +66,6 @@ func Parse(c *caddy.Controller) ([]Config, error) { cfg.Scope = "." cfg.FileSystem = webdav.Dir(cfg.Scope) cfg.BaseURL = "" - cfg.FrontMatter = "yaml" cfg.HugoEnabled = false cfg.Users = map[string]*User{} cfg.AllowCommands = true @@ -102,15 +101,6 @@ func Parse(c *caddy.Controller) ([]Config, error) { for c.NextBlock() { switch c.Val() { - case "frontmatter": - if !c.NextArg() { - return configs, c.ArgErr() - } - - user.FrontMatter = c.Val() - if user.FrontMatter != "yaml" && user.FrontMatter != "json" && user.FrontMatter != "toml" { - return configs, c.Err("frontmatter type not supported") - } case "before_save": if cfg.BeforeSave, err = CommandRunner(c); err != nil { return configs, err @@ -239,7 +229,6 @@ func Parse(c *caddy.Controller) ([]Config, error) { user.AllowEdit = cfg.AllowEdit user.AllowNew = cfg.AllowEdit user.Commands = cfg.Commands - user.FrontMatter = cfg.FrontMatter user.Scope = cfg.Scope user.FileSystem = cfg.FileSystem user.Rules = cfg.Rules diff --git a/config/user.go b/config/user.go index 4f72d2ce..4f4f47f6 100644 --- a/config/user.go +++ b/config/user.go @@ -12,7 +12,6 @@ type User struct { FileSystem webdav.FileSystem `json:"-"` // The virtual file system the user have access Handler *webdav.Handler `json:"-"` // The WebDav HTTP Handler StyleSheet string `json:"-"` // Costum stylesheet - FrontMatter string `json:"-"` // Default frontmatter to save files in AllowNew bool // Can create files and folders AllowEdit bool // Can edit/rename files AllowCommands bool // Can execute commands diff --git a/frontmatter/runes.go b/frontmatter/runes.go index 718f17c7..da2e4831 100644 --- a/frontmatter/runes.go +++ b/frontmatter/runes.go @@ -1,6 +1,9 @@ package frontmatter -import "strings" +import ( + "bytes" + "strings" +) // HasRune checks if the file has the frontmatter rune func HasRune(file []byte) bool { @@ -10,15 +13,43 @@ func HasRune(file []byte) bool { } // AppendRune appends the frontmatter rune to a file -func AppendRune(frontmatter []byte, language string) []byte { - switch language { - case "yaml": +func AppendRune(frontmatter []byte, mark rune) []byte { + frontmatter = bytes.TrimSpace(frontmatter) + + switch mark { + case '-': return []byte("---\n" + string(frontmatter) + "\n---") - case "toml": + case '+': return []byte("+++\n" + string(frontmatter) + "\n+++") - case "json": + case '{': return []byte("{\n" + string(frontmatter) + "\n}") } return frontmatter } + +func RuneToStringFormat(mark rune) string { + switch mark { + case '-': + return "yaml" + case '+': + return "toml" + case '{': + return "json" + default: + return "" + } +} + +func StringFormatToRune(format string) rune { + switch format { + case "yaml": + return '-' + case "toml": + return '+' + case "json": + return '{' + default: + return '0' + } +} diff --git a/handlers/editor.go b/handlers/editor.go index 0726ace2..496b1931 100644 --- a/handlers/editor.go +++ b/handlers/editor.go @@ -18,7 +18,10 @@ type Editor struct { Mode string Visual bool Content string - FrontMatter *frontmatter.Content + FrontMatter struct { + Content *frontmatter.Content + Rune rune + } } // GetEditor gets the editor based on a FileInfo struct @@ -41,9 +44,10 @@ func GetEditor(r *http.Request, i *file.Info) (*Editor, error) { if editor.Class == "frontmatter-only" { // Checks if the file already has the frontmatter rune and parses it if frontmatter.HasRune(i.Content) { - editor.FrontMatter, _, err = frontmatter.Pretty(i.Content) + editor.FrontMatter.Content, _, err = frontmatter.Pretty(i.Content) } else { - editor.FrontMatter, _, err = frontmatter.Pretty(frontmatter.AppendRune(i.Content, editor.Mode)) + editor.FrontMatter.Rune = frontmatter.StringFormatToRune(editor.Mode) + editor.FrontMatter.Content, _, err = frontmatter.Pretty(frontmatter.AppendRune(i.Content, editor.FrontMatter.Rune)) } } @@ -52,12 +56,12 @@ func GetEditor(r *http.Request, i *file.Info) (*Editor, error) { // Starts a new buffer and parses the file using Hugo's functions buffer := bytes.NewBuffer(i.Content) page, err = parser.ReadFrom(buffer) - editor.Class = "complete" if err == nil { // Parses the page content and the frontmatter editor.Content = strings.TrimSpace(string(page.Content())) - editor.FrontMatter, _, err = frontmatter.Pretty(page.FrontMatter()) + editor.FrontMatter.Rune = rune(i.Content[0]) + editor.FrontMatter.Content, _, err = frontmatter.Pretty(page.FrontMatter()) } } diff --git a/handlers/put.go b/handlers/put.go index fe2acb69..08a091ea 100644 --- a/handlers/put.go +++ b/handlers/put.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "net/http" "path/filepath" + "strconv" "strings" "github.com/hacdias/caddy-filemanager/config" @@ -48,7 +49,18 @@ func PreProccessPUT( mainContent = strings.TrimSpace(mainContent) file = []byte(mainContent) case "complete": - if file, err = ParseCompleteFile(data, r.URL.Path, u.FrontMatter); err != nil { + var mark rune + + if v := r.Header.Get("Rune"); v != "" { + n, err := strconv.Atoi(v) + if err != nil { + return err + } + + mark = rune(n) + } + + if file, err = ParseCompleteFile(data, r.URL.Path, mark); err != nil { return } default: @@ -100,7 +112,7 @@ func ParseFrontMatter(data interface{}, front string) ([]byte, error) { } // ParseCompleteFile parses a complete file -func ParseCompleteFile(data map[string]interface{}, filename string, frontmatter string) ([]byte, error) { +func ParseCompleteFile(data map[string]interface{}, filename string, mark rune) ([]byte, error) { mainContent := "" if _, ok := data["content"]; ok { @@ -116,12 +128,13 @@ func ParseCompleteFile(data map[string]interface{}, filename string, frontmatter data["date"] = data["date"].(string) + ":00" } - front, err := ParseFrontMatter(data, frontmatter) - + front, err := frontmatter.Marshal(data, mark) if err != nil { return []byte{}, err } + front = frontmatter.AppendRune(front, mark) + // Generates the final file f := new(bytes.Buffer) f.Write(front)