filebrowser/internal/file/editor.go

126 lines
3.0 KiB
Go
Raw Normal View History

2016-06-25 21:05:45 +00:00
package file
2016-06-23 22:21:44 +00:00
import (
"bytes"
"path/filepath"
"strings"
2016-06-26 22:03:27 +00:00
"github.com/hacdias/caddy-filemanager/internal/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
Content string
2016-06-26 22:03:27 +00:00
FrontMatter *frontmatter.Content
2016-06-23 22:21:44 +00:00
}
// GetEditor gets the editor based on a FileInfo struct
2016-06-25 21:05:45 +00:00
func (i *Info) GetEditor() (*Editor, error) {
2016-06-23 22:21:44 +00:00
// Create a new editor variable and set the mode
editor := new(Editor)
2016-06-25 20:57:10 +00:00
editor.Mode = strings.TrimPrefix(filepath.Ext(i.Name), ".")
2016-06-23 22:21:44 +00:00
switch editor.Mode {
case "md", "markdown", "mdown", "mmark":
editor.Mode = "markdown"
case "asciidoc", "adoc", "ad":
editor.Mode = "asciidoc"
case "rst":
editor.Mode = "rst"
case "html", "htm":
editor.Mode = "html"
case "js":
editor.Mode = "javascript"
}
var page parser.Page
var err error
// Handle the content depending on the file extension
switch editor.Mode {
case "markdown", "asciidoc", "rst":
2016-06-25 20:57:10 +00:00
if editor.hasFrontMatterRune(i.Raw) {
2016-06-23 22:21:44 +00:00
// Starts a new buffer and parses the file using Hugo's functions
2016-06-25 20:57:10 +00:00
buffer := bytes.NewBuffer(i.Raw)
2016-06-23 22:21:44 +00:00
page, err = parser.ReadFrom(buffer)
if err != nil {
return editor, err
}
// Parses the page content and the frontmatter
editor.Content = strings.TrimSpace(string(page.Content()))
2016-06-25 21:05:45 +00:00
editor.FrontMatter, _, err = frontmatter.Pretty(page.FrontMatter())
2016-06-23 22:21:44 +00:00
editor.Class = "complete"
} else {
// The editor will handle only content
editor.Class = "content-only"
2016-06-25 21:05:45 +00:00
editor.Content = i.Content
2016-06-23 22:21:44 +00:00
}
case "json", "toml", "yaml":
// Defines the class and declares an error
editor.Class = "frontmatter-only"
// Checks if the file already has the frontmatter rune and parses it
2016-06-25 20:57:10 +00:00
if editor.hasFrontMatterRune(i.Raw) {
2016-06-25 21:05:45 +00:00
editor.FrontMatter, _, err = frontmatter.Pretty(i.Raw)
2016-06-23 22:21:44 +00:00
} else {
2016-06-25 21:05:45 +00:00
editor.FrontMatter, _, err = frontmatter.Pretty(editor.appendFrontMatterRune(i.Raw, editor.Mode))
2016-06-23 22:21:44 +00:00
}
// Check if there were any errors
if err != nil {
return editor, err
}
default:
// The editor will handle only content
editor.Class = "content-only"
2016-06-25 20:57:10 +00:00
editor.Content = i.Content
2016-06-23 22:21:44 +00:00
}
return editor, nil
}
func (e Editor) hasFrontMatterRune(file []byte) bool {
return strings.HasPrefix(string(file), "---") ||
strings.HasPrefix(string(file), "+++") ||
strings.HasPrefix(string(file), "{")
}
func (e Editor) 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
}
// CanBeEdited checks if the extension of a file is supported by the editor
func CanBeEdited(filename string) bool {
extensions := [...]string{
"md", "markdown", "mdown", "mmark",
"asciidoc", "adoc", "ad",
"rst",
".json", ".toml", ".yaml",
".css", ".sass", ".scss",
".js",
".html",
".txt",
}
for _, extension := range extensions {
if strings.HasSuffix(filename, extension) {
return true
}
}
return false
}