filebrowser/editor/editor.go

261 lines
6.1 KiB
Go
Raw Normal View History

2015-09-18 15:49:53 +00:00
package editor
import (
"bytes"
2015-09-18 18:12:47 +00:00
"encoding/json"
2015-09-18 15:49:53 +00:00
"errors"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"text/template"
2015-09-20 08:15:21 +00:00
"github.com/hacdias/caddy-hugo/config"
2015-09-18 15:49:53 +00:00
"github.com/hacdias/caddy-hugo/frontmatter"
"github.com/hacdias/caddy-hugo/utils"
"github.com/spf13/hugo/parser"
)
type editor struct {
Name string
Class string
2015-09-19 13:47:59 +00:00
Mode string
2015-09-18 15:49:53 +00:00
Content string
FrontMatter interface{}
2015-09-20 08:15:21 +00:00
Config *config.Config
2015-09-18 15:49:53 +00:00
}
2015-09-20 08:15:21 +00:00
// ServeHTTP serves the editor page
func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
2015-09-18 15:49:53 +00:00
filename := strings.Replace(r.URL.Path, "/admin/edit/", "", 1)
if r.Method == "POST" {
2015-09-20 08:15:21 +00:00
return servePost(w, r, filename)
2015-09-19 13:25:35 +00:00
}
2015-09-18 18:12:47 +00:00
2015-09-20 08:15:21 +00:00
return serveGet(w, r, c, filename)
2015-09-19 13:25:35 +00:00
}
2015-09-18 18:12:47 +00:00
2015-09-20 08:15:21 +00:00
func servePost(w http.ResponseWriter, r *http.Request, filename string) (int, error) {
2015-09-19 13:25:35 +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)
// Initializes the file content to write
var file []byte
switch r.Header.Get("X-Content-Type") {
case "frontmatter-only":
frontmatter := strings.TrimPrefix(filepath.Ext(filename), ".")
var mark rune
switch frontmatter {
case "toml":
mark = rune('+')
case "json":
mark = rune('{')
case "yaml":
mark = rune('-')
default:
return 400, nil
}
2015-09-18 15:49:53 +00:00
2015-09-19 13:25:35 +00:00
f, err := parser.InterfaceToFrontMatter(rawFile, mark)
fString := string(f)
2015-09-18 15:49:53 +00:00
2015-09-19 13:25:35 +00:00
// If it's toml or yaml, strip frontmatter identifier
if frontmatter == "toml" {
fString = strings.TrimSuffix(fString, "+++\n")
fString = strings.TrimPrefix(fString, "+++\n")
}
2015-09-18 15:49:53 +00:00
2015-09-19 13:25:35 +00:00
if frontmatter == "yaml" {
fString = strings.TrimSuffix(fString, "---\n")
fString = strings.TrimPrefix(fString, "---\n")
2015-09-18 18:12:47 +00:00
}
2015-09-18 15:49:53 +00:00
2015-09-19 13:25:35 +00:00
f = []byte(fString)
2015-09-18 15:49:53 +00:00
2015-09-18 18:12:47 +00:00
if err != nil {
log.Print(err)
return 500, err
}
2015-09-18 15:49:53 +00:00
2015-09-19 13:25:35 +00:00
file = f
case "content-only":
// The main content of the file
mainContent := rawFile["content"].(string)
mainContent = "\n\n" + strings.TrimSpace(mainContent)
2015-09-18 15:49:53 +00:00
2015-09-19 13:25:35 +00:00
file = []byte(mainContent)
2015-09-19 21:17:38 +00:00
case "complete":
2015-09-19 13:25:35 +00:00
// The main content of the file
mainContent := rawFile["content"].(string)
mainContent = "\n\n" + strings.TrimSpace(mainContent)
// Removes the main content from the rest of the frontmatter
delete(rawFile, "content")
// Converts the frontmatter in JSON
jsonFrontmatter, err := json.Marshal(rawFile)
2015-09-18 15:49:53 +00:00
if err != nil {
log.Print(err)
return 500, err
}
2015-09-19 13:25:35 +00:00
// Indents the json
frontMatterBuffer := new(bytes.Buffer)
json.Indent(frontMatterBuffer, jsonFrontmatter, "", " ")
// Generates the final file
f := new(bytes.Buffer)
f.Write(frontMatterBuffer.Bytes())
f.Write([]byte(mainContent))
file = f.Bytes()
default:
return 400, nil
}
// Write the file
err := ioutil.WriteFile(filename, file, 0666)
if err != nil {
log.Print(err)
return 500, err
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{}"))
return 200, nil
}
2015-09-20 08:15:21 +00:00
func serveGet(w http.ResponseWriter, r *http.Request, c *config.Config, filename string) (int, error) {
2015-09-19 13:25:35 +00:00
// Check if the file format is supported. If not, send a "Not Acceptable"
// header and an error
2015-09-20 08:15:21 +00:00
if !utils.CanBeEdited(filename) {
2015-09-19 13:25:35 +00:00
return 406, errors.New("File format not supported.")
}
// Check if the file exists. If it doesn't, send a "Not Found" message
if _, err := os.Stat(filename); os.IsNotExist(err) {
log.Print(err)
return 404, nil
}
// Open the file and check if there was some error while opening
file, err := ioutil.ReadFile(filename)
if err != nil {
log.Print(err)
return 500, err
}
2015-09-18 15:49:53 +00:00
2015-09-19 13:25:35 +00:00
// Create a new editor variable and set the extension
page := new(editor)
2015-09-19 13:47:59 +00:00
page.Mode = strings.TrimPrefix(filepath.Ext(filename), ".")
2015-09-19 13:25:35 +00:00
page.Name = filename
2015-09-20 08:15:21 +00:00
page.Config = c
2015-09-19 13:25:35 +00:00
2015-09-19 13:47:59 +00:00
// Sanitize the extension
page.Mode = sanitizeMode(page.Mode)
2015-09-19 13:25:35 +00:00
// Handle the content depending on the file extension
2015-09-19 13:47:59 +00:00
switch page.Mode {
case "markdown":
2015-09-19 13:25:35 +00:00
if hasFrontMatterRune(file) {
// Starts a new buffer and parses the file using Hugo's functions
buffer := bytes.NewBuffer(file)
file, err := parser.ReadFrom(buffer)
2015-09-18 15:49:53 +00:00
if err != nil {
log.Print(err)
return 500, err
}
2015-09-19 13:25:35 +00:00
// Parses the page content and the frontmatter
page.Content = strings.TrimSpace(string(file.Content()))
page.FrontMatter, err = frontmatter.Pretty(file.FrontMatter())
2015-09-19 21:17:38 +00:00
page.Class = "complete"
2015-09-19 13:25:35 +00:00
} else {
2015-09-18 15:49:53 +00:00
// The editor will handle only content
page.Class = "content-only"
page.Content = string(file)
}
2015-09-19 13:25:35 +00:00
case "json", "toml", "yaml":
// Defines the class and declares an error
page.Class = "frontmatter-only"
var err error
// Checks if the file already has the frontmatter rune and parses it
if hasFrontMatterRune(file) {
page.FrontMatter, err = frontmatter.Pretty(file)
} else {
2015-09-19 13:47:59 +00:00
page.FrontMatter, err = frontmatter.Pretty(appendFrontMatterRune(file, page.Mode))
2015-09-19 13:25:35 +00:00
}
2015-09-18 15:49:53 +00:00
2015-09-19 13:25:35 +00:00
// Check if there were any errors
2015-09-18 15:49:53 +00:00
if err != nil {
log.Print(err)
return 500, err
}
2015-09-19 13:25:35 +00:00
default:
// The editor will handle only content
page.Class = "content-only"
page.Content = string(file)
2015-09-18 15:49:53 +00:00
}
2015-09-19 13:25:35 +00:00
// Create the functions map, then the template, check for erros and
// execute the template if there aren't errors
functions := template.FuncMap{
2015-09-20 08:15:21 +00:00
"SplitCapitalize": utils.SplitCapitalize,
2015-09-19 13:25:35 +00:00
"Defined": utils.Defined,
2015-09-18 15:49:53 +00:00
}
2015-09-19 13:25:35 +00:00
tpl, err := utils.GetTemplate(r, functions, "editor", "frontmatter")
if err != nil {
log.Print(err)
return 500, err
2015-09-18 15:49:53 +00:00
}
2015-09-19 13:25:35 +00:00
return 200, tpl.Execute(w, page)
2015-09-18 15:49:53 +00:00
}
func hasFrontMatterRune(file []byte) bool {
return strings.HasPrefix(string(file), "---") ||
strings.HasPrefix(string(file), "+++") ||
strings.HasPrefix(string(file), "{")
}
func appendFrontMatterRune(frontmatter []byte, language string) []byte {
switch language {
case "yaml":
return []byte("---\n" + string(frontmatter) + "\n---")
case "toml":
return []byte("+++\n" + string(frontmatter) + "\n+++")
case "json":
return frontmatter
}
return frontmatter
}
2015-09-19 13:47:59 +00:00
func sanitizeMode(extension string) string {
switch extension {
case "markdown", "md":
return "markdown"
case "css", "scss":
return "css"
case "html":
return "htmlmixed"
case "js":
return "javascript"
default:
return extension
}
}