Former-commit-id: e4a5da856ccf794aa1517ca44e8ee47d83ef0f2d
This commit is contained in:
Henrique Dias
2017-01-18 18:20:56 +00:00
parent b29d284591
commit 3b9c687af7
7 changed files with 69 additions and 31 deletions

View File

@@ -18,7 +18,10 @@ type Editor struct {
Mode string
Visual bool
Content string
FrontMatter *frontmatter.Content
FrontMatter struct {
Content *frontmatter.Content
Rune rune
}
}
// GetEditor gets the editor based on a FileInfo struct
@@ -41,9 +44,10 @@ func GetEditor(r *http.Request, i *file.Info) (*Editor, error) {
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)
editor.FrontMatter.Content, _, err = frontmatter.Pretty(i.Content)
} else {
editor.FrontMatter, _, err = frontmatter.Pretty(frontmatter.AppendRune(i.Content, editor.Mode))
editor.FrontMatter.Rune = frontmatter.StringFormatToRune(editor.Mode)
editor.FrontMatter.Content, _, err = frontmatter.Pretty(frontmatter.AppendRune(i.Content, editor.FrontMatter.Rune))
}
}
@@ -52,12 +56,12 @@ func GetEditor(r *http.Request, i *file.Info) (*Editor, error) {
// 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())
editor.FrontMatter.Rune = rune(i.Content[0])
editor.FrontMatter.Content, _, err = frontmatter.Pretty(page.FrontMatter())
}
}

View File

@@ -7,6 +7,7 @@ import (
"io/ioutil"
"net/http"
"path/filepath"
"strconv"
"strings"
"github.com/hacdias/caddy-filemanager/config"
@@ -48,7 +49,18 @@ func PreProccessPUT(
mainContent = strings.TrimSpace(mainContent)
file = []byte(mainContent)
case "complete":
if file, err = ParseCompleteFile(data, r.URL.Path, u.FrontMatter); err != nil {
var mark rune
if v := r.Header.Get("Rune"); v != "" {
n, err := strconv.Atoi(v)
if err != nil {
return err
}
mark = rune(n)
}
if file, err = ParseCompleteFile(data, r.URL.Path, mark); err != nil {
return
}
default:
@@ -100,7 +112,7 @@ func ParseFrontMatter(data interface{}, front string) ([]byte, error) {
}
// ParseCompleteFile parses a complete file
func ParseCompleteFile(data map[string]interface{}, filename string, frontmatter string) ([]byte, error) {
func ParseCompleteFile(data map[string]interface{}, filename string, mark rune) ([]byte, error) {
mainContent := ""
if _, ok := data["content"]; ok {
@@ -116,12 +128,13 @@ func ParseCompleteFile(data map[string]interface{}, filename string, frontmatter
data["date"] = data["date"].(string) + ":00"
}
front, err := ParseFrontMatter(data, frontmatter)
front, err := frontmatter.Marshal(data, mark)
if err != nil {
return []byte{}, err
}
front = frontmatter.AppendRune(front, mark)
// Generates the final file
f := new(bytes.Buffer)
f.Write(front)