This commit is contained in:
Henrique Dias
2016-10-18 17:56:35 +01:00
parent 0a755ec954
commit 22e0ad0831
5 changed files with 81 additions and 86 deletions

View File

@@ -42,23 +42,26 @@ func (i *Info) GetEditor() (*Editor, error) {
// Handle the content depending on the file extension
switch editor.Mode {
case "markdown", "asciidoc", "rst":
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)
if err != nil {
return editor, err
}
// Parses the page content and the frontmatter
editor.Content = strings.TrimSpace(string(page.Content()))
editor.FrontMatter, _, err = frontmatter.Pretty(page.FrontMatter())
editor.Class = "complete"
} else {
// The editor will handle only content
if !HasFrontMatterRune(i.Raw) {
editor.Class = "content-only"
editor.Content = i.Content
break
}
// Starts a new buffer and parses the file using Hugo's functions
buffer := bytes.NewBuffer(i.Raw)
page, err = parser.ReadFrom(buffer)
editor.Class = "complete"
if err != nil {
editor.Class = "content-only"
editor.Content = i.Content
break
}
// Parses the page content and the frontmatter
editor.Content = strings.TrimSpace(string(page.Content()))
editor.FrontMatter, _, err = frontmatter.Pretty(page.FrontMatter())
case "json", "toml", "yaml":
// Defines the class and declares an error
editor.Class = "frontmatter-only"
@@ -72,13 +75,15 @@ func (i *Info) GetEditor() (*Editor, error) {
// Check if there were any errors
if err != nil {
return editor, err
editor.Class = "content-only"
editor.Content = i.Content
break
}
default:
// The editor will handle only content
editor.Class = "content-only"
editor.Content = i.Content
}
return editor, nil
}

View File

@@ -145,8 +145,6 @@ func (i *Info) serveSingleFile(w http.ResponseWriter, r *http.Request, c *config
}
page.Info.Data = editor
// TODO: if serve Single File finds an error while parsing, show the raw content to edit instead of giving 500
return page.PrintAsHTML(w, "frontmatter", "editor")
}

View File

@@ -16,54 +16,45 @@ import (
// Update is used to update a file that was edited
func (i *Info) Update(w http.ResponseWriter, r *http.Request, c *config.Config, u *config.User) (int, error) {
// TODO: review this
var (
data map[string]interface{}
file []byte
code int
err error
kind string
rawBuffer = new(bytes.Buffer)
)
var data map[string]interface{}
kind := r.Header.Get("kind")
var file []byte
var code int
rawBuffer := new(bytes.Buffer)
kind = r.Header.Get("kind")
rawBuffer.ReadFrom(r.Body)
if kind == "" {
file = rawBuffer.Bytes()
} else {
err := json.Unmarshal(rawBuffer.Bytes(), &data)
if kind != "" {
err = json.Unmarshal(rawBuffer.Bytes(), &data)
if err != nil {
return http.StatusInternalServerError, err
}
switch kind {
case "frontmatter-only":
if file, code, err = ParseFrontMatterOnlyFile(data, i.Name); err != nil {
return http.StatusInternalServerError, err
}
case "content-only":
mainContent := data["content"].(string)
mainContent = strings.TrimSpace(mainContent)
file = []byte(mainContent)
case "complete":
if file, code, err = ParseCompleteFile(data, i.Name, u.FrontMatter); err != nil {
return http.StatusInternalServerError, err
}
default:
return http.StatusBadRequest, nil
}
}
// Overwrite the Body
switch kind {
case "frontmatter-only":
if file, code, err = ParseFrontMatterOnlyFile(data, i.Name); err != nil {
return http.StatusInternalServerError, err
}
case "content-only":
mainContent := data["content"].(string)
mainContent = strings.TrimSpace(mainContent)
file = []byte(mainContent)
case "complete":
if file, code, err = ParseCompleteFile(data, i.Name, u.FrontMatter); err != nil {
return http.StatusInternalServerError, err
}
default:
file = rawBuffer.Bytes()
}
// Overwrite the request Body
r.Body = ioutil.NopCloser(bytes.NewReader(file))
// Write the file
// err = ioutil.WriteFile(i.Path, file, 0666)
//if err != nil {
//return http.StatusInternalServerError, err
// }
return code, nil
}