filebrowser/frontmatter/runes.go
Henrique Dias 3b9c687af7 fix hacdias/caddy-hugo#105
Former-commit-id: e4a5da856c
2017-01-18 18:20:56 +00:00

56 lines
1013 B
Go

package frontmatter
import (
"bytes"
"strings"
)
// HasRune checks if the file has the frontmatter rune
func HasRune(file []byte) bool {
return strings.HasPrefix(string(file), "---") ||
strings.HasPrefix(string(file), "+++") ||
strings.HasPrefix(string(file), "{")
}
// AppendRune appends the frontmatter rune to a file
func AppendRune(frontmatter []byte, mark rune) []byte {
frontmatter = bytes.TrimSpace(frontmatter)
switch mark {
case '-':
return []byte("---\n" + string(frontmatter) + "\n---")
case '+':
return []byte("+++\n" + string(frontmatter) + "\n+++")
case '{':
return []byte("{\n" + string(frontmatter) + "\n}")
}
return frontmatter
}
func RuneToStringFormat(mark rune) string {
switch mark {
case '-':
return "yaml"
case '+':
return "toml"
case '{':
return "json"
default:
return ""
}
}
func StringFormatToRune(format string) rune {
switch format {
case "yaml":
return '-'
case "toml":
return '+'
case "json":
return '{'
default:
return '0'
}
}