close #71; fix bugs

This commit is contained in:
Henrique Dias
2016-06-07 18:15:55 +01:00
parent acc4c92b15
commit 4cd72ca58d
11 changed files with 38 additions and 32 deletions

View File

@@ -16,7 +16,7 @@ var (
// ServeHTTP serves the editor page
func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
conf = c
filename = strings.Replace(r.URL.Path, "/admin/edit/", "", 1)
filename = strings.Replace(r.URL.Path, c.Admin+"/edit/", "", 1)
filename = c.Path + filename
switch r.Method {

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
@@ -17,8 +18,6 @@ import (
"github.com/spf13/hugo/parser"
)
var data info
type info struct {
ContentType string
Schedule bool
@@ -32,11 +31,15 @@ type response struct {
// POST handles the POST method on editor page
func POST(w http.ResponseWriter, r *http.Request) (int, error) {
var data info
// Get the JSON information sent using a buffer
rawBuffer := new(bytes.Buffer)
rawBuffer.ReadFrom(r.Body)
err := json.Unmarshal(rawBuffer.Bytes(), &data)
fmt.Println(string(rawBuffer.Bytes()))
if err != nil {
return server.RespondJSON(w, &response{"Error decrypting json."}, http.StatusInternalServerError, err)
}
@@ -46,7 +49,7 @@ func POST(w http.ResponseWriter, r *http.Request) (int, error) {
switch data.ContentType {
case "frontmatter-only":
f, code, err := parseFrontMatterOnlyFile()
f, code, err := parseFrontMatterOnlyFile(data)
if err != nil {
return server.RespondJSON(w, &response{err.Error()}, code, err)
}
@@ -59,7 +62,7 @@ func POST(w http.ResponseWriter, r *http.Request) (int, error) {
file = []byte(mainContent)
case "complete":
f, code, err := parseCompleteFile()
f, code, err := parseCompleteFile(data)
if err != nil {
return server.RespondJSON(w, &response{err.Error()}, code, err)
}
@@ -83,7 +86,7 @@ func POST(w http.ResponseWriter, r *http.Request) (int, error) {
return server.RespondJSON(w, nil, http.StatusOK, nil)
}
func parseFrontMatterOnlyFile() ([]byte, int, error) {
func parseFrontMatterOnlyFile(data info) ([]byte, int, error) {
frontmatter := strings.TrimPrefix(filepath.Ext(filename), ".")
var mark rune
@@ -121,7 +124,7 @@ func parseFrontMatterOnlyFile() ([]byte, int, error) {
return f, http.StatusOK, nil
}
func parseCompleteFile() ([]byte, int, error) {
func parseCompleteFile(data info) ([]byte, int, error) {
// The main content of the file
mainContent := data.Content["content"].(string)
mainContent = "\n\n" + strings.TrimSpace(mainContent) + "\n"