filebrowser/handlers/editor.go

122 lines
2.5 KiB
Go
Raw Normal View History

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
2017-01-18 18:20:56 +00:00
FrontMatter struct {
Content *frontmatter.Content
Rune rune
}
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
e := new(Editor)
e.Mode = editorMode(i.Name)
e.Class = editorClass(e.Mode)
2016-06-23 22:21:44 +00:00
if e.Class == "frontmatter-only" || e.Class == "complete" {
e.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" {
e.Class = "content-only"
2017-01-15 11:47:52 +00:00
}
2016-06-23 22:21:44 +00:00
hasRune := frontmatter.HasRune(i.Content)
if e.Class == "frontmatter-only" && !hasRune {
e.FrontMatter.Rune, err = frontmatter.StringFormatToRune(e.Mode)
if err != nil {
goto Error
2016-06-23 22:21:44 +00:00
}
i.Content = frontmatter.AppendRune(i.Content, e.FrontMatter.Rune)
hasRune = true
2017-01-15 11:47:52 +00:00
}
if e.Class == "frontmatter-only" && hasRune {
e.FrontMatter.Content, _, err = frontmatter.Pretty(i.Content)
if err != nil {
goto Error
}
}
if e.Class == "complete" && hasRune {
2017-01-15 11:47:52 +00:00
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)
2016-06-23 22:21:44 +00:00
if err != nil {
goto Error
2016-06-23 22:21:44 +00:00
}
// Parses the page content and the frontmatter
e.Content = strings.TrimSpace(string(page.Content()))
e.FrontMatter.Rune = rune(i.Content[0])
e.FrontMatter.Content, _, err = frontmatter.Pretty(page.FrontMatter())
2017-01-15 11:47:52 +00:00
}
2016-10-18 20:49:46 +00:00
if e.Class == "complete" && !hasRune {
2017-01-15 11:47:52 +00:00
err = errors.New("Complete but without rune")
}
2016-10-18 20:49:46 +00:00
Error:
if e.Class == "content-only" || err != nil {
e.Class = "content-only"
e.Content = i.StringifyContent()
2016-06-23 22:21:44 +00:00
}
2016-10-18 16:56:35 +00:00
return e, nil
2016-06-23 22:21:44 +00:00
}
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
}