filebrowser/frontmatter/runes.go

59 lines
1.2 KiB
Go
Raw Normal View History

2016-10-18 20:49:46 +00:00
package frontmatter
2017-01-18 18:20:56 +00:00
import (
"bytes"
"errors"
2017-01-18 18:20:56 +00:00
"strings"
)
2016-10-18 20:49:46 +00:00
// 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
2017-01-18 18:20:56 +00:00
func AppendRune(frontmatter []byte, mark rune) []byte {
frontmatter = bytes.TrimSpace(frontmatter)
switch mark {
case '-':
2016-10-18 20:49:46 +00:00
return []byte("---\n" + string(frontmatter) + "\n---")
2017-01-18 18:20:56 +00:00
case '+':
2016-10-18 20:49:46 +00:00
return []byte("+++\n" + string(frontmatter) + "\n+++")
2017-01-18 18:20:56 +00:00
case '{':
2017-01-04 18:17:47 +00:00
return []byte("{\n" + string(frontmatter) + "\n}")
2016-10-18 20:49:46 +00:00
}
return frontmatter
}
2017-01-18 18:20:56 +00:00
// RuneToStringFormat converts the rune to a string with the format
func RuneToStringFormat(mark rune) (string, error) {
2017-01-18 18:20:56 +00:00
switch mark {
case '-':
return "yaml", nil
2017-01-18 18:20:56 +00:00
case '+':
return "toml", nil
2017-01-18 18:20:56 +00:00
case '{':
return "json", nil
2017-01-18 18:20:56 +00:00
default:
return "", errors.New("Unsupported format type.")
2017-01-18 18:20:56 +00:00
}
}
// StringFormatToRune converts the format name to its rune
func StringFormatToRune(format string) (rune, error) {
2017-01-18 18:20:56 +00:00
switch format {
case "yaml":
return '-', nil
2017-01-18 18:20:56 +00:00
case "toml":
return '+', nil
2017-01-18 18:20:56 +00:00
case "json":
return '{', nil
2017-01-18 18:20:56 +00:00
default:
return '0', errors.New("Unsupported format type.")
2017-01-18 18:20:56 +00:00
}
}