2015-09-12 19:02:26 +00:00
|
|
|
package edit
|
|
|
|
|
|
|
|
import (
|
2015-09-14 21:36:15 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-09-12 19:02:26 +00:00
|
|
|
"io/ioutil"
|
2015-09-14 09:46:31 +00:00
|
|
|
"log"
|
2015-09-12 19:02:26 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2015-09-13 20:10:54 +00:00
|
|
|
"strings"
|
2015-09-18 12:52:10 +00:00
|
|
|
"text/template"
|
2015-09-13 11:14:18 +00:00
|
|
|
|
2015-09-14 21:03:09 +00:00
|
|
|
"github.com/hacdias/caddy-hugo/frontmatter"
|
2015-09-18 12:52:10 +00:00
|
|
|
"github.com/hacdias/caddy-hugo/utils"
|
2015-09-14 21:03:09 +00:00
|
|
|
"github.com/spf13/hugo/parser"
|
2015-09-12 19:02:26 +00:00
|
|
|
)
|
|
|
|
|
2015-09-18 12:52:10 +00:00
|
|
|
type editor struct {
|
2015-09-14 21:03:09 +00:00
|
|
|
Name string
|
|
|
|
Content string
|
|
|
|
FrontMatter interface{}
|
2015-09-12 19:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute sth
|
2015-09-13 20:10:54 +00:00
|
|
|
func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
|
2015-09-13 20:45:40 +00:00
|
|
|
filename := strings.Replace(r.URL.Path, "/admin/edit/", "", 1)
|
2015-09-13 20:10:54 +00:00
|
|
|
|
2015-09-12 19:02:26 +00:00
|
|
|
if r.Method == "POST" {
|
2015-09-14 21:36:15 +00:00
|
|
|
// Get the JSON information sent using a buffer
|
|
|
|
rawBuffer := new(bytes.Buffer)
|
|
|
|
rawBuffer.ReadFrom(r.Body)
|
|
|
|
|
|
|
|
// Creates the raw file "map" using the JSON
|
|
|
|
var rawFile map[string]interface{}
|
|
|
|
json.Unmarshal(rawBuffer.Bytes(), &rawFile)
|
|
|
|
|
|
|
|
// The main content of the file
|
|
|
|
mainContent := rawFile["content"].(string)
|
2015-09-15 18:26:18 +00:00
|
|
|
mainContent = "\n\n" + strings.TrimSpace(mainContent)
|
2015-09-14 21:36:15 +00:00
|
|
|
|
|
|
|
// Removes the main content from the rest of the frontmatter
|
|
|
|
delete(rawFile, "content")
|
|
|
|
|
|
|
|
// Converts the frontmatter in JSON
|
|
|
|
jsonFrontmatter, err := json.Marshal(rawFile)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return 500, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Indents the json
|
|
|
|
frontMatterBuffer := new(bytes.Buffer)
|
|
|
|
json.Indent(frontMatterBuffer, jsonFrontmatter, "", " ")
|
|
|
|
|
|
|
|
// Generates the final file
|
|
|
|
file := new(bytes.Buffer)
|
|
|
|
file.Write(frontMatterBuffer.Bytes())
|
|
|
|
file.Write([]byte(mainContent))
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(filename, file.Bytes(), 0666)
|
2015-09-13 19:37:20 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2015-09-14 09:46:31 +00:00
|
|
|
log.Print(err)
|
2015-09-13 19:37:20 +00:00
|
|
|
return 500, err
|
|
|
|
}
|
|
|
|
|
2015-09-14 21:36:15 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write([]byte("{}"))
|
2015-09-12 19:02:26 +00:00
|
|
|
} else {
|
2015-09-13 19:37:20 +00:00
|
|
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
2015-09-14 09:46:31 +00:00
|
|
|
log.Print(err)
|
2015-09-12 19:02:26 +00:00
|
|
|
return 404, nil
|
|
|
|
}
|
|
|
|
|
2015-09-14 21:03:09 +00:00
|
|
|
reader, err := os.Open(filename)
|
2015-09-13 11:14:18 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2015-09-14 09:46:31 +00:00
|
|
|
log.Print(err)
|
2015-09-13 11:14:18 +00:00
|
|
|
return 500, err
|
|
|
|
}
|
|
|
|
|
2015-09-14 21:03:09 +00:00
|
|
|
file, err := parser.ReadFrom(reader)
|
|
|
|
|
2015-09-18 12:52:10 +00:00
|
|
|
inf := new(editor)
|
2015-09-15 18:26:18 +00:00
|
|
|
inf.Content = strings.TrimSpace(string(file.Content()))
|
2015-09-14 21:03:09 +00:00
|
|
|
inf.FrontMatter, err = frontmatter.Pretty(file.FrontMatter())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return 500, err
|
|
|
|
}
|
2015-09-12 21:18:29 +00:00
|
|
|
|
2015-09-18 12:52:10 +00:00
|
|
|
functions := template.FuncMap{
|
|
|
|
"splitCapitalize": utils.SplitCapitalize,
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl, err := utils.GetTemplate(r, functions, "edit", "frontmatter")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return 500, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl.Execute(w, inf)
|
2015-09-12 19:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 200, nil
|
|
|
|
}
|
2015-09-18 12:52:10 +00:00
|
|
|
|
|
|
|
// CanBeEdited checks if a file has a supported extension
|
|
|
|
func CanBeEdited(filename string) bool {
|
|
|
|
extensions := [...]string{".markdown", ".md",
|
|
|
|
".json", ".toml", ".yaml",
|
|
|
|
".css", ".sass", ".scss",
|
|
|
|
".js",
|
|
|
|
".html",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, extension := range extensions {
|
|
|
|
if strings.HasSuffix(filename, extension) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|