filebrowser/directory/editor.go

127 lines
3.1 KiB
Go
Raw Normal View History

2016-06-28 09:24:02 +00:00
package directory
2016-06-23 22:21:44 +00:00
import (
"bytes"
"path/filepath"
"strings"
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
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
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-29 09:40:20 +00:00
if 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-28 09:24:02 +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-29 09:40:20 +00:00
if HasFrontMatterRune(i.Raw) {
2016-06-28 09:24:02 +00:00
editor.FrontMatter, _, err = frontmatter.Pretty(i.Raw)
2016-06-23 22:21:44 +00:00
} else {
2016-06-29 09:40:20 +00:00
editor.FrontMatter, _, err = frontmatter.Pretty(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
}
2016-06-29 09:40:20 +00:00
// HasFrontMatterRune checks if the file has the frontmatter rune
func HasFrontMatterRune(file []byte) bool {
2016-06-23 22:21:44 +00:00
return strings.HasPrefix(string(file), "---") ||
strings.HasPrefix(string(file), "+++") ||
strings.HasPrefix(string(file), "{")
}
2016-06-29 09:40:20 +00:00
// AppendFrontMatterRune appends the frontmatter rune to a file
func AppendFrontMatterRune(frontmatter []byte, language string) []byte {
2016-06-23 22:21:44 +00:00
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
}