2016-10-22 11:07:19 +00:00
|
|
|
package handlers
|
2016-06-23 22:21:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-01-15 11:47:52 +00:00
|
|
|
"errors"
|
|
|
|
"net/http"
|
2016-06-23 22:21:44 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2016-10-22 11:07:19 +00:00
|
|
|
"github.com/hacdias/caddy-filemanager/file"
|
2016-06-28 09:24:02 +00:00
|
|
|
"github.com/hacdias/caddy-filemanager/frontmatter"
|
2016-06-23 22:21:44 +00:00
|
|
|
"github.com/spf13/hugo/parser"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Editor contains the information for the editor page
|
|
|
|
type Editor struct {
|
|
|
|
Class string
|
|
|
|
Mode string
|
2017-01-15 11:47:52 +00:00
|
|
|
Visual bool
|
2016-06-23 22:21:44 +00:00
|
|
|
Content string
|
2016-06-28 09:24:02 +00:00
|
|
|
FrontMatter *frontmatter.Content
|
2016-06-23 22:21:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetEditor gets the editor based on a FileInfo struct
|
2017-01-15 11:47:52 +00:00
|
|
|
func GetEditor(r *http.Request, i *file.Info) (*Editor, error) {
|
|
|
|
var err error
|
|
|
|
|
2016-06-23 22:21:44 +00:00
|
|
|
// Create a new editor variable and set the mode
|
|
|
|
editor := new(Editor)
|
2017-01-15 11:47:52 +00:00
|
|
|
editor.Mode = editorMode(i.Name)
|
|
|
|
editor.Class = editorClass(editor.Mode)
|
2016-06-23 22:21:44 +00:00
|
|
|
|
2017-01-15 11:47:52 +00:00
|
|
|
if editor.Class == "frontmatter-only" || editor.Class == "complete" {
|
|
|
|
editor.Visual = true
|
2016-06-23 22:21:44 +00:00
|
|
|
}
|
|
|
|
|
2017-01-15 11:47:52 +00:00
|
|
|
if r.URL.Query().Get("visual") == "false" {
|
|
|
|
editor.Class = "content-only"
|
|
|
|
}
|
2016-06-23 22:21:44 +00:00
|
|
|
|
2017-01-15 11:47:52 +00:00
|
|
|
if editor.Class == "frontmatter-only" {
|
2016-06-23 22:21:44 +00:00
|
|
|
// Checks if the file already has the frontmatter rune and parses it
|
2016-10-18 20:49:46 +00:00
|
|
|
if frontmatter.HasRune(i.Content) {
|
2016-10-18 20:06:31 +00:00
|
|
|
editor.FrontMatter, _, err = frontmatter.Pretty(i.Content)
|
2016-06-23 22:21:44 +00:00
|
|
|
} else {
|
2016-10-18 20:49:46 +00:00
|
|
|
editor.FrontMatter, _, err = frontmatter.Pretty(frontmatter.AppendRune(i.Content, editor.Mode))
|
2016-06-23 22:21:44 +00:00
|
|
|
}
|
2017-01-15 11:47:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if editor.Class == "complete" && frontmatter.HasRune(i.Content) {
|
|
|
|
var page parser.Page
|
|
|
|
// Starts a new buffer and parses the file using Hugo's functions
|
|
|
|
buffer := bytes.NewBuffer(i.Content)
|
|
|
|
page, err = parser.ReadFrom(buffer)
|
|
|
|
editor.Class = "complete"
|
2016-06-23 22:21:44 +00:00
|
|
|
|
2016-10-18 20:49:46 +00:00
|
|
|
if err == nil {
|
2017-01-15 11:47:52 +00:00
|
|
|
// Parses the page content and the frontmatter
|
|
|
|
editor.Content = strings.TrimSpace(string(page.Content()))
|
|
|
|
editor.FrontMatter, _, err = frontmatter.Pretty(page.FrontMatter())
|
2016-06-23 22:21:44 +00:00
|
|
|
}
|
2017-01-15 11:47:52 +00:00
|
|
|
}
|
2016-10-18 20:49:46 +00:00
|
|
|
|
2017-01-15 11:47:52 +00:00
|
|
|
if editor.Class == "complete" && !frontmatter.HasRune(i.Content) {
|
|
|
|
err = errors.New("Complete but without rune")
|
|
|
|
}
|
2016-10-18 20:49:46 +00:00
|
|
|
|
2017-01-15 11:47:52 +00:00
|
|
|
if editor.Class == "content-only" || err != nil {
|
2016-06-23 22:21:44 +00:00
|
|
|
editor.Class = "content-only"
|
2016-10-18 20:06:31 +00:00
|
|
|
editor.Content = i.StringifyContent()
|
2016-06-23 22:21:44 +00:00
|
|
|
}
|
2016-10-18 16:56:35 +00:00
|
|
|
|
2016-06-23 22:21:44 +00:00
|
|
|
return editor, nil
|
|
|
|
}
|
2017-01-15 11:47:52 +00:00
|
|
|
|
|
|
|
func editorClass(mode string) string {
|
|
|
|
switch mode {
|
|
|
|
case "json", "toml", "yaml":
|
|
|
|
return "frontmatter-only"
|
|
|
|
case "markdown", "asciidoc", "rst":
|
|
|
|
return "complete"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "content-only"
|
|
|
|
}
|
|
|
|
|
|
|
|
func editorMode(filename string) string {
|
|
|
|
mode := strings.TrimPrefix(filepath.Ext(filename), ".")
|
|
|
|
|
|
|
|
switch mode {
|
|
|
|
case "md", "markdown", "mdown", "mmark":
|
|
|
|
mode = "markdown"
|
|
|
|
case "asciidoc", "adoc", "ad":
|
|
|
|
mode = "asciidoc"
|
|
|
|
case "rst":
|
|
|
|
mode = "rst"
|
|
|
|
case "html", "htm":
|
|
|
|
mode = "html"
|
|
|
|
case "js":
|
|
|
|
mode = "javascript"
|
|
|
|
case "go":
|
|
|
|
mode = "golang"
|
|
|
|
}
|
|
|
|
|
|
|
|
return mode
|
|
|
|
}
|