some updates and fixes

This commit is contained in:
Henrique Dias
2016-06-29 10:40:20 +01:00
parent b86c7fc7ed
commit 3917414dcc
3 changed files with 55 additions and 41 deletions

View File

@@ -42,7 +42,7 @@ func (i *Info) GetEditor() (*Editor, error) {
// Handle the content depending on the file extension
switch editor.Mode {
case "markdown", "asciidoc", "rst":
if editor.hasFrontMatterRune(i.Raw) {
if HasFrontMatterRune(i.Raw) {
// Starts a new buffer and parses the file using Hugo's functions
buffer := bytes.NewBuffer(i.Raw)
page, err = parser.ReadFrom(buffer)
@@ -64,10 +64,10 @@ func (i *Info) GetEditor() (*Editor, error) {
editor.Class = "frontmatter-only"
// Checks if the file already has the frontmatter rune and parses it
if editor.hasFrontMatterRune(i.Raw) {
if HasFrontMatterRune(i.Raw) {
editor.FrontMatter, _, err = frontmatter.Pretty(i.Raw)
} else {
editor.FrontMatter, _, err = frontmatter.Pretty(editor.appendFrontMatterRune(i.Raw, editor.Mode))
editor.FrontMatter, _, err = frontmatter.Pretty(AppendFrontMatterRune(i.Raw, editor.Mode))
}
// Check if there were any errors
@@ -82,13 +82,15 @@ func (i *Info) GetEditor() (*Editor, error) {
return editor, nil
}
func (e Editor) hasFrontMatterRune(file []byte) bool {
// HasFrontMatterRune checks if the file has the frontmatter rune
func 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 {
// AppendFrontMatterRune appends the frontmatter rune to a file
func AppendFrontMatterRune(frontmatter []byte, language string) []byte {
switch language {
case "yaml":
return []byte("---\n" + string(frontmatter) + "\n---")

View File

@@ -64,7 +64,22 @@ func (i *Info) Update(w http.ResponseWriter, r *http.Request, c *config.Config)
func parseFrontMatterOnlyFile(data interface{}, filename string) ([]byte, int, error) {
frontmatter := strings.TrimPrefix(filepath.Ext(filename), ".")
return parseFrontMatter(data, frontmatter)
f, code, err := parseFrontMatter(data, frontmatter)
fString := string(f)
// If it's toml or yaml, strip frontmatter identifier
if frontmatter == "toml" {
fString = strings.TrimSuffix(fString, "+++\n")
fString = strings.TrimPrefix(fString, "+++\n")
}
if frontmatter == "yaml" {
fString = strings.TrimSuffix(fString, "---\n")
fString = strings.TrimPrefix(fString, "---\n")
}
f = []byte(fString)
return f, code, err
}
func parseFrontMatter(data interface{}, frontmatter string) ([]byte, int, error) {
@@ -82,20 +97,6 @@ func parseFrontMatter(data interface{}, frontmatter string) ([]byte, int, error)
}
f, err := parser.InterfaceToFrontMatter(data, mark)
fString := string(f)
// If it's toml or yaml, strip frontmatter identifier
if frontmatter == "toml" {
fString = strings.TrimSuffix(fString, "+++\n")
fString = strings.TrimPrefix(fString, "+++\n")
}
if frontmatter == "yaml" {
fString = strings.TrimSuffix(fString, "---\n")
fString = strings.TrimPrefix(fString, "---\n")
}
f = []byte(fString)
if err != nil {
return []byte{}, http.StatusInternalServerError, err