@@ -2,7 +2,8 @@ package handlers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"errors"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
@@ -15,78 +16,91 @@ import (
|
||||
type Editor struct {
|
||||
Class string
|
||||
Mode string
|
||||
Visual bool
|
||||
Content string
|
||||
FrontMatter *frontmatter.Content
|
||||
}
|
||||
|
||||
// GetEditor gets the editor based on a FileInfo struct
|
||||
func GetEditor(i *file.Info) (*Editor, error) {
|
||||
// Create a new editor variable and set the mode
|
||||
editor := new(Editor)
|
||||
editor.Mode = strings.TrimPrefix(filepath.Ext(i.Name), ".")
|
||||
|
||||
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"
|
||||
case "go":
|
||||
editor.Mode = "golang"
|
||||
}
|
||||
|
||||
var page parser.Page
|
||||
func GetEditor(r *http.Request, i *file.Info) (*Editor, error) {
|
||||
var err error
|
||||
|
||||
// Handle the content depending on the file extension
|
||||
switch editor.Mode {
|
||||
case "json", "toml", "yaml":
|
||||
// Defines the class and declares an error
|
||||
editor.Class = "frontmatter-only"
|
||||
// Create a new editor variable and set the mode
|
||||
editor := new(Editor)
|
||||
editor.Mode = editorMode(i.Name)
|
||||
editor.Class = editorClass(editor.Mode)
|
||||
|
||||
if editor.Class == "frontmatter-only" || editor.Class == "complete" {
|
||||
editor.Visual = true
|
||||
}
|
||||
|
||||
if r.URL.Query().Get("visual") == "false" {
|
||||
editor.Class = "content-only"
|
||||
}
|
||||
|
||||
if editor.Class == "frontmatter-only" {
|
||||
// Checks if the file already has the frontmatter rune and parses it
|
||||
if frontmatter.HasRune(i.Content) {
|
||||
editor.FrontMatter, _, err = frontmatter.Pretty(i.Content)
|
||||
} else {
|
||||
editor.FrontMatter, _, err = frontmatter.Pretty(frontmatter.AppendRune(i.Content, editor.Mode))
|
||||
}
|
||||
}
|
||||
|
||||
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"
|
||||
|
||||
// Check if there were any errors
|
||||
if err == nil {
|
||||
break
|
||||
// Parses the page content and the frontmatter
|
||||
editor.Content = strings.TrimSpace(string(page.Content()))
|
||||
editor.FrontMatter, _, err = frontmatter.Pretty(page.FrontMatter())
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("Hey")
|
||||
if editor.Class == "complete" && !frontmatter.HasRune(i.Content) {
|
||||
err = errors.New("Complete but without rune")
|
||||
}
|
||||
|
||||
fallthrough
|
||||
case "markdown", "asciidoc", "rst":
|
||||
if frontmatter.HasRune(i.Content) {
|
||||
// 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"
|
||||
|
||||
if err == nil {
|
||||
// Parses the page content and the frontmatter
|
||||
editor.Content = strings.TrimSpace(string(page.Content()))
|
||||
editor.FrontMatter, _, err = frontmatter.Pretty(page.FrontMatter())
|
||||
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fallthrough
|
||||
default:
|
||||
if editor.Class == "content-only" || err != nil {
|
||||
editor.Class = "content-only"
|
||||
editor.Content = i.StringifyContent()
|
||||
}
|
||||
|
||||
return editor, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user