2015-09-26 21:02:49 +00:00
|
|
|
package editor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
2015-10-18 14:10:32 +00:00
|
|
|
"github.com/hacdias/caddy-hugo/config"
|
|
|
|
"github.com/hacdias/caddy-hugo/frontmatter"
|
|
|
|
"github.com/hacdias/caddy-hugo/utils"
|
2015-09-26 21:02:49 +00:00
|
|
|
"github.com/spf13/hugo/parser"
|
|
|
|
)
|
|
|
|
|
2015-09-27 18:49:58 +00:00
|
|
|
type editor struct {
|
|
|
|
Name string
|
|
|
|
Class string
|
|
|
|
IsPost bool
|
|
|
|
Mode string
|
|
|
|
Content string
|
|
|
|
FrontMatter interface{}
|
|
|
|
Config *config.Config
|
|
|
|
}
|
|
|
|
|
2015-09-26 21:02:49 +00:00
|
|
|
// GET handles the GET method on editor page
|
|
|
|
func GET(w http.ResponseWriter, r *http.Request, c *config.Config, filename string) (int, error) {
|
|
|
|
// Check if the file format is supported. If not, send a "Not Acceptable"
|
|
|
|
// header and an error
|
|
|
|
if !utils.CanBeEdited(filename) {
|
2015-09-26 21:19:22 +00:00
|
|
|
return http.StatusNotAcceptable, errors.New("File format not supported.")
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the file exists. If it doesn't, send a "Not Found" message
|
|
|
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
2015-09-26 21:19:22 +00:00
|
|
|
return http.StatusNotFound, nil
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open the file and check if there was some error while opening
|
|
|
|
file, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
2015-09-26 21:19:22 +00:00
|
|
|
return http.StatusInternalServerError, err
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new editor variable and set the extension
|
|
|
|
page := new(editor)
|
|
|
|
page.Mode = strings.TrimPrefix(filepath.Ext(filename), ".")
|
2015-09-30 21:03:28 +00:00
|
|
|
page.Name = strings.Replace(filename, c.Path, "", 1)
|
2015-09-26 21:02:49 +00:00
|
|
|
page.Config = c
|
|
|
|
page.IsPost = false
|
|
|
|
|
|
|
|
// Sanitize the extension
|
|
|
|
page.Mode = sanitizeMode(page.Mode)
|
|
|
|
|
|
|
|
// Handle the content depending on the file extension
|
|
|
|
switch page.Mode {
|
2016-02-07 09:01:17 +00:00
|
|
|
case "markdown", "asciidoc", "rst":
|
2015-09-26 21:02:49 +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)
|
|
|
|
if err != nil {
|
2015-09-26 21:19:22 +00:00
|
|
|
return http.StatusInternalServerError, err
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(string(file.FrontMatter()), "date") {
|
|
|
|
page.IsPost = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parses the page content and the frontmatter
|
|
|
|
page.Content = strings.TrimSpace(string(file.Content()))
|
2016-01-31 22:14:17 +00:00
|
|
|
page.FrontMatter, page.Name, err = frontmatter.Pretty(file.FrontMatter())
|
2015-09-26 21:02:49 +00:00
|
|
|
page.Class = "complete"
|
|
|
|
} else {
|
|
|
|
// The editor will handle only content
|
|
|
|
page.Class = "content-only"
|
|
|
|
page.Content = string(file)
|
|
|
|
}
|
|
|
|
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) {
|
2016-01-31 22:14:17 +00:00
|
|
|
page.FrontMatter, _, err = frontmatter.Pretty(file)
|
2015-09-26 21:02:49 +00:00
|
|
|
} else {
|
2016-01-31 22:14:17 +00:00
|
|
|
page.FrontMatter, _, err = frontmatter.Pretty(appendFrontMatterRune(file, page.Mode))
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if there were any errors
|
|
|
|
if err != nil {
|
2015-09-26 21:19:22 +00:00
|
|
|
return http.StatusInternalServerError, err
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
// The editor will handle only content
|
|
|
|
page.Class = "content-only"
|
|
|
|
page.Content = string(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the functions map, then the template, check for erros and
|
|
|
|
// execute the template if there aren't errors
|
|
|
|
functions := template.FuncMap{
|
|
|
|
"SplitCapitalize": utils.SplitCapitalize,
|
|
|
|
"Defined": utils.Defined,
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl, err := utils.GetTemplate(r, functions, "editor", "frontmatter")
|
|
|
|
|
|
|
|
if err != nil {
|
2015-09-26 21:19:22 +00:00
|
|
|
return http.StatusInternalServerError, err
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
2015-09-26 21:19:22 +00:00
|
|
|
return http.StatusOK, tpl.Execute(w, page)
|
2015-09-26 21:02:49 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
func sanitizeMode(extension string) string {
|
|
|
|
switch extension {
|
2016-02-07 09:01:17 +00:00
|
|
|
case "md", "markdown", "mdown", "mmark":
|
2015-09-26 21:02:49 +00:00
|
|
|
return "markdown"
|
2016-02-07 09:01:17 +00:00
|
|
|
case "asciidoc", "adoc", "ad":
|
|
|
|
return "asciidoc"
|
|
|
|
case "rst":
|
|
|
|
return "rst"
|
|
|
|
case "html", "htm":
|
|
|
|
return "html"
|
2015-09-26 21:02:49 +00:00
|
|
|
case "js":
|
|
|
|
return "javascript"
|
|
|
|
default:
|
|
|
|
return extension
|
|
|
|
}
|
|
|
|
}
|