some updates and fixes

pull/144/head
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 // Handle the content depending on the file extension
switch editor.Mode { switch editor.Mode {
case "markdown", "asciidoc", "rst": 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 // Starts a new buffer and parses the file using Hugo's functions
buffer := bytes.NewBuffer(i.Raw) buffer := bytes.NewBuffer(i.Raw)
page, err = parser.ReadFrom(buffer) page, err = parser.ReadFrom(buffer)
@ -64,10 +64,10 @@ func (i *Info) GetEditor() (*Editor, error) {
editor.Class = "frontmatter-only" editor.Class = "frontmatter-only"
// Checks if the file already has the frontmatter rune and parses it // 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) editor.FrontMatter, _, err = frontmatter.Pretty(i.Raw)
} else { } 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 // Check if there were any errors
@ -82,13 +82,15 @@ func (i *Info) GetEditor() (*Editor, error) {
return editor, nil 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), "---") || return strings.HasPrefix(string(file), "---") ||
strings.HasPrefix(string(file), "+++") || 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 { switch language {
case "yaml": case "yaml":
return []byte("---\n" + string(frontmatter) + "\n---") 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) { func parseFrontMatterOnlyFile(data interface{}, filename string) ([]byte, int, error) {
frontmatter := strings.TrimPrefix(filepath.Ext(filename), ".") 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) { 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) 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 { if err != nil {
return []byte{}, http.StatusInternalServerError, err return []byte{}, http.StatusInternalServerError, err

View File

@ -26,29 +26,11 @@ var mainTitle = ""
// Pretty creates a new FrontMatter object // Pretty creates a new FrontMatter object
func Pretty(content []byte) (*Content, string, error) { func Pretty(content []byte) (*Content, string, error) {
mark := rune(content[0]) data, err := Unmarshal(content)
var data interface{}
switch mark { if err != nil {
case '-':
// If it's YAML
if err := yaml.Unmarshal(content, &data); err != nil {
return &Content{}, "", err return &Content{}, "", err
} }
case '+':
// If it's TOML
content = bytes.Replace(content, []byte("+"), []byte(""), -1)
if _, err := toml.Decode(string(content), &data); err != nil {
return &Content{}, "", err
}
case '{', '[':
// If it's JSON
if err := json.Unmarshal(content, &data); err != nil {
return &Content{}, "", err
}
default:
return &Content{}, "", errors.New("Invalid frontmatter type.")
}
kind := reflect.ValueOf(data).Kind() kind := reflect.ValueOf(data).Kind()
@ -65,6 +47,35 @@ func Pretty(content []byte) (*Content, string, error) {
return rawToPretty(data, object), mainTitle, nil return rawToPretty(data, object), mainTitle, nil
} }
// Unmarshal returns the data of the frontmatter
func Unmarshal(content []byte) (interface{}, error) {
mark := rune(content[0])
var data interface{}
switch mark {
case '-':
// If it's YAML
if err := yaml.Unmarshal(content, &data); err != nil {
return nil, err
}
case '+':
// If it's TOML
content = bytes.Replace(content, []byte("+"), []byte(""), -1)
if _, err := toml.Decode(string(content), &data); err != nil {
return nil, err
}
case '{', '[':
// If it's JSON
if err := json.Unmarshal(content, &data); err != nil {
return nil, err
}
default:
return nil, errors.New("Invalid frontmatter type.")
}
return data, nil
}
// Content is the block content // Content is the block content
type Content struct { type Content struct {
Other interface{} Other interface{}